]> git.madduck.net Git - etc/vim.git/blob - src/blib2to3/pygram.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Document shebang comment behaviour (#3787)
[etc/vim.git] / src / blib2to3 / pygram.py
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
9 from typing import Union
10
11 # Local imports
12 from .pgen2 import driver
13
14 from .pgen2.grammar import Grammar
15
16 # Moved into initialize because mypyc can't handle __file__ (XXX bug)
17 # # The grammar file
18 # _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
19 # _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
20 #                                      "PatternGrammar.txt")
21
22
23 class Symbols:
24     def __init__(self, grammar: Grammar) -> None:
25         """Initializer.
26
27         Creates an attribute for each grammar symbol (nonterminal),
28         whose value is the symbol's type (an int >= 256).
29         """
30         for name, symbol in grammar.symbol2number.items():
31             setattr(self, name, symbol)
32
33
34 class _python_symbols(Symbols):
35     and_expr: int
36     and_test: int
37     annassign: int
38     arglist: int
39     argument: int
40     arith_expr: int
41     asexpr_test: int
42     assert_stmt: int
43     async_funcdef: int
44     async_stmt: int
45     atom: int
46     augassign: int
47     break_stmt: int
48     case_block: int
49     classdef: int
50     comp_for: int
51     comp_if: int
52     comp_iter: int
53     comp_op: int
54     comparison: int
55     compound_stmt: int
56     continue_stmt: int
57     decorated: int
58     decorator: int
59     decorators: int
60     del_stmt: int
61     dictsetmaker: int
62     dotted_as_name: int
63     dotted_as_names: int
64     dotted_name: int
65     encoding_decl: int
66     eval_input: int
67     except_clause: int
68     exec_stmt: int
69     expr: int
70     expr_stmt: int
71     exprlist: int
72     factor: int
73     file_input: int
74     flow_stmt: int
75     for_stmt: int
76     funcdef: int
77     global_stmt: int
78     guard: int
79     if_stmt: int
80     import_as_name: int
81     import_as_names: int
82     import_from: int
83     import_name: int
84     import_stmt: int
85     lambdef: int
86     listmaker: int
87     match_stmt: int
88     namedexpr_test: int
89     not_test: int
90     old_comp_for: int
91     old_comp_if: int
92     old_comp_iter: int
93     old_lambdef: int
94     old_test: int
95     or_test: int
96     parameters: int
97     paramspec: int
98     pass_stmt: int
99     pattern: int
100     patterns: int
101     power: int
102     print_stmt: 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
145
146
147 class _pattern_symbols(Symbols):
148     Alternative: int
149     Alternatives: int
150     Details: int
151     Matcher: int
152     NegatedUnit: int
153     Repeater: int
154     Unit: int
155
156
157 python_grammar: Grammar
158 python_grammar_no_print_statement: Grammar
159 python_grammar_no_print_statement_no_exec_statement: Grammar
160 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
161 python_grammar_no_exec_statement: Grammar
162 pattern_grammar: Grammar
163 python_grammar_soft_keywords: Grammar
164
165 python_symbols: _python_symbols
166 pattern_symbols: _pattern_symbols
167
168
169 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
170     global python_grammar
171     global python_grammar_no_print_statement
172     global python_grammar_no_print_statement_no_exec_statement
173     global python_grammar_no_print_statement_no_exec_statement_async_keywords
174     global python_grammar_soft_keywords
175     global python_symbols
176     global pattern_grammar
177     global pattern_symbols
178
179     # The grammar file
180     _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
181     _PATTERN_GRAMMAR_FILE = os.path.join(
182         os.path.dirname(__file__), "PatternGrammar.txt"
183     )
184
185     # Python 2
186     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
187     python_grammar.version = (2, 0)
188
189     soft_keywords = python_grammar.soft_keywords.copy()
190     python_grammar.soft_keywords.clear()
191
192     python_symbols = _python_symbols(python_grammar)
193
194     # Python 2 + from __future__ import print_function
195     python_grammar_no_print_statement = python_grammar.copy()
196     del python_grammar_no_print_statement.keywords["print"]
197
198     # Python 3.0-3.6
199     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
200     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
201     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
202     python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
203
204     # Python 3.7+
205     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
206         python_grammar_no_print_statement_no_exec_statement.copy()
207     )
208     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
209         True
210     )
211     python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
212
213     # Python 3.10+
214     python_grammar_soft_keywords = (
215         python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
216     )
217     python_grammar_soft_keywords.soft_keywords = soft_keywords
218     python_grammar_soft_keywords.version = (3, 10)
219
220     pattern_grammar = driver.load_packaged_grammar(
221         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
222     )
223     pattern_symbols = _pattern_symbols(pattern_grammar)