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))
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))
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)), [])
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))
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))
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))
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'])
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)), [])
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')
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)))
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'])
def test_cut2_no_Cut3_begin(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') eq_(eval(letr([(a, function([[x], begin(b(x),c(x))], [[x], d(x)])), (b, function([[1], 'b1'], [[4], 'b4'])), (c, function([[4], 'c4'])), (d, function([[3], 'd3']))], a(x), x)), 4)
def make_expression(text): code = begin( sexpression_defines, classic_defines, set_text(text), from_(classic, program)(exp), eoi, eval_(pycall(cons2tuple, exp))) return to_sexpression(code)
def make_fun6(name, rules, klass): fun = varcache(name) rules = preparse(rules) if isinstance(rules, AtForm): clauses = [(head if head is not None else (), bodies) for head, bodies in rules.clauses] return special.begin(*[insert_def(fun, head, bodies, klass) for head, bodies in clauses]) elif isinstance(rules, list): return insert_def(fun, head, [rules], klass) else: raise DinpySyntaxError()
def make_fun1(name, rules, klass): fun = varcache(name) if len(rules)==0: return replace_def(fun, (), [[]], klass) if len(rules)==1: return replace_def(fun, preparse(rules[0][0]), preparse(rules[0][1]), klass) replaces = [] for head, bodies in rules: head = preparse(head) bodies = preparse(bodies) replaces .append(replace_def(fun, head, bodies, klass)) return special.begin(*replaces)
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))
def make_fun6(name, rules, klass): fun = varcache(name) rules = preparse(rules) if isinstance(rules, AtForm): clauses = [(head if head is not None else (), bodies) for head, bodies in rules.clauses] return special.begin( * [insert_def(fun, head, bodies, klass) for head, bodies in clauses]) elif isinstance(rules, list): return insert_def(fun, head, [rules], klass) else: raise DinpySyntaxError()
def make_fun1(name, rules, klass): fun = varcache(name) if len(rules) == 0: return replace_def(fun, (), [[]], klass) if len(rules) == 1: return replace_def(fun, preparse(rules[0][0]), preparse(rules[0][1]), klass) replaces = [] for head, bodies in rules: head = preparse(head) bodies = preparse(bodies) replaces.append(replace_def(fun, head, bodies, klass)) return special.begin(*replaces)
def ___parse___(self, parser): # x << y << z << value y = parser.parse(self.y) if isinstance(self.x, _VarSymbol): return special.set(varcache(self.x.name), y) if isinstance(self.x, _lshift): vars = _get_assign_vars_chain(self.x) i = len(vars)-1 set_stmts = [special.set(vars[i], y)] while i>0: i -= 1 set_stmts.append(special.set(vars[i], vars[i+1])) return special.begin(*set_stmts) elif isinstance(self.x, tuple) or isinstance(self.x, list): vars = self.x elif isinstance(self.x, _div): vars = _get_assign_vars_list(self.x) else: raise DinpySyntaxError() return special.set_list(vars, y)
def test_next(self): eq_(eval(begin(set_text('ab'), next_char(), next_char())), 'a')
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)), [])
def test_spaces(self): x, y = Var('x'), Var('y') eq_(eval(begin(parse_text(unify_tabspaces(x), ' '), x)), " ") eq_(eval(begin(parse_text(unify_whitespaces(y), "\r\t\n"), y)), "\r\t\n")
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)))
def test_dummy_times(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text(times(char(_), 3, _, Y), '234'), Y)), ['2','3','4'])
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))
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)), [])
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'])
def test_string(self): x, y = Var('x'), Var('y') eq_(eval(begin(parse_text(dqstring(x), '"2"'), x)), "2") eq_(eval(begin(parse_text(sqstring(y), "'1'"), y)), "1")
def test_step(self): eq_(eval(begin(set_text('ab'), step(), step())), 'b') eq_(eval(begin(set_text('ab'), step(), left())), 'b')
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)), [])
def test_position(self): eq_(eval(begin(set_text('ab'), position())), 0)
def test_dummy_times(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text(times(char(_), 3, _, Y), '234'), Y)), ['2', '3', '4'])
def test_skip(self): eq_(eval(begin(set_text('abcde'), skip(3), next())), 'd') eq_(eval(begin(set_text('abcde'), skip(4), skip(-2), next())), 'c')
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))
def test_goto(self): eq_(eval(begin(set_text('abcde'), goto(1))), 'bcde')
def test_times(self): X, Y = Var('X'), Var('Y') eq_(eval(begin(parse_text(times(char(X), 3, X, Y), '222'), Y)), ['2','2','2']) assert_raises(NoSolutionFound, eval, parse_text(times(char(X), 3, X, Y), '2234'))
def test_string(self): x ,y = Var('x'), Var('y') eq_(eval(begin(parse_text(dqstring(x), '"2"'), x)), "2") eq_(eval(begin(parse_text(sqstring(y), "'1'"), y)), "1")
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))
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'])
def test_uLetterdigit(self): x, y = Var('x'), Var('y') eq_(eval(begin(parse_text(uLetterdigitString(x), '_ad123'), x)), "_ad123") eq_(eval(begin(parse_text(uLetterdigitString(y), "ad1sff23"), y)), "ad1sff23")
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))
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'])
def test_skip(self): eq_(eval(begin(set_text('abcde'), goto(2), next_char())), 'c')
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'])
def test_left(self): eq_(eval(begin(set_text('ab'), left())), 'ab')
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)), [])
def test_subtext(self): eq_(eval(begin(set_text('abcde'), subtext(0,3))), 'abc')
def test_times(self): X, Y = Var('X'), Var('Y') eq_(eval(begin(parse_text(times(char(X), 3, X, Y), '222'), Y)), ['2', '2', '2']) assert_raises(NoSolutionFound, eval, parse_text(times(char(X), 3, X, Y), '2234'))
def test_number(self): x, y, z = Var('y'), Var('x'), Var('z') eq_(eval(begin(parse_text(number(x), '2'), x)), 2) eq_(eval(begin(parse_text(number(y), '234'), y)), 234) eq_(eval(begin(parse_text(number(z), '0232'), z)), 154) #0ctal
def test_spaces(self): x ,y = Var('x'), Var('y') eq_(eval(begin(parse_text(spaces(x), ' '), x)), " ") eq_(eval(begin(parse_text(spaces(y), "\r\t\n"), y)), "\r\t\n")
def test_subtext(self): eq_(eval(begin(set_text('abcde'), subtext(0, 3))), 'abc')