]> 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 pre-commit mirror (#3828)
[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 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     exec_stmt: int
67     expr: int
68     expr_stmt: int
69     exprlist: int
70     factor: int
71     file_input: int
72     flow_stmt: int
73     for_stmt: int
74     funcdef: int
75     global_stmt: int
76     guard: int
77     if_stmt: int
78     import_as_name: int
79     import_as_names: int
80     import_from: int
81     import_name: int
82     import_stmt: int
83     lambdef: int
84     listmaker: int
85     match_stmt: int
86     namedexpr_test: int
87     not_test: int
88     old_comp_for: int
89     old_comp_if: int
90     old_comp_iter: int
91     old_lambdef: int
92     old_test: int
93     or_test: int
94     parameters: int
95     paramspec: int
96     pass_stmt: int
97     pattern: int
98     patterns: int
99     power: int
100     print_stmt: int
101     raise_stmt: int
102     return_stmt: int
103     shift_expr: int
104     simple_stmt: int
105     single_input: int
106     sliceop: int
107     small_stmt: int
108     subject_expr: int
109     star_expr: int
110     stmt: int
111     subscript: int
112     subscriptlist: int
113     suite: int
114     term: int
115     test: int
116     testlist: int
117     testlist1: int
118     testlist_gexp: int
119     testlist_safe: int
120     testlist_star_expr: int
121     tfpdef: int
122     tfplist: int
123     tname: int
124     tname_star: int
125     trailer: int
126     try_stmt: int
127     type_stmt: int
128     typedargslist: int
129     typeparam: int
130     typeparams: int
131     typevar: int
132     typevartuple: int
133     varargslist: int
134     vfpdef: int
135     vfplist: int
136     vname: int
137     while_stmt: int
138     with_stmt: int
139     xor_expr: int
140     yield_arg: int
141     yield_expr: int
142     yield_stmt: int
143
144
145 class _pattern_symbols(Symbols):
146     Alternative: int
147     Alternatives: int
148     Details: int
149     Matcher: int
150     NegatedUnit: int
151     Repeater: int
152     Unit: int
153
154
155 python_grammar: Grammar
156 python_grammar_no_print_statement: Grammar
157 python_grammar_no_print_statement_no_exec_statement: Grammar
158 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
159 python_grammar_no_exec_statement: Grammar
160 pattern_grammar: Grammar
161 python_grammar_soft_keywords: Grammar
162
163 python_symbols: _python_symbols
164 pattern_symbols: _pattern_symbols
165
166
167 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
168     global python_grammar
169     global python_grammar_no_print_statement
170     global python_grammar_no_print_statement_no_exec_statement
171     global python_grammar_no_print_statement_no_exec_statement_async_keywords
172     global python_grammar_soft_keywords
173     global python_symbols
174     global pattern_grammar
175     global pattern_symbols
176
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     )
182
183     # Python 2
184     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
185     python_grammar.version = (2, 0)
186
187     soft_keywords = python_grammar.soft_keywords.copy()
188     python_grammar.soft_keywords.clear()
189
190     python_symbols = _python_symbols(python_grammar)
191
192     # Python 2 + from __future__ import print_function
193     python_grammar_no_print_statement = python_grammar.copy()
194     del python_grammar_no_print_statement.keywords["print"]
195
196     # Python 3.0-3.6
197     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
198     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
199     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
200     python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
201
202     # Python 3.7+
203     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
204         python_grammar_no_print_statement_no_exec_statement.copy()
205     )
206     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
207         True
208     )
209     python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
210
211     # Python 3.10+
212     python_grammar_soft_keywords = (
213         python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
214     )
215     python_grammar_soft_keywords.soft_keywords = soft_keywords
216     python_grammar_soft_keywords.version = (3, 10)
217
218     pattern_grammar = driver.load_packaged_grammar(
219         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
220     )
221     pattern_symbols = _pattern_symbols(pattern_grammar)