pmacs3/mode_javascript.py

98 lines
4.7 KiB
Python

import color, mode2, tab2
from lex3 import Grammar, PatternRule, RegionRule
from point2 import Point
from mode_python import StringGrammar
class JavascriptGrammar(Grammar):
rules = [
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_]*"),
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_])'),
PatternRule(r"method", r"(?<=\.)[a-zA-Z_][a-zA-Z0-9_]*(?= *\()"),
PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"),
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]+"),
# 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!
PatternRule(r'delimiter', r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
PatternRule(r'operator', r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\'),
RegionRule('regex', "/", StringGrammar, "/"),
RegionRule('string', "'", StringGrammar, "'"),
RegionRule('string', '"', StringGrammar, '"'),
]
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]
return t.name == 'reserved' and t.string == 'function'
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
class Javascript(mode2.Fundamental):
grammar = JavascriptGrammar
tabbercls = JavascriptTabber
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_bindings('close-paren', (')',))
self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',))
self.colors = {
'comment': color.build('red', 'default'),
'comment.start': color.build('red', 'default'),
'comment.null': color.build('red', 'default'),
'comment.end': color.build('red', 'default'),
'continuation': color.build('red', 'default'),
'function': color.build('blue', 'default'),
'class': color.build('green', 'default'),
'reserved': color.build('cyan', 'default'),
'nonreserved': color.build('cyan', 'default'),
'delimiter': color.build('default', 'default'),
'operator': color.build('default', 'default'),
'integer': color.build('default', 'default'),
'float': color.build('default', 'default'),
'string.start': color.build('green', 'default'),
'string.null': color.build('green', 'default'),
'string.octal': color.build('magenta', 'default'),
'string.escaped': color.build('magenta', 'default'),
'string.end': color.build('green', 'default'),
'regex.start': color.build('cyan', 'default'),
'regex.null': color.build('cyan', 'default'),
'regex.octal': color.build('magenta', 'default'),
'regex.escaped': color.build('magenta', 'default'),
'regex.end': color.build('cyan', 'default'),
}
def name(self):
return "Javascript"