visitor.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """API for traversing the AST nodes. Implemented by the compiler and
  2. meta introspection.
  3. """
  4. import typing as t
  5. from .nodes import Node
  6. if t.TYPE_CHECKING:
  7. import typing_extensions as te
  8. class VisitCallable(te.Protocol):
  9. def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any: ...
  10. class NodeVisitor:
  11. """Walks the abstract syntax tree and call visitor functions for every
  12. node found. The visitor functions may return values which will be
  13. forwarded by the `visit` method.
  14. Per default the visitor functions for the nodes are ``'visit_'`` +
  15. class name of the node. So a `TryFinally` node visit function would
  16. be `visit_TryFinally`. This behavior can be changed by overriding
  17. the `get_visitor` function. If no visitor function exists for a node
  18. (return value `None`) the `generic_visit` visitor is used instead.
  19. """
  20. def get_visitor(self, node: Node) -> "t.Optional[VisitCallable]":
  21. """Return the visitor function for this node or `None` if no visitor
  22. exists for this node. In that case the generic visit function is
  23. used instead.
  24. """
  25. return getattr(self, f"visit_{type(node).__name__}", None)
  26. def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:
  27. """Visit a node."""
  28. f = self.get_visitor(node)
  29. if f is not None:
  30. return f(node, *args, **kwargs)
  31. return self.generic_visit(node, *args, **kwargs)
  32. def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:
  33. """Called if no explicit visitor function exists for a node."""
  34. for child_node in node.iter_child_nodes():
  35. self.visit(child_node, *args, **kwargs)
  36. class NodeTransformer(NodeVisitor):
  37. """Walks the abstract syntax tree and allows modifications of nodes.
  38. The `NodeTransformer` will walk the AST and use the return value of the
  39. visitor functions to replace or remove the old node. If the return
  40. value of the visitor function is `None` the node will be removed
  41. from the previous location otherwise it's replaced with the return
  42. value. The return value may be the original node in which case no
  43. replacement takes place.
  44. """
  45. def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:
  46. for field, old_value in node.iter_fields():
  47. if isinstance(old_value, list):
  48. new_values = []
  49. for value in old_value:
  50. if isinstance(value, Node):
  51. value = self.visit(value, *args, **kwargs)
  52. if value is None:
  53. continue
  54. elif not isinstance(value, Node):
  55. new_values.extend(value)
  56. continue
  57. new_values.append(value)
  58. old_value[:] = new_values
  59. elif isinstance(old_value, Node):
  60. new_node = self.visit(old_value, *args, **kwargs)
  61. if new_node is None:
  62. delattr(node, field)
  63. else:
  64. setattr(node, field, new_node)
  65. return node
  66. def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.List[Node]:
  67. """As transformers may return lists in some places this method
  68. can be used to enforce a list as return value.
  69. """
  70. rv = self.visit(node, *args, **kwargs)
  71. if not isinstance(rv, list):
  72. return [rv]
  73. return rv