[pypy-svn] r44452 - in pypy/dist/pypy/lang/scheme: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Jun 23 00:42:20 CEST 2007


Author: cfbolz
Date: Sat Jun 23 00:42:18 2007
New Revision: 44452

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/ssparser.py
   pypy/dist/pypy/lang/scheme/test/test_parser.py
Log:
use the experimental parsing thingy to write the scheme parser. seems to
work somewhat.


Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Sat Jun 23 00:42:18 2007
@@ -27,7 +27,7 @@
         return self.to_string() + "W"
 
     def __repr__(self):
-        return "<W_Root " + self.to_string + " >"
+        return "<W_Root " + self.to_string() + " >"
 
 class W_Symbol(W_Root):
     def __init__(self, val):

Modified: pypy/dist/pypy/lang/scheme/ssparser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/ssparser.py	(original)
+++ pypy/dist/pypy/lang/scheme/ssparser.py	Sat Jun 23 00:42:18 2007
@@ -1,29 +1,60 @@
 import autopath
-from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
-from pypy.rlib.parsing.parsing import ParseError
+from pypy.rlib.parsing.pypackrat import PackratParser
+from pypy.lang.scheme.object import W_Pair, W_Fixnum, W_String, W_Symbol
+from pypy.lang.scheme.object import W_Nil
 
 DEBUG = False
 
-grammar = r'''
-STRING: "\"([^\\\"]|\\\"|\\\\)*\"";
-IDENTIFIER: "[\+\-\*\^\?a-zA-Z0-9!<=>_~/$%&:]+";
-IGNORE: " |\n|\t|;[^\n]*";
-sexpr: ["("] sexpr* [")"] | <IDENTIFIER> | <STRING>;
-'''
-
-try:
-    regexs, rules, ToAST = parse_ebnf(grammar)
-except ParseError, e:
-    #print e.nice_error_message()
-    raise
-
-parsef = make_parse_function(regexs, rules, eof=True)
+class SchemeParser(PackratParser):
+    r'''
+    STRING:
+        c = `\"([^\\\"]|\\\"|\\\\)*\"`
+        IGNORE*
+        return {W_String(c)};
+
+    IDENTIFIER:
+        c = `[\+\-\*\^\?a-zA-Z!<=>_~/$%&:][\+\-\*\^\?a-zA-Z0-9!<=>_~/$%&:]*`
+        IGNORE*
+        return {W_Symbol(c)};
+
+    FIXNUM:
+        c = `0|([1-9][0-9]*)`
+        IGNORE*
+        return {W_Fixnum(int(c))};
+
+    IGNORE:
+        ` |\n|\t|;[^\n]*`;
+    
+    EOF:
+        !__any__;
+    
+    file:
+        IGNORE*
+        s = sexpr
+        EOF
+        return {s};
+    
+    sexpr:
+        list
+      | FIXNUM
+      | IDENTIFIER
+      | STRING;
+
+    list:
+        '('
+        IGNORE*
+        p = pair
+        ')'
+        IGNORE*
+        return {p};
+
+    pair:
+        car = sexpr
+        cdr = pair
+        return {W_Pair(car, cdr)}
+      | return {W_Nil()};
+    '''
 
 def parse(code):
-    t = parsef(code)
-    #tree = t.visit(ToAST())[0]
-    tree = ToAST().transform(t)
-    if DEBUG:
-        tree.view()
-    return tree
-
+    p = SchemeParser(code)
+    return p.file()

Modified: pypy/dist/pypy/lang/scheme/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_parser.py	Sat Jun 23 00:42:18 2007
@@ -1,18 +1,43 @@
-import py
 from pypy.lang.scheme.ssparser import parse
-from pypy.lang.scheme.astbuilder import ASTBuilder
-from pypy.rlib.parsing.parsing import Symbol, Nonterminal
+from pypy.lang.scheme.object import W_Pair, W_Fixnum, W_String, W_Symbol
+from pypy.lang.scheme.object import W_Nil
 
-def test_simple_sexpr():
-    #parse simple sexpr
-    t = parse(r'''(+ 1 2)''')
-    assert isinstance(t, Nonterminal)
-    assert len(t.children) == 3
-
-def test_string():
-    #parse string
+def unwrap(w_obj):
+    """for testing purposes: unwrap a scheme object into a python object"""
+    if isinstance(w_obj, W_Fixnum):
+        return w_obj.to_number()
+    elif isinstance(w_obj, W_String):
+        return w_obj.strval
+    elif isinstance(w_obj, W_Symbol):
+        return w_obj.name
+    elif isinstance(w_obj, W_Pair):
+        result = []
+        while not isinstance(w_obj, W_Nil):
+            result.append(unwrap(w_obj.car))
+            w_obj = w_obj.cdr
+        return result
+    raise NotImplementedError("don't know what to do with: %s" % (w_obj, ))
+    
+def test_simple():
+    w_fixnum = parse(r'''1''')
+    assert isinstance(w_fixnum, W_Fixnum)
+    w_fixnum = parse(r'''0''')
+    assert isinstance(w_fixnum, W_Fixnum)
+    w_fixnum = parse(r'''1123''')
+    assert isinstance(w_fixnum, W_Fixnum)
+    w_fixnum = parse(r'''abfa__''')
+    assert isinstance(w_fixnum, W_Symbol)
+    w_fixnum = parse(r'''+''')
+    assert isinstance(w_fixnum, W_Symbol)
     t = parse(r'''"don't beleive \"them\""''')
-    assert isinstance(t, Symbol)
+    assert isinstance(t, W_String)
+    w_list = parse(r'''(+ 1 2)''')
+    assert isinstance(w_list, W_Pair)
+    assert isinstance(w_list.car, W_Symbol)
+    assert isinstance(w_list.cdr, W_Pair)
+    assert isinstance(w_list.cdr.car, W_Fixnum)
+    assert isinstance(w_list.cdr.cdr.car, W_Fixnum)
+    assert isinstance(w_list.cdr.cdr.cdr, W_Nil)
 
 def test_complex_sexpr():
     #parse more complex sexpr
@@ -21,31 +46,20 @@
             (if (< n 2) n
                 (* (fac (- n 1)) n)))
         ''')
-    assert isinstance(t, Nonterminal)
-    assert len(t.children) == 3
-    assert isinstance(t.children[0], Symbol)
-    assert isinstance(t.children[1], Nonterminal)
-    assert isinstance(t.children[2], Nonterminal)
+    assert isinstance(t, W_Pair)
+    assert unwrap(t) == ['define', ['fac', 'n'],
+                            ['if', ['<', 'n', 2], 'n',
+                                   ['*', ['fac', ['-', 'n', 1]], 'n']]] 
 
 def test_ident_gen():
-    ch_list = "+-*/azAZ09<=>-_~!$%&:?^"
+    ch_list = "+-*/azAZ<=>-_~!$%&:?^"
     for char in ch_list:
         yield check_ident_ch, char
 
 def check_ident_ch(char):
     t = parse("(" + char + ")")
-    assert isinstance(t, Nonterminal)
-    assert isinstance(t.children[0], Symbol)
+    assert isinstance(t, W_Pair)
+    assert isinstance(t.car, W_Symbol)
+    assert isinstance(t.cdr, W_Nil)
 
-def eval_expr(expr):
-    t = parse(expr)
-    astb = ASTBuilder()
-    ast = astb.dispatch(t)
-    # evaluate with no context
-    return ast.eval(None)
-
-def test_eval_simple():
-    py.test.skip("in progress")
-    w_num = eval_expr(r'''1''')
-    assert w_num.to_number() == 1 
 



More information about the Pypy-commit mailing list