[pypy-svn] r40134 - in pypy/dist/pypy/module/recparser: . hooksamples
afayolle at codespeak.net
afayolle at codespeak.net
Fri Mar 9 18:30:01 CET 2007
Author: afayolle
Date: Fri Mar 9 18:30:00 2007
New Revision: 40134
Modified:
pypy/dist/pypy/module/recparser/__init__.py
pypy/dist/pypy/module/recparser/app_class.py
pypy/dist/pypy/module/recparser/hooksamples/constchanger.py
Log:
Added ASTMutator app-level class
Changed constchanger to use ASTMutator
Modified: pypy/dist/pypy/module/recparser/__init__.py
==============================================================================
--- pypy/dist/pypy/module/recparser/__init__.py (original)
+++ pypy/dist/pypy/module/recparser/__init__.py Fri Mar 9 18:30:00 2007
@@ -18,6 +18,7 @@
appleveldefs = {
'ParserError' : 'app_class.ParserError',
'ASTVisitor': 'app_class.ASTVisitor',
+ 'ASTMutator': 'app_class.ASTMutator',
}
interpleveldefs = {
'__name__' : '(space.wrap("parser"))',
Modified: pypy/dist/pypy/module/recparser/app_class.py
==============================================================================
--- pypy/dist/pypy/module/recparser/app_class.py (original)
+++ pypy/dist/pypy/module/recparser/app_class.py Fri Mar 9 18:30:00 2007
@@ -20,11 +20,8 @@
def visitExpression(self, node):
return self.default(node)
-
def visitEmptyNode(self, node):
return self.default(node)
-
-
def visitAbstractFunction(self, node):
return self.default( node )
def visitAbstractTest(self, node):
@@ -183,3 +180,11 @@
return self.default( node )
def visitYield(self, node):
return self.default( node )
+
+class ASTMutator(ASTVisitor):
+ """This class is similar to ASTVisitor, but will call
+ node.mutate(self) instead of node.accept(self). The visitXXX
+ methods of derived class should return the mutated node"""
+ def default(self, node):
+ return node
+
Modified: pypy/dist/pypy/module/recparser/hooksamples/constchanger.py
==============================================================================
--- pypy/dist/pypy/module/recparser/hooksamples/constchanger.py (original)
+++ pypy/dist/pypy/module/recparser/hooksamples/constchanger.py Fri Mar 9 18:30:00 2007
@@ -1,21 +1,14 @@
-class ChangeConstVisitor:
+import parser
+class ConstMutator(parser.ASTMutator):
def visitConst(self, node):
if node.value == 3:
node.value = 2
+ return node
- def defaultvisit(self, node):
- for child in node.getChildNodes():
- child.accept(self)
-
- def __getattr__(self, attrname):
- if attrname.startswith('visit'):
- return self.defaultvisit
- raise AttributeError(attrname)
-
def threebecomestwo(ast, enc):
- ast.accept(ChangeConstVisitor())
+ ast.mutate(ConstMutator())
return ast
# install the hook
-import parser
parser.install_compiler_hook(threebecomestwo)
+print eval('3*2')
More information about the Pypy-commit
mailing list