pmacs3/mode/javascript.py

108 lines
4.9 KiB
Python
Raw Normal View History

2007-10-21 20:55:29 -04:00
import color, mode, tab
2008-09-30 18:05:42 -04:00
from lex import Grammar, PatternRule, RegionRule, PatternGroupRule
from point import Point
2008-09-30 18:05:42 -04:00
class StringGrammar1(Grammar):
rules = [
PatternRule(r'octal', r'\\[0-7]{3}'),
PatternRule(r'hex', r'\\x[0-9a-fA-F]{2}'),
PatternRule(r'escaped', r'\\.'),
PatternRule(r'data', r'[^\\\']+'),
]
class StringGrammar2(Grammar):
rules = [
PatternRule(r'octal', r'\\[0-7]{3}'),
PatternRule(r'hex', r'\\x[0-9a-fA-F]{2}'),
PatternRule(r'escaped', r'\\.'),
PatternRule(r'data', r'[^\\\"]+'),
]
class RegexGrammar(Grammar):
rules = [
PatternRule(r'octal', r'\\[0-7]{3}'),
PatternRule(r'hex', r'\\x[0-9a-fA-F]{2}'),
PatternRule(r'escaped', r'\\.'),
PatternRule(r'data', r'[^/\\]+'),
]
2007-07-21 11:40:53 -04:00
class JavascriptGrammar(Grammar):
rules = [
PatternRule(r'comment', r'//.*$'),
RegionRule(r'comment', '/\*', Grammar, '\*/'),
PatternRule(r'continuation', r'\\(?= *$)'),
2008-09-30 18:05:42 -04:00
PatternGroupRule(r'func_def', 'js_reserved', r'function', r'spaces', r' +', r'js_function', r'[a-zA-Z_][a-zA-Z0-9_]*'),
PatternGroupRule(r'class_def', 'js_reserved', r'class', r'spaces', r' +', r'js_class', r'[a-zA-Z_][a-zA-Z0-9_]*'),
PatternGroupRule(r'class_new', r'js_reserved', r'new', r'spaces', r' +', r'js_class', r'[a-zA-Z_][a-zA-Z0-9_]*'),
2007-07-21 11:40:53 -04:00
2008-09-30 18:05:42 -04:00
PatternRule(r'js_reserved', r'(?:abstract|as|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|function|get|goto|if|import|implements|include|instanceof|interface|in|is|namespace|native|new|null|package|private|protected|public|return|set|super|switch|synchronized|this|throws|throw|transient|true|try|typeof|use|var|void|volatile|while|with)(?![a-zA-Z0-9_])'),
2007-07-21 11:40:53 -04:00
2008-09-30 18:05:42 -04:00
PatternGroupRule(r'func_use', r"js_function", r"[a-zA-Z_][a-zA-Z0-9_]", r'spaces', ' +',
r'delimiter', r"\("),
PatternRule(r"js_function", r"[a-zA-Z_][a-zA-Z0-9_]*(?= *\()"),
2007-07-21 11:40:53 -04:00
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!
2008-09-29 21:02:23 -04:00
PatternRule(r'eol', r'\n'),
PatternRule(r'spaces', r' +'),
2007-07-21 11:40:53 -04:00
PatternRule(r'delimiter', r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
2008-09-29 21:02:23 -04:00
PatternRule(r'operator', r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\|\|\|'),
2007-07-21 11:40:53 -04:00
2008-09-30 18:05:42 -04:00
RegionRule('js_regex', "/", RegexGrammar, "/[a-z]*"),
RegionRule('string', "'", StringGrammar1, "'"),
RegionRule('string', '"', StringGrammar2, '"'),
2007-07-21 11:40:53 -04:00
]
2007-10-21 20:55:29 -04:00
class JavascriptTabber(tab.StackTabber):
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
t = highlighter.tokens[y][0]
return t.name == 'reserved' and t.string == 'function'
def _handle_other_token(self, currlvl, y, i):
2008-04-02 19:06:52 -04:00
w = self.mode.tabwidth
2007-07-21 11:40:53 -04:00
token = self.get_token(y, i)
fqname = token.fqname()
if token.name == 'operator' and token.string == '=':
2008-04-02 19:06:52 -04:00
self._opt_append("cont", currlvl + w)
2007-07-21 11:40:53 -04:00
elif token.name == 'delimiter' and token.string == ";":
self._opt_pop("cont")
return currlvl
2007-10-21 20:55:29 -04:00
class Javascript(mode.Fundamental):
2007-10-18 17:07:35 -04:00
modename = 'Javascript'
extensions = ['.js']
2007-07-21 11:40:53 -04:00
grammar = JavascriptGrammar
tabbercls = JavascriptTabber
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
2007-10-18 17:07:35 -04:00
colors = {
'js_function': ('blue', 'default', 'bold'),
2008-09-30 18:05:42 -04:00
'js_class': ('magenta', 'default', 'bold'),
'js_reserved': ('cyan', 'default', 'bold'),
'js_regex.start': ('cyan', 'default', 'bold'),
2008-09-30 18:05:42 -04:00
'js_regex.data': ('cyan', 'default', 'bold'),
'js_regex.null': ('cyan', 'default', 'bold'),
'js_regex.octal': ('magenta', 'default', 'bold'),
'js_regex.escaped': ('magenta', 'default', 'bold'),
'js_regex.end': ('cyan', 'default', 'bold'),
2007-07-21 11:40:53 -04:00
}
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-19 02:41:33 -04:00
install = Javascript.install