diff --git a/mode/erlang.py b/mode/erlang.py index c7a9b29..214ea41 100644 --- a/mode/erlang.py +++ b/mode/erlang.py @@ -14,7 +14,7 @@ class ErlangGrammar(Grammar): PatternRule(r'comment', r'%.*$'), PatternRule(r'erl_attribute', r'-[a-z][a-zA-Z0-9_@]+'), PatternRule(r'erl_reserved', r"(?:xor|when|try|rem|receive|query|orelse|or|of|not|let|if|fun|end|div|cond|catch|case|bxor|bsr|bsl|bor|bnot|begin|band|andalso|and|after)(?![a-zA-Z0-9_'])"), - PatternRule(r'erl_arity', r'[a-z][a-zA-Z0-9_@]*/[1-9][0-9]*'), + PatternRule(r'erl_arity', r'[a-z][a-zA-Z0-9_@]*/(?:0|[1-9][0-9]*)'), PatternRule(r'erl_function', r'[a-z][a-zA-Z0-9_@]*(?= *\()'), PatternRule(r'erl_namespace', r'[a-z][a-zA-Z0-9_@]*(?=:)'), PatternRule(r'erl_builtin', r"whereis|unregister|unlink|tuple_to_list|tuple_size|trunc|tl|time|throw|term_to_binary|term_to_binary|statistics|split_binary|spawn_opt|spawn_opt|spawn_opt|spawn_opt|spawn_monitor|spawn_monitor|spawn_link|spawn_link|spawn_link|spawn_link|spawn|spawn|spawn|spawn|size|setelement|self|round|registered|register|put|purge_module|process_info|process_info|process_flag|process_flag|processes|pre_loaded|port_control|port_connect|port_command|port_close|pid_to_list|open_port|now|nodes|nodes|node|node|monitor_node|module_loaded|make_ref|load_module|list_to_tuple|list_to_pid|list_to_integer|list_to_float|list_to_existing_atom|list_to_bitstring|list_to_binary|list_to_atom|link|length|is_tuple|is_reference|is_record|is_record|is_process_alive|is_port|is_pid|is_number|is_list|is_integer|is_function|is_function|is_float|is_boolean|is_bitstring|is_binary|is_atom|is_alive|iolist_to_binary|iolist_size|iolist|iodata|integer_to_list|hd|halt|halt|group_leader|group_leader|get_keys|get|get|garbage_collect|garbage_collect|float_to_list|float|ext_binary|exit|exit|erase|erase|ence|element|disconnect_node|delete_module|date|concat_binary|check_process_code|byte_size|bitstring_to_list|bit_size|binary_to_term|binary_to_list|binary_to_list|atom_to_list|atom_to_list|apply|apply|apply|abs"), @@ -28,26 +28,45 @@ class ErlangGrammar(Grammar): ] class ErlangTabber(tab.StackTabber): - enterlvl = ('after', 'begin', 'case', 'catch', 'fun', 'if', 'query', 'receive', - 'try', 'when') + enterlvl = ('after', 'begin', 'case', 'catch', 'fun', 'if', 'query', 'receive') + def _handle_close_token(self, currlvl, y, i): + self._opt_pop(*self.enterlvl) + return tab.StackTabber._handle_close_token(self, currlvl, y, i) def _handle_other_token(self, currlvl, y, i): token = self.get_token(y, i) rtoken = self.get_next_right_token(y, i) ltoken = self.get_next_left_token(y, i) - if token.name == 'eol': - if ltoken is not None and ltoken.name == 'comment': - ltoken = self.get_next_left_token(y, i - 1) +# if token.name == 'eol': +# pass +# if ltoken is not None and ltoken.name == 'comment': +# ltoken = self.get_next_left_token(y, i - 1) +# +# if ltoken is None: +# pass +# elif ltoken.name == 'erl_reserved' and ltoken.string in self.enterlvl: +# pass +# elif ltoken.name == 'delimiter': +# if ltoken.string == ';': +# #self._opt_pop('->') +# pass +# if ltoken.string == '.': +# #self._opt_pop('->') +# pass +# else: +# pass +# else: +# pass +# #self._pop_while('->') +# #self._pop_while('->') +# #if not self._empty() and self._peek_name() != '->': +# # self._pop() - if ltoken is None: - pass - elif ltoken.name == 'erl_reserved' and ltoken.string in self.enterlvl: - pass - elif ltoken.name == 'delimiter' and ltoken.string in ('.', ';', ',', '->'): - pass - else: - self._pop_while('->') - if not self._empty() and self._peek_name() != '->': - self._pop() + if token.name == 'eol': + pass + elif token.name == 'string.start': + self._append('string', None) + elif token.name == 'string.end': + self._opt_pop('string') elif token.name == 'delimiter': if token.string == '->': if rtoken is None or rtoken.name == 'comment': @@ -66,9 +85,12 @@ class ErlangTabber(tab.StackTabber): else: level = rtoken.x self._append(token.string, level) + elif token.string == 'of': + self._opt_pop('case') elif token.string == 'end': self._opt_pop('->') self._pop_unless('->') + currlvl = self.get_curr_level() return currlvl class Erlang(mode.Fundamental): diff --git a/mode/php.py b/mode/php.py new file mode 100644 index 0000000..5189677 --- /dev/null +++ b/mode/php.py @@ -0,0 +1,189 @@ +import color, mode, tab +from lex import Grammar, PatternRule, RegionRule +from mode.python import StringGrammar +#from mode.c import CTabber + +class JavaGrammar(Grammar): + rules = [ + #PatternRule(r'import', r'(?<=import ) *[a-zA-Z0-9_.*]+'), + #PatternRule(r'package', r'(?<=package ) *[a-zA-Z0-9_.*]+'), + + RegionRule(r'comment', r'/\*', Grammar, '\*/'), + PatternRule(r'comment', r'//.*$'), + PatternRule(r'comment', r'#.*$'), + + PatternRule(r'php_builtin_vars', r'GATEWAY_INTERFACE|SERVER_NAME|SERVER_SOFTWARE|SERVER_PROTOCOL|REQUEST_METHOD|QUERY_STRIN|G|DOCUMENT_ROOT|HTTP_ACCEPT|HTTP_ACCEPT_CHARSET|HTTP_ENCODING|HTTP_ACCEPT_LANGUAGE|HTTP_CONNECTION|HTTP_HOST|HTTP_REFERER|HTTP_USER_AGENT|REMOTE_ADDR|REMOTE_PORT|SCRIPT_FILENAME|SERVER_ADMIN|SERVER_PORT|SERVER_SIGNATURE|PATH_TRANSLATED|SCRIPT_NAME|REQUEST_URI'), + PatternRule(r'php_builtin_vars', r'GLOBALS|PHP_ERRMSG|PHP_SELF|HTTP_GET_VARS|HTTP_POST_VARS|HTTP_COOKIE_VARS|HTTP_POST_FILES|HTTP_ENV_VARS|HTTP_SERVER_VARS|HTTP_SESSION_VARS|HTTP_RAW_POST_DATA|HTTP_STATE_VARS|_GET|_POST|_COOKIE|_FILES|_SERVER|_ENV|_SERVER|_REQUEST|_SESSION'), + + PatternRule(r'php_constants', r'__LINE__|__FILE__|__FUNCTION__|__METHOD__|__CLASS__'), + PatternRule(r'php_constants', r'PHP_VERSION|PHP_OS|DEFAULT_INCLUDE_PATH|PEAR_INSTALL_DIR|PEAR_EXTENSION_DIR|PHP_EXTENSION_DIR|PHP_BINDIR|PHP_LIBDIR|PHP_DATADIR|PHP_SYSCONFDIR|PHP_LOCALSTATEDIR|PHP_CONFIG_FILE_PATH|PHP_OUTPUT_HANDLER_START|PHP_OUTPUT_HANDLER_CONT|PHP_OUTPUT_HANDLER_END|E_ERROR|E_WARNING|E_PARSE|E_NOTICE|E_CORE_ERROR|E_CORE_WARNING|E_COMPILE_ERROR|E_COMPILE_WARNING|E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE|E_ALL'), + + PatternRule(r'keyword', r"(?:abstract|assert|boolean|break|byte|case|catch|char|class|continue|default|double|do|else|extends|finally|final|float|for|if|implements|import|instanceof|interface|int|long|native|new|package|private|protected|public|return|short|static|switch|super|synchronized|threadsafe|throws|throw|transient|try|void|while)(?![a-zA-Z_])"), + PatternRule(r'java_label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), + + PatternRule(r'java_builtin', r"(?:null|true|false|this)"), + PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"), + PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), + PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), + + PatternRule(r"delimiter", r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"), + + PatternRule(r"java_integer", r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"), + PatternRule(r"java_float", r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"), + RegionRule(r'string', '"', StringGrammar, '"'), + PatternRule(r'java_char', r"'.'|'\\.'|'\\[0-7]{3}'"), + PatternRule(r"eol", r"\n$"), + ] + +#class JavaTabber(CTabber): +# def is_base(self, y): +# if y == 0: +# return True +# +# highlighter = self.mode.window.buffer.highlights[self.mode.name()] +# if not highlighter.tokens[y]: +# return False +# +# for t in highlighter.tokens[y]: +# if t.name == 'null': +# pass +# elif t.name == 'keyword': +# if t.string in ('class', 'interface'): +# return True +# elif t.string in ('public', 'private', 'protected', 'static', +# 'final', 'native', 'synchronized', 'abstract', +# 'threadsafe', 'transient'): +# pass +# else: +# return False +# else: +# return False +# +# return False +# +# # detecting function declarations is annoying; this assumes that people +# # won't put a variable type and name on different lines, but that they +# # might do that for function return type and name. +# # +# # unfortunately, valid function return types might include any of the +# # four types of tokens below +# decl = False +# for t in highlighter.tokens[y]: +# if t.name in ('keyword', 'identifier', 'structname', 'enumname'): +# decl = True +# continue +# if decl and t.name == 'function': +# break +# else: +# decl = False +# break +# if decl: +# return True +# +# return False +# +# def _handle_open_token(self, currlvl, y, i): +# self._opt_pop('cont') +# token = self.get_token(y, i) +# if token.string == '{': +# self._opt_pop('cond') +# currlvl = tab.StackTabber._handle_open_token(self, currlvl, y, i) +# return currlvl +# def _handle_close_token(self, currlvl, y, i): +# w = self.mode.tabwidth +# self._opt_pop('cont') +# currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i) +# token = self.get_token(y, i) +# if self.is_rightmost_token(y, i): +# if token.string == '}': +# self._opt_pop('cond') +# self._opt_pop('cont') +# elif self._peek_name() == 'cond': +# pass +# else: +# self._opt_append('cont', currlvl + w) +# return currlvl +# def _handle_other_token(self, currlvl, y, i): +# w = self.mode.tabwidth +# token = self.get_token(y, i) +# fqname = token.fqname() +# if fqname == 'delimiter' and token.string == ';': +# self._opt_pop('cond') +# self._opt_pop('cont') +# self._opt_pop('cond') +# +# elif fqname == 'keyword': +# if token.string in ('do', 'else', 'for', 'if', 'while'): +# self._append('cond', currlvl + w) +# elif token.string == 'break': +# self._opt_pop('case', 'while', 'for') +# elif token.string == 'continue': +# self._opt_pop('while', 'for') +# elif token.string == 'case': +# self._opt_pop('case') +# currlvl = self.get_curr_level() +# self._opt_append('case', currlvl + w) +# +# elif fqname == 'string.start': +# self._opt_append('string', None) +# elif fqname == 'string.end': +# self._opt_pop('string') +# if self.is_rightmost_token(y, i): +# self._opt_append('cont', currlvl + w) +# +# # TODO: this could be a lot better +# elif fqname == 'macro': +# currlvl = 0 +# elif fqname.startswith('macro.start'): +# self._opt_append('macro', None) +# currlvl = 0 +# elif fqname.startswith('macro.end'): +# self._opt_pop('macro', None) +# +# elif fqname.startswith('macroblock.start'): +# self._opt_append('macroblock', None) +# currlvl = 0 +# elif fqname.startswith('macroblock.end'): +# self._opt_pop('macroblock', None) +# +# if self.is_rightmost_token(y, i): +# if self._has_markers() and self._peek_name() == 'cond': +# pass +# elif(not fqname.startswith('string') and +# not fqname.startswith('comment') and +# not fqname.startswith('macro') and +# not fqname == 'delimiter' and +# not fqname == 'header' and +# not fqname == 'null' and +# not fqname == 'eol' and +# token.string not in ('}', ';', '(', '{', '[', ',')): +# self._opt_append('cont', currlvl + w) +# return currlvl + +class Java(mode.Fundamental): + modename = 'Java' + extensions = ['.java'] + #tabbercls = JavaTabber + grammar = JavaGrammar + opentokens = ('delimiter',) + opentags = {'(': ')', '[': ']', '{': '}'} + closetokens = ('delimiter',) + closetags = {')': '(', ']': '[', '}': '{'} + colors = { + 'doccomment.start': ('red', 'default', 'bold'), + 'doccomment.end': ('red', 'default', 'bold'), + 'doccomment.null': ('red', 'default', 'bold'), + 'import': ('blue', 'default', 'bold'), + 'java_label': ('magenta', 'default', 'bold'), + 'java_builtin': ('magenta', 'default', 'bold'), + 'java_char': ('green', 'default', 'bold'), + 'java_integer': ('green', 'default', 'bold'), + 'java_float': ('green', 'default', 'bold'), + } + def __init__(self, w): + mode.Fundamental.__init__(self, w) + self.add_bindings('close-paren', (')',)) + self.add_bindings('close-brace', ('}',)) + self.add_bindings('close-bracket', (']',)) + +install = Java.install