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."""
8 from typing import Union
11 from .pgen2 import driver
12 from .pgen2.grammar import Grammar
14 # Moved into initialize because mypyc can't handle __file__ (XXX bug)
16 # _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
17 # _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
18 # "PatternGrammar.txt")
22 def __init__(self, grammar: Grammar) -> None:
25 Creates an attribute for each grammar symbol (nonterminal),
26 whose value is the symbol's type (an int >= 256).
28 for name, symbol in grammar.symbol2number.items():
29 setattr(self, name, symbol)
32 class _python_symbols(Symbols):
118 testlist_star_expr: int
143 class _pattern_symbols(Symbols):
153 python_grammar: Grammar
154 python_grammar_async_keywords: Grammar
155 python_grammar_soft_keywords: Grammar
156 pattern_grammar: Grammar
157 python_symbols: _python_symbols
158 pattern_symbols: _pattern_symbols
161 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
162 global python_grammar
163 global python_grammar_async_keywords
164 global python_grammar_soft_keywords
165 global python_symbols
166 global pattern_grammar
167 global pattern_symbols
170 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
171 _PATTERN_GRAMMAR_FILE = os.path.join(
172 os.path.dirname(__file__), "PatternGrammar.txt"
175 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
176 assert "print" not in python_grammar.keywords
177 assert "exec" not in python_grammar.keywords
179 soft_keywords = python_grammar.soft_keywords.copy()
180 python_grammar.soft_keywords.clear()
182 python_symbols = _python_symbols(python_grammar)
185 python_grammar.version = (3, 0)
188 python_grammar_async_keywords = python_grammar.copy()
189 python_grammar_async_keywords.async_keywords = True
190 python_grammar_async_keywords.version = (3, 7)
193 python_grammar_soft_keywords = python_grammar_async_keywords.copy()
194 python_grammar_soft_keywords.soft_keywords = soft_keywords
195 python_grammar_soft_keywords.version = (3, 10)
197 pattern_grammar = driver.load_packaged_grammar(
198 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
200 pattern_symbols = _pattern_symbols(pattern_grammar)