Fred Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 1 | import parser |
| 2 | import pprint |
| 3 | import sys |
| 4 | |
| 5 | from parser import expr, suite, sequence2ast |
| 6 | from 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 | |
| 14 | def roundtrip(f, s): |
| 15 | print s |
| 16 | st1 = f(s) |
| 17 | t = st1.totuple() |
| 18 | st2 = parser.sequence2ast(t) |
| 19 | |
| 20 | |
| 21 | print "Expressions:" |
| 22 | |
| 23 | roundtrip(expr, "foo(1)") |
| 24 | roundtrip(expr, "[1, 2, 3]") |
| 25 | roundtrip(expr, "[x**3 for x in range(20)]") |
| 26 | roundtrip(expr, "[x**3 for x in range(20) if x % 3]") |
| 27 | roundtrip(expr, "foo(*args)") |
| 28 | roundtrip(expr, "foo(*args, **kw)") |
| 29 | roundtrip(expr, "foo(**kw)") |
| 30 | roundtrip(expr, "foo(key=value)") |
| 31 | roundtrip(expr, "foo(key=value, *args)") |
| 32 | roundtrip(expr, "foo(key=value, *args, **kw)") |
| 33 | roundtrip(expr, "foo(key=value, **kw)") |
| 34 | roundtrip(expr, "foo(a, b, c, *args)") |
| 35 | roundtrip(expr, "foo(a, b, c, *args, **kw)") |
| 36 | roundtrip(expr, "foo(a, b, c, **kw)") |
| 37 | roundtrip(expr, "foo + bar") |
| 38 | |
| 39 | print |
| 40 | print "Statements:" |
| 41 | roundtrip(suite, "print") |
| 42 | roundtrip(suite, "print 1") |
| 43 | roundtrip(suite, "print 1,") |
| 44 | roundtrip(suite, "print >>fp") |
| 45 | roundtrip(suite, "print >>fp, 1") |
| 46 | roundtrip(suite, "print >>fp, 1,") |
| 47 | |
| 48 | # |
| 49 | # Second, we take *invalid* trees and make sure we get ParserError |
| 50 | # rejections for them. |
| 51 | # |
| 52 | |
| 53 | print |
| 54 | print "Invalid parse trees:" |
| 55 | |
| 56 | def 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: |
| 70 | check_bad_tree((1, 2, 3), "<junk>") |
| 71 | |
| 72 | # print >>fp, |
| 73 | tree = \ |
| 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 | |
| 93 | check_bad_tree(tree, "print >>fp,") |
| 94 | |
| 95 | # a,,c |
| 96 | tree = \ |
| 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 | |
| 116 | check_bad_tree(tree, "a,,c") |