2009-04-01 15:07:28 -04:00
|
|
|
import os.path
|
|
|
|
import time
|
|
|
|
import tab
|
|
|
|
from mode import Fundamental
|
|
|
|
from method.shell import Interact
|
2009-03-28 22:20:37 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule, PatternMatchRule
|
2007-10-21 20:50:11 -04:00
|
|
|
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
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
chr1 = '[a-zA-Z_]'
|
|
|
|
chr2 = '[a-zA-Z_0-9]'
|
|
|
|
word = chr1 + chr2 + '*'
|
|
|
|
|
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
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
PatternMatchRule(r'x', r'(function)( +)('+word+')', 'js_reserved', r'spaces', r'js_function'),
|
|
|
|
PatternMatchRule(r'x', r'(class|new)( +)('+word+')', 'js_reserved', r'spaces', r'js_class'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2009-03-30 01:10:25 -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)(?!'+chr2+')'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
PatternRule(r'identifier', word),
|
2007-07-21 11:40:53 -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]+"),
|
|
|
|
|
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}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
|
|
|
|
|
2009-03-30 01:10:25 -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.
|
|
|
|
RegionRule('js_regex', "(?<=[\(=:,]) */", RegexGrammar, "/[a-z]*"),
|
|
|
|
PatternRule(r'operator', r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\|\|\|'),
|
|
|
|
|
2008-09-30 18:05:42 -04:00
|
|
|
RegionRule('string', "'", StringGrammar1, "'"),
|
|
|
|
RegionRule('string', '"', StringGrammar2, '"'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2009-02-02 17:08:33 -05:00
|
|
|
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}}
|
2009-03-30 01:10:25 -04:00
|
|
|
nocontinue_tokens = {'delimiter': {';': 1, ',': 1},
|
2009-02-02 17:08:33 -05:00
|
|
|
'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'}
|
2007-07-21 11:40:53 -04:00
|
|
|
def is_base(self, y):
|
2009-03-30 01:10:25 -04:00
|
|
|
if y == 0: return True
|
2009-03-17 15:24:10 -04:00
|
|
|
highlighter = self.mode.window.buffer.highlights[self.mode.name]
|
2009-03-30 01:10:25 -04:00
|
|
|
if not highlighter.tokens[y]: return False
|
2007-07-21 11:40:53 -04:00
|
|
|
t = highlighter.tokens[y][0]
|
2009-02-02 17:08:33 -05:00
|
|
|
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')
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2009-04-01 15:07:28 -04:00
|
|
|
class RhinoStart(Interact):
|
|
|
|
args = []
|
2009-04-07 00:34:26 -04:00
|
|
|
reuse = True
|
2009-04-01 15:07:28 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
cmd = w.application.config.get('rhino.cmd', 'rhino')
|
|
|
|
Interact._execute(self, w, bname='*Rhino*', cmd=cmd)
|
|
|
|
|
|
|
|
class RhinoLoadFile(RhinoStart):
|
|
|
|
args = []
|
2009-04-07 00:34:26 -04:00
|
|
|
reuse = True
|
2009-04-01 15:07:28 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
RhinoStart._execute(self, w, **vargs)
|
|
|
|
b = w.application.get_buffer_by_name('*Rhino*')
|
|
|
|
path = os.path.realpath(w.buffer.path)
|
|
|
|
time.sleep(0.5)
|
|
|
|
b.pipe_write('load("env.js");\n')
|
|
|
|
time.sleep(0.5)
|
|
|
|
b.pipe_write('load("%s");\n' % path)
|
|
|
|
|
|
|
|
class Javascript(Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'Javascript'
|
2007-10-18 17:07:35 -04:00
|
|
|
extensions = ['.js']
|
2007-07-21 11:40:53 -04:00
|
|
|
grammar = JavascriptGrammar
|
2009-02-02 17:08:33 -05:00
|
|
|
tabbercls = JavascriptTabber2
|
2009-02-15 12:06:35 -05:00
|
|
|
commentc = '//'
|
2007-07-21 11:40:53 -04:00
|
|
|
opentokens = ('delimiter',)
|
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
|
|
|
closetokens = ('delimiter',)
|
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
2007-10-18 17:07:35 -04:00
|
|
|
colors = {
|
2008-05-03 13:31:30 -04:00
|
|
|
'js_function': ('blue', 'default', 'bold'),
|
2008-09-30 18:05:42 -04:00
|
|
|
'js_class': ('magenta', 'default', 'bold'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'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'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'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
|
|
|
}
|
2009-04-01 15:07:28 -04:00
|
|
|
config = {'rhino.cmd': 'rhino'}
|
|
|
|
actions = [RhinoStart, RhinoLoadFile]
|
2009-02-15 12:06:35 -05:00
|
|
|
_bindings = {
|
|
|
|
'close-paren': (')',),
|
|
|
|
'close-brace': ('}',),
|
|
|
|
'close-bracket': (']',),
|
|
|
|
}
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = Javascript.install
|