tiny helper class for replacing all occurrences of a node in a subtree
authorStefan Behnel <scoder@users.berlios.de>
Sun, 4 Oct 2009 07:29:28 +0000 (09:29 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 4 Oct 2009 07:29:28 +0000 (09:29 +0200)
Cython/Compiler/Visitor.py

index 823301985a996bd574bd280f756bdaee96c9abd7..cfc79f96d94223173ae2118aad0f1bedae69de61 100644 (file)
@@ -272,6 +272,22 @@ class CythonTransform(VisitorTransform):
         self.visitchildren(node)
         return node
 
+class RecursiveNodeReplacer(VisitorTransform):
+    """
+    Recursively replace all occurrences of a node in a subtree by
+    another node.
+    """
+    def __init__(self, orig_node, new_node):
+        super(RecursiveNodeReplacer, self).__init__()
+        self.orig_node, self.new_node = orig_node, new_node
+
+    def visit_Node(self, node):
+        self.visitchildren(node)
+        if node is self.orig_node:
+            return self.new_node
+        else:
+            return node
+