import color, mode, tab from lex import Grammar, PatternRule, RegionRule, PatternGroupRule from point import Point 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'[^/\\]+'), ] class JavascriptGrammar(Grammar): rules = [ PatternRule(r'comment', r'//.*$'), RegionRule(r'comment', '/\*', Grammar, '\*/'), PatternRule(r'continuation', r'\\(?= *$)'), 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_]*'), 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_])'), 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_]*(?= *\()"), 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'eol', r'\n'), PatternRule(r'spaces', r' +'), PatternRule(r'delimiter', r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='), PatternRule(r'operator', r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\|\|\|'), RegionRule('js_regex', "/", RegexGrammar, "/[a-z]*"), RegionRule('string', "'", StringGrammar1, "'"), RegionRule('string', '"', StringGrammar2, '"'), ] class JavascriptTabber2(tab.StackTabber2): open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} control_tokens = {'keyword': {'if': 1, 'else': 1, 'while': 1, 'do': 1, 'for': 1}} end_at_eof = False end_at_tokens = {'delimiter': {';': 1}} nocontinue_tokens = {'delimiter': {';': 1}, 'comment': 1, 'comment.start': 1, 'comment.data': 1, 'comment.end': 1} start_free_tokens = {'string.start': 'string.end'} end_free_tokens = {'string.end': 'string.start'} 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 == 'js_reserved' and t.string == 'function' def _is_indent(self, t): return t.name == 'spaces' def _is_ignored(self, t): return t.fqname() in ('spaces', 'eol', 'comment', 'comment.start', 'comment.data', 'comment.null', 'comment.end') class Javascript(mode.Fundamental): modename = 'Javascript' extensions = ['.js'] grammar = JavascriptGrammar tabbercls = JavascriptTabber2 commentc = '//' opentokens = ('delimiter',) opentags = {'(': ')', '[': ']', '{': '}'} closetokens = ('delimiter',) closetags = {')': '(', ']': '[', '}': '{'} colors = { 'js_function': ('blue', 'default', 'bold'), 'js_class': ('magenta', 'default', 'bold'), 'js_reserved': ('cyan', 'default', 'bold'), 'js_regex.start': ('cyan', 'default', 'bold'), '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'), } _bindings = { 'close-paren': (')',), 'close-brace': ('}',), 'close-bracket': (']',), } install = Javascript.install