Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Visitor:
- @contextlib.contextmanager
- def __call__(self, node):
- yield node
- def visit(visitor, node):
- with visitor(node=node):
- for field in node.subnodes:
- subnode = getattr(node, field.name)
- with visitor(node=subnode):
- pass
- def walk(root):
- nodes = _collections.deque([root])
- while nodes:
- node = nodes.popleft()
- nodes.extend(node.subnodes)
- yield node
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement