diff --git a/mode_c.py b/mode_c.py index f0d9d9a..daf4d8c 100644 --- a/mode_c.py +++ b/mode_c.py @@ -1,18 +1,10 @@ -import color, mode2 +import color, mode2, tab2 from lex2 import Grammar, PatternRule, RegionRule from mode_python import StringGrammar # this might not be complete... # see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_3.html#SEC44 -class MacroGrammar(Grammar): - rules = [ - PatternRule('name', r'(?:(?<=#define )) *[a-zA-Z_][a-zA-Z0-9_]*'), - PatternRule('continued', r'\\ *$'), - RegionRule(r'string', '"', StringGrammar, '"'), - PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'") - ] - class MacroGrammar(Grammar): rules = [ PatternRule('name', r'(?:(?<=#define )) *[a-zA-Z_][a-zA-Z0-9_]*'), @@ -26,11 +18,11 @@ class CGrammar(Grammar): PatternRule(r'include', r'#include(?!=[a-zA-Z0-9_])'), PatternRule(r'header', r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'), PatternRule(r'macro', r'#(?:else|endif)'), - RegionRule(r'macro', r'#(?:assert|cpu|define|elif|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar(), r'[^\\ ] *$'), + RegionRule(r'macro', r'#(?:assert|cpu|define|elif|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar, r'[^\\ ] *$'), RegionRule(r'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'), - RegionRule(r'comment1', '/\*', Grammar(), '\*/'), - PatternRule(r'comment2', r'//.*$'), + RegionRule(r'comment', '/\*', Grammar, '\*/'), + PatternRule(r'comment', r'//.*$'), PatternRule(r'keyword', r"(?:auto|break|case|char|const|continue|default|double|do|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)(?![a-zA-z_])"), PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), @@ -48,13 +40,104 @@ class CGrammar(Grammar): PatternRule(r"integer", r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"), PatternRule(r"float", r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"), - RegionRule(r'string', '"', StringGrammar(), '"'), + RegionRule(r'string', '"', StringGrammar, '"'), PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'") ] +class CTabber(tab2.StackTabber): + 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 + + # this assumes that people aren't gonna use these macros inside of + # blocks, which is probably ok. + t0 = highlighter.tokens[y][0] + if t0.name == 'macro.start' and t0.string in ('#define', '#include'): + return True + + # 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') + currlvl = tab2.StackTabber._handle_open_token(self, currlvl, y, i) + return currlvl + def _handle_close_token(self, currlvl, y, i): + self._opt_pop('cont') + currlvl = tab2.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('cont') + else: + self._opt_append('cont', currlvl + 4) + return currlvl + def _handle_other_token(self, currlvl, y, i): + token = self.get_token(y, i) + fqname = token.fqname() + if fqname == 'delimiter' and token.string == ';': + self._opt_pop('cont') + + 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 + 4) + + # 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(not fqname.startswith('string') and + not fqname.startswith('comment') and + not fqname.startswith('macro') and + not fqname == 'header' and + not fqname == 'null' and + token.string not in ('}', ';', '(', '{', '[', ',')): + try: + self._opt_append('cont', currlvl + 4) + except: + raise Exception, "%r + 4 for %r (%r) on line %d" % \ + (currlvl, fqname, token.string, y) + return currlvl + class C(mode2.Fundamental): - #tabbercls = CTabber - grammar = CGrammar() + tabbercls = CTabber + grammar = CGrammar opentoken = 'delimiter' opentags = {'(': ')', '[': ']', '{': '}'} closetoken = 'delimiter' @@ -68,10 +151,10 @@ class C(mode2.Fundamental): 'macrocomment.start': color.build('red', 'default'), 'macrocomment.null': color.build('red', 'default'), 'macrocomment.end': color.build('red', 'default'), - 'comment1.start': color.build('red', 'default'), - 'comment1.end': color.build('red', 'default'), - 'comment1.null': color.build('red', 'default'), - 'comment2': color.build('red', 'default'), + 'comment': color.build('red', 'default'), + 'comment.start': color.build('red', 'default'), + 'comment.end': color.build('red', 'default'), + 'comment.null': color.build('red', 'default'), 'include': color.build('blue', 'default'), 'header': color.build('green', 'default'),