Beispiel #1
0
    def test_annotations(self):
        grammar = """
X ::= "x" {Node(child=#0)};
Y ::= "y" {[#0,#3]};
Z ::= "z" {#1+#2};
A ::= "a" {Node2(child=#0, child2=[#1,#3], child3=#3+[#4])};

%%

x:"x"
"""
        bp = BootstrapParser()
        bp.parse(grammar)
        assert bp.rules[Nonterminal("X")].annotations == [
            AstNode("Node", {"child": LookupExpr(0)})
        ]
        assert bp.rules[Nonterminal("Y")].annotations == [
            ListExpr([LookupExpr(0), LookupExpr(3)])
        ]
        assert bp.rules[Nonterminal("Z")].annotations == [
            AddExpr(LookupExpr(1), LookupExpr(2))
        ]
        assert bp.rules[Nonterminal("A")].annotations == [
            AstNode(
                "Node2", {
                    'child': LookupExpr(0),
                    'child2': ListExpr([LookupExpr(1),
                                        LookupExpr(3)]),
                    'child3': AddExpr(LookupExpr(3), ListExpr([LookupExpr(4)]))
                })
        ]
Beispiel #2
0
    def test_alternatives(self):
        grammar = """
X ::= "x" {Node(child=#0)}
    | "y" "z" {#1};

%%

x:"x"
"""
        bp = BootstrapParser()
        bp.parse(grammar)
        assert bp.rules[Nonterminal("X")].annotations == [
            AstNode("Node", {"child": LookupExpr(0)}),
            LookupExpr(1)
        ]
Beispiel #3
0
    def test_lookup_reference(self):
        grammar = """
X ::= "x" {X(name=#0.x)}
    ;

%%

x:"x"
"""
        bp = BootstrapParser()
        bp.parse(grammar)
        assert bp.rules[Nonterminal("X")].annotations == [
            AstNode("X", {"name": LookupExpr(0, "x")})
        ]
Beispiel #4
0
    def test_annotations_extension(self):
        grammar = """
X ::= "x" {foreach(#2) X(name=item.x)}
    ;

%%

x:"x"
"""
        bp = BootstrapParser()
        bp.parse(grammar)
        assert bp.rules[Nonterminal("X")].annotations == [
            Foreach("foreach", LookupExpr(2),
                    AstNode("X", {"name": ReferenceExpr("item", "x")}))
        ]