branch : pmacs2
This commit is contained in:
moculus 2007-06-24 20:46:29 +00:00
parent a01a44b165
commit 57966e6420
1 changed files with 61 additions and 91 deletions

124
mode_c.py
View File

@ -6,87 +6,45 @@ from lex2 import Grammar, PatternRule, RegionRule
class MacroGrammar(Grammar): class MacroGrammar(Grammar):
rules = [ rules = [
PatternRule(name=r'continued', pattern=r'.*\\ *$'), PatternRule(name='continued', pattern=r'.*\\ *$'),
] ]
class IncludeGrammar(Grammar): class StringGrammar(Grammar):
rules = [ rules = [
PatternRule(name=r'header', pattern=r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'), PatternRule(name=r'escaped', pattern=r'\\.'),
] ]
class CGrammar(Grammar): class CGrammar(Grammar):
rules = [ rules = [
PatternRule(name=r'macro1', pattern=r'#(?:define|import|include|undef)(?!=[a-zA-Z0-9_])'), #PatternRule(name=r'macro1', pattern=r'#(?:define|import|include|undef)(?!=[a-zA-Z0-9_])'),
RegionRule(name=r'macro2', start=r"#(?:assert|cpu|elif|else|error|endif|ident|ifdef|ifndef|if|include_next|line|machine|pragma|pragma_once|system|unassert|warning)", grammar=MacroGrammar(), end=r'(?<!\\) *$'), RegionRule(
RegionRule(name=r'include', start=r'#include', grammar=IncludeGrammar(), end=r'$'), name=r'macro2',
RegionRule(name=r'define', start=r'#define', grammar=Grammar(), end='(?<!\\) *$'), start=r"#(?:assert|cpu|elif|else|error|endif|ident|ifdef|ifndef|if|include_next|line|machine|pragma|pragma_once|system|unassert|warning)(?!=[a-zA-Z0-9_])",
] grammar=Grammar(),
end=r'(?<!\\) *$',
foo = [ ),
{'name': 'constant', PatternRule(name=r'include', pattern=r'#include(?!=[a-zA-Z0-9_])'),
'expr': r'''(?<=#define) +[A-Za-z0-9_]+(?= |\(|\n|$)''', PatternRule(name=r'header', pattern=r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'),
'action': lex.make_token}, RegionRule(
name=r'macro',
{'name': 'label', start=r'#(?:assert|cpu|define|elif|else|error|endif|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])',
'expr': r"""[a-zA-Z_]+(?=:)""", grammar=MacroGrammar(),
'action': lex.make_token}, end=r'(?:.*[^\\])?$',
),
{'name': "c++ comment", PatternRule(name=r'label', pattern=r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
'expr': r'//.*(?:\n|$)', RegionRule(name=r'comment1', start='/\*', grammar=Grammar(), end='\*/'),
'action': lex.make_token}, PatternRule(name=r'comment2', pattern=r'//.*$'),
PatternRule(name=r'control', pattern=r"(?:break|case|continue|default|do|else|for|goto|if|return|switch|while)(?![a-zA-Z_])"),
{'name': "c comment", PatternRule(name=r'keyword', pattern=r"(?:auto|break|case|char|const|continue|default|do|double|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_])"),
'expr': r"/\*(?:.|\n)*?(?:\*/|$)", PatternRule(name=r'builtin', pattern=r"(?:NULL|TRUE|FALSE)"),
'action' : lex.make_token}, PatternRule(name=r'identifier', pattern=r"[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(name=r"unop", pattern=r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="),
{'name': 'control', PatternRule(name=r'binop', pattern=r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
'expr': r"(?:break|case|continue|default|do|else|for|goto|if|return|switch|while)(?![a-zA-Z_])",
'action': lex.make_token},
{'name': 'keyword',
'expr': r"(?:auto|break|case|char|const|continue|default|do|double|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_])",
'action': lex.make_token},
{'name': 'builtin',
'expr': r"(?:NULL|TRUE|FALSE)",
'action': lex.make_token},
{'name': "identifier",
'expr': r"[a-zA-Z_][a-zA-Z0-9_]*",
'action': lex.make_token},
{'name': "unary operator",
'expr': r"""\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*=""",
'action': lex.make_token},
{'name': "operator",
'expr': r"""\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%""",
'action': lex.make_token},
# this is sketchy as hell # this is sketchy as hell
{'name': "delimiter", PatternRule(name=r"delimiter", pattern=r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"),
'expr': r"""->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?""", PatternRule(name=r"integer", pattern=r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"),
'action': lex.make_token}, PatternRule(name=r"float", pattern=r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"),
RegionRule(name=r'string', start='"', grammar=StringGrammar(), end='"'),
{'name': "integer", PatternRule(name=r'char', pattern=r"'.'|'\\.'|'\\[0-7]{3}'")
'expr': r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?",
'action': lex.make_token},
{'name': "float",
'expr': r"""[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+""",
'action': lex.make_token},
{'name': "string1",
'expr': r'"(?:\\.|[^"])*(?:"|.?$)',
'action': lex.make_token},
# Doesn't handle octal . . (yeah it does..heh...ughhh)
{'name': "char",
'expr': r"'(?:\\[^']+|[^'])(?:'|.?$)",
'action': lex.make_token},
{'name': "default",
'expr': r'\\.|.|\n',
'action': lex.silent}
] ]
class C(mode2.Fundamental): class C(mode2.Fundamental):
@ -102,6 +60,13 @@ class C(mode2.Fundamental):
self.add_bindings('close-brace', ('}',)) self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',)) self.add_bindings('close-bracket', (']',))
self.colors = { self.colors = {
'include': color.build('blue', 'default'),
'header': color.build('green', 'default'),
'macro.start': color.build('blue', 'default', 'bold'),
'macro.continued': color.build('default', 'default', 'bold'),
'macro.end': color.build('default', 'default', 'bold'),
'control': color.build('blue', 'default', 'bold'), 'control': color.build('blue', 'default', 'bold'),
'keyword': color.build('cyan', 'default', 'bold'), 'keyword': color.build('cyan', 'default', 'bold'),
'macro1': color.build('blue', 'default', 'bold'), 'macro1': color.build('blue', 'default', 'bold'),
@ -111,9 +76,14 @@ class C(mode2.Fundamental):
'label': color.build('magenta', 'default', 'bold'), 'label': color.build('magenta', 'default', 'bold'),
'char': color.build('green', 'default'), 'char': color.build('green', 'default'),
'builtin': color.build('magenta', 'default', 'bold'), 'builtin': color.build('magenta', 'default', 'bold'),
'string1': color.build('green', 'default'), 'comment1.start': color.build('red', 'default'),
'c comment': color.build('red', 'default'), 'comment1.end': color.build('red', 'default'),
'c++ comment': color.build('red', 'default'), 'comment1.null': color.build('red', 'default'),
'comment2': color.build('red', 'default'),
'string.start': color.build('green', 'default'),
'string.escaped': color.build('green', 'default'),
'string.null': color.build('green', 'default'),
'string.end': color.build('green', 'default'),
'macro comment': color.build('red', 'default'), 'macro comment': color.build('red', 'default'),
'function name': color.build('blue', 'default'), 'function name': color.build('blue', 'default'),
'integer': color.build('green', 'default'), 'integer': color.build('green', 'default'),