83 lines
3.1 KiB
Python
Executable File
83 lines
3.1 KiB
Python
Executable File
#!/bin/env python
|
|
|
|
# 2.3 imports
|
|
from optparse import OptionParser
|
|
|
|
# our imports
|
|
import lex
|
|
|
|
class JavascriptGrammar(lex.Grammar):
|
|
GRAMMAR_LIST = [
|
|
## {'name': "import statement",
|
|
## 'expr': r"""(?:^|(?<= ))import [ .]*(?=\n)""",
|
|
## 'action': lex.make_token},
|
|
|
|
{'name': "comment",
|
|
'expr': r'//.*(?=\n|$)',
|
|
'action': lex.make_token},
|
|
|
|
{'name': "function declaration",
|
|
'expr': r"(?<=function ) *[a-zA-Z0-9_]* *(?=\()",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "class declaration",
|
|
'expr': r"(?<=class )[a-zA-Z_][a-zA-Z0-9_]*",
|
|
'action': lex.make_token},
|
|
|
|
{'name': 'keyword',
|
|
'expr': r"""(?:and|break|class|continue|def|del|elif|else|except|exec|finally|for|from|function|global|if|import|in|is|lambda|new|not|or|pass|print|raise|return|try|var|while|yield)(?![a-zA-Z0-9_])""",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "pseudo-keyword",
|
|
'expr': r"""(?:as|self|True|False|None|Exception)(?![a-zA-Z0-9_])""",
|
|
'action': lex.make_token},
|
|
|
|
## {'name': "built-in method",
|
|
## 'expr': r"""(?<!\.)(?:bool|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|frozenset|getattr|globals|hasattr|hash|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|long|map|min|max|object|oct|open|ord|pow|property|range|raw_input|reduce|repr|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)(?![a-zA-Z0-9_])""",
|
|
## 'action': lex.make_token},
|
|
|
|
{'name': "bound method",
|
|
'expr': r"(?<=\.)[a-zA-Z_][a-zA-Z0-9_]*(?= *\()",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "identifier",
|
|
'expr': r"[a-zA-Z_][a-zA-Z0-9_]*",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "delimiter",
|
|
'expr': r"""\(|\)|\[|\]|{|}|@|,|:|\.|`|=|;|\+=|-=|\*=|/=|//=|%=|&=|\|=|\^=|>>=|<<=|\*\*=""",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "operator",
|
|
'expr': r"""\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%""",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "integer",
|
|
'expr': r"(?:0|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "float",
|
|
'expr': r"""[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+""",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "imaginary",
|
|
'expr': r"""[0-9]+|(?:[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+)[jJ]""",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "string1",
|
|
'expr': r'"(?:\\.|[^\\"])*(?:"|.?$)',
|
|
'action': lex.make_token},
|
|
|
|
{'name': "string2",
|
|
'expr': r"'(?:\\.|[^\\'])*(?:'|.?$)",
|
|
'action': lex.make_token},
|
|
|
|
{'name': "continuation",
|
|
'expr': r'\\(?=(?:\n|$))',
|
|
'action': lex.make_token},
|
|
|
|
{'name': "default",
|
|
'expr': r'\\.|.|\n',
|
|
'action': lex.silent}
|
|
]
|