def test__elseif_block__basic():
    data = "else if a:\n    a = b"
    expected = {
        "node":
        "else if",
        "condition": {
            "name": "a",
            "node": "variable"
        },
        "body": [{
            "from": {
                "name": "b",
                "node": "variable"
            },
            "node": "assign",
            "to": {
                "name": "a",
                "node": "variable"
            },
        }],
    }

    data = pre.insert_brackets(data)
    grammar = parser.read_grammar()
    output = parser.parse(grammar, data, entry="elseif_block")
    assert output == expected
Exemplo n.º 2
0
def parse(grammar, source, entry="source"):
    source = pre.insert_brackets(source)

    tree = grammar[entry].parse(source)
    sv = SourceVisitor()
    output = sv.visit(tree)
    return output
def test__if__with_elseif():
    data = "\n".join([
        "if a:", "    d = 1", "else if b:", "    d = 2", "else:", "    d = 4"
    ])
    expected = {
        "node":
        "conditional",
        "if": {
            "condition": {
                "name": "a",
                "node": "variable"
            },
            "body": [{
                "from": {
                    "node": "literal",
                    "value": 1
                },
                "node": "assign",
                "to": {
                    "name": "d",
                    "node": "variable"
                },
            }],
        },
        "else if": [{
            "body": [{
                "from": {
                    "node": "literal",
                    "value": 2
                },
                "node": "assign",
                "to": {
                    "name": "d",
                    "node": "variable"
                },
            }],
            "condition": {
                "name": "b",
                "node": "variable"
            },
        }],
        "else": {
            "body": [{
                "from": {
                    "node": "literal",
                    "value": 4
                },
                "node": "assign",
                "to": {
                    "name": "d",
                    "node": "variable"
                },
            }]
        },
    }

    data = pre.insert_brackets(data)
    grammar = parser.read_grammar()
    output = parser.parse(grammar, data, entry="if")
    assert output == expected