Exemple #1
0
 def test_dummy_any(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(eval(begin(parse_text(any(char(_), _, Y), '222'), Y)),
         ['2', '2', '2'])
     eq_(eval(begin(parse_text(any(char(_), _, Y), '234'), Y)),
         ['2', '3', '4'])
     eq_(eval(begin(parse_text(any(char(_), _, Y), ''), Y)), [])
Exemple #2
0
 def testoptionalcut(self):
     x, s = Var('x'), Var('s')
     ruleList = [(s, function(((x, ), and_p(-char('a'), cut, char(x)))))]
     eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a')
     eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b')
     assert_raises(NoSolutionFound, eval,
                   let(ruleList, parse_text(s(x), 'a'), x))
Exemple #3
0
 def test_right_recursive2(self):
   x, p = Var('x'), Var('p')
   function1 = [(p,function( ((), and_p(char(x), p())),
                    ((),char(x))))]
   eq_(eval(letr(function1, parse_text(p(),'a'), parse_text(p(),'ab'), parse_text(p(),'abc'))),
       'c')
   assert_raises(NoSolutionFound, eval, letr(function1, parse_text(p(), '')))
Exemple #4
0
 def test_right_recursive1(self):
   function1 = function( ((), and_p(char('a'), f())),
                    ((),char('b')))
   eq_(eval(letr([(f,function1)], 
              parse_text(f(),'b'), parse_text(f(),'ab'), parse_text(f(),'aab'))), 
       'b')
   assert_raises(NoSolutionFound, eval, letr([(f,function1)], parse_text(f(), 'a')))
Exemple #5
0
 def test_some(self):
     X, Y = Var('X'), Var('Y')
     eq_(eval(begin(parse_text(some(char(X), X, Y), '222'), Y)),
         ['2', '2', '2'])
     eq_(eval(begin(parse_text(some(char(X), X, Y), '234'), Y)), ['2'])
     assert_raises(NoSolutionFound, eval,
                   begin(parse_text(some(char(X), X, Y), ''), Y))
Exemple #6
0
 def test_dummy_times_between(self):
   _, Y = DummyVar('_'), Var('Y')
   eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '234'), Y)), ['2','3', '4'])
   eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '23'), Y)), ['2','3'])
   eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '2345'), Y)), ['2','3', '4'])
   assert_raises(NoSolutionFound, eval, begin(parse_text(times_between(char(_), 2, 3, _, Y), '2'), Y))
   assert_raises(NoSolutionFound, eval, begin(parse_text((and_p, times_between(char(_), 2, 3, _, Y), eoi), '2345'), Y))
Exemple #7
0
 def test_unify_right_recursive(self):
   x, p = Var('x'), Var('p')
   function1 = [(p,function( ((x,), and_p(char(x), p(x))),
                         ((x,),char(x))))]
   eq_(eval(letr(function1, parse_text(p(x), 'aa'))), 'a')
   eq_(eval(letr(function1, parse_text(p(x), 'a'))), 'a')
   assert_raises(NoSolutionFound, eval, letr(function1, parse_text(and_p(p(x), eoi), 'xy')))
   assert_raises(NoSolutionFound, eval, letr(function1, parse_text(p(x), '')))
Exemple #8
0
 def test_any_some(self):
     X, Y = Var('X'), Var('Y')
     eq_(
         eval(
             begin(
                 parse_text(
                     char(X) + any(~char('b') + some(char(X))) + eoi,
                     'abaaaa'), X)), 'a')
Exemple #9
0
 def testABCD(self):
   A, B, C, D = vars('A, B, C, D')
   ruleList = [(A,function(((), B()|C()))), 
               (B,function(((), D()+char('b')))), 
               (C,function(((), D()+char('c')))), 
               (D,function(((), char('d')))), 
              ]
   eq_(eval(letr(ruleList, parse_text(A()+eoi,  'dc'))), True)
