pmacs3/mode_c.py

125 lines
4.8 KiB
Python
Raw Normal View History

2007-06-24 11:05:44 -04:00
import color, mode2
import lex
from lex2 import Grammar, PatternRule, RegionRule
# this might not be complete...
# see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_3.html#SEC44
2007-03-06 10:05:38 -05:00
2007-06-24 11:05:44 -04:00
class MacroGrammar(Grammar):
rules = [
PatternRule(name=r'continued', pattern=r'.*\\ *$'),
]
class IncludeGrammar(Grammar):
rules = [
PatternRule(name=r'header', pattern=r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'),
]
2007-03-06 10:05:38 -05:00
2007-06-24 11:05:44 -04:00
class CGrammar(Grammar):
rules = [
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(name=r'include', start=r'#include', grammar=IncludeGrammar(), end=r'$'),
RegionRule(name=r'define', start=r'#define', grammar=Grammar(), end='(?<!\\) *$'),
]
foo = [
{'name': 'constant',
'expr': r'''(?<=#define) +[A-Za-z0-9_]+(?= |\(|\n|$)''',
'action': lex.make_token},
{'name': 'label',
'expr': r"""[a-zA-Z_]+(?=:)""",
'action': lex.make_token},
{'name': "c++ comment",
'expr': r'//.*(?:\n|$)',
'action': lex.make_token},
{'name': "c comment",
'expr': r"/\*(?:.|\n)*?(?:\*/|$)",
'action' : lex.make_token},
{'name': 'control',
'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},
2007-03-06 10:05:38 -05:00
2007-06-24 11:05:44 -04:00
{'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},
2007-03-06 10:05:38 -05:00
2007-06-24 11:05:44 -04:00
# this is sketchy as hell
{'name': "delimiter",
'expr': r"""->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?""",
'action': lex.make_token},
{'name': "integer",
'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):
#tabbercls = CTabber
grammar = CGrammar()
opentoken = 'delimiter'
opentags = {'(': ')', '[': ']', '{': '}'}
closetoken = 'delimiter'
closetags = {')': '(', ']': '[', '}': '{'}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
2007-03-06 10:05:38 -05:00
self.add_bindings('close-paren', (')',))
self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',))
self.colors = {
'control': color.build('blue', 'default', 'bold'),
'keyword': color.build('cyan', 'default', 'bold'),
'macro1': color.build('blue', 'default', 'bold'),
'macro2': color.build('blue', 'default', 'bold'),
'constant': color.build('magenta', 'default', 'bold'),
'header': color.build('green', 'default', 'bold'),
'label': color.build('magenta', 'default', 'bold'),
'char': color.build('green', 'default'),
'builtin': color.build('magenta', 'default', 'bold'),
'string1': color.build('green', 'default'),
'c comment': color.build('red', 'default'),
'c++ comment': color.build('red', 'default'),
'macro comment': color.build('red', 'default'),
'function name': color.build('blue', 'default'),
'integer': color.build('green', 'default'),
'float': color.build('green', 'default'),
'bizzaro': color.build('magenta', 'green'),
}
def name(self):
return "C"