File: showparse

package info (click to toggle)
python-docutils 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,732 kB
  • ctags: 4,443
  • sloc: python: 36,034; lisp: 3,402; sh: 111; makefile: 106
file content (47 lines) | stat: -rwxr-xr-x 1,109 bytes parent folder | download
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
#! /usr/bin/env python

"""
This is a tool for exploring abstract syntax trees generated by
``parser.suite()`` from test data in
docutils/test/test_readers/test_python/test_parser or stdin.

Usage::

    showparse <key> <index>

    showparse < <module.py>

Where ``<key>`` is the key to the ``totest`` dictionary, and ``<index>`` is
the index of the list ``totest[key]``.  If no arguments are given, stdin is
used for input.
"""

import sys
import parser
import token
import symbol
import pprint
import test_parser

names = token.tok_name.copy()
names.update(symbol.sym_name)

def name_elements(ast):
    if ast:
        name = names[ast[0]]
        ast[0] = '%s (%s)' % (name, ast[0])
    for node in ast[1:]:
        if type(node) is list:
            name_elements(node)

if len(sys.argv) > 1:
    key, caseno = sys.argv[1:]
    print 'totest["%s"][%s][0]:\n' % (key, caseno)
    input_text = test_parser.totest[key][int(caseno)][0]
else:
    input_text = sys.stdin.read()
print input_text
module = parser.suite(input_text)
ast = parser.ast2list(module, line_info=1)
name_elements(ast)
pprint.pprint(ast)