1 import os 2 from pyparser import parser, pytoken, metaparser 3 4 class PythonGrammar(parser.Grammar): 5 6 KEYWORD_TOKEN = pytoken.python_tokens["NAME"] 7 TOKENS = pytoken.python_tokens 8 OPERATOR_MAP = pytoken.python_opmap 9 10 def _get_python_grammar(): 11 here = os.path.dirname(__file__) 12 fp = open(os.path.join(here, "data", "Grammar-Lichen")) 13 try: 14 gram_source = fp.read() 15 finally: 16 fp.close() 17 pgen = metaparser.ParserGenerator(gram_source) 18 return pgen.build_grammar(PythonGrammar) 19 20 21 python_grammar = _get_python_grammar() 22 23 # For token module compatibility, expose name-to-index and index-to-name 24 # mappings. 25 26 tokens = pytoken.python_tokens 27 tok_name = pytoken.python_opmap 28 29 # For symbol module compatibility, expose name-to-index and index-to-name 30 # mappings. 31 32 syms = python_grammar.symbol_ids 33 sym_name = {} 34 for name, idx in python_grammar.symbol_ids.iteritems(): 35 sym_name[idx] = name 36 37 del _get_python_grammar, name, idx