Example #1
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'))
Example #2
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), '')))
Example #3
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))
Example #4
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), '')))
Example #5
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))
Example #6
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))
Example #7
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))
Example #8
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))
Example #9
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(), '')))
Example #10
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')))
Example #11
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))
Example #12
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)
Example #13
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))
Example #14
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')))
Example #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))
Example #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(), '')))
Example #17
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), '')))
Example #18
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))
Example #19
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))
Example #20
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), '')))
Example #21
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))
Example #22
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'))
Example #23
0
 def testnullword2(self):
     assert_raises(NoSolutionFound, eval,
                   parse_text(and_p(nullword, eoi), 'a'))
     eq_(eval(parse_text(nullword, '')), True)
Example #24
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))
Example #25
0
 def test_nongreedy_optional(self):
   x, s = Var('x'), Var('s')
   ruleList =[(s, function( ((x,), and_p(-char('a'),char(x)))))]
   eq_(eval(let(ruleList, parse_text(s(x), 'a'), x)), 'a')
   eq_(eval(let(ruleList, parse_text(s(x),  'aa'), x)), 'a')         
   eq_(eval(let(ruleList, parse_text(s(x),  'b'), x)), 'b')
Example #26
0
def getitem_to_list(argument=None):
  if argument is not None:
    _x = DummyVar('_x')
    return and_p(getitem(_x), special.set(argument, to_list(_x)))
  else: return getitem()
Example #27
0
 def __and__(self, other):
   from dao.builtins.control import and_p
   return and_p(self, other)
Example #28
0
 def test_nongreedy_optional(self):
     x, s = Var('x'), Var('s')
     ruleList = [(s, function(((x, ), and_p(-char('a'), char(x)))))]
     eq_(eval(let(ruleList, parse_text(s(x), 'a'), x)), 'a')
     eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a')
     eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b')
Example #29
0
 def test_repeat2(self):
   return
   # the code below loops for ever.
   eq_(eval(and_p(set_text('123'), repeat, char(x), unify(x, '4'))), True) 
Example #30
0
 def testnullword2(self):
   assert_raises(NoSolutionFound, eval, parse_text(and_p(nullword, eoi), 'a')) 
   eq_(eval(parse_text(nullword, '')), True)
Example #31
0
 def test_repeat2(self):
     return
     # the code below loops for ever.
     eq_(eval(and_p(set_text('123'), repeat, char(x), unify(x, '4'))), True)
Example #32
0
def attr_call(name): return lambda *args: and_p(getattr(name), call(*args))

def getitem_to_list(argument=None):
Example #33
0
 def test_repeat(self):
     return
     # the code below loops for ever, after modifie the behaviour of solver.parse_state and terminals.
     eq_(eval(and_p(set_text('123'), repeat, char(x), unify(x, '3'))), True)
Example #34
0
def attr_item(name): return lambda arg: and_p(getattr(name),getitem(arg))

def attr_call(name): return lambda *args: and_p(getattr(name), call(*args))
Example #35
0
 def test_repeat(self):
   return
   # the code below loops for ever, after modifie the behaviour of solver.parse_state and terminals.
   eq_(eval(and_p(set_text('123'), repeat, char(x), unify(x, '3'))), True)
Example #36
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)
Example #37
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'))
Example #38
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'))
Example #39
0
_ = DummyVar('_')

def from_sexp(var):
  return from_(sexpression_module, var)

defines = in_module(sexpression_module, 
  
  define(atom, function(
    ([x], integer(x)),
    ([x], dqstring(x)),
    ([x], symbol(x))
    )),
  
  define(bracket_expression, function(
    ([exp_list], and_p(char('('), spaces0(_), from_sexp(sexpression_list)(exp_list), spaces0(_), char(')'))),
    ([exp_list], and_p(char('['), spaces0(_), from_sexp(sexpression_list)(exp_list), spaces0(_), char(']'))))),
  
  define(punct_expression, function(
    ([L(quote, exp)], and_p(char("'"), from_sexp(sexpression)(exp))),
    ([L(quasiquote, exp)], and_p(char("`"), from_sexp(sexpression)(exp))),
    ([L(unquote_splice, exp)], and_p(literal(",@"), from_sexp(sexpression)(exp))),
    ([L(unquote, exp)], and_p(char(","), from_sexp(sexpression)(exp))))),
  
  define(sexpression_list, function(
    ([Cons(exp, exp_list)], and_p(from_sexp(sexpression)(exp), from_sexp(maybe_spaces)(), from_sexp(sexpression_list)(exp_list))),
    ([nil], null))),
  
  define(sexpression1, function(
    ([exp], and_p(spaces0(_), from_sexp(sexpression_list)(exp), spaces0(_))))),