Exemplo n.º 1
0
 def test_block(self):
     g = Grammar("""function abc() {;}""")
     result,error = g.apply("sourceElements")
     assert result == [g.ast("func","abc",[],[g.ast("empty")])]
     
     g = Grammar("""function abc() {; }""")
     result,error = g.apply("sourceElements")
     assert result == [g.ast("func","abc",[],[g.ast("empty")])]
     
     g = Grammar(""" {;}""")
     result,error = g.apply("sourceElements")
     assert result == [g.ast("block",[g.ast("empty")])]
     
     g = Grammar(""" {; }""")
     result,error = g.apply("sourceElements")
     assert result == [g.ast("block",[g.ast("empty")])]
Exemplo n.º 2
0
    def test_simple_statements(self):
        g = Grammar("""delete a; delete b
        delete c""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","delete", 'a'),g.ast("simple","delete", 'b'),g.ast("simple","delete", 'c')]

        g = Grammar("""return a; return b
        return c""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","return", 'a'),g.ast("simple","return", 'b'),g.ast("simple","return", 'c')]

        g = Grammar("""throw a; throw b
        throw c""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","throw", 'a'),g.ast("simple","throw", 'b'),g.ast("simple","throw", 'c')]
Exemplo n.º 3
0
    def test_keyword_statements(self):
        g = Grammar("""debugger; debugger
        debugger""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","debugger", []),g.ast("simple","debugger", []),g.ast("simple","debugger", [])]

        g = Grammar("""continue; continue
        continue""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","continue", []),g.ast("simple","continue", []),g.ast("simple","continue", [])]

        g = Grammar("""break; break
        break""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("simple","break", []),g.ast("simple","break", []),g.ast("simple","break", [])]
Exemplo n.º 4
0
 def test_Statement(self):
     g = Grammar(""";""")
     result,error = g.apply("sourceElement")
     assert result == g.ast("empty")
Exemplo n.º 5
0
    def test_var(self):
        g = Grammar("""a""")
        result,error = g.apply("varDeclarations")
        assert result == [g.ast("a",[])]
        
        g = Grammar("""a,b,c,
        d""")
        result,error = g.apply("varDeclarations")
        assert result == [g.ast("a",[]),g.ast("b",[]),g.ast("c",[]),g.ast("d",[])]
        
        g = Grammar("""a = b""")
        result,error = g.apply("varDeclarations")
        assert result == [g.ast("a",["b"])]
        
        g = Grammar("""var abc = def
        var b = c; var d = e""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("var",[g.ast("abc",['def'])]),g.ast("var",[g.ast("b",['c'])]),g.ast("var",[g.ast("d",['e'])])]
        
        g = Grammar("""var abc
        var b; var d""")
        result,error = g.apply("sourceElements")
        assert result == [ 
            g.ast("var",[g.ast("abc",[])]), 
            g.ast("var",[g.ast("b",[])]), 
            g.ast("var",[g.ast("d",[])]) 
            ]

        g = Grammar("""var abc, def
        var a,b,c; var d,e""")
        result,error = g.apply("sourceElements")
        assert result == [
            g.ast("var",[
                g.ast("abc",[]),g.ast("def",[])
            ]),
            g.ast("var",[
                g.ast("a",[]),g.ast("b",[]),g.ast("c",[])
            ]),
            g.ast("var",[
                g.ast("d",[]),g.ast("e",[])
            ])
            ]
        
        g = Grammar("""var a,
        b = 5; var d/* = undefined */,e
        var
        x = 10""")
        result,error = g.apply("sourceElements")
        expect = [
            g.ast("var",[ g.ast("a",[]), g.ast("b",["5"]) ]),
            g.ast("var",[ g.ast("d",[],postfix=g.ast("mlcomment"," = undefined ")), g.ast("e",[]) ]),
            g.ast("var",[ g.ast("x",['10']) ])
        ]
Exemplo n.º 6
0
    def test_function(self):
        g = Grammar("""function abc(){}""")
        result,error = g.apply("sourceElement")
        assert result == g.ast("func","abc",[],[])
        
        g = Grammar("""function abc(){}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("func","abc",[],[])]
        
        g = Grammar("""/* lead comment */ function abc(){}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("mlcomment"," lead comment "),g.ast("func","abc",[],[])]
        
        g = Grammar("""function abc(/* in args */){}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("func","abc",[g.ast("mlcomment"," in args ")],[])]
        
        g = Grammar("""function abc(// in args
def,ghi){}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("func","abc",[g.ast("comment"," in args"),"def,ghi"],[])]
        
        g = Grammar("""function abc(){;;}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("func","abc",[],[g.ast("empty"),g.ast("empty")])]
        
        g = Grammar("""function abc(){/* in block */;;}""")
        result,error = g.apply("sourceElements")
        assert result == [g.ast("func","abc",[],[g.ast("mlcomment"," in block "),g.ast("empty"),g.ast("empty")])]