From: Stefan Behnel Date: Sun, 4 Oct 2009 07:29:28 +0000 (+0200) Subject: tiny helper class for replacing all occurrences of a node in a subtree X-Git-Tag: 0.12.alpha0~182^2~14 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=6cbb99b3211abd7c9522260e281620612cf019a1;p=cython.git tiny helper class for replacing all occurrences of a node in a subtree --- diff --git a/Cython/Compiler/Visitor.py b/Cython/Compiler/Visitor.py index 82330198..cfc79f96 100644 --- a/Cython/Compiler/Visitor.py +++ b/Cython/Compiler/Visitor.py @@ -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 +