blob: 01fa1087115b2f57c997c948150d047a31c26490 [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001# Copyright 2006 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Export the Python grammar and symbols."""
5
6# Python imports
7import os
8
9# Local imports
10from .pgen2 import token
11from .pgen2 import driver
12from . import pytree
13
14# The grammar file
15_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000016_PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
17 "PatternGrammar.txt")
Martin v. Löwisef04c442008-03-19 05:04:44 +000018
19
20class Symbols(object):
21
22 def __init__(self, grammar):
23 """Initializer.
24
25 Creates an attribute for each grammar symbol (nonterminal),
26 whose value is the symbol's type (an int >= 256).
27 """
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000028 for name, symbol in grammar.symbol2number.items():
Martin v. Löwisef04c442008-03-19 05:04:44 +000029 setattr(self, name, symbol)
30
31
32python_grammar = driver.load_grammar(_GRAMMAR_FILE)
Benjamin Peterson3059b002009-07-20 16:42:03 +000033
Martin v. Löwisef04c442008-03-19 05:04:44 +000034python_symbols = Symbols(python_grammar)
Benjamin Peterson3059b002009-07-20 16:42:03 +000035
36python_grammar_no_print_statement = python_grammar.copy()
37del python_grammar_no_print_statement.keywords["print"]
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000038
39pattern_grammar = driver.load_grammar(_PATTERN_GRAMMAR_FILE)
40pattern_symbols = Symbols(pattern_grammar)