[pypy-svn] r66298 - in pypy/branch/parser-compiler/pypy: config module/parser module/parser/test module/recparser

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 17 00:53:01 CEST 2009


Author: benjamin
Date: Fri Jul 17 00:53:00 2009
New Revision: 66298

Added:
   pypy/branch/parser-compiler/pypy/module/parser/
   pypy/branch/parser-compiler/pypy/module/parser/__init__.py   (contents, props changed)
   pypy/branch/parser-compiler/pypy/module/parser/pyparser.py   (contents, props changed)
   pypy/branch/parser-compiler/pypy/module/parser/test/
   pypy/branch/parser-compiler/pypy/module/parser/test/__init__.py   (contents, props changed)
   pypy/branch/parser-compiler/pypy/module/parser/test/test_parser.py   (contents, props changed)
Removed:
   pypy/branch/parser-compiler/pypy/module/recparser/
Modified:
   pypy/branch/parser-compiler/pypy/config/pypyoption.py
Log:
rip out the old recparser module and replace it with a simple cpython like one

Modified: pypy/branch/parser-compiler/pypy/config/pypyoption.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/config/pypyoption.py	(original)
+++ pypy/branch/parser-compiler/pypy/config/pypyoption.py	Fri Jul 17 00:53:00 2009
@@ -18,7 +18,7 @@
 default_modules.update(dict.fromkeys(
     ["_codecs", "gc", "_weakref", "marshal", "errno",
      "math", "_sre", "_pickle_support", "operator",
-     "recparser", "symbol", "_random", "__pypy__"]))
+     "parser", "symbol", "_random", "__pypy__"]))
 
 
 # --allworkingmodules

Added: pypy/branch/parser-compiler/pypy/module/parser/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/pypy/module/parser/__init__.py	Fri Jul 17 00:53:00 2009
@@ -0,0 +1,28 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+
+class Module(MixedModule):
+     """The builtin parser module."""
+
+     applevel_name = 'parser'
+
+     appleveldefs = {}
+
+     interpleveldefs = {
+         '__name__'     : '(space.wrap("parser"))',
+         '__doc__'      : '(space.wrap("parser module"))',
+
+         'suite'        : 'pyparser.suite',
+         'expr'         : 'pyparser.expr',
+         'issuite'      : 'pyparser.issuite',
+         'isexpr'       : 'pyparser.isexpr',
+         'STType'       : 'pyparser.STType',
+         'ast2tuple'    : 'pyparser.st2tuple',
+         'st2tuple'     : 'pyparser.st2tuple',
+         'ast2list'     : 'pyparser.st2list',
+         'ast2tuple'    : 'pyparser.st2tuple',
+         'ASTType'      : 'pyparser.STType',
+         'sequence2st'  : 'pyparser.sequence2st',
+         'compilest'    : 'pyparser.compilest',
+         'compileast'   : 'pyparser.compileast'
+         }

