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