[pypy-commit] pypy kill-someobject: Make rlib/parsing work
alex_gaynor
noreply at buildbot.pypy.org
Mon Oct 8 14:20:04 CEST 2012
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: kill-someobject
Changeset: r57920:1da2d2d81a3f
Date: 2012-10-08 14:19 +0200
http://bitbucket.org/pypy/pypy/changeset/1da2d2d81a3f/
Log: Make rlib/parsing work
diff --git a/pypy/rlib/parsing/test/test_deterministic.py b/pypy/rlib/parsing/test/test_deterministic.py
--- a/pypy/rlib/parsing/test/test_deterministic.py
+++ b/pypy/rlib/parsing/test/test_deterministic.py
@@ -1,5 +1,5 @@
from pypy.rlib.parsing.deterministic import *
-from pypy.translator.interactive import Translation
+from pypy.translator.c.test.test_genc import compile
def test_DFA_simple():
@@ -35,9 +35,7 @@
a[s0, "b"] = s2
a[s1, "b"] = s2
recognize = a.make_code()
- t = Translation(recognize)
- t.backendopt([str], backend="c")
- cfn = t.compile_c()
+ cfn = compile(recognize, [str])
assert cfn("aaaaaaaaaab")
assert cfn("b")
assert cfn("aaaacb")
@@ -69,6 +67,7 @@
assert not r.recognize("")
assert not r.recognize("100101101111")
+
def test_NFA_with_epsilon():
a = NFA()
z0 = a.add_state("z0", start=True)
@@ -88,6 +87,7 @@
fda = a.make_deterministic()
r = fda.get_runner()
+
def test_NFA_to_DFA_simple():
a = NFA()
z0 = a.add_state("z0", start=True)
@@ -107,8 +107,8 @@
assert not r.recognize("")
assert not r.recognize("100101101111")
+
def test_simplify():
- #py.test.skip("optimize not working yet")
a = DFA()
z0 = a.add_state("z0")
z1 = a.add_state("z1")
@@ -140,6 +140,7 @@
assert r.recognize("00")
assert not r.recognize("111111011111111")
+
def test_something():
a = NFA()
z0 = a.add_state("z0", start=True, final=True)
@@ -150,8 +151,8 @@
a.add_transition(z1, z1, "a")
a.add_transition(z1, z1, "b")
a.add_transition(z1, z2, "a")
- fda = a.make_deterministic()
-
+ a.make_deterministic()
+
def test_compress_char_set():
import string
assert compress_char_set("ace") == [('a', 1), ('c', 1), ('e', 1)]
@@ -160,7 +161,7 @@
assert compress_char_set("zycba") == [('a',3), ('y',2)]
assert compress_char_set(string.ascii_letters) == [('A', 26), ('a', 26)]
assert compress_char_set(string.printable) == [(' ', 95), ('\t', 5)]
-
+
def test_make_nice_charset_repr():
import string
assert make_nice_charset_repr("ace") == 'ace'
@@ -168,7 +169,7 @@
assert make_nice_charset_repr("ABCabc") == 'A-Ca-c'
assert make_nice_charset_repr("zycba") == 'a-cyz'
assert make_nice_charset_repr(string.ascii_letters) == 'A-Za-z'
-
+
# this next one is ugly because it's being generated from a dict, so the order is not stable
nice = make_nice_charset_repr(string.printable)
chunks = ['A-Z','a-z','0-9','\\t','\\x0b','\\n','\\r','\\x0c','\\\\','\\-']
diff --git a/pypy/rlib/parsing/test/test_regex.py b/pypy/rlib/parsing/test/test_regex.py
--- a/pypy/rlib/parsing/test/test_regex.py
+++ b/pypy/rlib/parsing/test/test_regex.py
@@ -1,19 +1,13 @@
-import py
from pypy.rlib.parsing.regex import *
+from pypy.translator.c.test.test_genc import compile
+
def compile_rex(rex):
- try:
- from pypy.translator.interactive import Translation
- except ImportError:
- py.test.skip("pypy not found")
fda = rex.make_automaton().make_deterministic()
fda.optimize()
fn = fda.make_code()
- t = Translation(fn)
- t.backendopt([str], backend="c")
- if py.test.config.option.view:
- t.view()
- return t.compile_c()
+ return compile(fn, [str])
+
def test_simple():
r = StringExpression("hallo")
@@ -25,6 +19,7 @@
assert not r.recognize("")
assert r.recognize("hallo")
+
def test_string_add():
r1 = StringExpression("Hello")
r2 = StringExpression(", World!\n")
@@ -33,6 +28,7 @@
assert r.recognize("Hello, World!\n")
assert not r.recognize("asfdasdfasDF")
+
def test_kleene():
r1 = StringExpression("ab")
r2 = r1.kleene()
@@ -43,6 +39,7 @@
assert r.recognize("abab")
assert not r.recognize("ababababababb")
+
def test_or():
r1 = StringExpression("ab").kleene() | StringExpression("cd").kleene()
nda = r1.make_automaton()
@@ -78,11 +75,12 @@
assert not r.recognize("acbbcbcaaccacacccc")
assert r.recognize("aaaa")
+
def test_bigger_than_101001():
O = StringExpression("0")
l = StringExpression("1")
d = O | l
- rex = ((l + d + d + d + d + d + +d) | (l + l + d + d + d + d) |
+ rex = ((l + d + d + d + d + d + +d) | (l + l + d + d + d + d) |
(l + O + l + l + d + d) | (l + O + l + O + l + d))
nda = rex.make_automaton()
fda = nda.make_deterministic()
@@ -104,7 +102,7 @@
assert fn("101010")
assert fn("101100")
assert fn("110000")
-
+
def test_even_length():
a = StringExpression("a")
diff --git a/pypy/rlib/parsing/test/test_regexparse.py b/pypy/rlib/parsing/test/test_regexparse.py
--- a/pypy/rlib/parsing/test/test_regexparse.py
+++ b/pypy/rlib/parsing/test/test_regexparse.py
@@ -1,8 +1,4 @@
-import py
-from pypy.rlib.parsing.regexparse import make_runner, unescape, RegexParser
-from pypy.rlib.parsing import regex
-import operator
-from pypy.rlib.parsing.makepackrat import PackratParser as _PackratParser
+from pypy.rlib.parsing.regexparse import make_runner
@@ -170,7 +166,7 @@
assert r.recognize("-0.912E+0001")
assert not r.recognize("-0.a912E+0001")
assert r.recognize("5")
-
+
def test_charclass():
r = make_runner(r"\d")
assert r.recognize('0')
diff --git a/pypy/rlib/parsing/test/test_translate.py b/pypy/rlib/parsing/test/test_translate.py
--- a/pypy/rlib/parsing/test/test_translate.py
+++ b/pypy/rlib/parsing/test/test_translate.py
@@ -1,11 +1,10 @@
-import py
-from pypy.translator.interactive import Translation
from pypy.rlib.parsing.lexer import *
+# Unused, but needed for some obscure reason
+from pypy.rlib.parsing.makepackrat import BacktrackException, Status
from pypy.rlib.parsing.regex import *
from pypy.rlib.parsing.parsing import *
-from pypy.rlib.parsing import deterministic
-from pypy.rlib.parsing.pypackrat import BacktrackException, Status
-from pypy.conftest import option
+from pypy.translator.c.test.test_genc import compile
+
class TestTranslateLexer(object):
def get_lexer(self, rexs, names, ignore=None):
@@ -34,16 +33,14 @@
"INT WHITE KEYWORD").split()
res = lex("if A a 12341 0 else", True).split("-%-")
assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
- t = Translation(lex)
- t.annotate([str, bool])
- t.rtype()
- func = t.compile_c()
+ func = compile(lex, [str, bool])
res = lex("if A a 12341 0 else", False).split("-%-")
assert res == ("KEYWORD WHITE VAR WHITE ATOM WHITE INT WHITE "
"INT WHITE KEYWORD").split()
res = lex("if A a 12341 0 else", True).split("-%-")
assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
+
def test_translate_parser():
r0 = Rule("expression", [["additive", "EOF"]])
r1 = Rule("additive", [["multitive", "+", "additive"], ["multitive"]])
@@ -59,11 +56,7 @@
def parse(choose):
tree = p.parse(data, lazy=False)
return tree.symbol + " " + "-%-".join([c.symbol for c in tree.children])
- t = Translation(parse)
- t.annotate([bool])
- t.backendopt()
- t.rtype()
- func = t.compile_c()
+ func = compile(parse, [bool])
res1 = parse(True)
res2 = func(True)
assert res1 == res2
@@ -88,15 +81,12 @@
def parse(choose):
tree = p.parse(data)
return tree.symbol + " " + "-%-".join([c.symbol for c in tree.children])
- t = Translation(parse)
- t.annotate([bool])
- t.backendopt()
- t.rtype()
- func = t.compile_c()
+ func = compile(parse, [bool])
res1 = parse(True)
res2 = func(True)
assert res1 == res2
+
def test_translate_ast_visitor():
from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
regexs, rules, ToAST = parse_ebnf("""
@@ -114,14 +104,11 @@
tree = tree[0]
return tree.symbol + " " + "-&-".join([c.symbol for c in tree.children])
res1 = f()
- t = Translation(f)
- t.annotate()
- t.rtype()
- t.backendopt()
- func = t.compile_c()
+ func = compile(f, [])
res2 = func()
assert res1 == res2
+
def test_translate_pypackrat():
from pypy.rlib.parsing.pypackrat import PackratParser
class parser(PackratParser):
@@ -149,13 +136,7 @@
return p.expr()
res = parse("5-5-5")
assert res == '((5 - 5) - 5)'
- t = Translation(parse)
- t.annotate([str])
- t.rtype()
- t.backendopt()
- if option.view:
- t.view()
- func = t.compile_c()
+ func = compile(parse, [str])
res = func("5-5-5")
assert res == '((5 - 5) - 5)'
@@ -172,13 +153,7 @@
return p.num()
res = parse("1234")
assert res == '1234'
- t = Translation(parse)
- t.annotate([str])
- t.rtype()
- t.backendopt()
- if option.view:
- t.view()
- func = t.compile_c()
+ func = compile(parse, [str])
res = func("12345")
assert res == '12345'
res = func("0")
More information about the pypy-commit
mailing list