From 34b16ad19e3260db94729fd651194d6a709fb729 Mon Sep 17 00:00:00 2001 From: moculus Date: Sun, 24 Jun 2007 15:05:44 +0000 Subject: [PATCH] --HG-- branch : pmacs2 --- application.py | 8 ++-- mode_c.py | 111 ++++++++++++++++++++++++++++++++++++++++++------- mode_perl.py | 2 - 3 files changed, 101 insertions(+), 20 deletions(-) diff --git a/application.py b/application.py index f0e929a..d96c583 100755 --- a/application.py +++ b/application.py @@ -7,12 +7,12 @@ import util, window2 from point2 import Point # modes -# TODO: mode_c mode_nasm mode_sql mode_javascript mode_tt +# TODO: mode_sql mode_javascript mode_tt import mode2 import mode_mini, mode_search, mode_replace, mode_which import mode_console, mode_consolemini import mode_blame, mode_diff -import mode_python, mode_perl, mode_xml, mode_nasm, mode_sh +import mode_c, mode_python, mode_perl, mode_xml, mode_nasm, mode_sh import mode_life, mode_text, mode_mutt def run(buffers, jump_to_line=None, init_mode=None): @@ -82,7 +82,7 @@ class Application(object): # initialize our modes self.modes = { 'blame': mode_blame.Blame, -# 'c': mode_c.C, + 'c': mode_c.C, 'console': mode_console.Console, 'consolemini': mode_consolemini.Console, 'diff': mode_diff.Diff, @@ -119,7 +119,7 @@ class Application(object): '.pl': 'perl', '.pm': 'perl', '.t': 'perl', -# '.c': 'c', + '.c': 'c', '.txt': 'text', '.s': 'nasm', '.sh': 'sh', diff --git a/mode_c.py b/mode_c.py index 49f170e..93b8ff2 100644 --- a/mode_c.py +++ b/mode_c.py @@ -1,20 +1,106 @@ -import sets, sys +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 -import color, mode, lex, lex_c, method, tab_c +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_\.]+"'), + ] -class C(mode.Fundamental): +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'(?>=|<<=|\*\*=""", + 'action': lex.make_token}, + + {'name': "operator", + 'expr': r"""\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%""", + 'action': lex.make_token}, + + # 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): - mode.Fundamental.__init__(self, w) - - self.tag_matching = True - self.grammar = lex_c.CGrammar() - self.lexer = lex.Lexer(self.grammar) - + mode2.Fundamental.__init__(self, w) self.add_bindings('close-paren', (')',)) self.add_bindings('close-brace', ('}',)) self.add_bindings('close-bracket', (']',)) - - self.default_color = color.build('default', 'default') self.colors = { 'control': color.build('blue', 'default', 'bold'), 'keyword': color.build('cyan', 'default', 'bold'), @@ -34,8 +120,5 @@ class C(mode.Fundamental): 'float': color.build('green', 'default'), 'bizzaro': color.build('magenta', 'green'), } - - self.tabber = tab_c.CTabber(self) - def name(self): return "C" diff --git a/mode_perl.py b/mode_perl.py index e65fcb1..4eaea5e 100644 --- a/mode_perl.py +++ b/mode_perl.py @@ -184,8 +184,6 @@ class Perl(mode2.Fundamental): self.add_bindings('close-bracket', (']')) self.add_bindings('close-brace', ('}')) - self.default_color = color.build('default', 'default') - self.colors = { # basic stuff 'escaped': color.build('magenta', 'default'),