pygram.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Export the Python grammar and symbols."""
  4. # Python imports
  5. import os
  6. # Local imports
  7. from .pgen2 import token
  8. from .pgen2 import driver
  9. from . import pytree
  10. # The grammar file
  11. _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
  12. _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
  13. "PatternGrammar.txt")
  14. class Symbols(object):
  15. def __init__(self, grammar):
  16. """Initializer.
  17. Creates an attribute for each grammar symbol (nonterminal),
  18. whose value is the symbol's type (an int >= 256).
  19. """
  20. for name, symbol in grammar.symbol2number.items():
  21. setattr(self, name, symbol)
  22. python_grammar = driver.load_packaged_grammar("lib2to3", _GRAMMAR_FILE)
  23. python_symbols = Symbols(python_grammar)
  24. python_grammar_no_print_statement = python_grammar.copy()
  25. del python_grammar_no_print_statement.keywords["print"]
  26. python_grammar_no_print_and_exec_statement = python_grammar_no_print_statement.copy()
  27. del python_grammar_no_print_and_exec_statement.keywords["exec"]
  28. pattern_grammar = driver.load_packaged_grammar("lib2to3", _PATTERN_GRAMMAR_FILE)
  29. pattern_symbols = Symbols(pattern_grammar)