pmacs3/mode_javascript.py

76 lines
4.1 KiB
Python
Raw Normal View History

2007-06-25 10:06:16 -04:00
import color, mode2
from lex2 import Grammar, PatternRule, RegionRule
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 = [
PatternRule(name=r'comment', pattern=r'//.*$'),
RegionRule(name=r'comment', start='/\*', grammar=Grammar(), end='\*/'),
PatternRule(name=r'continuation', pattern=r'\\(?= *$)'),
PatternRule(name=r'function', pattern=r"(?<=function )[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(name=r'class', pattern=r"(?<=class )[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(name=r'reserved1', pattern=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(name=r'reserved2', pattern=r'(?:abstract|debugger|enum|goto|implements|interface|native|protected|synchronized|throws|transient|volatile)(?![a-zA-Z0-9_])'),
PatternRule(name=r'nonreserved', pattern=r'(?:get|include|set)(?![a-zA-Z0-9_])'),
PatternRule(name=r"method", pattern=r"(?<=\.)[a-zA-Z_][a-zA-Z0-9_]*(?= *\()"),
PatternRule(name=r'identifier', pattern=r"[a-zA-Z_][a-zA-Z0-9_]*"),
2007-03-06 10:05:38 -05:00
2007-06-25 10:06:16 -04:00
PatternRule(name=r'integer', pattern=r"(?:0|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"),
PatternRule(name=r'float', pattern=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!
PatternRule(name=r'delimiter', pattern=r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
PatternRule(name=r'operator', pattern=r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\'),
RegionRule(name='regex', start="/", grammar=StringGrammar(), end="/"),
RegionRule(name='string', start="'", grammar=StringGrammar(), end="'"),
RegionRule(name='string', start='"', grammar=StringGrammar(), end='"'),
]
class Javascript(mode2.Fundamental):
grammar = JavascriptGrammar()
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', (']',))
self.colors = {
2007-06-25 10:06:16 -04:00
'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'),
'reserved1': color.build('cyan', 'default'),
'reserved2': color.build('cyan', 'default'),
'nonreserved': color.build('cyan', 'default'),
2007-03-06 10:05:38 -05:00
2007-06-25 10:06:16 -04:00
'delimiter': color.build('default', 'default'),
'operator': color.build('default', 'default'),
'integer': color.build('default', 'default'),
'float': color.build('default', 'default'),
2007-03-06 10:05:38 -05:00
2007-06-25 10:06:16 -04:00
'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'),
}
2007-03-06 10:05:38 -05:00
def name(self):
return "Javascript"