blob: 559a0f4b9bad5aaacbb0ee49cdc21987fb49bd9f [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
2import pprint
3import sys
4
5from parser import expr, suite, sequence2ast
6from test_support import verbose
7
8#
9# First, we test that we can generate trees from valid source fragments,
10# and that these valid trees are indeed allowed by the tree-loading side
11# of the parser module.
12#
13
14def roundtrip(f, s):
15 print s
16 st1 = f(s)
17 t = st1.totuple()
18 st2 = parser.sequence2ast(t)
19
20
21print "Expressions:"
22
23roundtrip(expr, "foo(1)")
24roundtrip(expr, "[1, 2, 3]")
25roundtrip(expr, "[x**3 for x in range(20)]")
26roundtrip(expr, "[x**3 for x in range(20) if x % 3]")
27roundtrip(expr, "foo(*args)")
28roundtrip(expr, "foo(*args, **kw)")
29roundtrip(expr, "foo(**kw)")
30roundtrip(expr, "foo(key=value)")
31roundtrip(expr, "foo(key=value, *args)")
32roundtrip(expr, "foo(key=value, *args, **kw)")
33roundtrip(expr, "foo(key=value, **kw)")
34roundtrip(expr, "foo(a, b, c, *args)")
35roundtrip(expr, "foo(a, b, c, *args, **kw)")
36roundtrip(expr, "foo(a, b, c, **kw)")
37roundtrip(expr, "foo + bar")
38
39print
40print "Statements:"
41roundtrip(suite, "print")
42roundtrip(suite, "print 1")
43roundtrip(suite, "print 1,")
44roundtrip(suite, "print >>fp")
45roundtrip(suite, "print >>fp, 1")
46roundtrip(suite, "print >>fp, 1,")
47
48#
49# Second, we take *invalid* trees and make sure we get ParserError
50# rejections for them.
51#
52
53print
54print "Invalid parse trees:"
55
56def check_bad_tree(tree, label):
57 print
58 print label
59 try:
60 sequence2ast(tree)
61 except parser.ParserError:
62 print "caught expected exception for invalid tree"
63 pass
64 else:
65 print "test failed: did not properly detect invalid tree:"
66 pprint.pprint(tree)
67
68
69# not even remotely valid:
70check_bad_tree((1, 2, 3), "<junk>")
71
72# print >>fp,
73tree = \
74(257,
75 (264,
76 (265,
77 (266,
78 (268,
79 (1, 'print'),
80 (35, '>>'),
81 (290,
82 (291,
83 (292,
84 (293,
85 (295,
86 (296,
87 (297,
88 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
89 (12, ','))),
90 (4, ''))),
91 (0, ''))
92
93check_bad_tree(tree, "print >>fp,")
94
95# a,,c
96tree = \
97(258,
98 (311,
99 (290,
100 (291,
101 (292,
102 (293,
103 (295,
104 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
105 (12, ','),
106 (12, ','),
107 (290,
108 (291,
109 (292,
110 (293,
111 (295,
112 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
113 (4, ''),
114 (0, ''))
115
116check_bad_tree(tree, "a,,c")