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
):
120 testlist_star_expr
: int
145 class _pattern_symbols(Symbols
):
155 python_grammar
: Grammar
156 python_grammar_no_print_statement
: Grammar
157 python_grammar_no_print_statement_no_exec_statement
: Grammar
158 python_grammar_no_print_statement_no_exec_statement_async_keywords
: Grammar
159 python_grammar_no_exec_statement
: Grammar
160 pattern_grammar
: Grammar
161 python_grammar_soft_keywords
: Grammar
163 python_symbols
: _python_symbols
164 pattern_symbols
: _pattern_symbols
167 def initialize(cache_dir
: Union
[str, "os.PathLike[str]", None] = None) -> None:
168 global python_grammar
169 global python_grammar_no_print_statement
170 global python_grammar_no_print_statement_no_exec_statement
171 global python_grammar_no_print_statement_no_exec_statement_async_keywords
172 global python_grammar_soft_keywords
173 global python_symbols
174 global pattern_grammar
175 global pattern_symbols
178 _GRAMMAR_FILE
= os
.path
.join(os
.path
.dirname(__file__
), "Grammar.txt")
179 _PATTERN_GRAMMAR_FILE
= os
.path
.join(
180 os
.path
.dirname(__file__
), "PatternGrammar.txt"
184 python_grammar
= driver
.load_packaged_grammar("blib2to3", _GRAMMAR_FILE
, cache_dir
)
185 python_grammar
.version
= (2, 0)
187 soft_keywords
= python_grammar
.soft_keywords
.copy()
188 python_grammar
.soft_keywords
.clear()
190 python_symbols
= _python_symbols(python_grammar
)
192 # Python 2 + from __future__ import print_function
193 python_grammar_no_print_statement
= python_grammar
.copy()
194 del python_grammar_no_print_statement
.keywords
["print"]
197 python_grammar_no_print_statement_no_exec_statement
= python_grammar
.copy()
198 del python_grammar_no_print_statement_no_exec_statement
.keywords
["print"]
199 del python_grammar_no_print_statement_no_exec_statement
.keywords
["exec"]
200 python_grammar_no_print_statement_no_exec_statement
.version
= (3, 0)
203 python_grammar_no_print_statement_no_exec_statement_async_keywords
= (
204 python_grammar_no_print_statement_no_exec_statement
.copy()
206 python_grammar_no_print_statement_no_exec_statement_async_keywords
.async_keywords
= (
209 python_grammar_no_print_statement_no_exec_statement_async_keywords
.version
= (3, 7)
212 python_grammar_soft_keywords
= (
213 python_grammar_no_print_statement_no_exec_statement_async_keywords
.copy()
215 python_grammar_soft_keywords
.soft_keywords
= soft_keywords
216 python_grammar_soft_keywords
.version
= (3, 10)
218 pattern_grammar
= driver
.load_packaged_grammar(
219 "blib2to3", _PATTERN_GRAMMAR_FILE
, cache_dir
221 pattern_symbols
= _pattern_symbols(pattern_grammar
)