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