pmacs3/mode_c.py

102 lines
4.8 KiB
Python
Raw Normal View History

2007-06-24 11:05:44 -04:00
import color, mode2
from lex2 import Grammar, PatternRule, RegionRule
2007-07-11 04:55:54 -04:00
from mode_python import StringGrammar
2007-06-24 11:05:44 -04:00
# 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 = [
2007-07-11 05:28:08 -04:00
PatternRule('name', r'(?:(?<=#define )) *[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule('continued', r'\\ *$'),
RegionRule(r'string', '"', StringGrammar(), '"'),
PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'")
2007-06-24 11:05:44 -04:00
]
2007-03-06 10:05:38 -05:00
2007-06-24 11:05:44 -04:00
class CGrammar(Grammar):
rules = [
2007-07-11 05:28:08 -04:00
PatternRule(r'include', r'#include(?!=[a-zA-Z0-9_])'),
PatternRule(r'header', r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'),
PatternRule(r'macro', r'#(?:endif)'),
RegionRule(r'macro', r'#(?:assert|cpu|define|elif|else|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar(), r'[^\\ ] *$'),
PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
RegionRule(r'comment1', '/\*', Grammar(), '\*/'),
PatternRule(r'comment2', r'//.*$'),
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_])"),
2007-07-11 04:55:54 -04:00
PatternRule(r'structname', r'(?<=struct ) *[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule(r'enumname', r'(?<=enum ) *[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
2007-07-11 05:28:08 -04:00
PatternRule(r'builtin', r"(?:NULL|TRUE|FALSE)"),
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="),
PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
2007-07-11 04:55:54 -04:00
2007-06-24 11:05:44 -04:00
# this is sketchy as hell
2007-07-11 05:28:08 -04:00
PatternRule(r"delimiter", r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"),
PatternRule(r"integer", r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"),
PatternRule(r"float", r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"),
RegionRule(r'string', '"', StringGrammar(), '"'),
PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'")
2007-06-24 11:05:44 -04:00
]
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 = {
2007-07-11 04:55:54 -04:00
'label': color.build('magenta', 'default'),
2007-06-24 16:46:29 -04:00
'include': color.build('blue', 'default'),
'header': color.build('green', 'default'),
2007-07-11 04:55:54 -04:00
'macro.start': color.build('blue', 'default'),
'macro.name': color.build('yellow', 'default'),
'macro.null': color.build('blue', 'default'),
'macro.continued': color.build('red', 'default'),
'macro.char': color.build('green', 'default'),
'macro.string.start': color.build('green', 'default'),
'macro.string.escaped': color.build('magenta', 'default'),
'macro.string.octal': color.build('magenta', 'default'),
'macro.string.null': color.build('green', 'default'),
'macro.string.end': color.build('green', 'default'),
'macro.end': color.build('blue', 'default'),
'keyword': color.build('cyan', 'default'),
2007-06-24 16:46:29 -04:00
'comment1.start': color.build('red', 'default'),
'comment1.end': color.build('red', 'default'),
'comment1.null': color.build('red', 'default'),
'comment2': color.build('red', 'default'),
2007-07-11 04:55:54 -04:00
'function': color.build('blue', 'default'),
'builtin': color.build('magenta', 'default'),
'structname': color.build('yellow', 'default'),
'enumname': color.build('yellow', 'default'),
'char': color.build('green', 'default'),
2007-06-24 16:46:29 -04:00
'string.start': color.build('green', 'default'),
2007-07-11 04:55:54 -04:00
'string.octal': color.build('green', 'default'),
2007-06-24 16:46:29 -04:00
'string.escaped': color.build('green', 'default'),
'string.null': color.build('green', 'default'),
'string.end': color.build('green', 'default'),
2007-07-11 04:55:54 -04:00
2007-06-24 16:46:29 -04:00
'integer': color.build('green', 'default'),
'float': color.build('green', 'default'),
2007-07-11 04:55:54 -04:00
2007-06-24 16:46:29 -04:00
'bizzaro': color.build('magenta', 'green'),
2007-03-06 10:05:38 -05:00
}
def name(self):
return "C"