From 9bdaade444598fd65291b8f9ab6c72c12c5f4db6 Mon Sep 17 00:00:00 2001 From: moculus Date: Mon, 16 Mar 2009 00:46:50 +0000 Subject: [PATCH] better? who knows --HG-- branch : pmacs2 --- mode/c.py | 108 +++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/mode/c.py b/mode/c.py index 5350034..65acc41 100644 --- a/mode/c.py +++ b/mode/c.py @@ -9,21 +9,26 @@ class CommentGrammar(Grammar): PatternRule(r'data', r'(?:[^\*]|\*(?!/))+'), ] +class ErrorGrammar(Grammar): + rules = [ + PatternRule(r'data', r'[^\\\n]+'), + PatternRule('continuation', r'\\\n$'), + ] + class MacroGrammar(Grammar): rules = [ - PatternRule(r'spaces', r' +'), - RegionRule(r'comment', r'/\*', CommentGrammar, r'\*/'), - PatternRule(r'comment', r'//.*$'), - PatternRule('name', r'(?:(?<=#define )) *[a-zA-Z_][a-zA-Z0-9_]*'), - PatternRule(r"unop", r"!(?!=)|\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), - PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), - PatternRule(r"delimiter", r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"), - PatternRule(r"identifier", r"[a-zA-Z_][a-zA-Z0-9_]*"), - 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', '"', StringGrammar2, '"'), - PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"), - PatternRule(r"continued", r"\\\n$"), + PatternRule('continuation', r'\\\n$'), + PatternRule('name', r'(?<=#define ) *[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule('name', r'(?<=#ifdef ) *[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule('name', r'(?<=#ifndef ) *[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule('name', r'(?<=#undef ) *[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule(r'concat', r'##[a-zA-Z0-9_]+'), + PatternRule(r'quoted', r'#[a-zA-Z0-9_]+'), + PatternGroupRule(r'xyz', + r'function', r'defined', + r'delimiter', r'\(', + 'name', r'[a-zA-Z_][a-zA-Z0-9_]*', + r'delimiter', r'\)'), ] class CGrammar(Grammar): @@ -35,28 +40,50 @@ class CGrammar(Grammar): r' +', r'structname', r'[a-zA-Z_][a-zA-Z0-9_]*'), PatternGroupRule(r'enumgroup', r'keyword', r'enum', r'spaces', r' +', r'enumname', r'[a-zA-Z_][a-zA-Z0-9_]*'), - 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'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'builtin', r"(?:break|case|continue|default|do|else|for|goto|if|return|sizeof|switch|while)(?![a-zA-Z_])"), + PatternRule(r'type', r"(?:auto|char|const|double|enum|extern|float|int|long|register|short|signed|static|struct|typedef|union|unsigned|void|volatile)(?![a-zA-Z_])"), + + PatternGroupRule(r'xyz', + r'type', r'[a-zA-Z_][a-zA-Z0-9_]*', + r'spaces', r' +', r'binop', r'\*+', + r'identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'), + PatternGroupRule(r'xyz', + r'type', r'[a-zA-Z_][a-zA-Z0-9_]*', + r'binop', r'\*+', r'spaces', r' +', + r'identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'), + PatternGroupRule(r'xyz', + r'type', r'[a-zA-Z_][a-zA-Z0-9_]*', r'spaces', r' +', + r'identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'), - PatternRule(r'builtin', r"(?:NULL|TRUE|FALSE)"), + PatternRule(r'constant', r"[A-Z_][A-Z0-9_]+"), + #PatternRule(r'builtin', r"(?:NULL|TRUE|FALSE)(?![a-zA-Z0-9_])"), PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), + RegionRule(r'error', r'# *error', ErrorGrammar, r'\n$'), RegionRule(r'macro', r'# *(?:assert|cpu|define|elif|else|endif|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma_once|pragma|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar, r'\n$'), RegionRule(r'comment', r'/\*', CommentGrammar, r'\*/'), PatternRule(r'comment', r'//.*$'), RegionRule(r'string', '"', StringGrammar2, '"'), - PatternRule(r"unop", r"!(?!=)|\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), - PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), + #PatternRule(r"unop", r"!(?!=)|\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), + #PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), + PatternRule(r"operator", r"!(?!=)|\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), + PatternRule(r'operator', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), 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'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'), PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"), PatternGroupRule(r'includegrp', r'macro.start', r'# *include', r'spaces', - r' +', r'header', r'< *[-A-Za-z/0-9_.]+ *>|" *[-A-Za-z/0-9_.]+ *"', + #r' +', r'header', r'< *[-A-Za-z/0-9_.]+ *>|" *[-A-Za-z/0-9_.]+ *"', + r' +', r'header', r'< *[-A-Za-z/0-9_.]+ *>|" *[-A-Za-z/0-9_.]+ *"|[A-Za-z0-9_]+', 'macro.end', r'\n$'), PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"), OverridePatternRule(r'comment', r'/\* *@@:(?P[.a-zA-Z0-9_]+):(?P[.a-zA-Z0-9_]+) *\*/$'), OverridePatternRule(r'comment', r'// *@@:(?P[.a-zA-Z0-9_]+):(?P[.a-zA-Z0-9_]+) *$'), ] +MacroGrammar.rules.extend(CGrammar.rules) + class CTabber2(tab.StackTabber2): open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} @@ -102,41 +129,6 @@ class CTabber2(tab.StackTabber2): return t.fqname() in ('spaces', 'eol', 'comment', 'comment.start', 'comment.data', 'comment.null', 'comment.end') -#class CContext(context.Context): -# def _regen_stack(self, y): -# if y > 0 and self.namelines[y - 1][1]: -# return list(self.namelines[y - 1][1]) -# else: -# return [] -# -# def _build_name_map(self, y1, y2, last, curr, stack): -# highlights = self.mode.window.get_highlighter() -# -# i = y1 -# while i < y2: -# if not stack: curr = None -# tokens = highlights.tokens[i] -# -# result = self.class_match.match(tokens) -# if result: curr = tokens[result[0] - self.class_offset].string -# result = self.method_match.match(tokens) -# if result: curr = tokens[result[0] - self.method_offset].string -# -# if curr is not None: self.names.setdefault(curr, i) -# -# for t in tokens: -# if t.match('delimiter', '{'): -# stack.append(curr) -# elif t.match('delimiter', '}'): -# if stack: stack.pop(-1) -# if stack: -# curr = stack[-1] -# else: -# curr = None -# -# if curr: self.namelines[i] = (curr, tuple(stack)) -# i += 1 - class CCheckSyntax(method.shell.Exec): '''Build this C program (using the mode's make cmd)''' show_success = False @@ -186,8 +178,9 @@ class C(mode.Fundamental): 'macro.null': ('magenta', 'default', 'bold'), 'macro.continued': ('red', 'default', 'bold'), 'macro.delimiter': ('default', 'default', 'bold'), - 'macro.identifier': ('yellow', 'default', 'bold'), - 'macro.bareword': ('yellow', 'default', 'bold'), + 'macro.concat': ('yellow', 'default', 'bold'), + 'macro.quoted': ('yellow', 'default', 'bold'), + #'macro.identifier': ('yellow', 'default', 'bold'), 'macro.integer': ('green', 'default', 'bold'), 'macro.float': ('green', 'default', 'bold'), 'macro.char': ('green', 'default', 'bold'), @@ -199,6 +192,11 @@ class C(mode.Fundamental): 'macro.end': ('magenta', 'default', 'bold'), 'include': ('blue', 'default', 'bold'), 'header': ('green', 'default', 'bold'), + 'type': ('cyan', 'default', 'bold'), + 'constant': ('yellow', 'default', 'bold'), + 'error.start': ('blue', 'default', 'bold'), + 'error.data': ('green', 'default', 'bold'), + 'error.null': ('green', 'default', 'bold'), } config = { 'c.syntax-cmd': "gcc -x c -fsyntax-only %(path)s",