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