-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_custom_grammar.py
352 lines (323 loc) · 9.71 KB
/
test_custom_grammar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import ast
import sys
import textwrap
from collections.abc import Sequence
from pathlib import Path
from typing import Any
from blib2to3 import pygram
from blib2to3.pgen2 import token
from blib2to3.pgen2.grammar import Grammar
from blib2to3.pytree import NL, Node
from lib2toast.api import load_grammar, run
from lib2toast.compile import (
Compiler,
Consumer,
LineRange,
extract_name,
get_line_range,
replace,
unify_line_ranges,
)
syms = pygram.python_symbols
def check_run(
code: str, expected_vars: dict[str, Any], grammar: Grammar, compiler: Compiler
) -> None:
ns = run(textwrap.dedent(code), grammar=grammar, compiler=compiler)
for key, value in expected_vars.items():
assert ns[key] == value
class BracesCompiler(Compiler):
def consume_and_compile_suite(
self, consumer: Consumer
) -> tuple[list[ast.stmt], LineRange]:
return self.compile_suite(consumer.expect())
def compile_suite(self, node: NL) -> tuple[list[ast.stmt], LineRange]:
consumer = Consumer(node.children)
statement_list = []
consumer.consume(token.LBRACE)
while not consumer.next_is(token.RBRACE):
statement_list.append(consumer.expect())
statements = self.compile_statement_list(statement_list)
return statements, get_line_range(node, ignore_last_leaf=True)
def compile_statement_list(self, nodes: Sequence[NL]) -> list[ast.stmt]:
statements = []
for node in nodes:
if node.type in (token.ENDMARKER, token.NEWLINE):
continue
if isinstance(node, Node) and node.type == syms.simple_stmt:
statements += self.compile_simple_stmt(node)
else:
stmt = self.visit(node)
if isinstance(stmt, ast.stmt):
statements.append(stmt)
elif isinstance(stmt, ast.expr):
statements.append(ast.Expr(stmt, **get_line_range(node)))
else:
raise AssertionError(f"Unexpected statement: {stmt}")
return statements
def test_braces() -> None:
braces_path = Path(__file__).parent / "grammars" / "braces.txt"
grammar = load_grammar(braces_path)
compiler = BracesCompiler(grammar=grammar)
check_run(
"""
if True {
x = 1
}
else {
x = 2
}
""",
{"x": 1},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
def f(x) {
return x + 1
}
lst = []
for i in range(4) {
lst.append(f(i))
}
""",
{"lst": [1, 2, 3, 4]},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
class C {
def meth(self) {
self.x = 1
}
def meth2(self, x) {
self.x = x
}
}
c = C()
c.meth()
x1 = c.x
c.meth2(2)
x2 = c.x
""",
{"x1": 1, "x2": 2},
grammar=grammar,
compiler=compiler,
)
class DataclassCompiler(Compiler):
def visit_dataclassdef(self, node: Node) -> ast.stmt:
consumer = Consumer(node.children)
keyword_node = consumer.expect_name("dataclass")
dataclass_bases: list[ast.expr] = []
dataclass_keywords: list[ast.keyword] = []
if consumer.consume(token.LPAR) is not None:
next_node = consumer.expect()
if next_node.type != token.RPAR:
dataclass_bases, dataclass_keywords = self._compile_arglist(
next_node, next_node
)
consumer.expect(token.RPAR)
name = extract_name(consumer.expect())
if (type_params_node := consumer.consume(self.syms.typeparams)) is not None:
type_params = self.compile_typeparams(type_params_node)
else:
type_params = []
bases: list[ast.expr] = []
keywords: list[ast.keyword] = []
if consumer.consume(token.LPAR) is not None:
next_node = consumer.expect()
if next_node.type != token.RPAR:
bases, keywords = self._compile_arglist(next_node, next_node)
consumer.expect(token.RPAR)
suite, end_line_range = self.consume_and_compile_suite(consumer)
line_range = unify_line_ranges(get_line_range(node.children[0]), end_line_range)
keyword_line_range = get_line_range(keyword_node)
dataclass = ast.Attribute(
value=ast.Call(
func=ast.Name(id="__import__", ctx=ast.Load(), **keyword_line_range),
args=[ast.Constant(value="dataclasses", **keyword_line_range)],
keywords=[],
**keyword_line_range,
),
attr="dataclass",
ctx=ast.Load(),
**keyword_line_range,
)
decorator: ast.expr
if dataclass_bases or dataclass_keywords:
decorator = ast.Call(
func=dataclass,
args=dataclass_bases,
keywords=dataclass_keywords,
**keyword_line_range,
)
else:
decorator = dataclass
if sys.version_info >= (3, 12):
return ast.ClassDef(
name=name,
bases=bases,
keywords=keywords,
body=suite,
decorator_list=[decorator],
type_params=type_params,
**line_range,
)
else:
return ast.ClassDef(
name=name,
bases=bases,
keywords=keywords,
body=suite,
decorator_list=[decorator],
**line_range,
)
def visit_decorated(self, node: Node) -> ast.stmt:
decorator_list = self.compile_decorators(node.children[0])
stmt = self.visit_typed(node.children[1], ast.stmt)
if isinstance(stmt, ast.ClassDef):
new_decorator_list = decorator_list + stmt.decorator_list
else:
new_decorator_list = decorator_list
return replace(stmt, decorator_list=new_decorator_list)
def test_dataclass() -> None:
dataclass_path = Path(__file__).parent / "grammars" / "dataclass.txt"
grammar = load_grammar(dataclass_path)
compiler = DataclassCompiler(grammar=grammar)
check_run(
"""
dataclass C:
x: int
y: int = 0
c = C(1)
x = c.x
y = c.y
""",
{"x": 1, "y": 0},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
dataclass(frozen=True) C:
x: int
y: int = 0
c = C(1)
x = c.x
y = c.y
try:
c.y = 4
except Exception:
caught = True
""",
{"x": 1, "y": 0, "caught": True},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
def deco(cls):
cls.x = 1
return cls
@deco
dataclass C:
y: int
c = C(2)
x = C.x
y = c.y
""",
{"x": 1, "y": 2},
grammar=grammar,
compiler=compiler,
)
def test_soft_dataclass() -> None:
dataclass_path = Path(__file__).parent / "grammars" / "soft-dataclass.txt"
grammar = load_grammar(dataclass_path)
compiler = DataclassCompiler(grammar=grammar)
check_run(
"""
dataclass dataclass:
dataclass: int
y: int = 0
c = dataclass(1)
x = c.dataclass
y = c.y
""",
{"x": 1, "y": 0},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
dataclass = True
dataclass(frozen=dataclass) C:
dataclass: int
y: int = 0
c = C(1)
x = c.dataclass
y = c.y
try:
c.y = 4
except Exception:
dataclass = True
""",
{"x": 1, "y": 0, "dataclass": True},
grammar=grammar,
compiler=compiler,
)
check_run(
"""
def dataclass(cls):
cls.dataclass = 1
return cls
@dataclass
dataclass C:
y: int
c = C(2)
x = C.dataclass
y = c.y
""",
{"x": 1, "y": 2},
grammar=grammar,
compiler=compiler,
)
class RangeLiteralCompiler(Compiler):
def compile_trailer(
self,
trailer: NL,
*,
parent: ast.expr,
ctx: ast.expr_context,
begin_range: LineRange,
) -> ast.expr:
if (
trailer.children[0].type == token.DOT
and trailer.children[1].type == token.DOT
):
assert trailer.children[2].type == token.DOT
rhs_node = trailer.children[3]
line_range = unify_line_ranges(begin_range, get_line_range(trailer))
rhs = self.visit(rhs_node)
assert isinstance(rhs, ast.expr)
return ast.Call(
func=ast.Name(id="range", ctx=ast.Load(), **line_range),
args=[parent, rhs],
keywords=[],
**line_range,
)
return super().compile_trailer(
trailer, parent=parent, ctx=ctx, begin_range=begin_range
)
def test_range_literal() -> None:
dataclass_path = Path(__file__).parent / "grammars" / "range-literal.txt"
grammar = load_grammar(dataclass_path)
compiler = RangeLiteralCompiler(grammar=grammar)
check_run(
"""
x = 1 ... 2 # must be spaced or it gets tokenized wrong
""",
{"x": range(1, 2)},
grammar=grammar,
compiler=compiler,
)