[pypy-svn] r65782 - pypy/branch/parser-compiler/pypy/interpreter/astcompiler/tools

benjamin at codespeak.net benjamin at codespeak.net
Mon Jun 15 02:09:49 CEST 2009


Author: benjamin
Date: Mon Jun 15 02:09:49 2009
New Revision: 65782

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/tools/asdl_py.py
Log:
add support for ast visitors

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/tools/asdl_py.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/tools/asdl_py.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/tools/asdl_py.py	Mon Jun 15 02:09:49 2009
@@ -17,6 +17,20 @@
         for df in mod.dfns:
             self.visit(df)
 
+    def visitSum(self, sum):
+        for tp in sum.types:
+            self.visit(tp)
+
+    def visitType(self, tp):
+        self.visit(tp.value)
+
+    def visitProduct(self, prod):
+        for field in prod.fields:
+            self.visit(field)
+
+    def visitField(self, field):
+        pass
+
     def emit(self, line, level=0):
         indent = "    "*level
         self.stream.write(indent + line + "\n")
@@ -67,21 +81,42 @@
             self.emit("class %s(%s):" % (cons.name, base))
             self.emit("")
             self.make_constructor(cons.fields + extra_attributes)
+            self.emit("")
+            self.emit("def walkabout(self, visitor):", 1)
+            self.emit("visitor.visit_%s(self)" % (cons.name,), 2)
 
     def visitField(self, field):
         self.emit("self.%s = %s" % (field.name, field.name), 2)
 
 
+class ASTVisitorVisitor(ASDLVisitor):
+    """A meta visitor! :)"""
+
+    def visitModule(self, mod):
+        self.emit("class ASTVisitor(object):")
+        self.emit("")
+        super(ASTVisitorVisitor, self).visitModule(mod)
+
+    def visitConstructor(self, cons):
+        self.emit("def visit_%s(self, node):" % (cons.name,), 1)
+        self.emit("raise NodeVisitorNotImplemented", 2)
+
+
 HEAD = """# Generated by tools/asdl_py.py
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter import typedef
 
 class AST(Wrappable):
+
+    def walkabout(self, visitor):
+        raise AssertionError("walkabout() implementation not provided")
+
+class NodeVisitorNotImplemented(Exception):
     pass
 
 """
 
-visitors = [ASTNodeVisitor]
+visitors = [ASTNodeVisitor, ASTVisitorVisitor]
 
 
 def main(argv):



More information about the Pypy-commit mailing list