| 1 |
# Copyright 2006 Google, Inc. All Rights Reserved. |
| 2 |
# Licensed to PSF under a Contributor Agreement. |
| 3 |
|
| 4 |
"""Export the Python grammar and symbols.""" |
| 5 |
|
| 6 |
# Python imports |
| 7 |
import os |
| 8 |
from typing import Union |
| 9 |
|
| 10 |
# Local imports |
| 11 |
from .pgen2 import driver |
| 12 |
from .pgen2.grammar import Grammar |
| 13 |
|
| 14 |
# Moved into initialize because mypyc can't handle __file__ (XXX bug) |
| 15 |
# # The grammar file |
| 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") |
| 19 |
|
| 20 |
|
| 21 |
class Symbols: |
| 22 |
def __init__(self, grammar: Grammar) -> None: |
| 23 |
"""Initializer. |
| 24 |
|
| 25 |
Creates an attribute for each grammar symbol (nonterminal), |
| 26 |
whose value is the symbol's type (an int >= 256). |
| 27 |
""" |
| 28 |
for name, symbol in grammar.symbol2number.items(): |
| 29 |
setattr(self, name, symbol) |
| 30 |
|
| 31 |
|
| 32 |
class _python_symbols(Symbols): |
| 33 |
and_expr: int |
| 34 |
and_test: int |
| 35 |
annassign: int |
| 36 |
arglist: int |
| 37 |
argument: int |
| 38 |
arith_expr: int |
| 39 |
asexpr_test: int |
| 40 |
assert_stmt: int |
| 41 |
async_funcdef: int |
| 42 |
async_stmt: int |
| 43 |
atom: int |
| 44 |
augassign: int |
| 45 |
break_stmt: int |
| 46 |
case_block: int |
| 47 |
classdef: int |
| 48 |
comp_for: int |
| 49 |
comp_if: int |
| 50 |
comp_iter: int |
| 51 |
comp_op: int |
| 52 |
comparison: int |
| 53 |
compound_stmt: int |
| 54 |
continue_stmt: int |
| 55 |
decorated: int |
| 56 |
decorator: int |
| 57 |
decorators: int |
| 58 |
del_stmt: int |
| 59 |
dictsetmaker: int |
| 60 |
dotted_as_name: int |
| 61 |
dotted_as_names: int |
| 62 |
dotted_name: int |
| 63 |
encoding_decl: int |
| 64 |
eval_input: int |
| 65 |
except_clause: int |
| 66 |
expr: int |
| 67 |
expr_stmt: int |
| 68 |
exprlist: int |
| 69 |
factor: int |
| 70 |
file_input: int |
| 71 |
flow_stmt: int |
| 72 |
for_stmt: int |
| 73 |
fstring: int |
| 74 |
fstring_format_spec: int |
| 75 |
fstring_middle: int |
| 76 |
fstring_replacement_field: int |
| 77 |
funcdef: int |
| 78 |
global_stmt: int |
| 79 |
guard: int |
| 80 |
if_stmt: int |
| 81 |
import_as_name: int |
| 82 |
import_as_names: int |
| 83 |
import_from: int |
| 84 |
import_name: int |
| 85 |
import_stmt: int |
| 86 |
lazy_import: int |
| 87 |
lambdef: int |
| 88 |
listmaker: int |
| 89 |
match_stmt: int |
| 90 |
namedexpr_test: int |
| 91 |
not_test: int |
| 92 |
old_comp_for: int |
| 93 |
old_comp_if: int |
| 94 |
old_comp_iter: int |
| 95 |
old_lambdef: int |
| 96 |
old_test: int |
| 97 |
or_test: int |
| 98 |
parameters: int |
| 99 |
paramspec: int |
| 100 |
pass_stmt: int |
| 101 |
pattern: int |
| 102 |
patterns: int |
| 103 |
power: int |
| 104 |
raise_stmt: int |
| 105 |
return_stmt: int |
| 106 |
shift_expr: int |
| 107 |
simple_stmt: int |
| 108 |
single_input: int |
| 109 |
sliceop: int |
| 110 |
small_stmt: int |
| 111 |
subject_expr: int |
| 112 |
star_expr: int |
| 113 |
stmt: int |
| 114 |
subscript: int |
| 115 |
subscriptlist: int |
| 116 |
suite: int |
| 117 |
term: int |
| 118 |
test: int |
| 119 |
testlist: int |
| 120 |
testlist1: int |
| 121 |
testlist_gexp: int |
| 122 |
testlist_safe: int |
| 123 |
testlist_star_expr: int |
| 124 |
tfpdef: int |
| 125 |
tfplist: int |
| 126 |
tname: int |
| 127 |
tname_star: int |
| 128 |
trailer: int |
| 129 |
try_stmt: int |
| 130 |
tstring: int |
| 131 |
tstring_format_spec: int |
| 132 |
tstring_middle: int |
| 133 |
tstring_replacement_field: int |
| 134 |
type_stmt: int |
| 135 |
typedargslist: int |
| 136 |
typeparam: int |
| 137 |
typeparams: int |
| 138 |
typevar: int |
| 139 |
typevartuple: int |
| 140 |
varargslist: int |
| 141 |
vfpdef: int |
| 142 |
vfplist: int |
| 143 |
vname: int |
| 144 |
while_stmt: int |
| 145 |
with_stmt: int |
| 146 |
xor_expr: int |
| 147 |
yield_arg: int |
| 148 |
yield_expr: int |
| 149 |
yield_stmt: int |
| 150 |
|
| 151 |
|
| 152 |
class _pattern_symbols(Symbols): |
| 153 |
Alternative: int |
| 154 |
Alternatives: int |
| 155 |
Details: int |
| 156 |
Matcher: int |
| 157 |
NegatedUnit: int |
| 158 |
Repeater: int |
| 159 |
Unit: int |
| 160 |
|
| 161 |
|
| 162 |
python_grammar: Grammar |
| 163 |
python_grammar_async_keywords: Grammar |
| 164 |
python_grammar_soft_keywords: Grammar |
| 165 |
pattern_grammar: Grammar |
| 166 |
python_symbols: _python_symbols |
| 167 |
pattern_symbols: _pattern_symbols |
| 168 |
|
| 169 |
|
| 170 |
def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None: |
| 171 |
global python_grammar |
| 172 |
global python_grammar_async_keywords |
| 173 |
global python_grammar_soft_keywords |
| 174 |
global python_symbols |
| 175 |
global pattern_grammar |
| 176 |
global pattern_symbols |
| 177 |
|
| 178 |
# The grammar file |
| 179 |
_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt") |
| 180 |
_PATTERN_GRAMMAR_FILE = os.path.join( |
| 181 |
os.path.dirname(__file__), "PatternGrammar.txt" |
| 182 |
) |
| 183 |
|
| 184 |
python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir) |
| 185 |
assert "print" not in python_grammar.keywords |
| 186 |
assert "exec" not in python_grammar.keywords |
| 187 |
|
| 188 |
soft_keywords = python_grammar.soft_keywords.copy() |
| 189 |
python_grammar.soft_keywords.clear() |
| 190 |
|
| 191 |
python_symbols = _python_symbols(python_grammar) |
| 192 |
|
| 193 |
# Python 3.0-3.6 |
| 194 |
python_grammar.version = (3, 0) |
| 195 |
|
| 196 |
# Python 3.7+ |
| 197 |
python_grammar_async_keywords = python_grammar.copy() |
| 198 |
python_grammar_async_keywords.async_keywords = True |
| 199 |
python_grammar_async_keywords.version = (3, 7) |
| 200 |
|
| 201 |
# Python 3.10+ |
| 202 |
python_grammar_soft_keywords = python_grammar_async_keywords.copy() |
| 203 |
python_grammar_soft_keywords.soft_keywords = soft_keywords |
| 204 |
python_grammar_soft_keywords.version = (3, 10) |
| 205 |
|
| 206 |
pattern_grammar = driver.load_packaged_grammar( |
| 207 |
"blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir |
| 208 |
) |
| 209 |
pattern_symbols = _pattern_symbols(pattern_grammar) |