示例#1
0
    def test_infix(self):
        actions = []

        def infix_action(parser, tokens):
            actions.append(tokens)
            return tokens
        var = Word()

        def var_action(parser, tokens):
            actions.extend(tokens)
            return tokens[0]
        var.set_action(var_action)
        hede = Infix(var, Literal("+"))
        hede.set_action(infix_action)
        hede.parse_string({}, 'a + b')
        self.assertEquals(actions, ['a', 'b', ['a', '+', 'b']])
示例#2
0
文件: grasp.py 项目: oguzalb/grasp
functionmethodcall = (trailerwithcall | trailerwithoutcall | funccall)
# TODO should be fixed using postfix or sth
trailer = atom + Postfix(Optional(accessor) + Optional(
    functionmethodcall | getitem
))


def trailerwithcall_process(children, parser):
    index = parser.add_atom("str", children[0][1][0])
    parser.add_instruction("pushconst %s" % index)
    parser.add_instruction("getmethod")
    return [child.process(child, parser) for child in children[0]]

trailerwithcall.process = trailerwithcall_process
trailerwithoutcall.set_action(accessor_action)
divexpr = Infix(trailer, Literal('/'))
divexpr.process = infix_process("div")
mulexpr = Infix(divexpr, Literal('*'))
mulexpr.process = infix_process("mul")
modexpr = Infix(mulexpr, Literal('%'))
modexpr.process = infix_process("mod")
subexpr = Infix(modexpr, Literal('-'))
subexpr.process = infix_process("sub")
addexpr = Infix(subexpr, Literal('+'))
addexpr.process = infix_process("add")
equalsexpr = Infix(addexpr, Literal("=="))
equalsexpr.process = infix_process("equals")
inexpr = Infix(equalsexpr, Literal("in"))
inexpr.process = infix_process("contains", reverse=True)
orexpr = Infix(inexpr, Literal('or'))
orexpr.process = infix_process("or")