2007-10-21 20:55:29 -04:00
|
|
|
import color, mode, tab
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2008-10-01 16:58:43 -04:00
|
|
|
from mode.python import StringGrammar2
|
2007-08-02 17:43:04 -04:00
|
|
|
from mode.c import CTabber
|
|
|
|
|
|
|
|
class JavaGrammar(Grammar):
|
|
|
|
rules = [
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r"spaces", r" +"),
|
2007-08-03 09:43:57 -04:00
|
|
|
PatternRule(r'import', r'(?<=import ) *[a-zA-Z0-9_.*]+'),
|
|
|
|
PatternRule(r'package', r'(?<=package ) *[a-zA-Z0-9_.*]+'),
|
2007-08-02 17:43:04 -04:00
|
|
|
|
2007-08-03 09:43:57 -04:00
|
|
|
RegionRule(r'doccomment', '/\*\*', Grammar, '\*/'),
|
2007-08-02 17:43:04 -04:00
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
|
|
|
PatternRule(r'comment', r'//.*$'),
|
|
|
|
|
2007-08-03 09:43:57 -04:00
|
|
|
PatternRule(r'keyword', r"(?:abstract|assert|boolean|break|byte|case|catch|char|class|continue|default|double|do|else|extends|finally|final|float|for|if|implements|import|instanceof|interface|int|long|native|new|package|private|protected|public|return|short|static|switch|super|synchronized|threadsafe|throws|throw|transient|try|void|while)(?![a-zA-Z_])"),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'java_label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'),
|
2007-08-02 17:43:04 -04:00
|
|
|
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'java_builtin', r"(?:null|true|false|this)"),
|
2007-08-02 17:43:04 -04:00
|
|
|
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"),
|
|
|
|
PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="),
|
|
|
|
PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
|
|
|
|
|
|
|
|
PatternRule(r"delimiter", r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"),
|
|
|
|
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r"java_integer", r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"),
|
|
|
|
PatternRule(r"java_float", r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"),
|
2008-10-01 16:58:43 -04:00
|
|
|
RegionRule(r'string', '"', StringGrammar2, '"'),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'java_char', r"'.'|'\\.'|'\\[0-7]{3}'"),
|
2007-08-02 17:43:04 -04:00
|
|
|
PatternRule(r"eol", r"\n$"),
|
|
|
|
]
|
|
|
|
|
2007-08-03 09:43:57 -04:00
|
|
|
class JavaTabber(CTabber):
|
|
|
|
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
|
|
|
|
|
|
|
|
for t in highlighter.tokens[y]:
|
|
|
|
if t.name == 'null':
|
|
|
|
pass
|
|
|
|
elif t.name == 'keyword':
|
|
|
|
if t.string in ('class', 'interface'):
|
|
|
|
return True
|
|
|
|
elif t.string in ('public', 'private', 'protected', 'static',
|
|
|
|
'final', 'native', 'synchronized', 'abstract',
|
|
|
|
'threadsafe', 'transient'):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
# 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-10-21 20:55:29 -04:00
|
|
|
currlvl = tab.StackTabber._handle_open_token(self, currlvl, y, i)
|
2007-08-03 09:43:57 -04:00
|
|
|
return currlvl
|
|
|
|
def _handle_close_token(self, currlvl, y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
w = self.mode.tabwidth
|
2007-08-03 09:43:57 -04:00
|
|
|
self._opt_pop('cont')
|
2007-10-21 20:55:29 -04:00
|
|
|
currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i)
|
2007-08-03 09:43:57 -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')
|
|
|
|
elif self._peek_name() == 'cond':
|
|
|
|
pass
|
|
|
|
else:
|
2008-04-02 19:06:52 -04:00
|
|
|
self._opt_append('cont', currlvl + w)
|
2007-08-03 09:43:57 -04:00
|
|
|
return currlvl
|
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
w = self.mode.tabwidth
|
2007-08-03 09:43:57 -04:00
|
|
|
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')
|
|
|
|
|
|
|
|
elif fqname == 'keyword':
|
|
|
|
if token.string in ('do', 'else', 'for', 'if', 'while'):
|
2008-04-02 19:06:52 -04:00
|
|
|
self._append('cond', currlvl + w)
|
2007-08-03 09:43:57 -04:00
|
|
|
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()
|
2008-04-02 19:06:52 -04:00
|
|
|
self._opt_append('case', currlvl + w)
|
2007-08-03 09:43:57 -04:00
|
|
|
|
|
|
|
elif fqname == 'string.start':
|
|
|
|
self._opt_append('string', None)
|
|
|
|
elif fqname == 'string.end':
|
|
|
|
self._opt_pop('string')
|
|
|
|
if self.is_rightmost_token(y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
self._opt_append('cont', currlvl + w)
|
2007-08-03 09:43:57 -04:00
|
|
|
|
|
|
|
# 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 == 'eol' and
|
|
|
|
token.string not in ('}', ';', '(', '{', '[', ',')):
|
2008-04-02 19:06:52 -04:00
|
|
|
self._opt_append('cont', currlvl + w)
|
2007-08-03 09:43:57 -04:00
|
|
|
return currlvl
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class Java(mode.Fundamental):
|
2007-10-18 17:07:35 -04:00
|
|
|
modename = 'Java'
|
|
|
|
extensions = ['.java']
|
|
|
|
tabbercls = JavaTabber
|
|
|
|
grammar = JavaGrammar
|
2007-08-02 17:43:04 -04:00
|
|
|
opentokens = ('delimiter',)
|
2007-10-18 17:07:35 -04:00
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
2007-08-02 17:43:04 -04:00
|
|
|
closetokens = ('delimiter',)
|
2007-10-18 17:07:35 -04:00
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
|
|
|
colors = {
|
2008-05-03 13:31:30 -04:00
|
|
|
'doccomment.start': ('red', 'default', 'bold'),
|
|
|
|
'doccomment.end': ('red', 'default', 'bold'),
|
|
|
|
'doccomment.null': ('red', 'default', 'bold'),
|
|
|
|
'import': ('blue', 'default', 'bold'),
|
|
|
|
'java_label': ('magenta', 'default', 'bold'),
|
|
|
|
'java_builtin': ('magenta', 'default', 'bold'),
|
|
|
|
'java_char': ('green', 'default', 'bold'),
|
|
|
|
'java_integer': ('green', 'default', 'bold'),
|
|
|
|
'java_float': ('green', 'default', 'bold'),
|
2007-08-02 17:43:04 -04:00
|
|
|
}
|
|
|
|
def __init__(self, w):
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.Fundamental.__init__(self, w)
|
2007-08-02 17:43:04 -04:00
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = Java.install
|