1
0

visitor.py 3.2 KB

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