From 08e10069333f56e92be7cb55d4d56671b175ce5e Mon Sep 17 00:00:00 2001 From: moculus Date: Sat, 28 Mar 2009 02:33:36 +0000 Subject: [PATCH] pattern-match-rule installed and tested in c-mode --HG-- branch : pmacs2 --- lex.py | 21 ++++++++++++++++--- mode/c.py | 56 ++++++++++++++++---------------------------------- mode/scheme.py | 8 ++++++++ 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/lex.py b/lex.py index da06431..c8736a8 100755 --- a/lex.py +++ b/lex.py @@ -91,9 +91,6 @@ class Token(object): class Rule(object): reflags = 0 def __init__(self, name): - assert regex.valid_token_name.match(name), 'invalid name %r' % name - assert not regex.reserved_token_names.match(name), \ - "rule name %r is reserved and thus illegal" % name self.name = name def match(self, lexer, parent): raise Exception, "not implemented" @@ -127,6 +124,24 @@ class PatternRule(Rule): class NocasePatternRule(PatternRule): reflags = re.IGNORECASE +class PatternMatchRule(PatternRule): + reflags = 0 + def __init__(self, name, pattern, *names): + PatternRule.__init__(self, name, pattern) + self.names = names + def match(self, lexer, parent): + return self.re.match(self.get_line(lexer), lexer.x) + def lex(self, lexer, parent, m): + if not m: + raise StopIteration + for group, name in zip(m.groups(), self.names): + if not group: + continue + yield self.make_token(lexer, group, name, parent, m.groupdict()) + raise StopIteration +class NocasePatternMatchRule(PatternMatchRule): + reflags = re.IGNORECASE + class FakeWindow(object): def __init__(self, app, b): self.application = app diff --git a/mode/c.py b/mode/c.py index 4bb5d2f..17557ea 100644 --- a/mode/c.py +++ b/mode/c.py @@ -2,6 +2,7 @@ import os, re from subprocess import Popen, PIPE, STDOUT import color, default, method, method.shell, mode, tab from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverridePatternRule +from lex import PatternMatchRule from mode.python import StringGrammar2 class CommentGrammar(Grammar): @@ -24,11 +25,8 @@ class MacroGrammar(Grammar): 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'\)'), + PatternMatchRule(r'xyz', r'(defined)(\()([a-zA-Z_][a-zA-Z0-9_]*)(\))', + r'function', r'delimiter', r'name', r'delimiter'), ] class CGrammar(Grammar): @@ -36,26 +34,16 @@ class CGrammar(Grammar): PatternRule(r'spaces', r' +'), PatternRule(r"delimiter", r"\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=(?!=)|\?|->"), PatternRule(r'eol', r"\n$"), - PatternGroupRule(r'structgroup', r'keyword', r'struct', r'spaces', - 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'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_])"), + PatternMatchRule(r'x', r'(struct|enum|union)( +)([a-zA-Z_][a-zA-Z0-9_]*)', + r'builtin', r'spaces', r'type'), + + PatternRule(r'builtin', r"(?:break|case|continue|default|do|else|for|goto|if|return|sizeof|switch|while)(?![a-zA-Z_])"), + PatternRule(r'builtin', r"(?:signed|register|extern|const|static|enum|struct|typedef|union|unsigned|volatile)(?![a-zA-Z_])"), + PatternRule(r'type', r"(?:auto|char|double|float|int|long|short|void|volatile)(?![a-zA-Z_])"), + + PatternMatchRule(r'x', r'([a-zA-Z_][a-zA-Z0-9_]*)(\**)( +)(\**)([a-zA-Z_][a-zA-Z0-9_]*)', + r'type', r'spaces', r'binop', r'spaces', r'identifier'), - 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'constant', r"[A-Z_][A-Z0-9_]+(?![a-zA-Z0-9_])"), PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), @@ -72,10 +60,12 @@ class CGrammar(Grammar): PatternRule(r'operator', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), 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_.]+ *"|[A-Za-z0-9_]+', - 'macro.end', r'\n$'), + PatternMatchRule(r'x', r'(# *include)( +)(.+)(\n|$)', + r'macro.start', r'spaces', r'header', r'macro.end'), + #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_.]+ *"|[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_]+) *$'), @@ -179,16 +169,6 @@ class C(mode.Fundamental): 'macro.delimiter': ('default', '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'), - 'macro.string.start': ('green', 'default', 'bold'), - 'macro.string.escaped': ('magenta', 'default', 'bold'), - 'macro.string.octal': ('magenta', 'default', 'bold'), - 'macro.string.null': ('green', 'default', 'bold'), - 'macro.string.end': ('green', 'default', 'bold'), - 'macro.end': ('magenta', 'default', 'bold'), 'include': ('blue', 'default', 'bold'), 'header': ('green', 'default', 'bold'), 'type': ('cyan', 'default', 'bold'), diff --git a/mode/scheme.py b/mode/scheme.py index a367f13..62840b9 100644 --- a/mode/scheme.py +++ b/mode/scheme.py @@ -2,6 +2,7 @@ from method import Method from method.shell import Interact from point import Point from lex import Grammar, PatternRule, RegionRule, OverridePatternRule +from lex import PatternMatchRule from mode.lisp import StringGrammar2, Lisp, LispTabber class SchemeGrammar(Grammar): @@ -13,6 +14,8 @@ class SchemeGrammar(Grammar): PatternRule(r'eol', r'\n'), PatternRule(r'abbrev', r"'|`|,\@|,"), + #PatternMatchRule(r'xyz', r'(abc)(def)(g*)(hij)', 'f1', 'f2', 'f3', 'f4'), + # from r5rs PatternRule(r'scheme_keyword', r'(?:=>|unquote-splicing|unquote|syntax-rules|set!|quote|quasiquote|or|map|loop|letrec-syntax|letrec|let-syntax|let\*|let|lambda|if|for-each|else|dynamic-wind|do|delay|define-syntax|define-macro|define|cond|case|call-with-output-file|call-with-input-file|call-with-current-continuation|begin|and)(?![^\n )])'), @@ -59,6 +62,11 @@ class Scheme(Lisp): 'scheme_char': ('green', 'default', 'bold'), 'scheme_boolean': ('magenta', 'default', 'bold'), 'scheme_number': ('default', 'default', 'bold'), + + #'f1': ('red', 'default'), + #'f2': ('yellow', 'default'), + #'f3': ('green', 'default'), + #'f4': ('cyan', 'default'), } actions = [SchemeCheckSyntax, GuileStart, GuileLoadFile] _bindings = {