diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2006-08-22 20:46:00 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2006-08-22 20:46:00 (GMT) |
commit | 60e96f666ce4c8880c3d8ce5fe130dcb4c3c62db (patch) | |
tree | 66b764cae36bbeee1f4382cd622f513158f7e9cb /Lib/test | |
parent | 670f875a7cccfb3b63196bb560a8dd3cf5950ad9 (diff) | |
download | cpython-60e96f666ce4c8880c3d8ce5fe130dcb4c3c62db.zip cpython-60e96f666ce4c8880c3d8ce5fe130dcb4c3c62db.tar.gz cpython-60e96f666ce4c8880c3d8ce5fe130dcb4c3c62db.tar.bz2 |
Expose column offset information in parse trees.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_parser.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py index 8aa1657..bddec16 100644 --- a/Lib/test/test_parser.py +++ b/Lib/test/test_parser.py @@ -183,6 +183,44 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): def test_assert(self): self.check_suite("assert alo < ahi and blo < bhi\n") + def test_position(self): + # An absolutely minimal test of position information. Better + # tests would be a big project. + code = "def f(x):\n return x + 1\n" + st1 = parser.suite(code) + st2 = st1.totuple(line_info=1, col_info=1) + + def walk(tree): + node_type = tree[0] + next = tree[1] + if isinstance(next, tuple): + for elt in tree[1:]: + for x in walk(elt): + yield x + else: + yield tree + + terminals = list(walk(st2)) + self.assertEqual([ + (1, 'def', 1, 0), + (1, 'f', 1, 4), + (7, '(', 1, 5), + (1, 'x', 1, 6), + (8, ')', 1, 7), + (11, ':', 1, 8), + (4, '', 1, 9), + (5, '', 2, -1), + (1, 'return', 2, 4), + (1, 'x', 2, 11), + (14, '+', 2, 13), + (2, '1', 2, 15), + (4, '', 2, 16), + (6, '', 2, -1), + (4, '', 2, -1), + (0, '', 2, -1)], + terminals) + + # # Second, we take *invalid* trees and make sure we get ParserError # rejections for them. |