From 6cbb99b3211abd7c9522260e281620612cf019a1 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sun, 4 Oct 2009 09:29:28 +0200 Subject: [PATCH] tiny helper class for replacing all occurrences of a node in a subtree --- Cython/Compiler/Visitor.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 + -- 2.26.2