示例#1
0
def test_parse_call():
    p = parse("f(x)")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Call(Variable(Token("IDENTIFIER", "f")), [Variable(Token("IDENTIFIER", "x"))], False),
    )
    p = parse("module.f(x)")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Call(
            Variable(Token("IDENTIFIER", "module.f")), [Variable(Token("IDENTIFIER", "x"))], False
        ),
    )
    p = parse("{x + y}")
    p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Call(
            Variable(Token("IDENTIFIER", "I")),
            [
                Binary(
                    Variable(Token("IDENTIFIER", "x")),
                    Token("PLUS", "+"),
                    Variable(Token("IDENTIFIER", "y")),
                )
            ],
            False,
        ),
    )
示例#2
0
def test_parse_intercept_disabled():
    p = parse("x + z", False)
    assert p == Binary(Variable(Token("IDENTIFIER", "x")), Token("PLUS", "+"),
                       Variable(Token("IDENTIFIER", "z")))

    p = parse("x ~ z", False)
    assert p == Binary(Variable(Token("IDENTIFIER", "x")), Token("TILDE", "~"),
                       Variable(Token("IDENTIFIER", "z")))
示例#3
0
def test_parse_literal():
    p = parse("'A'")
    assert p == Binary(Literal(1), Token("PLUS", "+"), Literal("A"))

    p = parse("1")
    assert p == Binary(Literal(1), Token("PLUS", "+"), Literal(1))

    p = parse("1.132")
    assert p == Binary(Literal(1), Token("PLUS", "+"), Literal(1.132))
示例#4
0
def test_parse_grouping():
    p = parse("(x + z)")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Grouping(
            Binary(
                Variable(Token("IDENTIFIER", "x")),
                Token("PLUS", "+"),
                Variable(Token("IDENTIFIER", "z")),
            )),
    )
示例#5
0
def test_parse_binary():
    p = parse("x + y")
    assert p == Binary(
        Binary(Literal(1), Token("PLUS", "+"), Variable(Token("IDENTIFIER", "x"))),
        Token("PLUS", "+"),
        Variable(Token("IDENTIFIER", "y")),
    )

    p = parse("x - y")
    assert p == Binary(
        Binary(Literal(1), Token("PLUS", "+"), Variable(Token("IDENTIFIER", "x"))),
        Token("MINUS", "-"),
        Variable(Token("IDENTIFIER", "y")),
    )

    p = parse("x * y")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Binary(
            Variable(Token("IDENTIFIER", "x")),
            Token("STAR", "*"),
            Variable(Token("IDENTIFIER", "y")),
        ),
    )

    p = parse("x / y")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Binary(
            Variable(Token("IDENTIFIER", "x")),
            Token("SLASH", "/"),
            Variable(Token("IDENTIFIER", "y")),
        ),
    )

    p = parse("x : y")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Binary(
            Variable(Token("IDENTIFIER", "x")),
            Token("COLON", ":"),
            Variable(Token("IDENTIFIER", "y")),
        ),
    )

    p = parse("x ** 2")
    assert p == Binary(
        Literal(1),
        Token("PLUS", "+"),
        Binary(Variable(Token("IDENTIFIER", "x")), Token("STAR_STAR", "**"), Literal(2)),
    )

    p = parse("x | y")
    assert p == Binary(
        Binary(Literal(1), Token("PLUS", "+"), Variable(Token("IDENTIFIER", "x"))),
        Token("PIPE", "|"),
        Variable(Token("IDENTIFIER", "y")),
    )

    p = parse("x ~ y")
    assert p == Binary(
        Variable(Token("IDENTIFIER", "x")),
        Token("TILDE", "~"),
        Binary(Literal(1), Token("PLUS", "+"), Variable(Token("IDENTIFIER", "y"))),
    )
示例#6
0
def test_parse_variable():
    p = parse("x")
    assert p == Binary(Literal(1), Token("PLUS", "+"), Variable(Token("IDENTIFIER", "x")))
示例#7
0
def test_parse_quoted_name():
    p = parse("`$$##!!`")
    assert p == Binary(Literal(1), Token("PLUS", "+"), QuotedName(Token("BQNAME", "`$$##!!`")))
示例#8
0
def test_parse_complex_expressions():
    p = parse("x*y + u:v")
    ast = Binary(
        Binary(
            Literal(1),
            Token("PLUS", "+"),
            Binary(
                Variable(Token("IDENTIFIER", "x")),
                Token("STAR", "*"),
                Variable(Token("IDENTIFIER", "y")),
            ),
        ),
        Token("PLUS", "+"),
        Binary(
            Variable(Token("IDENTIFIER", "u")),
            Token("COLON", ":"),
            Variable(Token("IDENTIFIER", "v")),
        ),
    )
    assert p == ast

    p = parse("y ~ x*z + (x|g)")
    ast = Binary(
        Variable(Token("IDENTIFIER", "y")),
        Token("TILDE", "~"),
        Binary(
            Binary(
                Literal(1),
                Token("PLUS", "+"),
                Binary(
                    Variable(Token("IDENTIFIER", "x")),
                    Token("STAR", "*"),
                    Variable(Token("IDENTIFIER", "z")),
                ),
            ),
            Token("PLUS", "+"),
            Grouping(
                Binary(
                    Variable(Token("IDENTIFIER", "x")),
                    Token("PIPE", "|"),
                    Variable(Token("IDENTIFIER", "g")),
                )
            ),
        ),
    )

    assert p == ast

    p = parse("np.log(y) ~ (`var 1` + `var 2` + `var 3`) ** 3 - `var 2`:`var 3`")
    ast = Binary(
        Call(Variable(Token("IDENTIFIER", "np.log")), [Variable(Token("IDENTIFIER", "y"))], False),
        Token("TILDE", "~"),
        Binary(
            Binary(
                Literal(1),
                Token("PLUS", "+"),
                Binary(
                    Grouping(
                        Binary(
                            Binary(
                                QuotedName(Token("BQNAME", "`var 1`")),
                                Token("PLUS", "+"),
                                QuotedName(Token("BQNAME", "`var 2`")),
                            ),
                            Token("PLUS", "+"),
                            QuotedName(Token("BQNAME", "`var 3`")),
                        )
                    ),
                    Token("STAR_STAR", "**"),
                    Literal(3),
                ),
            ),
            Token("MINUS", "-"),
            Binary(
                QuotedName(Token("BQNAME", "`var 2`")),
                Token("COLON", ":"),
                QuotedName(Token("BQNAME", "`var 3`")),
            ),
        ),
    )

    assert p == ast