Exemple #10
0
 def test_dummy_times_more(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(eval(begin(parse_text((times_more, char(_), 3, _, Y), '234'), Y)),
         ['2', '3', '4'])
     eq_(eval(begin(parse_text((times_more, char(_), 3, _, Y), '2345'), Y)),
         ['2', '3', '4', '5'])
     assert_raises(NoSolutionFound, eval,
                   begin(parse_text(times_more(char(_), 3, _, Y), '23'), Y))
Exemple #11
0
 def test_right_recursive1(self):
     function1 = function(((), and_p(char('a'), f())), ((), char('b')))
     eq_(
         eval(
             letr([(f, function1)], parse_text(f(), 'b'),
                  parse_text(f(), 'ab'), parse_text(f(), 'aab'))), 'b')
     assert_raises(NoSolutionFound, eval,
                   letr([(f, function1)], parse_text(f(), 'a')))
Exemple #12
0
 def test_dumy_some(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(eval(begin(parse_text(some(char(_), _, Y), '222'), Y)),
         ['2', '2', '2'])
     eq_(eval(begin(parse_text(some(char(_), _, Y), '234'), Y)),
         ['2', '3', '4'])
     assert_raises(NoSolutionFound, eval,
                   begin(parse_text(some(char(_), _, Y), ''), Y))
Exemple #13
0
 def test_dummy_times_less_lazy(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(
         eval(
             preparse(
                 begin(
                     parse_text(
                         times_less(char(_), 3, _, Y, lazy) + char('4'),
                         '234'), Y))), ['2', '3'])
Exemple #14
0
 def testABCD(self):
     A, B, C, D = vars('A, B, C, D')
     ruleList = [
         (A, function(((), B() | C()))),
         (B, function(((), D() + char('b')))),
         (C, function(((), D() + char('c')))),
         (D, function(((), char('d')))),
     ]
     eq_(eval(letr(ruleList, parse_text(A() + eoi, 'dc'))), True)
Exemple #15
0
 def testparallelRule(self):
   x, s, gt, lt = Var('x'), Var('s'), Var('>'), Var('<')
   ruleList = [(s,function( ((x,), parallel(gt(x, 3), lt(x, 5))))),
               (gt,function( ((4, 3),char('4')))),
               (lt,function( ((4, 5),char('4'))))]
   eq_(eval(letr(ruleList, parse_text(s(x), '4'), x)), 4)
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '6'), x))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), ''), x))
Exemple #16
0
 def test_right_recursive2(self):
     x, p = Var('x'), Var('p')
     function1 = [(p, function(((), and_p(char(x), p())), ((), char(x))))]
     eq_(
         eval(
             letr(function1, parse_text(p(), 'a'), parse_text(p(), 'ab'),
                  parse_text(p(), 'abc'))), 'c')
     assert_raises(NoSolutionFound, eval,
                   letr(function1, parse_text(p(), '')))
Exemple #17
0
 def testIndentUndent(self):
   _, n, s, line = DummyVar('_'), Var('n'), Var('s'), Var('line')
   space = char(' ')
   ruleList = [(s,function( ((n,), some(line(n)),s(add(n,1))),
                           ((n,), some(line(n))))),
               (line,function( ((n,), times(space, n),some(letter(_)),any(space),char('\n'))))
               ]
   eq_(eval(letr(ruleList, parse_text(s(0),  'a\n b\n c\n'))), True)
   eq_(eval(letr(ruleList, parse_text(s(0),  'asd\n bdf\n cdfh\n'))), True)    
Exemple #18
0
 def testDirectLeftRecursive(self):
   #assert 0, 'temporary mask'
   E = Var('E')
   ruleList = [(E,function( 
                    ((), E()+char('a')),
                    ((), char('b')),
                    ))]
   eq_(eval(letr(ruleList, parse_text(E()+eoi,  'b'))), True)
   eq_(eval(letr(ruleList, parse_text(E()+eoi,  'ba'))), True)
   eq_(eval(letr(ruleList, parse_text(E()+eoi,  'baa'))), True)
