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

Fix broken link in README.md (#2989)
[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     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     trailer: int
127     try_stmt: int
128     typedargslist: int
129     varargslist: int
130     vfpdef: int
131     vfplist: int
132     vname: int
133     while_stmt: int
134     with_stmt: int
135     xor_expr: int
136     yield_arg: int
137     yield_expr: int
138     yield_stmt: int
139
140
141 class _pattern_symbols(Symbols):
142     Alternative: int
143     Alternatives: int
144     Details: int
145     Matcher: int
146     NegatedUnit: int
147     Repeater: int
148     Unit: int
149
150
151 python_grammar: Grammar
152 python_grammar_no_print_statement: Grammar
153 python_grammar_no_print_statement_no_exec_statement: Grammar
154 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
155 python_grammar_no_exec_statement: Grammar
156 pattern_grammar: Grammar
157 python_grammar_soft_keywords: Grammar
158
159 python_symbols: _python_symbols
160 pattern_symbols: _pattern_symbols
161
162
163 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
164     global python_grammar
165     global python_grammar_no_print_statement
166     global python_grammar_no_print_statement_no_exec_statement
167     global python_grammar_no_print_statement_no_exec_statement_async_keywords
168     global python_grammar_soft_keywords
169     global python_symbols
170     global pattern_grammar
171     global pattern_symbols
172
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     )
178
179     # Python 2
180     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
181     python_grammar.version = (2, 0)
182
183     soft_keywords = python_grammar.soft_keywords.copy()
184     python_grammar.soft_keywords.clear()
185
186     python_symbols = _python_symbols(python_grammar)
187
188     # Python 2 + from __future__ import print_function
189     python_grammar_no_print_statement = python_grammar.copy()
190     del python_grammar_no_print_statement.keywords["print"]
191
192     # Python 3.0-3.6
193     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
194     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
195     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
196     python_grammar_no_print_statement_no_exec_statement.version = (3, 0)
197
198     # Python 3.7+
199     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
200         python_grammar_no_print_statement_no_exec_statement.copy()
201     )
202     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
203         True
204     )
205     python_grammar_no_print_statement_no_exec_statement_async_keywords.version = (3, 7)
206
207     # Python 3.10+
208     python_grammar_soft_keywords = (
209         python_grammar_no_print_statement_no_exec_statement_async_keywords.copy()
210     )
211     python_grammar_soft_keywords.soft_keywords = soft_keywords
212     python_grammar_soft_keywords.version = (3, 10)
213
214     pattern_grammar = driver.load_packaged_grammar(
215         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
216     )
217     pattern_symbols = _pattern_symbols(pattern_grammar)