pattern-match-rule installed and tested in c-mode

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-03-28 02:33:36 +00:00
parent 15545e08d4
commit 08e1006933
3 changed files with 44 additions and 41 deletions

21
lex.py
View File

@ -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

View File

@ -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,25 +34,15 @@ 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'),
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'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'),
PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
PatternRule(r'constant', r"[A-Z_][A-Z0-9_]+(?![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<token>[.a-zA-Z0-9_]+):(?P<mode>[.a-zA-Z0-9_]+) *\*/$'),
OverridePatternRule(r'comment', r'// *@@:(?P<token>[.a-zA-Z0-9_]+):(?P<mode>[.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'),

View File

@ -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 = {