Ejemplo n.º 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)), [])
Ejemplo n.º 2
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)))
  )
Ejemplo n.º 3
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')
Ejemplo n.º 4
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)    
Ejemplo n.º 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)
Ejemplo n.º 6
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)))
    )
Ejemplo n.º 7
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)), [])
Ejemplo n.º 8
0
 def test_any2(self):
   eq_(eval(parse_text(any(or_p(char('1'), char('2'))), '2')), True)
Ejemplo n.º 9
0
 def test_any(self):
   X, Y = Var('X'), Var('Y')
   eq_(eval(begin(parse_text(any(char(X), X, Y), '222'), Y)), ['2','2','2'])
   eq_(eval(begin(parse_text(any(char(X), X, Y), '234'), Y)), ['2'])
   eq_(eval(begin(parse_text(any(char(X), X, Y), ''), Y)), [])
Ejemplo n.º 10
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')
Ejemplo n.º 11
0
 def test_any2(self):
     eq_(eval(parse_text(any(or_p(char('1'), char('2'))), '2')), True)
Ejemplo n.º 12
0
 def test_any(self):
     X, Y = Var('X'), Var('Y')
     eq_(eval(begin(parse_text(any(char(X), X, Y), '222'), Y)),
         ['2', '2', '2'])
     eq_(eval(begin(parse_text(any(char(X), X, Y), '234'), Y)), ['2'])
     eq_(eval(begin(parse_text(any(char(X), X, Y), ''), Y)), [])
Ejemplo n.º 13
0
@builtin.function('make_iff')
def make_iff(test, clause, clauses, els_clause):
    els_clause = preparse(els_clause) if not isinstance(els_clause,
                                                        Var) else None
    test = preparse(test[0])
    clause = preparse(clause)
    clauses1 = [(preparse(t), preparse(c)) for t, c in clauses]
    return special.iff([(test, clause)] + clauses1, els_clause)


_then, _elsif, _els = words('then, elsif, els')
_test, _test2, _body = dummies('_test, _test2, _body')

# iff(1).then[2], iff(1).then[2]  .elsif(3).then[4] .els[5]
iff = element('iff', (call(vv.test) + _do + getitem(vv.clause) + any(
    _elsif + call(_test) + _do + getitem(_body) + is_(_test2, first(_test)),
    (_test2, _body), vv.clauses) + may(_els + getitem(vv.els_clause)) + eoi +
                      make_iff(vv.test, vv.clause, vv.clauses, vv.els_clause)))


class CASE_ELS:
    pass


CASE_ELS = CASE_ELS()


@builtin.function('make_case')
def make_case(test, cases):
    case_dict = {}
    for case, clause in cases:
Ejemplo n.º 14
0
@builtin.function('make_iff')
def make_iff(test, clause, clauses, els_clause):
  els_clause = preparse(els_clause) if not isinstance(els_clause, Var) else None
  test = preparse(test[0])
  clause = preparse(clause)
  clauses1 =  [(preparse(t), preparse(c)) for t, c in clauses]
  return special.iff([(test, clause)]+clauses1, els_clause)

_then, _elsif, _els = words('then, elsif, els')
_test, _test2, _body =  dummies('_test, _test2, _body')

# iff(1).then[2], iff(1).then[2]  .elsif(3).then[4] .els[5]
iff = element('iff',
              (call(vv.test)+_do+getitem(vv.clause)
              +any(_elsif+call(_test)+_do+getitem(_body)+is_(_test2, first(_test)), 
                   (_test2, _body), vv.clauses)
              +may(_els+getitem(vv.els_clause))+eoi
              +make_iff(vv.test, vv.clause, vv.clauses, vv.els_clause))
              )
              

class CASE_ELS: pass
CASE_ELS = CASE_ELS()

@builtin.function('make_case')
def make_case(test, cases): 
  case_dict = {}
  for case, clause in cases:
    case = preparse(case)
    if case is CASE_ELS: 
      els_clause = preparse(clause) if clause is not None else None