def __init__(self, name, grammar): self.__form_name__ = name if grammar is None: self.__form_grammar__ = None else: #print grammar self.__form_grammar__ = to_sexpression(preparse(or_p(grammar, syntax_error))) self.__operator_data__ = []
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), '')))
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), '')))
def test_Or(self): eq_( compile(or_p(fail, succeed)), ''' def or_p(call1, call2): for x in call1: yield x for x in call2: yield x def fail(): if 0: yield True def succeed(): yield True for x in or_p(fail, succeed): yield x ''')
def __or__(self, other): from dao.builtins.control import or_p return or_p(self, other)
def test_some3(self): eq_(eval(parse_text(some(or_p(char('1'),char('2'))), '2')), True)
def test_any2(self): eq_(eval(parse_text(any(or_p(char('1'), char('2'))), '2')), True)
def test_Or(self): eq_(eval(or_p(fail, succeed)), True)
def test_some3(self): eq_(eval(parse_text(some(or_p(char('1'), char('2'))), '2')), True)
def test_Or(self): eq_(compile(or_p(fail, succeed)), '''for x in or_p(fail, succeed): yield x''')
def ___parse___(self, parser): return or_p(parser.parse(self.call1), parser.parse(self.call2))
([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(_))))), define(maybe_spaces, function( ([], or_p(if_p(and_p(not_lead_chars('([])'), not_follow_chars('([])'), not_p(eoi)), spaces(_)), spaces0(_) ) ) ) ), define(sexpression, function( # dynamic grammar arises! ([result], and_p(char('{'), from_sexp(sexpression)(exp2), char('}'), setvalue(result, eval_(pycall(cons2tuple, exp2))))), ([exp], from_sexp(atom)(exp)), ([exp], from_sexp(bracket_expression)(exp)), ([exp], from_sexp(punct_expression)(exp))), ), )