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 tstring: int
130 tstring_format_spec: int
131 tstring_middle: int
132 tstring_replacement_field: int
133 type_stmt: int
134 typedargslist: int
135 typeparam: int
136 typeparams: int
137 typevar: int
138 typevartuple: int
139 varargslist: int
140 vfpdef: int
141 vfplist: int
142 vname: int
143 while_stmt: int
144 with_stmt: int
145 xor_expr: int
146 yield_arg: int
147 yield_expr: int
148 yield_stmt: int
151class _pattern_symbols(Symbols):
152 Alternative: int
153 Alternatives: int
154 Details: int
155 Matcher: int
156 NegatedUnit: int
157 Repeater: int
158 Unit: int
161python_grammar: Grammar
162python_grammar_async_keywords: Grammar
163python_grammar_soft_keywords: Grammar
164pattern_grammar: Grammar
165python_symbols: _python_symbols
166pattern_symbols: _pattern_symbols
169def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
170 global python_grammar
171 global python_grammar_async_keywords
172 global python_grammar_soft_keywords
173 global python_symbols
174 global pattern_grammar
175 global pattern_symbols
177 # The grammar file
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"
181 )
183 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
184 assert "print" not in python_grammar.keywords
185 assert "exec" not in python_grammar.keywords
187 soft_keywords = python_grammar.soft_keywords.copy()
188 python_grammar.soft_keywords.clear()
190 python_symbols = _python_symbols(python_grammar)
192 # Python 3.0-3.6
193 python_grammar.version = (3, 0)
195 # Python 3.7+
196 python_grammar_async_keywords = python_grammar.copy()
197 python_grammar_async_keywords.async_keywords = True
198 python_grammar_async_keywords.version = (3, 7)
200 # Python 3.10+
201 python_grammar_soft_keywords = python_grammar_async_keywords.copy()
202 python_grammar_soft_keywords.soft_keywords = soft_keywords
203 python_grammar_soft_keywords.version = (3, 10)
205 pattern_grammar = driver.load_packaged_grammar(
206 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
207 )
208 pattern_symbols = _pattern_symbols(pattern_grammar)