2007-10-17 17:47:07 -04:00
|
|
|
import os, popen2, re
|
2007-10-21 20:55:29 -04:00
|
|
|
import color, default, method, mode, tab
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2007-07-21 11:40:53 -04:00
|
|
|
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$'),
|
|
|
|
|
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
|
|
|
PatternRule(r'comment', r'//.*$'),
|
|
|
|
|
2007-08-02 17:43:04 -04:00
|
|
|
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-21 11:40:53 -04:00
|
|
|
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'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}'"),
|
2007-08-03 12:33:25 -04:00
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
PatternRule(r'eol', r"\n$"),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class CTabber(tab.StackTabber):
|
2007-08-03 12:33:25 -04:00
|
|
|
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
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
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._opt_pop('cond')
|
2007-08-03 12:33:25 -04:00
|
|
|
self._opt_pop('cont')
|
|
|
|
if self.is_leftmost_token(y, i):
|
|
|
|
currlvl = self.get_curr_level()
|
2007-10-21 20:55:29 -04:00
|
|
|
tab.StackTabber._handle_open_token(self, currlvl, y, i)
|
2007-07-21 11:40:53 -04:00
|
|
|
return currlvl
|
|
|
|
def _handle_close_token(self, currlvl, y, i):
|
|
|
|
self._opt_pop('cont')
|
2007-10-21 20:55:29 -04:00
|
|
|
currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i)
|
2007-07-21 11:40:53 -04:00
|
|
|
token = self.get_token(y, i)
|
|
|
|
if self.is_rightmost_token(y, i):
|
|
|
|
if token.string == '}':
|
|
|
|
self._opt_pop('cond')
|
|
|
|
self._opt_pop('cont')
|
2007-08-03 12:33:25 -04:00
|
|
|
elif self._has_markers() and self._peek_name() == 'cond':
|
2007-07-21 11:40:53 -04:00
|
|
|
pass
|
|
|
|
else:
|
2007-08-03 12:33:25 -04:00
|
|
|
if token.fqname() != 'macro.delimiter':
|
|
|
|
self._opt_append('cont', currlvl + 4)
|
2007-07-21 11:40:53 -04:00
|
|
|
return currlvl
|
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
|
|
|
token = self.get_token(y, i)
|
|
|
|
fqname = token.fqname()
|
|
|
|
if fqname == 'delimiter' and token.string == ';':
|
|
|
|
self._opt_pop('cond')
|
|
|
|
self._opt_pop('cont')
|
|
|
|
self._opt_pop('cond')
|
2007-08-03 12:33:25 -04:00
|
|
|
self._opt_pop('cont')
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
elif fqname == 'keyword':
|
|
|
|
if token.string in ('do', 'else', 'for', 'if', 'while'):
|
|
|
|
self._append('cond', currlvl + 4)
|
|
|
|
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 + 4)
|
|
|
|
|
|
|
|
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 + 4)
|
|
|
|
|
|
|
|
# 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
|
2007-08-03 12:33:25 -04:00
|
|
|
#not fqname == 'null' and
|
|
|
|
not fqname == 'spaces' and
|
2007-07-21 11:40:53 -04:00
|
|
|
not fqname == 'eol' and
|
|
|
|
token.string not in ('}', ';', '(', '{', '[', ',')):
|
|
|
|
self._opt_append('cont', currlvl + 4)
|
|
|
|
return currlvl
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class C(mode.Fundamental):
|
2007-10-18 17:07:35 -04:00
|
|
|
modename = 'C'
|
|
|
|
extensions = ['.c']
|
|
|
|
tabbercls = CTabber
|
|
|
|
grammar = CGrammar
|
2007-07-21 11:40:53 -04:00
|
|
|
opentokens = ('delimiter',)
|
2007-10-18 17:07:35 -04:00
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
2007-07-21 11:40:53 -04:00
|
|
|
closetokens = ('delimiter',)
|
2007-10-18 17:07:35 -04:00
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
|
|
|
colors = {
|
2007-07-21 11:40:53 -04:00
|
|
|
'macrocomment.start': ('red', 'default'),
|
|
|
|
'macrocomment.null': ('red', 'default'),
|
|
|
|
'macrocomment.end': ('red', 'default'),
|
|
|
|
'comment': ('red', 'default'),
|
|
|
|
'comment.start': ('red', 'default'),
|
|
|
|
'comment.end': ('red', 'default'),
|
|
|
|
'comment.null': ('red', 'default'),
|
|
|
|
|
|
|
|
'include': ('blue', 'default'),
|
|
|
|
'header': ('green', 'default'),
|
|
|
|
|
|
|
|
'macro': ('blue', 'default'),
|
|
|
|
'macro.start': ('blue', 'default'),
|
|
|
|
'macro.name': ('yellow', 'default'),
|
|
|
|
'macro.null': ('magenta', 'default'),
|
|
|
|
#'macro.null': ('default', 'default'),
|
|
|
|
'macro.continued': ('red', 'default'),
|
|
|
|
'macro.delimiter': ('default', 'default'),
|
|
|
|
'macro.integer': ('green', 'default'),
|
|
|
|
'macro.float': ('green', 'default'),
|
|
|
|
'macro.char': ('green', 'default'),
|
|
|
|
'macro.string.start': ('green', 'default'),
|
|
|
|
'macro.string.escaped': ('magenta', 'default'),
|
|
|
|
'macro.string.octal': ('magenta', 'default'),
|
|
|
|
#'macro.string.escaped': ('default', 'default'),
|
|
|
|
#'macro.string.octal': ('default', 'default'),
|
|
|
|
'macro.string.null': ('green', 'default'),
|
|
|
|
'macro.string.end': ('green', 'default'),
|
|
|
|
'macro.end': ('magenta', 'default'),
|
|
|
|
#'macro.end': ('default', 'default'),
|
|
|
|
|
|
|
|
'label': ('magenta', 'default'),
|
|
|
|
'keyword': ('cyan', 'default'),
|
|
|
|
'function': ('blue', 'default'),
|
|
|
|
'builtin': ('magenta', 'default'),
|
|
|
|
'structname': ('yellow', 'default'),
|
|
|
|
'enumname': ('yellow', 'default'),
|
|
|
|
|
|
|
|
'char': ('green', 'default'),
|
|
|
|
'string.start': ('green', 'default'),
|
|
|
|
'string.octal': ('green', 'default'),
|
|
|
|
'string.escaped': ('green', 'default'),
|
|
|
|
'string.null': ('green', 'default'),
|
|
|
|
'string.end': ('green', 'default'),
|
|
|
|
'integer': ('green', 'default'),
|
|
|
|
'float': ('green', 'default'),
|
|
|
|
|
|
|
|
'bizzaro': ('magenta', 'green'),
|
|
|
|
}
|
|
|
|
def __init__(self, w):
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.Fundamental.__init__(self, w)
|
2007-07-21 11:40:53 -04:00
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
2007-10-17 17:39:26 -04:00
|
|
|
self.add_action_and_bindings(CMake(), ('C-c C-c',))
|
2007-10-17 17:47:07 -04:00
|
|
|
self.add_action_and_bindings(CSetMake(), ('C-c M-m',))
|
2007-10-17 17:39:26 -04:00
|
|
|
self.makecmd = "make"
|
|
|
|
|
|
|
|
class CSetMake(method.Method):
|
|
|
|
'''Set the path(s) to find perl modules'''
|
|
|
|
args = [method.Argument("cmd", type=type(""), prompt="Make Cmd: ",
|
|
|
|
default=default.build_constant("make"))]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.mode.makecmd = vargs['cmd']
|
|
|
|
|
|
|
|
class CMake(method.Method):
|
|
|
|
'''Check the syntax of the current python file'''
|
|
|
|
def _execute(self, w, **vargs):
|
2007-10-17 17:47:07 -04:00
|
|
|
p = popen2.Popen4(w.mode.makecmd)
|
2007-10-17 17:39:26 -04:00
|
|
|
p.tochild.close()
|
|
|
|
output = p.fromchild.read()
|
2007-10-17 17:59:08 -04:00
|
|
|
|
2007-10-17 17:39:26 -04:00
|
|
|
result = p.wait()
|
2007-10-17 17:59:08 -04:00
|
|
|
status = os.WEXITSTATUS(result)
|
2007-10-17 17:39:26 -04:00
|
|
|
|
|
|
|
if not os.WIFEXITED(result):
|
2007-10-17 17:59:08 -04:00
|
|
|
err = True
|
|
|
|
errmsg = "make: killed by signal %r" % os.WTERMSIG(result)
|
|
|
|
elif status != 0:
|
|
|
|
err = True
|
|
|
|
errmsg = "make: failed with status %r" % status
|
2007-10-17 17:39:26 -04:00
|
|
|
else:
|
2007-10-17 17:59:08 -04:00
|
|
|
err = False
|
|
|
|
errmsg = "make: OK (output in *CMake*)"
|
|
|
|
|
|
|
|
w.application.data_buffer("*CMake*", output, switch_to=err)
|
|
|
|
w.set_error(errmsg)
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = C.install
|