Exemple #19
0
 def testOr(self):
   x, s, one, two = Var('x'), Var('s'), Var('one'), Var('two')
   ruleList = [(s, function( ((x,), or_p(one(x), two(x))))),
               (one, function( (('1',),char('1')))),
               (two, function( (('2',),char('2'))))]
   eq_(eval(letr(ruleList, parse_text(s(x), '1'))), '1')
   eq_(eval(letr(ruleList, parse_text(s(y),  '2'))), '2')         
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '3')))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '12')))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '')))
Exemple #20
0
 def testIndirectLeftRecursive(self):
     #assert 0, 'temporary mask'
     A, B, C = vars('A, B, C')
     ruleList = [(A, function(((), B()))),
                 (B, function(
                     ((), A() + char('a')),
                     ((), char('b')),
                 ))]
     eq_(eval(letr(ruleList, parse_text(A() + eoi, 'b'))), True)
     eq_(eval(letr(ruleList, parse_text(A() + eoi, 'ba'))), True)
     eq_(eval(letr(ruleList, parse_text(A() + eoi, 'baa'))), True)
Exemple #21
0
 def test_unify_right_recursive(self):
     x, p = Var('x'), Var('p')
     function1 = [(p,
                   function(((x, ), and_p(char(x), p(x))),
                            ((x, ), char(x))))]
     eq_(eval(letr(function1, parse_text(p(x), 'aa'))), 'a')
     eq_(eval(letr(function1, parse_text(p(x), 'a'))), 'a')
     assert_raises(NoSolutionFound, eval,
                   letr(function1, parse_text(and_p(p(x), eoi), 'xy')))
     assert_raises(NoSolutionFound, eval,
                   letr(function1, parse_text(p(x), '')))
Exemple #22
0
 def testIndentUndent(self):
     _, n, s, line = DummyVar('_'), Var('n'), Var('s'), Var('line')
     space = char(' ')
     ruleList = [(s,
                  function(((n, ), some(line(n)), s(add(n, 1))),
                           ((n, ), some(line(n))))),
                 (line,
                  function(((n, ), times(space, n), some(letter(_)),
                            any(space), char('\n'))))]
     eq_(eval(letr(ruleList, parse_text(s(0), 'a\n b\n c\n'))), True)
     eq_(eval(letr(ruleList, parse_text(s(0), 'asd\n bdf\n cdfh\n'))), True)
Exemple #23
0
 def testIndirectLeftRecursive(self):
   #assert 0, 'temporary mask'
   A, B, C = vars('A, B, C')
   ruleList = [(A, function(((), B()))), 
               (B, function(
                     ((), A()+char('a')),
                      ((), char('b')),
                    ))]
   eq_(eval(letr(ruleList, parse_text(A()+eoi,  'b'))), True)
   eq_(eval(letr(ruleList, parse_text(A()+eoi,  'ba'))), True)
   eq_(eval(letr(ruleList, parse_text(A()+eoi,  'baa'))), True)
Exemple #24
0
 def xxxtest_left_recursive(self):
     assert 0, 'a good idea is waiting to be implemented'
     assert 0, 'googd idea failed!'
     engine = Engine()
     X = Var()
     f = userTerm("f")
     engine.add_rule(rule(f(X), f(X) + char('1')))
     engine.add_rule(rule(f(X), char('2')))
     X = Var()
     engine.run(f(X), '21')
     engine.run(f(X), '2')
     assert_raises(UnifyFail, engine.run, f(X), '1')
Exemple #25
0
 def testparallelRule(self):
     x, s, gt, lt = Var('x'), Var('s'), Var('>'), Var('<')
     ruleList = [(s, function(((x, ), parallel(gt(x, 3), lt(x, 5))))),
                 (gt, function(((4, 3), char('4')))),
                 (lt, function(((4, 5), char('4'))))]
     eq_(eval(letr(ruleList, parse_text(s(x), '4'), x)), 4)
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(s(x), '6'), x))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(s(x), ''), x))
Exemple #26
0
 def xxxtest_left_recursive(self):
   assert 0, 'a good idea is waiting to be implemented'
   assert 0, 'googd idea failed!'
   engine = Engine()
   X = Var()
   f = userTerm("f")
   engine.add_rule(rule(f(X), f(X)+char('1')))
   engine.add_rule(rule(f(X),char('2')))
   X = Var()
   engine.run(f(X), '21')
   engine.run(f(X), '2')
   assert_raises(UnifyFail, engine.run, f(X), '1')
