-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recursive.py
474 lines (417 loc) · 10.6 KB
/
test_recursive.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author: Tim Henderson
#Email: [email protected]
#For licensing see the LICENSE file in the top level directory.
import itertools
from nose.tools import eq_ as eq, ok_ as ok, raises
from betterast import Node
import lexer
from recursive import parse
from lexer import token
def new_parser():
return object.__new__(parse)
def test_mk_test_instance():
parser = new_parser()
assert isinstance(parser, parse)
def test_init():
parser = new_parser()
parser.init("2 + 3 / 4")
assert isinstance(parser.tokens, list)
expected = [
token(lexer.NUMBER, 2),
token(lexer.PLUS, '+'),
token(lexer.NUMBER, 3),
token(lexer.SLASH, '/'),
token(lexer.NUMBER, 4),
]
eq(len(parser.tokens), len(expected))
for a, b in itertools.izip(parser.tokens, expected):
eq(a, b)
def test_Factor_parse_number():
parser = new_parser()
parser.init("2")
i, node = parser.Factor(0)
eq(i, 1)
eq(node.label, 2)
def test_consume_pos():
parser = new_parser()
parser.init("(2 + 3) / 4")
i, tok = parser.consume(0, lexer.LPAREN)
eq(i, 1)
eq(tok.type, lexer.LPAREN)
@raises(SyntaxError)
def test_consume_neg():
parser = new_parser()
parser.init("(2 + 3) / 4")
parser.consume(0, lexer.NUMBER)
def test_alt_pos():
parser = new_parser()
parser.init("(2 + 3) / 4")
def one(i):
return parser.consume(i, lexer.NUMBER)
def two(i):
return parser.consume(i, lexer.LPAREN)
i, tok = parser.alt(0, [lexer.NUMBER, lexer.LPAREN], one, two)
eq(i, 1)
eq(tok.type, lexer.LPAREN)
@raises(SyntaxError)
def test_alt_neg():
parser = new_parser()
parser.init("(2 + 3) / 4")
def one(i):
return parser.consume(i, lexer.NUMBER)
def two(i):
return parser.consume(i, lexer.PLUS)
parser.alt(0, [lexer.NUMBER, lexer.LPAREN], one, two)
def test_Factor1():
parser = new_parser()
parser.init("(2 + 3) / 4")
i, node = parser.Factor1(1)
eq(i, 2)
eq(node.label, 2)
eq(len(node.children), 0)
def test_Factor2():
parser = new_parser()
parser.init("(2)")
i, node = parser.Factor2(0)
eq(i, 3)
eq(node.label, 2)
eq(len(node.children), 0)
def test_Unary1():
parser = new_parser()
parser.init("-2")
i, node = parser.Unary1(0)
eq(i, 2)
eq(node.label, "Neg")
eq(len(node.children), 1)
eq(node.children[0].label, 2)
def test_Unary2():
parser = new_parser()
parser.init("2")
i, node = parser.Unary2(0)
eq(i, 1)
eq(node.label, 2)
def test_Unary_1():
parser = new_parser()
parser.init("-2")
i, node = parser.Unary(0)
eq(i, 2)
eq(node.label, "Neg")
eq(len(node.children), 1)
eq(node.children[0].label, 2)
def test_Unary_2():
parser = new_parser()
parser.init("2")
i, node = parser.Unary(0)
eq(i, 1)
eq(node.label, 2)
def test_collapse_None():
parser = new_parser()
eq(parser.collapse(Node(1), None), Node(1))
def test_collapse_subtree():
parser = new_parser()
eq(
parser.collapse(Node(1), ("*", Node(2))),
Node("*").addkid(Node(1)).addkid(Node(2))
)
@raises(ValueError)
def test_collapse_bad_subtree():
parser = new_parser()
parser.collapse(Node(1), Node("*"))
def test_1_Term():
parser = new_parser()
parser.init("1 * 2")
i, node = parser.Term(0)
eq(i, 3)
eq(node, Node("*").addkid(Node(1)).addkid(Node(2)))
def test_2_Term():
parser = new_parser()
parser.init("1 / 2")
i, node = parser.Term(0)
eq(i, 3)
eq(node, Node("/").addkid(Node(1)).addkid(Node(2)))
def test_3_Term():
parser = new_parser()
parser.init("1 / 2 * 3")
i, node = parser.Term(0)
eq(i, 5)
eq(node, Node("/")
.addkid(Node(1))
.addkid(
Node("*")
.addkid(Node(2))
.addkid(Node(3))
)
)
def test_4_Term():
parser = new_parser()
parser.init("(1 / 2) * 3")
i, node = parser.Term(0)
eq(i, 7)
eq(node, Node("*")
.addkid(
Node("/")
.addkid(Node(1))
.addkid(Node(2))
)
.addkid(Node(3))
)
def test_1_Term_():
parser = new_parser()
parser.init("* 2")
i, (op, right) = parser.Term_(0)
eq(i, 2)
eq(op, "*")
eq(right.label, 2)
def test_Term_1():
parser = new_parser()
parser.init("* 2")
i, (op, right) = parser.Term_1(0)
eq(i, 2)
eq(op, "*")
eq(right.label, 2)
@raises(SyntaxError)
def test_Term_1_bad():
parser = new_parser()
parser.init("2 * 2")
i, node = parser.Term_1(0)
def test_2_Term_():
parser = new_parser()
parser.init("/ 2")
i, (op, right) = parser.Term_(0)
eq(i, 2)
eq(op, "/")
eq(right.label, 2)
def test_Term_2():
parser = new_parser()
parser.init("/ 2")
i, (op, right) = parser.Term_2(0)
eq(i, 2)
eq(op, "/")
eq(right.label, 2)
@raises(SyntaxError)
def test_Term_2_bad():
parser = new_parser()
parser.init("2 / 2")
i, node = parser.Term_2(0)
def test_3_Term_():
parser = new_parser()
parser.init("+ 2")
i, n = parser.Term_(0)
eq(n, None)
def test_Term_3():
parser = new_parser()
parser.init("+ / - *")
i, n = parser.Term_3(0)
eq(i, 0)
eq(n, None)
def test_2_level_Term_():
parser = new_parser()
parser.init("* 2 * 3")
i, (op, right) = parser.Term_(0)
eq(op, "*")
eq(right.label, "*")
eq(len(right.children), 2)
eq(right.children[0].label, 2)
eq(right.children[1].label, 3)
def test_2_level_Term():
parser = new_parser()
parser.init("1 / 2 * 3")
i, node = parser.Term(0)
eq(node.label, "/")
eq(len(node.children), 2)
eq(node.children[0].label, 1)
eq(node.children[1].label, "*")
eq(len(node.children[1].children), 2)
eq(node.children[1].children[0].label, 2)
eq(node.children[1].children[1].label, 3)
def test_2_level_Expr_Term():
parser = new_parser()
parser.init("1 / 2 * 3")
i, node = parser.Expr(0)
eq(node.label, "/")
eq(len(node.children), 2)
eq(node.children[0].label, 1)
eq(node.children[1].label, "*")
eq(len(node.children[1].children), 2)
eq(node.children[1].children[0].label, 2)
eq(node.children[1].children[1].label, 3)
def test_1_Expr_():
parser = new_parser()
parser.init("+ 2")
i, (op, right) = parser.Expr_(0)
eq(i, 2)
eq(op, "+")
eq(right.label, 2)
def test_Expr_1():
parser = new_parser()
parser.init("+ 2")
i, (op, right) = parser.Expr_1(0)
eq(i, 2)
eq(op, "+")
eq(right.label, 2)
def test_2_Expr_():
parser = new_parser()
parser.init("- 2")
i, (op, right) = parser.Expr_(0)
eq(i, 2)
eq(op, "-")
eq(right.label, 2)
def test_Expr_2():
parser = new_parser()
parser.init("- 2")
i, (op, right) = parser.Expr_2(0)
eq(i, 2)
eq(op, "-")
eq(right.label, 2)
def test_3_Expr_():
parser = new_parser()
parser.init("2 2")
i, n = parser.Expr_(0)
eq(i, 0)
eq(n, None)
def test_Expr_3():
parser = new_parser()
parser.init("2 2")
i, n = parser.Expr_3(0)
eq(i, 0)
eq(n, None)
def test_Expr_Factor():
parser = new_parser()
parser.init("3")
i, node = parser.Expr(0)
eq(node.label, 3)
def test_Expr_Factor2():
parser = new_parser()
parser.init("(3)")
i, node = parser.Expr(0)
eq(i, 3)
eq(node.label, 3)
def test_Expr_Unary_Factor():
parser = new_parser()
parser.init("-3")
i, node = parser.Expr(0)
eq(i, 2)
eq(node, Node("Neg").addkid(Node(3)))
def test_Expr_Term_Unary():
parser = new_parser()
parser.init("1*-3")
i, node = parser.Expr(0)
eq(i, 4)
eq(node,
Node("*")
.addkid(Node(1))
.addkid(Node("Neg").addkid(Node(3)))
)
def test_Expr_Expr_Unary():
parser = new_parser()
parser.init("1--3")
i, node = parser.Expr(0)
eq(i, 4)
eq(node,
Node("-")
.addkid(Node(1))
.addkid(Node("Neg").addkid(Node(3)))
)
def test_Expr_Unary_Expr_Unary():
parser = new_parser()
parser.init("-1--3")
i, node = parser.Expr(0)
eq(i, 5)
eq(node,
Node("-")
.addkid(Node("Neg").addkid(Node(1)))
.addkid(Node("Neg").addkid(Node(3)))
)
def test_precidence_mul():
parser = new_parser()
parser.init("1*3+4")
i, node = parser.Expr(0)
eq(node,
Node("+")
.addkid(
Node("*").addkid(Node(1)).addkid(Node(3)))
.addkid(Node(4))
)
def test_precidence_mul2():
parser = new_parser()
parser.init("4+1*3")
i, node = parser.Expr(0)
eq(node,
Node("+")
.addkid(Node(4))
.addkid(
Node("*").addkid(Node(1)).addkid(Node(3)))
)
def test_precidence_mul3():
parser = new_parser()
parser.init("1*(3+4)")
i, node = parser.Expr(0)
eq(node,
Node("*")
.addkid(Node(1))
.addkid(
Node("+").addkid(Node(3)).addkid(Node(4)))
)
def test_precidence_mul4():
parser = new_parser()
parser.init("(3+4)*1")
i, node = parser.Expr(0)
eq(node,
Node("*")
.addkid(
Node("+").addkid(Node(3)).addkid(Node(4)))
.addkid(Node(1))
)
@raises(SyntaxError)
def test_parse_neg():
parser = new_parser()
parser.init("(3)-/3")
node = parser.parse()
def test_parse_complex():
parser = new_parser()
parser.init("2+3+4*(1+(5-2)/3)-4/3*(2+1)")
node = parser.parse()
expected = (
Node("+")
.addkid(Node(2))
.addkid(
Node("+")
.addkid(Node(3))
.addkid(
Node("-")
.addkid(
Node("*")
.addkid(Node(4))
.addkid(
Node("+")
.addkid(Node(1))
.addkid(
Node("/")
.addkid(
Node("-")
.addkid(Node(5))
.addkid(Node(2))
)
.addkid(Node(3))
)
)
)
.addkid(
Node("/")
.addkid(Node(4))
.addkid(
Node("*")
.addkid(Node(3))
.addkid(
Node("+")
.addkid(Node(2))
.addkid(Node(1))
)
)
)
)
)
)
eq(node, expected)