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

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