All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 # Copyright 2006 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Export the Python grammar and symbols."""
9 from typing import Union
12 from .pgen2 import token
13 from .pgen2 import driver
15 from .pgen2.grammar import Grammar
17 # Moved into initialize because mypyc can't handle __file__ (XXX bug)
19 # _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
20 # _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
21 # "PatternGrammar.txt")
24 class Symbols(object):
25 def __init__(self, grammar: Grammar) -> None:
28 Creates an attribute for each grammar symbol (nonterminal),
29 whose value is the symbol's type (an int >= 256).
31 for name, symbol in grammar.symbol2number.items():
32 setattr(self, name, symbol)
35 class _python_symbols(Symbols):
115 testlist_star_expr: int
136 class _pattern_symbols(Symbols):
146 python_grammar: Grammar
147 python_grammar_no_print_statement: Grammar
148 python_grammar_no_print_statement_no_exec_statement: Grammar
149 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
150 python_grammar_no_exec_statement: Grammar
151 pattern_grammar: Grammar
153 python_symbols: _python_symbols
154 pattern_symbols: _pattern_symbols
157 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
158 global python_grammar
159 global python_grammar_no_print_statement
160 global python_grammar_no_print_statement_no_exec_statement
161 global python_grammar_no_print_statement_no_exec_statement_async_keywords
162 global python_symbols
163 global pattern_grammar
164 global pattern_symbols
167 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
168 _PATTERN_GRAMMAR_FILE = os.path.join(
169 os.path.dirname(__file__), "PatternGrammar.txt"
173 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
175 python_symbols = _python_symbols(python_grammar)
177 # Python 2 + from __future__ import print_function
178 python_grammar_no_print_statement = python_grammar.copy()
179 del python_grammar_no_print_statement.keywords["print"]
182 python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
183 del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
184 del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
187 python_grammar_no_print_statement_no_exec_statement_async_keywords = (
188 python_grammar_no_print_statement_no_exec_statement.copy()
190 python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
194 pattern_grammar = driver.load_packaged_grammar(
195 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
197 pattern_symbols = _pattern_symbols(pattern_grammar)