pmacs3/mode/c.py

321 lines
14 KiB
Python

import os, re
from subprocess import Popen, PIPE, STDOUT
import color, context, default, method, method.shell, mode, tab
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
from mode.python import StringGrammar
# this might not be complete...
# see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_3.html#SEC44
class MacroGrammar(Grammar):
rules = [
PatternRule('name', r'(?:(?<=#define )) *[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=|\^=|>>=|<<=|\*\*=|\|="),
PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
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}'"),
PatternRule(r"continued", r"\\\n$"),
]
class CGrammar(Grammar):
rules = [
PatternRule(r'include', r'#include(?!=[a-zA-Z0-9_])'),
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'),
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'\*/'),
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_])"),
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'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"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
# 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"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}'"),
PatternRule(r'spaces', r' +'),
PatternRule(r'eol', r"\n$"),
]
class CTabber(tab.StackTabber):
wst = ('spaces', 'eol', 'comment', 'comment.start', 'comment.null', 'comment.end')
def token_is_whitespace(self, y, i):
token = self.get_token(y, i)
return token.fqname() in self.wst
def is_base(self, y):
if y == 0:
return True
highlighter = self.mode.window.buffer.highlights[self.mode.name()]
if not highlighter.tokens[y]:
return False
# this assumes that people aren't gonna use these macros inside of
# blocks, which is probably ok.
t0 = highlighter.tokens[y][0]
if t0.name == 'macro.start' and t0.string in ('#define', '#include'):
return True
# detecting function declarations is annoying; this assumes that people
# won't put a variable type and name on different lines, but that they
# might do that for function return type and name.
#
# unfortunately, valid function return types might include any of the
# four types of tokens below
decl = False
for t in highlighter.tokens[y]:
if t.name in ('keyword', 'identifier', 'structname', 'enumname'):
decl = True
continue
if decl and t.name == 'function':
break
else:
decl = False
break
if decl:
return True
return False
def _handle_open_token(self, currlvl, y, i):
self._opt_pop('cont')
token = self.get_token(y, i)
if token.string == '{':
self._pop_while('cond', 'cont', 'case')
if self.is_leftmost_token(y, i):
currlvl = self.get_curr_level()
tab.StackTabber._handle_open_token(self, currlvl, y, i)
return currlvl
def _handle_close_token(self, currlvl, y, i):
w = self.mode.tabwidth
self._opt_pop('cont')
token = self.get_token(y, i)
if token.string == '}':
self._opt_pop('case')
currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i)
if self.is_rightmost_token(y, i):
if token.string == '}':
self._pop_while('cond', 'cont', 'case')
elif self._has_markers() and self._peek_name() == 'cond':
pass
else:
if token.fqname() != 'macro.delimiter':
self._opt_append('cont', currlvl + w)
return currlvl
def _handle_other_token(self, currlvl, y, i):
w = self.mode.tabwidth
token = self.get_token(y, i)
fqname = token.fqname()
if fqname == 'delimiter' and token.string == ';':
self._pop_while('cond', 'cont')
elif fqname == 'keyword':
if token.string in ('do', 'else', 'for', 'if', 'while'):
self._append('cond', currlvl + w)
elif token.string == 'break':
self._opt_pop('case', 'while', 'for')
elif token.string == 'continue':
self._opt_pop('while', 'for')
elif token.string == 'case':
self._opt_pop('case')
currlvl = self.get_curr_level()
self._opt_append('case', currlvl + w)
elif fqname == 'string.start':
self._opt_append('string', None)
elif fqname == 'string.end':
self._opt_pop('string')
if self.is_rightmost_token(y, i):
self._opt_append('cont', currlvl + w)
# TODO: this could be a lot better
elif fqname == 'macro':
currlvl = 0
elif fqname.startswith('macro.start'):
self._opt_append('macro', None)
currlvl = 0
elif fqname.startswith('macro.end'):
self._opt_pop('macro', None)
elif fqname.startswith('macroblock.start'):
self._opt_append('macroblock', None)
currlvl = 0
elif fqname.startswith('macroblock.end'):
self._opt_pop('macroblock', None)
if self.is_rightmost_token(y, i):
if self._has_markers() and self._peek_name() == 'cond':
pass
elif(not fqname.startswith('string') and
not fqname.startswith('comment') and
not fqname.startswith('macro') and
not fqname == 'delimiter' and
not fqname == 'header' and
#not fqname == 'null' and
not fqname == 'spaces' and
not fqname == 'eol' and
token.string not in ('}', ';', '(', '{', '[', ',')):
self._opt_append('cont', currlvl + w)
return currlvl
class CCheckSyntax(method.shell.Exec):
'''Build this C program (using the mode's make cmd)'''
show_success = False
args = []
def _execute(self, w, **vargs):
if w.application.config['c.syntax-rel-dir']:
d = os.path.dirname(w.buffer.path)
self._doit(w, w.buffer.path, w.application.config['c.syntax-cmd'],
cmdname='c-check-syntax', cmddir=d)
else:
self._doit(w, w.buffer.path, w.application.config['c.syntax-cmd'],
cmdname='c-check-syntax')
class CMake(method.shell.Exec):
'''Build this C program (using the mode's make cmd)'''
show_success = False
args = []
def _execute(self, w, **vargs):
if w.application.config['c.make-rel-dir']:
d = os.path.dirname(w.buffer.path)
self._doit(w, w.buffer.path, w.application.config['c.make-cmd'],
cmdname='c-make', cmddir=d)
else:
self._doit(w, w.buffer.path, w.application.config['c.make-cmd'],
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.
#self.build_name_map()
raise Exception, "we're totally hosed"
return
stack.pop(-1)
if curr:
self.namelines[i] = (curr, tuple(stack))
i += 1
class C(mode.Fundamental):
modename = 'C'
extensions = ['.c', '.h', '.cpp']
tabbercls = CTabber
grammar = CGrammar
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
colors = {
'macrocomment.start': ('red', 'default', 'bold'),
'macrocomment.null': ('red', 'default', 'bold'),
'macrocomment.end': ('red', 'default', 'bold'),
'macro': ('blue', 'default', 'bold'),
'macro.start': ('blue', 'default', 'bold'),
'macro.name': ('yellow', 'default', 'bold'),
'macro.null': ('magenta', 'default', 'bold'),
'macro.continued': ('red', 'default', 'bold'),
'macro.delimiter': ('default', 'default', 'bold'),
'macro.integer': ('green', 'default', 'bold'),
'macro.float': ('green', 'default', 'bold'),
'macro.char': ('green', 'default', 'bold'),
'macro.string.start': ('green', 'default', 'bold'),
'macro.string.escaped': ('magenta', 'default', 'bold'),
'macro.string.octal': ('magenta', 'default', 'bold'),
'macro.string.null': ('green', 'default', 'bold'),
'macro.string.end': ('green', 'default', 'bold'),
'macro.end': ('magenta', 'default', 'bold'),
'include': ('blue', 'default', 'bold'),
'header': ('green', 'default', 'bold'),
'structname': ('yellow', 'default', 'bold'),
'enumname': ('yellow', 'default', 'bold'),
'c_type': ('green', 'default', 'bold'),
}
config = {
'c.syntax-cmd': "gcc -x c -fsyntax-only %(path)s",
'c.syntax-rel-dir': False,
'c.make-cmd': "make",
'c.make-rel-dir': True,
}
actions = [CCheckSyntax, CMake]
format = "%(flag)s %(bname)-18s (%(mname)s) %(indent)s %(cursor)s/%(mark)s %(perc)s [%(func)s]"
def get_status_names(self):
names = mode.Fundamental.get_status_names(self)
c = self.window.logical_cursor()
names['func'] = self.get_line_function(c.y)
return names
def __init__(self, w):
mode.Fundamental.__init__(self, w)
self.add_bindings('close-paren', (')',))
self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',))
self.add_bindings('c-check-syntax', ('C-c s',))
self.add_bindings('c-make', ('C-c C-c',))
self.context = CContext(self)
self.functions = None
self.funclines = None
def get_functions(self):
return self.context.get_names()
def get_function_names(self):
return self.context.get_name_list()
def get_line_function(self, y):
return self.context.get_line_name(y)
install = C.install