javalang
javalang copied to clipboard
how to traverse all AST node?
dear all ! how to traverse all AST node? thank you very much!
Each node has attribute children
(it's a list). An element of that list is a list or Node.
In general you can traverse smth like this:
tree = javalang.parse.parse(open('file.java').read())
for path, node in tree:
for child in node.children:
if isinstance(child, list):
#recusively do it
for c in child.children:
if isinstance(child, list):
....
Each node has attribute
children
(it's a list). An element of that list is a list or Node. In general you can traverse smth like this:tree = javalang.parse.parse(open('file.java').read()) for path, node in tree: for child in node.children: if isinstance(child, list): #recusively do it for c in child.children: if isinstance(child, list): ....
hello,I find that there are some Node'children are not list, like this:
ReturnStatement(expression=MethodInvocation(arguments=[MemberReference(member=date, postfix_operators=[], prefix_operators=[], qualifier=, selectors=[])], member=parse, postfix_operators=[], prefix_operators=[], qualifier=format, selectors=[], type_arguments=None), label=None)
Node ReturnStatement's child is MethodInvocation, it's in 'expression=MethodInvocation', can you tell me how to solve it?