#!/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"""(?>=|<<=|\*\*=""", '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} ]