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

Update CHANGES.md for 21.5b2 release (#2290)
[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     assert_stmt: int
43     async_funcdef: int
44     async_stmt: int
45     atom: int
46     augassign: int
47     break_stmt: int
48     classdef: int
49     comp_for: int
50     comp_if: int
51     comp_iter: int
52     comp_op: int
53     comparison: int
54     compound_stmt: int
55     continue_stmt: int
56     decorated: int
57     decorator: int
58     decorators: int
59     del_stmt: int
60     dictsetmaker: int
61     dotted_as_name: int
62     dotted_as_names: int
63     dotted_name: int
64     encoding_decl: int
65     eval_input: int
66     except_clause: int
67     exec_stmt: int
68     expr: int
69     expr_stmt: int
70     exprlist: int
71     factor: int
72     file_input: int
73     flow_stmt: int
74     for_stmt: int
75     funcdef: int
76     global_stmt: 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     namedexpr_test: int
86     not_test: int
87     old_comp_for: int
88     old_comp_if: int
89     old_comp_iter: int
90     old_lambdef: int
91     old_test: int
92     or_test: int
93     parameters: int
94     pass_stmt: int
95     power: int
96     print_stmt: int
97     raise_stmt: int
98     return_stmt: int
99     shift_expr: int
100     simple_stmt: int
101     single_input: int
102     sliceop: int
103     small_stmt: int
104     star_expr: int
105     stmt: int
106     subscript: int
107     subscriptlist: int
108     suite: int
109     term: int
110     test: int
111     testlist: int
112     testlist1: int
113     testlist_gexp: int
114     testlist_safe: int
115     testlist_star_expr: int
116     tfpdef: int
117     tfplist: int
118     tname: int
119     trailer: int
120     try_stmt: int
121     typedargslist: int
122     varargslist: int
123     vfpdef: int
124     vfplist: int
125     vname: int
126     while_stmt: int
127     with_item: int
128     with_stmt: int
129     with_var: int
130     xor_expr: int
131     yield_arg: int
132     yield_expr: int
133     yield_stmt: int
134
135
136 class _pattern_symbols(Symbols):
137     Alternative: int
138     Alternatives: int
139     Details: int
140     Matcher: int
141     NegatedUnit: int
142     Repeater: int
143     Unit: int
144
145
146 python_grammar: Grammar
147 python_grammar_no_print_statement: Grammar
148 python_grammar_no_print_statement_no_exec_statement: Grammar
149 python_grammar_no_print_statement_no_exec_statement_async_keywords: Grammar
150 python_grammar_no_exec_statement: Grammar
151 pattern_grammar: Grammar
152
153 python_symbols: _python_symbols
154 pattern_symbols: _pattern_symbols
155
156
157 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
158     global python_grammar
159     global python_grammar_no_print_statement
160     global python_grammar_no_print_statement_no_exec_statement
161     global python_grammar_no_print_statement_no_exec_statement_async_keywords
162     global python_symbols
163     global pattern_grammar
164     global pattern_symbols
165
166     # The grammar file
167     _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
168     _PATTERN_GRAMMAR_FILE = os.path.join(
169         os.path.dirname(__file__), "PatternGrammar.txt"
170     )
171
172     # Python 2
173     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
174
175     python_symbols = _python_symbols(python_grammar)
176
177     # Python 2 + from __future__ import print_function
178     python_grammar_no_print_statement = python_grammar.copy()
179     del python_grammar_no_print_statement.keywords["print"]
180
181     # Python 3.0-3.6
182     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
183     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
184     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
185
186     # Python 3.7+
187     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
188         python_grammar_no_print_statement_no_exec_statement.copy()
189     )
190     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
191         True
192     )
193
194     pattern_grammar = driver.load_packaged_grammar(
195         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
196     )
197     pattern_symbols = _pattern_symbols(pattern_grammar)