Skip to content

Commit 9e6007a

Browse files
committed
Add filter children selector to filter_inorder
Now children are also traversed on selected nodes. Also children can be filtered out (to allow cut ! predicates)
1 parent a64bb39 commit 9e6007a

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

ast_/ast.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ def _visit(self, node):
5454
def generic_visit(node: Ast):
5555
raise RuntimeError("No {}() method defined".format('visit_' + node.token))
5656

57-
def filter_inorder(self, node, filter_func: Callable[[Any], bool]):
58-
""" Visit the tree inorder, but only those that return true for filter
57+
def filter_inorder(self,
58+
node,
59+
filter_func: Callable[[Any], bool],
60+
child_selector: Callable[[Ast], bool] = lambda x: True):
61+
""" Visit the tree inorder, but only those that return true for filter_func and visiting children which
62+
return true for child_selector.
5963
"""
6064
visited = set()
6165
stack = [node]
@@ -66,5 +70,5 @@ def filter_inorder(self, node, filter_func: Callable[[Any], bool]):
6670
visited.add(node)
6771
if filter_func(node):
6872
yield self.visit(node)
69-
elif isinstance(node, Ast):
73+
if isinstance(node, Ast) and child_selector(node):
7074
stack.extend(node.children[::-1])

0 commit comments

Comments
 (0)