示例#1
0
def test_next_previous(mio):
    m = Message("foo")
    m.next = Message("bar")

    assert m.previous is m
    assert m.name == "foo"
    assert m.next.name == "bar"
    assert m.next.next is None
    assert m.next.previous.name == "foo"
示例#2
0
文件: parser.py 项目: prologic/mio
def make_chain(messages, all=True):
    root = node = None

    while messages:
        if len(messages) > 1 and is_assignment(messages[1]):
            name = messages.pop(0).name
            object = runtime.find("String").clone(name)
            key = Message(name, object)

            op = messages.pop(0)

            if op.name == "=" and op.next is not None and op.next.name in ("()", "[]", "{}",):
                value = Message(
                    "()", args=[Message(op.next.name, args=op.next.args)])
            elif op.args:
                value = Message("()", args=op.args)
            else:
                value = make_chain(messages, all=False)

            message = Message("set", args=[key, value])
        elif is_operator(messages[0]):
            message = messages.pop(0)
            if messages and not message.args:
                if operators.get(message.name) == 1:
                    arg = messages.pop(0)
                    # Set the argument (a Message) previous attribute to the
                    # current message
                    arg.previous = message
                    message.args.append(arg)
                    message.call = True
                else:
                    chain = make_chain(messages, all=False)
                    if chain is not None:
                        # Set the argument (a Message) previous attribute to
                        # the current message
                        chain.previous = message
                        message.args.append(chain)
                        message.call = True
            elif message.next is not None:
                chain = message.next
                message.next = None
                chain.previous = message
                message.args.append(chain)
                message.call = True
        elif messages[0].terminator and not all:
            break
        else:
            message = messages.pop(0)

        if root is None:
            root = node = message
        else:
            node.next = node = message

    return root
示例#3
0
文件: parser.py 项目: prologic/mio
def make_message(n):
    if len(n) == 2:
        if n[1] is not None and getattr(n[1][0], "type") == "op":
            if n[1][0].value == "(":
                name = n[0]  # Message arguments i.e: foo(1, 2, 3)
            elif n[1][0].value == "[":
                name = (n[0], "[]")
            elif n[1][0].value == "{":
                name = (n[0], "{}")
            args = n[1][1]
        else:
            name, args = n
    else:
        if n[0].value == "(":
            name = "()"
        elif n[0].value == "[":
            name = "[]"
        elif n[0].value == "{":
            name = "{}"
        args = n[1]

    if isinstance(name, tuple):
        name, next = name
    else:
        next = None

    if hasattr(name, "value"):
        value = name
        name = name.value
    else:
        value = None

    if next is not None:
        message = Message(name, value)
        message.next = Message(next, None, args)
    else:
        message = Message(name, value, args)

    return message