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
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")
21class Symbols:
22 def __init__(self, grammar: Grammar) -> None:
23 """Initializer.
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)
32class _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 lambdef: int
87 listmaker: int
88 match_stmt: int
89 namedexpr_test: int
90 not_test: int
91 old_comp_for: int
92 old_comp_if: int
93 old_comp_iter: int
94 old_lambdef: int
95 old_test: int
96 or_test: int
97 parameters: int
98 paramspec: int
99 pass_stmt: int
100 pattern: int
101 patterns: int
102 power: int
103 raise_stmt: int
104 return_stmt: int
105 shift_expr: int
106 simple_stmt: int
107 single_input: int
108 sliceop: int
109 small_stmt: int
110 subject_expr: int
111 star_expr: int
112 stmt: int
113 subscript: int
114 subscriptlist: int
115 suite: int
116 term: int
117 test: int
118 testlist: int
119 testlist1: int
120 testlist_gexp: int
121 testlist_safe: int
122 testlist_star_expr: int
123 tfpdef: int
124 tfplist: int
125 tname: int
126 tname_star: int
127 trailer: int
128 try_stmt: int
129 type_stmt: int
130 typedargslist: int
131 typeparam: int
132 typeparams: int
133 typevar: int
134 typevartuple: int
135 varargslist: int
136 vfpdef: int
137 vfplist: int
138 vname: int
139 while_stmt: int
140 with_stmt: int
141 xor_expr: int
142 yield_arg: int
143 yield_expr: int
144 yield_stmt: int
147class _pattern_symbols(Symbols):
148 Alternative: int
149 Alternatives: int
150 Details: int
151 Matcher: int
152 NegatedUnit: int
153 Repeater: int
154 Unit: int
157python_grammar: Grammar
158python_grammar_async_keywords: Grammar
159python_grammar_soft_keywords: Grammar
160pattern_grammar: Grammar
161python_symbols: _python_symbols
162pattern_symbols: _pattern_symbols
165def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
166 global python_grammar
167 global python_grammar_async_keywords
168 global python_grammar_soft_keywords
169 global python_symbols
170 global pattern_grammar
171 global pattern_symbols
173 # The grammar file
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"
177 )
179 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
180 assert "print" not in python_grammar.keywords
181 assert "exec" not in python_grammar.keywords
183 soft_keywords = python_grammar.soft_keywords.copy()
184 python_grammar.soft_keywords.clear()
186 python_symbols = _python_symbols(python_grammar)
188 # Python 3.0-3.6
189 python_grammar.version = (3, 0)
191 # Python 3.7+
192 python_grammar_async_keywords = python_grammar.copy()
193 python_grammar_async_keywords.async_keywords = True
194 python_grammar_async_keywords.version = (3, 7)
196 # Python 3.10+
197 python_grammar_soft_keywords = python_grammar_async_keywords.copy()
198 python_grammar_soft_keywords.soft_keywords = soft_keywords
199 python_grammar_soft_keywords.version = (3, 10)
201 pattern_grammar = driver.load_packaged_grammar(
202 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
203 )
204 pattern_symbols = _pattern_symbols(pattern_grammar)