c improvements and benchmarking

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-09-23 13:58:23 +00:00
parent 9fcc1a1fd9
commit 2868deaec3
2 changed files with 35 additions and 77 deletions

View File

@ -584,6 +584,8 @@ class Application(object):
def run(self): def run(self):
self.done = False self.done = False
self.draw() self.draw()
if os.getenv('PMC_EARLY_OUT'):
return
while not self.done: while not self.done:
i = self.win.getch() i = self.win.getch()

110
mode/c.py
View File

@ -1,7 +1,7 @@
import os, re import os, re
from subprocess import Popen, PIPE, STDOUT from subprocess import Popen, PIPE, STDOUT
import color, context, default, method, method.shell, mode, tab import color, default, method, method.shell, mode, tab
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverridePatternRule
from mode.python import StringGrammar from mode.python import StringGrammar
# this might not be complete... # this might not be complete...
@ -21,37 +21,44 @@ class MacroGrammar(Grammar):
class CGrammar(Grammar): class CGrammar(Grammar):
rules = [ rules = [
PatternRule(r'include', r'#include(?!=[a-zA-Z0-9_])'), PatternRule(r'spaces', r' +'),
PatternRule(r'header', r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'),
RegionRule(r'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'), PatternRule(r"delimiter", r"\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=(?!=)|\?|->"),
PatternRule(r'eol', r"\n$"),
PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
PatternGroupRule(r'structgroup', r'keyword', r'struct', r'spaces',
r' +', r'structname', r'[a-zA-Z_][a-zA-Z0-9_]*'),
PatternGroupRule(r'enumgroup', r'keyword', r'enum', r'spaces',
r' +', r'enumname', r'[a-zA-Z_][a-zA-Z0-9_]*'),
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"(?:NULL|TRUE|FALSE)"),
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*(?! *[\(:])"),
RegionRule(r'macro', r'#(?:assert|cpu|define|elif|else|endif|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar, r'\n$'), RegionRule(r'macro', r'#(?:assert|cpu|define|elif|else|endif|error|ident|ifdef|ifndef|if|import|include_next|line|machine|pragma|pragma_once|system|unassert|undef|warning)(?!=[a-zA-Z0-9_])', MacroGrammar, r'\n$'),
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_]+) *$'),
RegionRule(r'comment', r'/\*', Grammar, r'\*/'), RegionRule(r'comment', r'/\*', Grammar, r'\*/'),
PatternRule(r'comment', r'//.*$'), PatternRule(r'comment', 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_])"), RegionRule(r'string', '"', StringGrammar, '"'),
PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
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_]*(?= *\()'),
PatternRule(r'builtin', r"(?:NULL|TRUE|FALSE)"), PatternRule(r"unop", r"!|\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="),
PatternRule(r'c_type', r'[a-zA-Z_][a-zA-Z0-9_]*(?= +[a-zA-Z_])'),
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="),
PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
# this is sketchy as hell
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"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]+"), 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, '"'),
RegionRule(r'macrocomment', r'#if +(?:0|NULL|FALSE)', Grammar, r'#endif'),
PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"), PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"),
PatternRule(r'spaces', r' +'), PatternRule(r'include', r'#include(?!=[a-zA-Z0-9_])'),
PatternRule(r'eol', r"\n$"), PatternRule(r'header', r'<[-A-Za-z/0-9_\.]+>|"[-A-Za-z/0-9_\.]+"'),
#PatternRule(r'enumname', r'(?<=enum ) *[a-zA-Z_][a-zA-Z0-9_]*'),
#PatternRule(r'structname', r'(?<=struct ) *[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_]+) *$'),
] ]
class CTabber(tab.StackTabber): class CTabber(tab.StackTabber):
@ -201,54 +208,6 @@ class CMake(method.shell.Exec):
self._doit(w, w.buffer.path, w.application.config['c.make-cmd'], self._doit(w, w.buffer.path, w.application.config['c.make-cmd'],
cmdname='c-make') cmdname='c-make')
class CContext(context.Context):
def _regen_stack(self, y):
if y > 0 and self.namelines[y - 1][1]:
return list(self.namelines[y - 1][1])
else:
return []
def _build_name_map(self, y1, y2, last, curr, stack):
blen = len(self.mode.window.buffer.lines)
highlights = self.mode.window.get_highlighter()
i = y1
starting = curr and not bool(stack)
while i < y2:
if not starting and not stack:
curr = None
g = highlights.tokens[i]
if not stack and len(g) > 2 and g[0].name in ('c_type', 'keyword'):
g2 = [x for x in g if x.name != 'spaces']
j = 0
while j < len(g2) - 1 and g2[j].name in ('keyword', 'c_type'):
j += 1
if j < len(g2) and g2[j].name == 'function':
curr = g2[j].string
starting = True
if curr is not None and curr not in self.names:
self.names[curr] = i
if i == y2 - 1 and curr != self.namelines[i][0] and y2 < blen:
y2 += 1
m = self.mode
for t in g:
if t.name == 'delimiter' and t.string == '{':
stack.append(t.string)
starting = False
elif t.name == 'delimiter' and t.string == '}':
if not stack:
# we are totally hosed. start over. ugh.
#raise Exception, "we're totally hosed"
self.build_name_map()
return
stack.pop(-1)
if curr:
self.namelines[i] = (curr, tuple(stack))
i += 1
class C(mode.Fundamental): class C(mode.Fundamental):
modename = 'C' modename = 'C'
extensions = ['.c', '.h', '.cpp'] extensions = ['.c', '.h', '.cpp']
@ -279,9 +238,9 @@ class C(mode.Fundamental):
'macro.end': ('magenta', 'default', 'bold'), 'macro.end': ('magenta', 'default', 'bold'),
'include': ('blue', 'default', 'bold'), 'include': ('blue', 'default', 'bold'),
'header': ('green', 'default', 'bold'), 'header': ('green', 'default', 'bold'),
'structname': ('yellow', 'default', 'bold'), #'structname': ('yellow', 'default', 'bold'),
'enumname': ('yellow', 'default', 'bold'), #'enumname': ('yellow', 'default', 'bold'),
'c_type': ('green', 'default', 'bold'), #'c_type': ('green', 'default', 'bold'),
} }
config = { config = {
'c.syntax-cmd': "gcc -x c -fsyntax-only %(path)s", 'c.syntax-cmd': "gcc -x c -fsyntax-only %(path)s",
@ -306,9 +265,6 @@ class C(mode.Fundamental):
self.add_bindings('close-bracket', (']',)) self.add_bindings('close-bracket', (']',))
self.add_bindings('c-check-syntax', ('C-c s',)) self.add_bindings('c-check-syntax', ('C-c s',))
self.add_bindings('c-make', ('C-c C-c',)) self.add_bindings('c-make', ('C-c C-c',))
#self.context = CContext(self)
#self.functions = None
#self.funclines = None
def get_functions(self): def get_functions(self):
#return self.context.get_names() #return self.context.get_names()