Added: pypy/branch/parser-compiler/pypy/module/parser/pyparser.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/pypy/module/parser/pyparser.py	Fri Jul 17 00:53:00 2009
@@ -0,0 +1,108 @@
+from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.argument import Arguments
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.pyparser import pyparse, pygram, error
+from pypy.interpreter.astcompiler.astbuilder import ast_from_node
+from pypy.interpreter.astcompiler.codegen import compile_ast
+
+
+class STType(Wrappable):
+
+    def __init__(self, tree, mode):
+        self.tree = tree
+        self.mode = mode
+
+    def _build_app_tree(self, space, node, seq_maker, with_lineno, with_column):
+        if node.children is not None:
+            seq_w = [None]*(1 + len(node.children))
+            seq_w[0] = space.wrap(node.type)
+            for i in range(1, len(node.children) + 1):
+                seq_w[i] = self._build_app_tree(space, node.children[i - 1],
+                                                seq_maker, with_lineno,
+                                                with_column)
+        else:
+            seq_w = [space.wrap(node.type), space.wrap(node.value)]
+            if with_lineno:
+                seq_w.append(space.wrap(node.lineno))
+            if with_column:
+                seq_w.append(space.wrap(node.column))
+        return seq_maker(seq_w)
+
+    def descr_issuite(self, space):
+        return space.wrap(self.tree.type == pygram.syms.file_input)
+    descr_issuite.unwrap_spec = ["self", ObjSpace]
+
+    def descr_isexpr(self, space):
+        return space.wrap(self.tree.type == pygram.syms.eval_input)
+    descr_isexpr.unwrap_spec = ["self", ObjSpace]
+
+    def descr_totuple(self, space, with_lineno=False, with_offset=False):
+        return self._build_app_tree(space, self.tree, space.newtuple,
+                                    with_lineno, with_offset)
+    descr_totuple.unwrap_spec = ["self", ObjSpace, bool, bool]
+
+    def descr_tolist(self, space, with_lineno=False, with_offset=False):
+        return self._build_app_tree(space, self.tree, space.newlist,
+                                    with_lineno, with_offset)
+    descr_tolist.unwrap_spec = ["self", ObjSpace, bool, bool]
+
+    def descr_compile(self, space, filename="<syntax-tree>"):
+        info = pyparse.CompileInfo(filename, self.mode)
+        ast = ast_from_node(space, self.tree, info)
+        return space.wrap(compile_ast(space, ast, info))
+    descr_compile.unwrap_spec = ["self", ObjSpace, str]
+
+STType.typedef = TypeDef("parser.st",
+    issuite=interp2app(STType.descr_issuite),
+    isexpr=interp2app(STType.descr_isexpr),
+    totuple=interp2app(STType.descr_totuple),
+    tolist=interp2app(STType.descr_tolist),
+    compile=interp2app(STType.descr_compile)
+)
+
+
+def parse_python(space, source, mode):
+    info = pyparse.CompileInfo("<string>", mode)
+    parser = pyparse.PythonParser(space)
+    try:
+       tree = parser.parse_source(source, info)
+    except error.IndentationError, e:
+        raise OperationError(space.w_IndentationError,
+                             e.wrap_info(space, "<string>"))
+    except error.SyntaxError, e:
+        raise OperationError(space.w_SyntaxError,
+                             e.wrap_info(space, "<string>"))
+    return space.wrap(STType(tree, mode))
+
+
+def suite(space, source):
+    return parse_python(space, source, 'exec')
+suite.unwrap_spec = [ObjSpace, str]
+
+
+def expr(space, source):
+    return parse_python(space, source, 'eval')
+expr.unwrap_spec = [ObjSpace, str]
+
+
+def isexpr(space, st):
+    return space.call_method(st, "isexpr")
+isexpr.unwrap_spec = [ObjSpace, STType]
+
+def issuite(space, st):
+    return space.call_method(st, "issuite")
+issuite.unwrap_spec = [ObjSpace, STType]
+
+def st2tuple(space, st, __args__):
+    return space.call_args(space.getattr(st, space.wrap("totuple")), __args__)
+st2tuple.unwrap_spec = [ObjSpace, STType, Arguments]
+
+def st2list(space, st, __args__):
+    return space.call_args(space.getattr(st, space.wrap("tolist")), __args__)
+st2list.unwrap_spec = [ObjSpace, STType, Arguments]
+
+def compilest(space, st, __args__):
+    return space.call_args(space.getattr(st, space.wrap("compile")), __args__)
+compilest.unwrap_spec = [ObjSpace, STType, Arguments]

Added: pypy/branch/parser-compiler/pypy/module/parser/test/__init__.py
==============================================================================

Added: pypy/branch/parser-compiler/pypy/module/parser/test/test_parser.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/pypy/module/parser/test/test_parser.py	Fri Jul 17 00:53:00 2009
@@ -0,0 +1,57 @@
+from pypy.conftest import gettestobjspace
+
+def setup_module(mod):
+    mod.space = gettestobjspace(usemodules=["parser"])
+
+class ParserModuleTest:
+
+    def setup_class(cls):
+        cls.space = space
+        cls.w_m = space.appexec([], """():
+    import parser
+    return parser""")
+
+
+class AppTestParser(ParserModuleTest):
+
+    def test_suite(self):
+        s = self.m.suite("x = 4")
+        assert isinstance(s, self.m.STType)
+        assert self.m.issuite(s)
+        assert s.issuite()
+        assert not self.m.isexpr(s)
+        assert not s.isexpr()
+
+    def test_expr(self):
+        s = self.m.expr("x")
+        assert isinstance(s, self.m.STType)
+        assert self.m.isexpr(s)
+        assert s.isexpr()
+        assert not self.m.issuite(s)
+        assert not s.issuite()
+
+    def test_totuple_and_tolist(self):
+        for meth, tp in (("totuple", tuple), ("tolist", list)):
+            s = self.m.suite("x = 4")
+            seq = getattr(s, meth)()
+            assert isinstance(seq, tp)
+            assert len(seq) == 4
+            assert seq[0] == 286
+            assert len(seq[2]) == 2
+            assert len(seq[3]) == 2
+            assert seq[2][0] == 4
+            assert seq[3][0] == 0
+            seq = getattr(s, meth)(True)
+            assert len(seq[2]) == 3
+            assert seq[2][2] == 1
+            seq = getattr(s, meth)(True, True)
+            assert len(seq[2]) == 4
+            assert seq[2][2] == 1
+            assert seq[2][3] == 0
+
+    def test_compile(self):
+        import types
+        for code in (self.m.suite("x = 4").compile(),
+                     self.m.compilest(self.m.suite("x = 4"))):
+            assert isinstance(code, types.CodeType)
+            assert code.co_filename == "<syntax-tree>"



More information about the Pypy-commit mailing list