]> 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:

Bump peter-evans/create-or-update-comment from 3.0.1 to 3.0.2 (#3730)
[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 token
13 from .pgen2 import driver
14
15 from .pgen2.grammar import Grammar
16
17 # Moved into initialize because mypyc can't handle __file__ (XXX bug)
18 # # The grammar file
19 # _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
20 # _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
21 #                                      "PatternGrammar.txt")
22
23
24 class Symbols(object):
25     def __init__(self, grammar: Grammar) -> None:
26         """Initializer.
27
28         Creates an attribute for each grammar symbol (nonterminal),
29         whose value is the symbol's type (an int >= 256).
30         """
31         for name, symbol in grammar.symbol2number.items():
32             setattr(self, name, symbol)
33
34
35 class _python_symbols(Symbols):
36     and_expr: int
37     and_test: int
38     annassign: int
39     arglist: int
40     argument: int
41     arith_expr: int
42     asexpr_test: int
43     assert_stmt: int
44     async_funcdef: int
45     async_stmt: int
46     atom: int
47     augassign: int
48     break_stmt: int
49     case_block: int
50     classdef: int
51     comp_for: int
52     comp_if: int
53     comp_iter: int
54     comp_op: int
55     comparison: int
56     compound_stmt: int
57     continue_stmt: int
58     decorated: int
59     decorator: int
60     decorators: int
61     del_stmt: int
62     dictsetmaker: int
63     dotted_as_name: int
64     dotted_as_names: int
65     dotted_name: int
66     encoding_decl: int
67     eval_input: int
68     except_clause: int
69     exec_stmt: int
70     expr: int
71     expr_stmt: int
72     exprlist: int
73     factor: int
74     file_input: int
75     flow_stmt: int
76     for_stmt: 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     print_stmt: int
104     raise_stmt: int
105     return_stmt: int
106     shift_expr: int
107     simple_stmt: int
108     single_input: int
109     sliceop: int
110     small_stmt: int
111     subject_expr: int
112     star_expr: int
113     stmt: int
114     subscript: int
115     subscriptlist: int
116     suite: int
117     term: int
118     test: int
119     testlist: int
120     testlist1: int
121     testlist_gexp: int
122     testlist_safe: int
123     testlist_star_expr: int
124     tfpdef: int
125     tfplist: int
126     tname: int
127     tname_star: int
128     trailer: int
129     try_stmt: int
130     type_stmt: int
131     typedargslist: int
132     typeparam: int
133     typeparams: int
134     typevar: int
135     typevartuple: int
136     varargslist: int
137     vfpdef: int
138     vfplist: int
139     vname: int
140     while_stmt: int
141     with_stmt: int
142     xor_expr: int
143     yield_arg: int
144     yield_expr: int
145     yield_stmt: int
146
147
148 class _pattern_symbols(Symbols):
149     Alternative: int
150     Alternatives: int
151     Details: int
152     Matcher: int
153     NegatedUnit: int
154     Repeater: int
155     Unit: int
156
157
158 python_grammar: Grammar
159 python_grammar_no_print_statement: Grammar
160 python_grammar_no_print_statement_no_exec_statement: Grammar
161 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
162 python_grammar_no_exec_statement: Grammar
163 pattern_grammar: Grammar
164 python_grammar_soft_keywords: Grammar
165
166 python_symbols: _python_symbols
167 pattern_symbols: _pattern_symbols
168
169
170 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
171     global python_grammar
172     global python_grammar_no_print_statement
173     global python_grammar_no_print_statement_no_exec_statement
174     global python_grammar_no_print_statement_no_exec_statement_async_keywords
175     global python_grammar_soft_keywords
176     global python_symbols
177     global pattern_grammar
178     global pattern_symbols
179
180     # The grammar file
181     _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
182     _PATTERN_GRAMMAR_FILE = os.path.join(
183         os.path.dirname(__file__), "PatternGrammar.txt"
184     )
185
186     # Python 2
187     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
188     python_grammar.version = (2, 0)
189
190     soft_keywords = python_grammar.soft_keywords.copy()
191     python_grammar.soft_keywords.clear()
192
193     python_symbols = _python_symbols(python_grammar)
194
195     # Python 2 + from __future__ import print_function
196     python_grammar_no_print_statement = python_grammar.copy()
197     del python_grammar_no_print_statement.keywords["print"]
198
199     # Python 3.0-3.6
200     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
201     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
202     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
203     python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
204
205     # Python 3.7+
206     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
207         python_grammar_no_print_statement_no_exec_statement.copy()
208     )
209     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
210         True
211     )
212     python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
213
214     # Python 3.10+
215     python_grammar_soft_keywords = (
216         python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
217     )
218     python_grammar_soft_keywords.soft_keywords = soft_keywords
219     python_grammar_soft_keywords.version = (3, 10)
220
221     pattern_grammar = driver.load_packaged_grammar(
222         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
223     )
224     pattern_symbols = _pattern_symbols(pattern_grammar)