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 driver
14 from .pgen2.grammar import Grammar
16 # Moved into initialize because mypyc can't handle __file__ (XXX bug)
18 # _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
19 # _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
20 # "PatternGrammar.txt")
24 def __init__(self, grammar: Grammar) -> None:
27 Creates an attribute for each grammar symbol (nonterminal),
28 whose value is the symbol's type (an int >= 256).
30 for name, symbol in grammar.symbol2number.items():
31 setattr(self, name, symbol)
34 class _python_symbols(Symbols):
122 testlist_star_expr: int
147 class _pattern_symbols(Symbols):
157 python_grammar: Grammar
158 python_grammar_no_print_statement: Grammar
159 python_grammar_no_print_statement_no_exec_statement: Grammar
160 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
161 python_grammar_no_exec_statement: Grammar
162 pattern_grammar: Grammar
163 python_grammar_soft_keywords: Grammar
165 python_symbols: _python_symbols
166 pattern_symbols: _pattern_symbols
169 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
170 global python_grammar
171 global python_grammar_no_print_statement
172 global python_grammar_no_print_statement_no_exec_statement
173 global python_grammar_no_print_statement_no_exec_statement_async_keywords
174 global python_grammar_soft_keywords
175 global python_symbols
176 global pattern_grammar
177 global pattern_symbols
180 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
181 _PATTERN_GRAMMAR_FILE = os.path.join(
182 os.path.dirname(__file__), "PatternGrammar.txt"
186 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
187 python_grammar.version = (2, 0)
189 soft_keywords = python_grammar.soft_keywords.copy()
190 python_grammar.soft_keywords.clear()
192 python_symbols = _python_symbols(python_grammar)
194 # Python 2 + from __future__ import print_function
195 python_grammar_no_print_statement = python_grammar.copy()
196 del python_grammar_no_print_statement.keywords["print"]
199 python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
200 del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
201 del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
202 python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
205 python_grammar_no_print_statement_no_exec_statement_async_keywords = (
206 python_grammar_no_print_statement_no_exec_statement.copy()
208 python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
211 python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
214 python_grammar_soft_keywords = (
215 python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
217 python_grammar_soft_keywords.soft_keywords = soft_keywords
218 python_grammar_soft_keywords.version = (3, 10)
220 pattern_grammar = driver.load_packaged_grammar(
221 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
223 pattern_symbols = _pattern_symbols(pattern_grammar)