[pypy-svn] r11520 - pypy/dist/pypy/module/recparser
ludal at codespeak.net
ludal at codespeak.net
Wed Apr 27 15:45:15 CEST 2005
Author: ludal
Date: Wed Apr 27 15:45:15 2005
New Revision: 11520
Added:
pypy/dist/pypy/module/recparser/pyparser.py
Modified:
pypy/dist/pypy/module/recparser/README
pypy/dist/pypy/module/recparser/__init__.py
Log:
* first attempt at making the recursive parser a pypy module
* most code taken and adapted from its sibling DFA parser module
Modified: pypy/dist/pypy/module/recparser/README
==============================================================================
--- pypy/dist/pypy/module/recparser/README (original)
+++ pypy/dist/pypy/module/recparser/README Wed Apr 27 15:45:15 2005
@@ -6,3 +6,8 @@
This should change once we figure out how to integrate properly with
pypy and add an option to switch between the two parsers
+
+to enable the module in py.py change :
+baseobjspace.py:89
+pypy/interpreter/baseobjspace.py:89
+setbuiltinmodule( "parser", "recparser" )
Modified: pypy/dist/pypy/module/recparser/__init__.py
==============================================================================
--- pypy/dist/pypy/module/recparser/__init__.py (original)
+++ pypy/dist/pypy/module/recparser/__init__.py Wed Apr 27 15:45:15 2005
@@ -1 +1,43 @@
-# emtpy
+from pypy.interpreter.error import OperationError, debug_print
+from pypy.interpreter import module
+from pypy.interpreter.lazymodule import LazyModule
+
+
+import pythonutil
+
+debug_print( "Loading grammar %s" % pythonutil.PYTHON_GRAMMAR )
+PYTHON_PARSER = pythonutil.python_grammar()
+
+class Module(LazyModule):
+ """The builtin parser module.
+ """
+
+
+ appleveldefs = {
+ # 'ParserError' : 'app_class.ParserError',
+ }
+ interpleveldefs = {
+ '__name__' : '(space.wrap("parser"))',
+ '__doc__' : '(space.wrap("parser (recparser version) module"))',
+
+ 'suite' : 'pyparser.suite',
+ 'expr' : 'pyparser.expr',
+ 'STType' : 'pyparser.STType',
+# 'ASTType' : 'pyparser.STType',
+ # 'sequence2st' : 'pyparser.sequence2st',
+ #'eval_input' : 'pyparser.eval_input',
+ #'file_input' : 'pyparser.file_input',
+ #'compileast' : 'pyparser.compileast',
+ #'st2tuple' : 'pyparser.st2tuple',
+ #'st2list' : 'pyparser.st2list',
+ #'issuite' : 'pyparser.issuite',
+ #'ast2tuple' : 'pyparser.ast2tuple',
+ #'tuple2st' : 'pyparser.tuple2st',
+ #'isexpr' : 'pyparser.isexpr',
+ #'ast2list' : 'pyparser.ast2list',
+ #'sequence2ast' : 'pyparser.sequence2ast',
+ #'tuple2ast' : 'pyparser.tuple2ast',
+ #'_pickler' : 'pyparser._pickler',
+ #'compilest' : 'pyparser.compilest',
+ }
+
Added: pypy/dist/pypy/module/recparser/pyparser.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/recparser/pyparser.py Wed Apr 27 15:45:15 2005
@@ -0,0 +1,112 @@
+# Emulation layer for the recparser module
+# make it so that pyparser matches the 'parser' module interface
+
+from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, W_Root
+from pypy.interpreter.gateway import interp2app, applevel
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import interp_attrproperty, GetSetProperty
+from pypy.interpreter.pycode import PyCode
+from syntaxtree import SyntaxNode
+from pythonparse import parse_python_source
+from pypy.module.recparser import PYTHON_PARSER
+
+__all__ = [ "ASTType", "STType", "suite", "expr" ]
+
+class STType (Wrappable):
+ """Class STType
+ """
+ def __init__ (self, space, syntaxnode ):
+ """STType.__init__()
+ Wrapper for parse tree data returned by parse_python_source.
+ This encapsulate the syntaxnode at the head of the syntax tree
+ """
+ self.space = space
+ self.node = syntaxnode
+
+ def totuple (self, line_info = 0):
+ """STType.totuple()
+ Convert the ST object into a tuple representation.
+ """
+ # lineinfo is ignored for now
+ return self.node.totuple()
+
+ def descr_totuple(self, line_info = 0):
+ return self.space.wrap(self.totuple(line_info))
+
+ descr_totuple.unwrap_spec=['self', int]
+
+ def tolist (self, line_info = 0):
+ """STType.tolist()
+ Convert the ST object into a list representation.
+ """
+ return self.node.tolist()
+
+ def isexpr (self):
+ """STType.isexpr()
+ Returns true if the root node in the syntax tree is an expr node,
+ false otherwise.
+ """
+ return self.node.name == "eval_input"
+
+ def issuite (self):
+ """STType.issuite()
+ Returns true if the root node in the syntax tree is a suite node,
+ false otherwise.
+ """
+ return self.node.name == "file_input"
+
+ def descr_compile (self, w_filename = "<syntax_tree>"):
+ """STType.compile()
+ """
+ # We use the compiler module for that
+ space = self.space
+ tup = self.totuple(line_info=1)
+ w_tup = space.wrap(tup)
+ w_compileAST = mycompile(space, w_tup, w_filename)
+ if self.isexpr():
+ return exprcompile(space, w_compileAST)
+ else:
+ return modcompile(space, w_compileAST)
+
+ASTType = STType
+
+app = applevel("""
+ import compiler
+ def mycompile(tup, filename):
+ transformer = compiler.transformer.Transformer()
+ compileAST = transformer.compile_node(tup)
+ compiler.misc.set_filename(filename, compileAST)
+ return compileAST
+
+ def exprcompile(compileAST):
+ gen = compiler.pycodegen.ExpressionCodeGenerator(compileAST)
+ return gen.getCode()
+
+ def modcompile(compileAST):
+ gen = compiler.pycodegen.ModuleCodeGenerator(compileAST)
+ return gen.getCode()
+""", filename=__file__)
+
+mycompile = app.interphook("mycompile")
+exprcompile = app.interphook("exprcompile")
+modcompile = app.interphook("modcompile")
+
+STType.typedef = TypeDef("parser.st",
+ compile = interp2app(STType.descr_compile),
+ totuple = interp2app(STType.descr_totuple),
+)
+
+def suite( space, source ):
+ builder = parse_python_source( source, PYTHON_PARSER, "file_input" )
+ return space.wrap( STType(space, builder.stack[-1]) )
+
+suite.unwrap_spec = [ObjSpace, str]
+
+def expr( space, source ):
+ builder = parse_python_source( source, PYTHON_PARSER, "eval_input" )
+ return space.wrap( STType(space, builder.stack[-1]) )
+
+expr.unwrap_spec = [ObjSpace, str]
+
+
More information about the Pypy-commit
mailing list