Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | # 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 |
| 7 | import os |
| 8 | |
| 9 | # Local imports |
| 10 | from .pgen2 import token |
| 11 | from .pgen2 import driver |
| 12 | from . import pytree |
| 13 | |
| 14 | # The grammar file |
| 15 | _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt") |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 16 | _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), |
| 17 | "PatternGrammar.txt") |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | class 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öwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 28 | for name, symbol in grammar.symbol2number.items(): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 29 | setattr(self, name, symbol) |
| 30 | |
| 31 | |
| 32 | python_grammar = driver.load_grammar(_GRAMMAR_FILE) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 33 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 34 | python_symbols = Symbols(python_grammar) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 35 | |
| 36 | python_grammar_no_print_statement = python_grammar.copy() |
| 37 | del python_grammar_no_print_statement.keywords["print"] |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 38 | |
| 39 | pattern_grammar = driver.load_grammar(_PATTERN_GRAMMAR_FILE) |
| 40 | pattern_symbols = Symbols(pattern_grammar) |