Exemple #1
0
def test_optimizer_visit_entity_node_does_nothing_without_macro() -> None:
    o = Optimizer({}, {})
    entity_1 = nodes.EntityNode(
        "some_entity", [nodes.LiteralNode("a"),
                        nodes.LiteralNode("b")])
    entity_2 = nodes.EntityNode(
        "some_entity", [nodes.LiteralNode("a"),
                        nodes.LiteralNode("b")])

    assert entity_2 == o.visit_entity_node(entity_1)
Exemple #2
0
def test_grammar_node_eq():
    g1 = nodes.Grammar({}, {})
    g2 = nodes.Grammar({"asdf": nodes.EntityNode("some_entity", [])}, {})
    assert g1 != g2

    g3 = nodes.Grammar(
        {"asdf": nodes.EntityNode("some_other", [])},
        {"some_macro": nodes.MacroNode("some_macro", [], [])},
    )
    assert g2 != g3

    g2.macros["some_macro"] = nodes.MacroNode("some_macro", [], [])

    assert g2 != g3

    g3.entities["asdf"].name = "some_entity"

    assert g2 == g3
Exemple #3
0
def test_grammar_node_generate():
    grammar = nodes.Grammar(
        {
            "some_entity":
            nodes.EntityNode(
                "some_entity",
                [nodes.LiteralNode("a"),
                 nodes.PlaceholderNode("a")])
        },
        {},
    )

    assert grammar.generate("some_entity", {"a": "sdf"}) == "a sdf"
Exemple #4
0
def test_optimizer_visit_entity_node_with_macro() -> None:
    parameters = [nodes.ParameterNode("a"), nodes.ParameterNode("b")]

    o = Optimizer(
        {},
        {"macro_a": nodes.MacroNode("macro_a", parameters, parameters[::-1])})

    entity_1 = nodes.EntityNode(
        "some_entity",
        [nodes.LiteralNode("a"),
         nodes.LiteralNode("b")],
        macro=nodes.MacroReferenceNode("macro_a"),
    )

    expected_entity = nodes.EntityNode(
        "some_entity",
        [
            nodes.ParameterNode("b", nodes.LiteralNode("b")),
            nodes.ParameterNode("a", nodes.LiteralNode("a")),
        ],
    )

    assert expected_entity == o.visit_entity_node(entity_1)
Exemple #5
0
    def entity(self) -> nodes.EntityNode:
        entity_macro = None

        self._expect(TokenType.Symbol)

        assert self.current_token is not None
        entity_name = self.current_token.value

        if self._accept(TokenType.AngleOpen):
            self._expect(TokenType.Symbol)

            assert self.current_token is not None
            entity_macro = nodes.MacroReferenceNode(self.current_token.value)
            self._expect(TokenType.AngleClose)

        entity_children = []

        while not self._accept(TokenType.ParenClose):
            entity_children.append(self.expression())

        return nodes.EntityNode(entity_name,
                                entity_children,
                                macro=entity_macro)
Exemple #6
0
                nodes.ReferenceNode("c"),
            ),
        ),
        ('(repeat 4 "hello")', nodes.RepeatNode(4, nodes.LiteralNode("hello"))),
    ],
)
def test_parser_expression(text: str, expected_expression: Expression) -> None:
    p = DescentParser(text)
    expr = p.expression()
    assert expected_expression == expr


@pytest.mark.parametrize(
    "text,expected_entity,want_err",
    [
        ("hello $a)", nodes.EntityNode("hello", [nodes.PlaceholderNode("a")]), None),
        (
            'hello $a "b")',
            nodes.EntityNode(
                "hello", [nodes.PlaceholderNode("a"), nodes.LiteralNode("b")]
            ),
            None,
        ),
        ('hello $a "b"', None, SyntaxError),
        ("hello)", nodes.EntityNode("hello", []), None),
        (
            "hello<some_macro> $a)",
            nodes.EntityNode(
                "hello",
                [nodes.PlaceholderNode("a")],
                nodes.MacroReferenceNode("some_macro"),
Exemple #7
0
)
def test_macro_reference_node_eq(node_a: nodes.Node, node_b: nodes.Node,
                                 should_eq: bool) -> None:
    assert should_eq == (node_a == node_b)


def test_macro_reference_node_generate_not_implemented():
    m = nodes.MacroReferenceNode("some_macro")
    with pytest.raises(NotImplementedError):
        m.generate()


@pytest.mark.parametrize(
    "node_a,node_b,should_eq",
    [
        (nodes.EntityNode("a", []), nodes.EntityNode("a", []), True),
        (nodes.EntityNode("a", []), nodes.EntityNode("b", []), False),
        (
            nodes.EntityNode("a", [nodes.LiteralNode("a")]),
            nodes.EntityNode("a", []),
            False,
        ),
        (
            nodes.EntityNode("a", [nodes.LiteralNode("a")]),
            nodes.EntityNode("a", [nodes.LiteralNode("b")]),
            False,
        ),
        (
            nodes.EntityNode("a", [], macro=nodes.MacroReferenceNode("a")),
            nodes.EntityNode("a", []),
            False,
Exemple #8
0
        [
            nodes.ParameterNode("b", nodes.LiteralNode("b")),
            nodes.ParameterNode("a", nodes.LiteralNode("a")),
        ],
    )

    assert expected_entity == o.visit_entity_node(entity_1)


@pytest.mark.parametrize(
    "node,entities,expected,want_err",
    [
        (
            nodes.ReferenceNode("a"),
            {
                "a": nodes.EntityNode("a", [nodes.LiteralNode("a")])
            },
            nodes.EntityNode("a", [nodes.LiteralNode("a")]),
            None,
        ),
        (
            nodes.ReferenceNode("a"),
            {
                "b": nodes.EntityNode("a", [nodes.LiteralNode("a")])
            },
            None,
            NameError,
        ),
    ],
)
def test_optimizer_visit_reference_node(