Exemple #27
0
 def testOr(self):
     x, s, one, two = Var('x'), Var('s'), Var('one'), Var('two')
     ruleList = [(s, function(((x, ), or_p(one(x), two(x))))),
                 (one, function((('1', ), char('1')))),
                 (two, function((('2', ), char('2'))))]
     eq_(eval(letr(ruleList, parse_text(s(x), '1'))), '1')
     eq_(eval(letr(ruleList, parse_text(s(y), '2'))), '2')
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(s(x), '3')))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), '12')))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(s(x), '')))
Exemple #28
0
 def testExpressionByRightRecursiveList(self):
   E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2')  
   ruleList = [(E,function( 
                    (((e1, '/', e2), 1), E(e1,2), char('/'), E(e2, 1)),
                    ((1, 2), char('1')),
                    ((e, 1), E(e, 2))))]
   eq_(eval(letr(ruleList, parse_text(E(e, 1),  '1/1/1'), e)), (1, '/', (1, '/', 1)))
   eq_(eval(letr(ruleList, parse_text(E(e, 1),  '1/1'), e)), (1, '/', 1))
   eq_(eval(letr(ruleList, parse_text(E(e, 1),  '1'), e)), 1)
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1+1/1'), e))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '2'), e))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1/'), e))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '/'), e))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), ''), e))
Exemple #29
0
 def test_chars(self):
     x, cs, chars = Var('x'), Var('cs'), Var('chars')
     eq_(
         eval(
             let([(chars, function(
                 ((x, cs), and_p(char(x), contain(cs, x)))))],
                 parse_text(chars(x, 'a'), 'a'))), True)
Exemple #30
0
 def testRecursiveReturnValue3(self):
     E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2')
     ruleList = [(E,
                  function(((e, 1), E(e, 2)), ((e, 2), char(e)),
                           (((e1, e2), 1), E(e1, 2), E(e2, 1))))]
     eq_(eval(letr(ruleList, parse_text(E(e, 1) + eoi, '12'), e)),
         ('1', '2'))
Exemple #31
0
 def test_kleene1(self):
     f, item, kleene = Var('f'), Var('item'), Var('kleene')
     fun = macro(((item, ),
                  letr([(f, macro(((), eval_(item), f()), ((), nullword)))],
                       f())))
     eq_(eval(let([(kleene, fun)], set_text('aa'), kleene(char('a')))),
         True)
Exemple #32
0
 def test_kleene1(self):
   f, item, kleene = Var('f'), Var('item'), Var('kleene')
   fun = macro(((item,),  
                        letr([(f,macro(((), eval_(item), f()),
                                        ((), nullword)))], 
                             f())))
   eq_(eval(let([(kleene,fun)], set_text('aa'), kleene(char('a')))), True)
Exemple #33
0
 def testRecursiveReturnValue3(self):
   E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2')  
   ruleList = [(E,function( 
                    ((e, 1), E(e, 2)),
                    ((e, 2), char(e)),
                    (((e1, e2), 1), E(e1,2), E(e2, 1))
                        ))]
   eq_(eval(letr(ruleList, parse_text(E(e, 1)+eoi, '12'), e)), ('1', '2'))
Exemple #34
0
 def test_times_a2(self):
     X, Y, S = Var('X'), Var('Y'), Var('S')
     function1 = function(((Y, ), times(char('a'), 2, 'a', Y)))
     eq_(eval(begin(parse_text(function1(X), 'aa'), X)), ['a', 'a'])
     assert_raises(NoSolutionFound, eval,
                   begin(parse_text(function1(X), 'a'), X))
     assert_raises(NoSolutionFound, eval,
                   begin(parse_text(and_p(function1(X), eoi), 'aaa'), X))
Exemple #35
0
 def test_some2(self):
     X = Var('X')
     eq_(eval(parse_text(and_p(some(char(X)), char('4')), '224')), '4')
     eq_(eval(parse_text(and_p(some(char(X)), cut, char('4')), '224')), '4')
     assert_raises(
         NoSolutionFound, eval,
         parse_text(and_p(some(char(X)), char('3'), char('5')), '2234'))
Exemple #36
0
 def test_dummy_times_less(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(
         eval(
             preparse(
                 begin(
                     parse_text(
                         times_less(char(_), 3, _, Y) + char('4'), '234'),
                     Y))), ['2', '3'])
     eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '234'), Y)),
         ['2', '3', '4'])
     eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '23'), Y)),
         ['2', '3'])
     assert_raises(
         NoSolutionFound, eval,
         preparse(
             begin(parse_text(times_less(char(_), 3, _, Y) + eoi, '2345'),
                   Y)))
