def push(a): global stack print "pushing", a.value stack.append(a.value) def pushop(op): def impl(a): global stack stack.append(op) return impl def pop(): tmp = stack.pop() expr = parser('expr') assignment = parser('assignment') term = parser('term') factor = parser('factor') number = parser('number') float_p = parser('float_p') stmt = parser('stmt') program = parser('program') program <<= stmt.plus >> end_p stmt <<= ((assignment | expr) >> tok_p('NEWLINE')) | tok_p('NEWLINE') assignment <<= (tok_p('IDENTIFIER') >> tok_p('ASSIGN') >> expr)[pushval(0), pushop('s')]
('x', ch_p('x').star, 'x', ('x',)), ('xaaaa', ch_p('x') >> ch_p('a').star, 'xaaaa', ('x', 'a', 'a', 'a', 'a')), ('', ch_p('x').star, '', ([''])), ('', ch_p('x').plus, False, None), ('x', ch_p('x').plus, 'x', ('x',)), ('xxxa', ch_p('x').plus, 'xxx', ('x','x','x')) ] a = parser() b = parser() a <<= ch_p('a') >> b b <<= (ch_p('b') >> a) | end_p tests += [('ababa', a, 'ababa', ('a', 'b', 'a', 'b', 'a'))] for t in tests: test_parse(*t)