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):
122 testlist_star_expr: int
141 class _pattern_symbols(Symbols):
151 python_grammar: Grammar
152 python_grammar_no_print_statement: Grammar
153 python_grammar_no_print_statement_no_exec_statement: Grammar
154 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
155 python_grammar_no_exec_statement: Grammar
156 pattern_grammar: Grammar
157 python_grammar_soft_keywords: Grammar
159 python_symbols: _python_symbols
160 pattern_symbols: _pattern_symbols
163 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
164 global python_grammar
165 global python_grammar_no_print_statement
166 global python_grammar_no_print_statement_no_exec_statement
167 global python_grammar_no_print_statement_no_exec_statement_async_keywords
168 global python_grammar_soft_keywords
169 global python_symbols
170 global pattern_grammar
171 global pattern_symbols
174 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
175 _PATTERN_GRAMMAR_FILE = os.path.join(
176 os.path.dirname(__file__), "PatternGrammar.txt"
180 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
181 python_grammar.version = (2, 0)
183 soft_keywords = python_grammar.soft_keywords.copy()
184 python_grammar.soft_keywords.clear()
186 python_symbols = _python_symbols(python_grammar)
188 # Python 2 + from __future__ import print_function
189 python_grammar_no_print_statement = python_grammar.copy()
190 del python_grammar_no_print_statement.keywords["print"]
193 python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
194 del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
195 del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
196 python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
199 python_grammar_no_print_statement_no_exec_statement_async_keywords = (
200 python_grammar_no_print_statement_no_exec_statement.copy()
202 python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
205 python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
208 python_grammar_soft_keywords = (
209 python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
211 python_grammar_soft_keywords.soft_keywords = soft_keywords
212 python_grammar_soft_keywords.version = (3, 10)
214 pattern_grammar = driver.load_packaged_grammar(
215 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
217 pattern_symbols = _pattern_symbols(pattern_grammar)