Exemple #37
0
 def testKleene1(self): #occurs_check
   x, s, kleene = Var('x'), Var('s'), Var('kleene')
   ruleList = [(s,function( ((x,), kleene(x)))),
               (kleene,function( 
                 ((Cons('a', x),), and_p(char('a'), kleene(x))),
                 ((nil,), nullword)))]
   eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
   eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '6'), x))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
Exemple #38
0
 def testKleene3(self):
   x, c, kleene = Var('x'), Var('c'), Var('kleene')
   ruleList = [(kleene,function( 
                 ((Cons(c, x),), char(c),kleene(x)),
                 ((nil,), nullword)))]
   eq_(eval(letr(ruleList, parse_text(kleene(x), 'a'), x)), L('a'))
   eq_(eval(letr(ruleList, parse_text(kleene(x), 'aa'), x)), L('a', 'a'))
   eq_(eval(letr(ruleList, parse_text(kleene(x), 'aaa'), x)), L('a', 'a', 'a'))
   eq_(eval(letr(ruleList, parse_text(kleene(x), 'bbb'), x)), L('b', 'b', 'b'))
   eq_(eval(letr(ruleList, parse_text(kleene(x), 'abc'), x)), L('a', 'b', 'c'))
   eq_(eval(letr(ruleList, parse_text(kleene(x), ''), x)), nil)
Exemple #39
0
 def test_dummy_times_between(self):
     _, Y = DummyVar('_'), Var('Y')
     eq_(
         eval(
             begin(parse_text(times_between(char(_), 2, 3, _, Y), '234'),
                   Y)), ['2', '3', '4'])
     eq_(
         eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '23'),
                    Y)), ['2', '3'])
     eq_(
         eval(
             begin(parse_text(times_between(char(_), 2, 3, _, Y), '2345'),
                   Y)), ['2', '3', '4'])
     assert_raises(
         NoSolutionFound, eval,
         begin(parse_text(times_between(char(_), 2, 3, _, Y), '2'), Y))
     assert_raises(
         NoSolutionFound, eval,
         begin(
             parse_text((and_p, times_between(char(_), 2, 3, _, Y), eoi),
                        '2345'), Y))
Exemple #40
0
 def testExpressionByRightRecursiveList(self):
     E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2')
     ruleList = [(E,
                  function(
                      (((e1, '/', e2), 1), E(e1, 2), char('/'), E(e2, 1)),
                      ((1, 2), char('1')), ((e, 1), E(e, 2))))]
     eq_(eval(letr(ruleList, parse_text(E(e, 1), '1/1/1'), e)),
         (1, '/', (1, '/', 1)))
     eq_(eval(letr(ruleList, parse_text(E(e, 1), '1/1'), e)), (1, '/', 1))
     eq_(eval(letr(ruleList, parse_text(E(e, 1), '1'), e)), 1)
     assert_raises(
         NoSolutionFound, eval,
         letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1+1/1'), e))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(E(e, 1), eoi), '2'), e))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1/'), e))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(E(e, 1), eoi), '/'), e))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(E(e, 1), eoi), ''), e))
Exemple #41
0
 def testKleene1(self):  #occurs_check
     x, s, kleene = Var('x'), Var('s'), Var('kleene')
     ruleList = [(s, function(((x, ), kleene(x)))),
                 (kleene,
                  function(((Cons('a', x), ), and_p(char('a'), kleene(x))),
                           ((nil, ), nullword)))]
     eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
     eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), '6'), x))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
Exemple #42
0
 def testKleene4(self):
   x, _, c, s, kleene = Var('x'), DummyVar('_'), Var('c'), Var('s'), Var('kleene')
   ruleList = [(s,function( ((x,), kleene(_, x)))),
               (kleene,function( 
                 ((_, Cons(c, x)), char(c)&kleene(_, x)),
                 ((_, nil), nullword)))]
   eq_(eval(letr(ruleList, parse_text(s(x), 'a'), x)), L('a'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'abc'), x)), L('a', 'b', 'c'))
   eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
