summaryrefslogtreecommitdiff
path: root/docutils/test/test_readers/test_python
diff options
context:
space:
mode:
authorgoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2002-12-06 02:39:38 +0000
committergoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2002-12-06 02:39:38 +0000
commit47a890798d92d0bde3f2eb2091fafe6e1396525e (patch)
tree83c8cd36bb40b9066a20373e13003e4807c82612 /docutils/test/test_readers/test_python
parent2d5d6ab008ed252ab5a40e4338fb5a78e143bb78 (diff)
downloaddocutils-47a890798d92d0bde3f2eb2091fafe6e1396525e.tar.gz
a tool for exploring abstract syntax trees generated by ``compiler.parse()``
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@997 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/test/test_readers/test_python')
-rwxr-xr-xdocutils/test/test_readers/test_python/showast51
1 files changed, 51 insertions, 0 deletions
diff --git a/docutils/test/test_readers/test_python/showast b/docutils/test/test_readers/test_python/showast
new file mode 100755
index 000000000..2bb4ad3a6
--- /dev/null
+++ b/docutils/test/test_readers/test_python/showast
@@ -0,0 +1,51 @@
+#! /usr/bin/env python
+
+"""
+This is a tool for exploring abstract syntax trees generated by
+``compiler.parse()`` from test data in
+docutils/test/test_readers/test_python/test_parser.
+
+Usage::
+
+ showast <key> <index>
+
+Where ``<key>`` is the key to the ``totest`` dictionary, and ``<index>`` is
+the index of the list ``totest[key]``.
+"""
+
+import sys
+import compiler
+from compiler.ast import Node
+import test_parser
+
+def pformat(ast, indent=' ', level=0):
+ assert isinstance(ast, Node), 'ast is not a Node: %r' % (ast,)
+ atts = {}
+ for name, value in vars(ast).items():
+ if not value or isinstance(value, Node):
+ continue
+ if isinstance(value, list):
+ if isinstance(value[0], Node):
+ continue
+ if isinstance(value[0], tuple) and value[0] \
+ and isinstance(value[0][0], Node):
+ continue
+ atts[name] = str(value).encode('unicode-escape')
+ attlist = atts.items()
+ attlist.sort()
+ parts = [ast.__class__.__name__]
+ for name, value in attlist:
+ parts.append('%s="%s"' % (name, value))
+ result = ['%s<%s>\n' % (indent * level, ' '.join(parts))]
+ for node in ast.getChildNodes():
+ result.extend(pformat(node, level=level+1))
+ return result
+
+key, caseno = sys.argv[1:]
+print 'totest["%s"][%s][0]:\n' % (key, caseno)
+input_text = test_parser.totest[key][int(caseno)][0]
+print input_text
+module = compiler.parse(input_text)
+print module
+print
+print ''.join(pformat(module)),