pattern-match-rule installed and tested in c-mode
--HG-- branch : pmacs2
This commit is contained in:
parent
15545e08d4
commit
08e1006933
21
lex.py
21
lex.py
|
@ -91,9 +91,6 @@ class Token(object):
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
reflags = 0
|
reflags = 0
|
||||||
def __init__(self, name):
|
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
|
self.name = name
|
||||||
def match(self, lexer, parent):
|
def match(self, lexer, parent):
|
||||||
raise Exception, "not implemented"
|
raise Exception, "not implemented"
|
||||||
|
@ -127,6 +124,24 @@ class PatternRule(Rule):
|
||||||
class NocasePatternRule(PatternRule):
|
class NocasePatternRule(PatternRule):
|
||||||
reflags = re.IGNORECASE
|
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):
|
class FakeWindow(object):
|
||||||
def __init__(self, app, b):
|
def __init__(self, app, b):
|
||||||
self.application = app
|
self.application = app
|
||||||
|
|
56
mode/c.py
56
mode/c.py
|
@ -2,6 +2,7 @@ import os, re
|
||||||
from subprocess import Popen, PIPE, STDOUT
|
from subprocess import Popen, PIPE, STDOUT
|
||||||
import color, default, method, method.shell, mode, tab
|
import color, default, method, method.shell, mode, tab
|
||||||
from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverridePatternRule
|
from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverridePatternRule
|
||||||
|
from lex import PatternMatchRule
|
||||||
from mode.python import StringGrammar2
|
from mode.python import StringGrammar2
|
||||||
|
|
||||||
class CommentGrammar(Grammar):
|
class CommentGrammar(Grammar):
|
||||||
|
@ -24,11 +25,8 @@ class MacroGrammar(Grammar):
|
||||||
PatternRule('name', r'(?<=#undef ) *[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'concat', r'##[a-zA-Z0-9_]+'),
|
||||||
PatternRule(r'quoted', r'#[a-zA-Z0-9_]+'),
|
PatternRule(r'quoted', r'#[a-zA-Z0-9_]+'),
|
||||||
PatternGroupRule(r'xyz',
|
PatternMatchRule(r'xyz', r'(defined)(\()([a-zA-Z_][a-zA-Z0-9_]*)(\))',
|
||||||
r'function', r'defined',
|
r'function', r'delimiter', r'name', r'delimiter'),
|
||||||
r'delimiter', r'\(',
|
|
||||||
'name', r'[a-zA-Z_][a-zA-Z0-9_]*',
|
|
||||||
r'delimiter', r'\)'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
class CGrammar(Grammar):
|
class CGrammar(Grammar):
|
||||||
|
@ -36,26 +34,16 @@ class CGrammar(Grammar):
|
||||||
PatternRule(r'spaces', r' +'),
|
PatternRule(r'spaces', r' +'),
|
||||||
PatternRule(r"delimiter", r"\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=(?!=)|\?|->"),
|
PatternRule(r"delimiter", r"\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=(?!=)|\?|->"),
|
||||||
PatternRule(r'eol', r"\n$"),
|
PatternRule(r'eol', r"\n$"),
|
||||||
PatternGroupRule(r'structgroup', r'keyword', r'struct', r'spaces',
|
PatternMatchRule(r'x', r'(struct|enum|union)( +)([a-zA-Z_][a-zA-Z0-9_]*)',
|
||||||
r' +', r'structname', r'[a-zA-Z_][a-zA-Z0-9_]*'),
|
r'builtin', r'spaces', r'type'),
|
||||||
PatternGroupRule(r'enumgroup', r'keyword', r'enum', r'spaces',
|
|
||||||
r' +', r'enumname', 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'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"(?:signed|register|extern|const|static|enum|struct|typedef|union|unsigned|volatile)(?![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|double|float|int|long|short|void|volatile)(?![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'([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'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
|
||||||
PatternRule(r'constant', r"[A-Z_][A-Z0-9_]+(?![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_]*(?=:)'),
|
PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
|
||||||
|
@ -72,10 +60,12 @@ class CGrammar(Grammar):
|
||||||
PatternRule(r'operator', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
|
PatternRule(r'operator', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
|
||||||
RegionRule(r'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'),
|
RegionRule(r'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'),
|
||||||
PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"),
|
PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"),
|
||||||
PatternGroupRule(r'includegrp', r'macro.start', r'# *include', r'spaces',
|
PatternMatchRule(r'x', r'(# *include)( +)(.+)(\n|$)',
|
||||||
#r' +', r'header', r'< *[-A-Za-z/0-9_.]+ *>|" *[-A-Za-z/0-9_.]+ *"',
|
r'macro.start', r'spaces', r'header', r'macro.end'),
|
||||||
r' +', r'header', r'< *[-A-Za-z/0-9_.]+ *>|" *[-A-Za-z/0-9_.]+ *"|[A-Za-z0-9_]+',
|
#PatternGroupRule(r'includegrp', r'macro.start', r'# *include', r'spaces',
|
||||||
'macro.end', r'\n$'),
|
# #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_]*"),
|
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_]+) *\*/$'),
|
||||||
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.delimiter': ('default', 'default', 'bold'),
|
||||||
'macro.concat': ('yellow', 'default', 'bold'),
|
'macro.concat': ('yellow', 'default', 'bold'),
|
||||||
'macro.quoted': ('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'),
|
'include': ('blue', 'default', 'bold'),
|
||||||
'header': ('green', 'default', 'bold'),
|
'header': ('green', 'default', 'bold'),
|
||||||
'type': ('cyan', 'default', 'bold'),
|
'type': ('cyan', 'default', 'bold'),
|
||||||
|
|
|
@ -2,6 +2,7 @@ from method import Method
|
||||||
from method.shell import Interact
|
from method.shell import Interact
|
||||||
from point import Point
|
from point import Point
|
||||||
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
|
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
|
||||||
|
from lex import PatternMatchRule
|
||||||
from mode.lisp import StringGrammar2, Lisp, LispTabber
|
from mode.lisp import StringGrammar2, Lisp, LispTabber
|
||||||
|
|
||||||
class SchemeGrammar(Grammar):
|
class SchemeGrammar(Grammar):
|
||||||
|
@ -13,6 +14,8 @@ class SchemeGrammar(Grammar):
|
||||||
PatternRule(r'eol', r'\n'),
|
PatternRule(r'eol', r'\n'),
|
||||||
PatternRule(r'abbrev', r"'|`|,\@|,"),
|
PatternRule(r'abbrev', r"'|`|,\@|,"),
|
||||||
|
|
||||||
|
#PatternMatchRule(r'xyz', r'(abc)(def)(g*)(hij)', 'f1', 'f2', 'f3', 'f4'),
|
||||||
|
|
||||||
# from r5rs
|
# 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 )])'),
|
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_char': ('green', 'default', 'bold'),
|
||||||
'scheme_boolean': ('magenta', 'default', 'bold'),
|
'scheme_boolean': ('magenta', 'default', 'bold'),
|
||||||
'scheme_number': ('default', 'default', 'bold'),
|
'scheme_number': ('default', 'default', 'bold'),
|
||||||
|
|
||||||
|
#'f1': ('red', 'default'),
|
||||||
|
#'f2': ('yellow', 'default'),
|
||||||
|
#'f3': ('green', 'default'),
|
||||||
|
#'f4': ('cyan', 'default'),
|
||||||
}
|
}
|
||||||
actions = [SchemeCheckSyntax, GuileStart, GuileLoadFile]
|
actions = [SchemeCheckSyntax, GuileStart, GuileLoadFile]
|
||||||
_bindings = {
|
_bindings = {
|
||||||
|
|
Loading…
Reference in New Issue