mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Implement more nodes in the ast. Add script to walk over AST of sourcecode.
This commit is contained in:
1 parent
f752c93c0a
commit
e06de30eb6
4 files changed
+222
-106
No files matched your search
@@ -0,0 +1,33 @@
|
||||
|
||||
import ast
|
||||
import sys
|
||||
|
||||
filename = sys.argv[1]
|
||||
print('Crawling file:', filename)
|
||||
|
||||
|
||||
with open(filename, 'r') as f:
|
||||
source = f.read()
|
||||
|
||||
t = ast.parse(source)
|
||||
print(t)
|
||||
|
||||
shift = 3
|
||||
def print_node(node, indent=0):
|
||||
if isinstance(node, ast.AST):
|
||||
print(' '*indent, "NODE", node.__class__.__name__)
|
||||
for field in node._fields:
|
||||
print(' '*indent,'-', field)
|
||||
f = getattr(node, field)
|
||||
if isinstance(f, list):
|
||||
for f2 in f:
|
||||
print_node(f2, indent=indent+shift)
|
||||
else:
|
||||
print_node(f, indent=indent+shift)
|
||||
else:
|
||||
print(' '*indent, 'OBJ', node)
|
||||
|
||||
print_node(t)
|
||||
|
||||
# print(ast.dump(t))
|
||||
|
||||
Reference in new issue
Block a user