Beispiel #1
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))
Beispiel #2
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'))
Beispiel #3
0
def fun_macro_grammar(klass1, klass2):
  return (
  # fun. a(x)[...],  fun. a(x) <= at[...][...]
    (getattr(vv.name)+
     any(~call(__.args) +assign(__.args, getvalue_default(__.args, ()))
          + some(getitem_to_list(__.body), __.body, __.bodies), 
          (__.args, __.bodies), vv.rules)
        +eoi+make_fun1(vv.name, vv.rules, klass2))
  # fun. a(x) >= [...],  fun. a(x) <= at[...][...]
  | (getattr(vv.name)+call(vv.args)+ge(vv.body)+eoi
     +make_fun2(vv.name,vv.args, vv.body, klass2))
  # fun. a(x) <= [...],  fun. a(x) <= at[...][...]
  | (getattr(vv.name)+call(vv.args)+le(vv.body)+eoi
     +make_fun3(vv.name,vv.args, vv.body, klass2))
  #  fun. a== at(..)[...]
  | (getattr(vv.name)+eq(vv.rules)+eoi
     +make_fun4(vv.name, vv.rules, klass1))
  #  fun. a>= at(..)[...]
  | (getattr(vv.name)+ge(vv.rules)+eoi+make_fun5(vv.name,vv.rules, klass2))
  #  fun. a<= at(..)[...]
  | (getattr(vv.name)+le(vv.rules)+eoi+make_fun6(vv.name,vv.rules, klass2))
  #  fun(args) [...](args)[...][...]
  | (some(may(call(__.args)) +assign(__.args, getvalue_default(__.args, ()))
          + some(getitem_to_list(__.body), __.body, __.bodies), 
          (__.args, __.bodies), vv.rules)
        +eoi+make_fun7(vv.rules, klass1))
  #   - fun.a/3,
  | (getattr(vv.name)+neg+div(vv.arity)+eoi
     +pycall(abolish, getvar(vv.name), vv.arity))
  #- fun.a(x),
  | (getattr(vv.name)+call(vv.args)+neg+eoi
     +make_fun8(vv.name, vv.args, klass2)) #retractall
  #- fun.a(x)[1],
##  | (getattr(vv.name)+call(vv.args)+getitem(vv.index)+neg+assign(result, retract(vv.name, vv.args)))
  )
Beispiel #4
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))
Beispiel #5
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)    
Beispiel #6
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)
Beispiel #7
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')
Beispiel #8
0
def fun_macro_grammar(klass1, klass2):
    return (
        # fun. a(x)[...],  fun. a(x) <= at[...][...]
        (getattr(vv.name) + any(
            ~call(__.args) + assign(__.args, getvalue_default(__.args, ())) +
            some(getitem_to_list(__.body), __.body, __.bodies),
            (__.args, __.bodies), vv.rules) + eoi +
         make_fun1(vv.name, vv.rules, klass2))
        # fun. a(x) >= [...],  fun. a(x) <= at[...][...]
        | (getattr(vv.name) + call(vv.args) + ge(vv.body) + eoi +
           make_fun2(vv.name, vv.args, vv.body, klass2))
        # fun. a(x) <= [...],  fun. a(x) <= at[...][...]
        | (getattr(vv.name) + call(vv.args) + le(vv.body) + eoi +
           make_fun3(vv.name, vv.args, vv.body, klass2))
        #  fun. a== at(..)[...]
        | (getattr(vv.name) + eq(vv.rules) + eoi +
           make_fun4(vv.name, vv.rules, klass1))
        #  fun. a>= at(..)[...]
        | (getattr(vv.name) + ge(vv.rules) + eoi +
           make_fun5(vv.name, vv.rules, klass2))
        #  fun. a<= at(..)[...]
        | (getattr(vv.name) + le(vv.rules) + eoi +
           make_fun6(vv.name, vv.rules, klass2))
        #  fun(args) [...](args)[...][...]
        | (some(
            may(call(__.args)) + assign(__.args, getvalue_default(__.args,
                                                                  ())) +
            some(getitem_to_list(__.body), __.body, __.bodies),
            (__.args, __.bodies), vv.rules) + eoi +
           make_fun7(vv.rules, klass1))
        #   - fun.a/3,
        | (getattr(vv.name) + neg + div(vv.arity) + eoi +
           pycall(abolish, getvar(vv.name), vv.arity))
        #- fun.a(x),
        | (getattr(vv.name) + call(vv.args) + neg + eoi +
           make_fun8(vv.name, vv.args, klass2))  #retractall
        #- fun.a(x)[1],
        ##  | (getattr(vv.name)+call(vv.args)+getitem(vv.index)+neg+assign(result, retract(vv.name, vv.args)))
    )
Beispiel #9
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))
Beispiel #10
0
 def test_some3(self):
   eq_(eval(parse_text(some(or_p(char('1'),char('2'))), '2')), True)
Beispiel #11
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'))
Beispiel #12
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))
Beispiel #13
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')
Beispiel #14
0
 def test_some3(self):
     eq_(eval(parse_text(some(or_p(char('1'), char('2'))), '2')), True)
Beispiel #15
0
        else:
            clause = preparse(clause)
            for x in case:
                case_dict[x] = clause
    return special.CaseForm(preparse(test[0]), case_dict, els_clause)


of_fun = attr_call('of')

# case(x).of(1)[write(1)].of(2,3)[write(4)].els[write(5)]
case = element(
    'case',
    call(vv.test) + (
        #.of(1)[write(1)].of(2,3)[write(4)].els[write(5)]
        (some(
            of_fun(__.values) + getitem_to_list(__.clause),
            (__.values, __.clause), vv.clauses) +
         may(_els + getitem_to_list(vv.els)) + eoi + make_case(
             vv.test,
             list_tail(vv.clauses, pytuple(CASE_ELS, getvalue_default(
                 vv.els)))))
        #/{1:[write(1)],2:[write(4)],3:[write(4)], els:[write(5)]}
        ##  | div(vv.clauses)+eoi+make_case(vv.test, items(vv.clauses))
    ))
els = CASE_ELS


@builtin.function('make_loop')
def make_loop(body):
    return special.LoopForm(preparse(body))
Beispiel #16
0
  for case, clause in cases:
    case = preparse(case)
    if case is CASE_ELS: 
      els_clause = preparse(clause) if clause is not None else None
    else:
      clause = preparse(clause)
      for x in case: case_dict[x] = clause
  return special.CaseForm(preparse(test[0]), case_dict, els_clause)

of_fun = attr_call('of')

# case(x).of(1)[write(1)].of(2,3)[write(4)].els[write(5)]
case = element('case',
  call(vv.test)+(
  #.of(1)[write(1)].of(2,3)[write(4)].els[write(5)]
    (some(of_fun(__.values)+getitem_to_list(__.clause),(__.values,__.clause), vv.clauses)
     +may(_els+getitem_to_list(vv.els))+eoi
    +make_case(vv.test, list_tail(vv.clauses, pytuple(CASE_ELS, getvalue_default(vv.els)))))
  #/{1:[write(1)],2:[write(4)],3:[write(4)], els:[write(5)]}
##  | div(vv.clauses)+eoi+make_case(vv.test, items(vv.clauses))
  ))
els = CASE_ELS

@builtin.function('make_loop')
def make_loop(body): 
  return special.LoopForm(preparse(body))

@builtin.function('make_loop_times')
def make_loop_times(body, times):
  return special.LoopTimesForm(preparse(times[0]), preparse(body))