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

Treat raw strings like other docstrings (#3947)
[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     expr: int
67     expr_stmt: int
68     exprlist: int
69     factor: int
70     file_input: int
71     flow_stmt: int
72     for_stmt: int
73     funcdef: int
74     global_stmt: int
75     guard: int
76     if_stmt: int
77     import_as_name: int
78     import_as_names: int
79     import_from: int
80     import_name: int
81     import_stmt: int
82     lambdef: int
83     listmaker: int
84     match_stmt: 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     paramspec: int
95     pass_stmt: int
96     pattern: int
97     patterns: int
98     power: int
99     raise_stmt: int
100     return_stmt: int
101     shift_expr: int
102     simple_stmt: int
103     single_input: int
104     sliceop: int
105     small_stmt: int
106     subject_expr: int
107     star_expr: int
108     stmt: int
109     subscript: int
110     subscriptlist: int
111     suite: int
112     term: int
113     test: int
114     testlist: int
115     testlist1: int
116     testlist_gexp: int
117     testlist_safe: int
118     testlist_star_expr: int
119     tfpdef: int
120     tfplist: int
121     tname: int
122     tname_star: int
123     trailer: int
124     try_stmt: int
125     type_stmt: int
126     typedargslist: int
127     typeparam: int
128     typeparams: int
129     typevar: int
130     typevartuple: int
131     varargslist: int
132     vfpdef: int
133     vfplist: int
134     vname: int
135     while_stmt: int
136     with_stmt: int
137     xor_expr: int
138     yield_arg: int
139     yield_expr: int
140     yield_stmt: int
141
142
143 class _pattern_symbols(Symbols):
144     Alternative: int
145     Alternatives: int
146     Details: int
147     Matcher: int
148     NegatedUnit: int
149     Repeater: int
150     Unit: int
151
152
153 python_grammar: Grammar
154 python_grammar_async_keywords: Grammar
155 python_grammar_soft_keywords: Grammar
156 pattern_grammar: Grammar
157 python_symbols: _python_symbols
158 pattern_symbols: _pattern_symbols
159
160
161 def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None:
162     global python_grammar
163     global python_grammar_async_keywords
164     global python_grammar_soft_keywords
165     global python_symbols
166     global pattern_grammar
167     global pattern_symbols
168
169     # The grammar file
170     _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
171     _PATTERN_GRAMMAR_FILE = os.path.join(
172         os.path.dirname(__file__), "PatternGrammar.txt"
173     )
174
175     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
176     assert "print" not in python_grammar.keywords
177     assert "exec" not in python_grammar.keywords
178
179     soft_keywords = python_grammar.soft_keywords.copy()
180     python_grammar.soft_keywords.clear()
181
182     python_symbols = _python_symbols(python_grammar)
183
184     # Python 3.0-3.6
185     python_grammar.version = (3, 0)
186
187     # Python 3.7+
188     python_grammar_async_keywords = python_grammar.copy()
189     python_grammar_async_keywords.async_keywords = True
190     python_grammar_async_keywords.version = (3, 7)
191
192     # Python 3.10+
193     python_grammar_soft_keywords = python_grammar_async_keywords.copy()
194     python_grammar_soft_keywords.soft_keywords = soft_keywords
195     python_grammar_soft_keywords.version = (3, 10)
196
197     pattern_grammar = driver.load_packaged_grammar(
198         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
199     )
200     pattern_symbols = _pattern_symbols(pattern_grammar)