2007-07-12 18:14:39 -04:00
|
|
|
import color, mode2, tab2
|
2007-07-15 09:44:20 -04:00
|
|
|
from lex3 import Grammar, PatternRule, RegionRule
|
2007-06-25 10:06:16 -04:00
|
|
|
from point2 import Point
|
2007-07-11 04:55:54 -04:00
|
|
|
from mode_python import StringGrammar
|
2007-06-25 10:06:16 -04:00
|
|
|
|
|
|
|
class JavascriptGrammar(Grammar):
|
|
|
|
rules = [
|
2007-07-15 15:07:36 -04:00
|
|
|
PatternRule(r'comment', r'//.*$'),
|
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
|
|
|
PatternRule(r'continuation', r'\\(?= *$)'),
|
|
|
|
PatternRule(r'function', r"(?<=function )[a-zA-Z_][a-zA-Z0-9_]*"),
|
|
|
|
PatternRule(r'class', r"(?<=class )[a-zA-Z_][a-zA-Z0-9_]*"),
|
2007-06-25 10:06:16 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
PatternRule(r'reserved', r'(?:as|break|case|catch|class|const|continue|default|delete|do|else|export|extends|false|finally|for|function|if|import|in|instanceof|is|namespace|new|null|package|private|public|return|super|switch|this|throw|true|try|typeof|use|var|void|while|with)(?![a-zA-Z0-9_])'),
|
|
|
|
PatternRule(r'reserved', r'(?:abstract|debugger|enum|goto|implements|interface|native|protected|synchronized|throws|transient|volatile)(?![a-zA-Z0-9_])'),
|
|
|
|
PatternRule(r'nonreserved', r'(?:get|include|set)(?![a-zA-Z0-9_])'),
|
2007-06-25 10:06:16 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
PatternRule(r"method", r"(?<=\.)[a-zA-Z_][a-zA-Z0-9_]*(?= *\()"),
|
|
|
|
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"),
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
PatternRule(r'integer', r"(?:0|[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]+"),
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-06-25 10:06:16 -04:00
|
|
|
# fucking javascript!
|
|
|
|
# their lexer grammar requires one-token look-behind in order to know
|
|
|
|
# whether a "/" starts a literal regex, or is part of a mathematical
|
|
|
|
# expression/assignment. so for now we will require either a space or $
|
|
|
|
# after / in order to *not* treat it as a regex. dammit!
|
2007-07-15 15:07:36 -04:00
|
|
|
PatternRule(r'delimiter', r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
|
|
|
|
PatternRule(r'operator', r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\'),
|
2007-06-25 10:06:16 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
RegionRule('regex', "/", StringGrammar, "/"),
|
|
|
|
RegionRule('string', "'", StringGrammar, "'"),
|
|
|
|
RegionRule('string', '"', StringGrammar, '"'),
|
2007-06-25 10:06:16 -04:00
|
|
|
]
|
|
|
|
|
2007-07-12 18:14:39 -04:00
|
|
|
class JavascriptTabber(tab2.StackTabber):
|
|
|
|
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
|
|
|
|
t = highlighter.tokens[y][0]
|
2007-07-13 08:30:47 -04:00
|
|
|
return t.name == 'reserved' and t.string == 'function'
|
2007-07-12 18:14:39 -04:00
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
|
|
|
token = self.get_token(y, i)
|
|
|
|
fqname = token.fqname()
|
|
|
|
if token.name == 'operator' and token.string == '=':
|
|
|
|
self._opt_append("cont", currlvl + 4)
|
|
|
|
elif token.name == 'delimiter' and token.string == ";":
|
|
|
|
self._opt_pop("cont")
|
|
|
|
return currlvl
|
|
|
|
|
2007-06-25 10:06:16 -04:00
|
|
|
class Javascript(mode2.Fundamental):
|
2007-07-12 19:06:33 -04:00
|
|
|
grammar = JavascriptGrammar
|
|
|
|
tabbercls = JavascriptTabber
|
|
|
|
opentokens = ('delimiter',)
|
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
|
|
|
closetokens = ('delimiter',)
|
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
2007-07-18 07:22:12 -04:00
|
|
|
colors = {
|
|
|
|
'comment': ('red', 'default'),
|
|
|
|
'comment.start': ('red', 'default'),
|
|
|
|
'comment.null': ('red', 'default'),
|
|
|
|
'comment.end': ('red', 'default'),
|
|
|
|
'continuation': ('red', 'default'),
|
|
|
|
'function': ('blue', 'default'),
|
|
|
|
'class': ('green', 'default'),
|
|
|
|
|
|
|
|
'reserved': ('cyan', 'default'),
|
|
|
|
'nonreserved': ('cyan', 'default'),
|
|
|
|
|
|
|
|
'delimiter': ('default', 'default'),
|
|
|
|
'operator': ('default', 'default'),
|
|
|
|
'integer': ('default', 'default'),
|
|
|
|
'float': ('default', 'default'),
|
|
|
|
|
|
|
|
'string.start': ('green', 'default'),
|
|
|
|
'string.null': ('green', 'default'),
|
|
|
|
'string.octal': ('magenta', 'default'),
|
|
|
|
'string.escaped': ('magenta', 'default'),
|
|
|
|
'string.end': ('green', 'default'),
|
|
|
|
|
|
|
|
'regex.start': ('cyan', 'default'),
|
|
|
|
'regex.null': ('cyan', 'default'),
|
|
|
|
'regex.octal': ('magenta', 'default'),
|
|
|
|
'regex.escaped': ('magenta', 'default'),
|
|
|
|
'regex.end': ('cyan', 'default'),
|
|
|
|
}
|
2007-06-25 10:06:16 -04:00
|
|
|
def __init__(self, w):
|
|
|
|
mode2.Fundamental.__init__(self, w)
|
2007-03-06 10:05:38 -05:00
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
|
|
|
def name(self):
|
|
|
|
return "Javascript"
|