Exemple #43
0
 def test_seplist(self):
     X, Y = Var('X'), Var('Y')
     eq_(
         eval(
             begin(parse_text(seplist(char(X), char(','), X, Y), '2,2,2'),
                   Y)), ['2', '2', '2'])
     eq_(
         eval(
             begin(parse_text(seplist(char(X), char(','), X, Y), '2,3,4'),
                   Y)), ['2'])
     eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), '2'), Y)),
         ['2'])
     eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), ''), Y)),
         [])
Exemple #44
0
 def testDirectLeftRecursive(self):
     #assert 0, 'temporary mask'
     E = Var('E')
     ruleList = [(
         E,
         function(
             (
                 (),  #println('e1'), 
                 E(),
                 #println('e2'),
                 char('a'),
                 #println('e3')
             ),
             (
                 (),
                 char('b'),
                 #println('eb')
             ),
         ))]
     eq_(eval(letr(ruleList, parse_text(E() + eoi, 'b'))), True)
     eq_(eval(letr(ruleList, parse_text(E() + eoi, 'ba'))), True)
     eq_(eval(letr(ruleList, parse_text(E() + eoi, 'baa'))), True)
Exemple #45
0
 def testKleene4(self):
     x, _, c, s, kleene = Var('x'), DummyVar('_'), Var('c'), Var('s'), Var(
         'kleene')
     ruleList = [(s, function(((x, ), kleene(_, x)))),
                 (kleene,
                  function(((_, Cons(c, x)), char(c) & kleene(_, x)),
                           ((_, nil), nullword)))]
     eq_(eval(letr(ruleList, parse_text(s(x), 'a'), x)), L('a'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'abc'), x)), L('a', 'b', 'c'))
     eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
Exemple #46
0
 def testKleene2(self):
   x, c, s, kleene = Var('x'), Var('c'), Var('s'), Var('kleene')
   ruleList = [(s,function( ((x,), kleene(c, x)))),
               (kleene,function( 
                 ((c, Cons(c, x)), and_p(char(c), kleene(c, x))),
                 ((c, nil), nullword)))]
   eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a'))
   eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b'))
   eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'aab'), x))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'abc'), x))
   assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
Exemple #47
0
 def testKleene3(self):
     x, c, kleene = Var('x'), Var('c'), Var('kleene')
     ruleList = [(kleene,
                  function(((Cons(c, x), ), char(c), kleene(x)),
                           ((nil, ), nullword)))]
     eq_(eval(letr(ruleList, parse_text(kleene(x), 'a'), x)), L('a'))
     eq_(eval(letr(ruleList, parse_text(kleene(x), 'aa'), x)), L('a', 'a'))
     eq_(eval(letr(ruleList, parse_text(kleene(x), 'aaa'), x)),
         L('a', 'a', 'a'))
     eq_(eval(letr(ruleList, parse_text(kleene(x), 'bbb'), x)),
         L('b', 'b', 'b'))
     eq_(eval(letr(ruleList, parse_text(kleene(x), 'abc'), x)),
         L('a', 'b', 'c'))
     eq_(eval(letr(ruleList, parse_text(kleene(x), ''), x)), nil)
Exemple #48
0
 def testKleene2(self):
     x, c, s, kleene = Var('x'), Var('c'), Var('s'), Var('kleene')
     ruleList = [(s, function(((x, ), kleene(c, x)))),
                 (kleene,
                  function(((c, Cons(c, x)), and_p(char(c), kleene(c, x))),
                           ((c, nil), nullword)))]
     eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a'))
     eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b'))
     eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil)
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), 'aab'), x))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), 'abc'), x))
     assert_raises(NoSolutionFound, eval,
                   letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
Exemple #49
0
 def testDirectLeftRecursiveWithArguments(self):
   #assert 0, 'temporary mask'
   E, X = Var('E'), Var('X')
   ruleList = [(E,function( 
                    ((), #println('e1'), 
                         E(), 
                         #println('e2'), 
                         digit(X), 
                         #println('e3', X)
                         ),
                    ((), char('b'), 
                         #println('eb')
                         ),
                    ))]
   #eq_(eval(letr(ruleList, parse_text(E()+eoi,  'b'))), True)
   #eq_(eval(letr(ruleList, parse_text(E()+eoi,  'b1'))), True)
   eq_(eval(letr(ruleList, parse_text(E()+eoi,  'b123'))), True)
Exemple #50
0
 def test_nullword1(self):
   eq_(eval(parse_text(char('a')&nullword&char('b'), 'ab')), 'b')    
Exemple #51
0
 def testnullword3(self):
   rule = and_p(char('a'), nullword, char('b'))
   eq_(eval(parse_text(rule, 'ab')), 'b')
   assert_raises(NoSolutionFound, eval, parse_text(rule, 'a b'))
   assert_raises(NoSolutionFound, eval, parse_text(rule, 'a'))
Exemple #52
0
 def test_dummy_times_less(self):
   _, Y = DummyVar('_'), Var('Y')
   eq_(eval(preparse(begin(parse_text(times_less(char(_), 3, _, Y)+char('4'), '234'), Y))), ['2','3'])
   eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '234'), Y)), ['2','3','4'])
   eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '23'), Y)), ['2','3'])
   assert_raises(NoSolutionFound, eval, preparse(begin(parse_text(times_less(char(_), 3, _, Y)+eoi, '2345'), Y)))
Exemple #53
0
 def test_char(self):
   eq_(eval(parse_text(char('a'), 'a')), 'a')
Exemple #54
0
 def test_dummy_times_less_lazy(self):
   _, Y = DummyVar('_'), Var('Y')
   eq_(eval(preparse(begin(parse_text(times_less(char(_), 3, _, Y, lazy)+char('4'), '234'), Y))), ['2','3'])
Exemple #55
0
 def test_chars(self):
   x, cs,chars = Var('x'), Var('cs'), Var('chars')
   eq_(eval(let([(chars, function(((x, cs), and_p(char(x), contain(cs, x)))))],
                           parse_text(chars(x, 'a'), 'a'))), True)
Exemple #56
0
 def test_times_a2(self): 
   X, Y, S = Var('X'), Var('Y'), Var('S')
   function1 = function(((Y,), times(char('a'), 2, 'a', Y)))
   eq_(eval(begin(parse_text(function1(X),'aa'), X)), ['a', 'a'])
   assert_raises(NoSolutionFound, eval, begin(parse_text(function1(X), 'a'), X))
   assert_raises(NoSolutionFound, eval, begin(parse_text(and_p(function1(X), eoi), 'aaa'), X))
Exemple #57
0
 def test_times_an(self): 
   X, Y, S, n = Var('X'), Var('Y'), Var('S'), Var('n')
   function1 = function( ((Y,), times(char('a'), n, 'a', Y)))
   eq_(eval(begin(parse_text(function1(X), 'a'), X)), ['a'])
   eq_(eval(begin(parse_text(function1(X), 'aa'), X)), ['a', 'a'])
   eq_(eval(begin(parse_text(function1(X), 'aaa'), X)), ['a', 'a', 'a'])
Exemple #58
0
 def test_seplist(self):
   X, Y = Var('X'), Var('Y')
   eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), '2,2,2'), Y)), ['2','2','2'])
   eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), '2,3,4'), Y)), ['2'])
   eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), '2'), Y)), ['2'])
   eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), ''), Y)), [])
Exemple #59
0
 def test_dummy_seplist(self):
   _, Y = DummyVar('_'), Var('Y')
   eq_(eval(begin(parse_text(seplist(char(_), char(','), _, Y), '2,2,2'), Y)), ['2','2','2'])
   eq_(eval(begin(parse_text(seplist(char(_), char(','), _, Y), '2,3,4'), Y)), ['2', '3', '4'])
   eq_(eval(begin(parse_text(seplist(char(_), char(','), _, Y), '2'), Y)), ['2'])
Exemple #60
0
 def testRecursiveReturnValue2(self):
   E, F, e, e1 = Var('E'), Var('F'), Var('e'), Var('e1')
   ruleList = [(E,function((((e, e),), F(e)))),
               (F,function(((1,), char('1'))))]
   eq_(eval(letr(ruleList, parse_text(E(e),  '1'), e)), (1, 1))