Exemple #1
0
        public void TestMessageStringificationIsConsistent()
        {
            string action = "$other.tree = null;";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar a;\n" +
                "options { output = AST;}" +
                "otherrule\n" +
                "    : 'y' ;" +
                "rule\n" +
                "    : other=otherrule {" + action + "}\n" +
                "    ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                        "rule",
                                                                        new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            object expectedArg = "other";
            object expectedArg2 = "tree";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            string expectedMessageString = expectedMessage.ToString();
            Assert.AreEqual( expectedMessageString, expectedMessage.ToString() );
        }
Exemple #2
0
        public void TestMessageStringificationIsConsistent() /*throws Exception*/
        {
            string     action = "$other.tree = null;";
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar a;\n" +
                "options { output = AST;}" +
                "otherrule\n" +
                "    : 'y' ;" +
                "rule\n" +
                "    : other=otherrule {" + action + "}\n" +
                "    ;");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator(generator,
                                                               "rule",
                                                               new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();

            int    expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            object expectedArg   = "other";
            object expectedArg2  = "tree";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
            string expectedMessageString = expectedMessage.ToString();

            Assert.AreEqual(expectedMessageString, expectedMessage.ToString());
        }
Exemple #3
0
        public void TestSetAttrOfExprInMembers() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "@members {\n" +
                "%code.instr = o;" + // must not get null ptr!
                "}\n" +
                "a : ID\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            assertNoErrors(equeue);
        }
Exemple #4
0
        public void TestCannotHaveSpaceAfterDot() /*throws Exception*/
        {
            string action = "%x. y = z;";
            //String expecting = null;

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            int    expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
            object expectedArg   = "%x.";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);

            checkError(equeue, expectedMessage);
        }
Exemple #5
0
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
                );
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
        public void TestNoWildcardAsRootError() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            string treeGrammar =
                "tree grammar TP;\n" +
                "options {output=AST;}\n" +
                "a : ^(. INT) \n" +
                "  ;\n";

            Grammar   g     = new Grammar(treeGrammar);
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);

            int    expectedMsgID                 = ErrorManager.MSG_WILDCARD_AS_ROOT;
            object expectedArg                   = null;
            RecognitionException expectedExc     = null;
            GrammarSyntaxMessage expectedMessage =
                new GrammarSyntaxMessage(expectedMsgID, g, null, expectedArg, expectedExc);

            checkError(equeue, expectedMessage);
        }
Exemple #7
0
        public void TestRewriteRuleAndRewriteModeNotSimple() /*throws Exception*/ {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a  : ID+ -> {ick}\n" +
                "   | INT INT -> {ick}\n" +
                "   ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Exemple #8
0
        public void TestTemplateConstructor() /*throws Exception*/
        {
            string action    = "x = %foo(name={$ID.text});";
            string expecting = "x = templateLib.getInstanceOf(\"foo\"," +
                               LINE_SEP + "  new STAttrMap().put(\"name\", (ID1!=null?ID1.getText():null)));";

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator(generator,
                                     "a",
                                     new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup(".", typeof(AngleBracketTemplateLexer));
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.ToString();

            assertNoErrors(equeue);

            assertEquals(expecting, found);
        }
Exemple #9
0
        public void TestNewlineLiterals() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar T;\n" +
                "A : '\\n\\n' ;\n"  // ANTLR sees '\n\n'
                );
            string expecting = "match(\"\\n\\n\")";

            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // codegen phase sets some vars we need
            StringTemplate codeST = generator.RecognizerST;
            string         code   = codeST.Render();
            int            m      = code.IndexOf("match(\"");
            string         found  = code.Substring(m, expecting.Length);

            Assert.AreEqual(expecting, found);
        }
Exemple #10
0
        public void TestSetAttr() /*throws Exception*/
        {
            string action    = "%x.y = z;";
            string expecting = "(x).setAttribute(\"y\", z);";

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator(generator,
                                     "a",
                                     new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup(".", typeof(AngleBracketTemplateLexer));
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.ToString();

            assertNoErrors(equeue);

            assertEquals(expecting, found);
        }
Exemple #11
0
        public void TestTemplateConstructorNoArgs() /*throws Exception*/
        {
            string action    = "x = %foo();";
            string expecting = "x = templateLib.getInstanceOf(\"foo\");";

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator(generator,
                                     "a",
                                     new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.Render();

            assertNoErrors(equeue);

            Assert.AreEqual(expecting, found);
        }
Exemple #12
0
        public void TestSetAttrOfExpr() /*throws Exception*/
        {
            string action    = "%{foo($ID.text).getST()}.y = z;";
            string expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);";

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator(generator,
                                                               "a",
                                                               new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.Render();

            assertNoErrors(equeue);

            Assert.AreEqual(expecting, found);
        }
        public void TestRefToRuleWithNoReturnValue() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            string grammarStr =
                "grammar P;\n" +
                "a : x=b ;\n" +
                "b : B ;\n" +
                "B : 'b' ;\n";
            Grammar g = new Grammar(grammarStr);

            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            StringTemplate recogST = generator.GenRecognizer();
            string         code    = recogST.Render();

            Assert.IsTrue(code.IndexOf("x=b();") < 0, "not expecting label");

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #14
0
        public void TestArguments()
        {
            string action = "$i; $i.x; $u; $u.x";
            string expecting = "i; i.x; u; u.x";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[User u, int i]\n" +
                "        : {" + action + "}\n" +
                "        ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #15
0
        public void TestForwardRefRuleLabels()
        {
            string action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
            string expecting = "(r!=null?r.x:0); (r!=null?((Token)r.start):null); (r!=null?((Token)r.stop):null); (r!=null?((Object)r.tree):null); (r!=null?r.x:0); (r!=null?((Object)r.tree):null);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "b : r=a {###" + action + "!!!}\n" +
                "  ;\n" +
                "a returns [int x]\n" +
                "  : ;\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // codegen phase sets some vars we need

            StringTemplate codeST = generator.RecognizerST;
            string code = codeST.Render();
            int startIndex = code.IndexOf("###") + 3;
            int endIndex = code.IndexOf("!!!");
            string found = code.Substring(startIndex, endIndex - startIndex);
            Assert.AreEqual( expecting, found );

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #16
0
        public void TestSetAttrOfExpr()
        {
            string action = "%{foo($ID.text).getST()}.y = z;";
            string expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
Exemple #17
0
        public void TestTemplateConstructorNoArgs()
        {
            string action = "x = %foo();";
            string expecting = "x = templateLib.getInstanceOf(\"foo\");";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator( generator,
                                            "a",
                                            new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
        public void TestNoWildcardAsRootError()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string treeGrammar =
                "tree grammar TP;\n" +
                "options {output=AST;}\n" +
                "a : ^(. INT) \n" +
                "  ;\n";

            Grammar g = new Grammar( treeGrammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            assertEquals( "unexpected errors: " + equeue, 1, equeue.errors.Count );

            int expectedMsgID = ErrorManager.MSG_WILDCARD_AS_ROOT;
            object expectedArg = null;
            RecognitionException expectedExc = null;
            GrammarSyntaxMessage expectedMessage =
                new GrammarSyntaxMessage( expectedMsgID, g, null, expectedArg, expectedExc );

            checkError( equeue, expectedMessage );
        }
        public void TestRewriteRuleAndRewriteModeRefRule()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a  : b+ -> {ick}\n" +
                "   | b b A -> {ick}\n" +
                "   ;\n" +
                "b  : B ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Exemple #20
0
        public void TestComplicatedSingleArgParsing()
        {
            string action = "(*a).foo(21,33,\",\")";
            string expecting = "(*a).foo(21,33,\",\")";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            // now check in actual grammar.
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[User u, int i]\n" +
                "        : A a[" + action + "] B\n" +
                "        ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation = translator.Translate();
            Assert.AreEqual( expecting, rawTranslation );

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #21
0
        public void TestWeirdRuleRef()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : ID a -> $a | INT ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT: '0'..'9'+ ;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            // $a is ambig; is it previous root or ref to a ref in alt?
            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #22
0
        public void TestCharListLabelInLexer()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "R : x+='z' ;\n" );

            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #23
0
        public void TestComplicatedArgParsingWithTranslation()
        {
            string action = "x, $A.text+\"3242\", (*$A).foo(21,33), 3.2+1, '\\n', " +
                            "\"a,oo\\nick\", {bl, \"fdkj\"eck}";
            string expecting = "x, (A1!=null?A1.getText():null)+\"3242\", (*A1).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            // now check in actual grammar.
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[User u, int i]\n" +
                "        : A a[" + action + "] B\n" +
                "        ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #24
0
        public void TestBracketArgParsing()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            // now check in actual grammar.
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[String[\\] ick, int i]\n" +
                "        : A \n" +
                "        ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            Rule r = g.GetRule( "a" );
            AttributeScope parameters = r.ParameterScope;
            var attrs = parameters.Attributes;
            Assert.AreEqual("String[] ick", attrs.ElementAt(0).Decl.ToString(), "attribute mismatch");
            Assert.AreEqual("ick", attrs.ElementAt(0).Name, "parameter name mismatch");
            Assert.AreEqual("String[]", attrs.ElementAt(0).Type, "declarator mismatch");

            Assert.AreEqual("int i", attrs.ElementAt(1).Decl.ToString(), "attribute mismatch");
            Assert.AreEqual("i", attrs.ElementAt(1).Name, "parameter name mismatch");
            Assert.AreEqual("int", attrs.ElementAt(1).Type, "declarator mismatch");

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #25
0
 public void TestAssignToTreeNodeAttribute()
 {
     //string action = "$tree.scope = localScope;";
     string expecting = "((Object)retval.tree).scope = localScope;";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
         "grammar a;\n" +
         "options { output=AST; }" +
         "rule\n" +
         "@init {\n" +
         "   Scope localScope=null;\n" +
         "}\n" +
         "@after {\n" +
         "   ###$tree.scope = localScope;!!!\n" +
         "}\n" +
         "   : 'a' -> ^('a')\n" +
         ";" );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer(); // forces load of templates
     StringTemplate codeST = generator.RecognizerST;
     string code = codeST.Render();
     string found = code.Substring(code.IndexOf("###") + 3, code.IndexOf("!!!") - code.IndexOf("###") - 3);
     Assert.AreEqual(expecting, found);
 }
Exemple #26
0
        public void TestAssignToOwnRulenameAttr()
        {
            string action = "$rule.tree = null;";
            string expecting = "retval.tree = null;";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar a;\n" +
                "rule\n" +
                "    : 'y' {" + action + "}\n" +
                "    ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         "rule",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #27
0
        public void TestFullyQualifiedRefToCurrentRuleParameter()
        {
            string action = "$a.i;";
            string expecting = "i;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a[int i]: {" + action + "}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #28
0
        public void TestArgsWhenNoneDefined()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r[32,34] ;" +
                "r : 'a';\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
            object expectedArg = "r";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Exemple #29
0
        public void TestUnknownToken()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : INT -> ICK ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT : '0'..'9'+;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE;
            object expectedArg = "ICK";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );

            checkError( equeue, expectedMessage );
        }
Exemple #30
0
        public void TestUnqualifiedRuleScopeAccessInsideRule()
        {
            string action = "$n;";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : {" + action + "}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_ATTRIBUTE;
            object expectedArg = "n";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg,
                                            expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Exemple #31
0
        public void TestKnownRuleButNotInLHS()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : INT -> b ;\n" +
                "b : 'b' ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT : '0'..'9'+;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS;
            object expectedArg = "b";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );

            checkError( equeue, expectedMessage );
        }
Exemple #32
0
 public void TestDoNotTranslateScopeAttributeCompare()
 {
     string action = "if ($rule::foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
     string expecting = "if (((rule_scope)rule_stack.peek()).foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
             "grammar a;\n" +
             "rule\n" +
             "scope {\n" +
             "   String foo;" +
             "} :\n" +
             "     twoIDs" +
             "    ;\n" +
             "twoIDs:\n" +
             "    ID ID {" + action + "}\n" +
             "    ;\n" +
             "ID : 'id';"
     );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer();
     ActionTranslator translator = new ActionTranslator( generator,
                                                                  "twoIDs",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 1 );
     string rawTranslation =
         translator.Translate();
     // check that we didn't use scopeSetAttributeRef int translation!
     bool foundScopeSetAttributeRef = false;
     for ( int i = 0; i < translator.chunks.Count; i++ )
     {
         object chunk = translator.chunks[i];
         if ( chunk is StringTemplate )
         {
             if ( ( (StringTemplate)chunk ).Name.Equals( "/scopeSetAttributeRef" ) )
             {
                 foundScopeSetAttributeRef = true;
             }
         }
     }
     Assert.IsFalse(foundScopeSetAttributeRef, "action translator used scopeSetAttributeRef template in comparison!");
     StringTemplateGroup templates =
         new StringTemplateGroup();
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.Render();
     Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
     Assert.AreEqual( expecting, found );
 }
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Exemple #34
0
        public void TestDynamicRuleScopeRefInSubrule()
        {
            string action = "$a::n;";
            string expecting = "((a_scope)a_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a\n" +
                "scope {\n" +
                "  float n;\n" +
                "} : b ;\n" +
                "b : {" + action + "}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #35
0
        public void TestUnknownGlobalScope()
        {
            string action = "$Symbols::names.add($id.text);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a scope Symbols; : (id=ID ';' {" + action + "} )+\n" +
                "  ;\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );

            Assert.AreEqual(2, equeue.errors.Count, "unexpected errors: " + equeue);

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE;
            object expectedArg = "Symbols";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Exemple #36
0
        public void TestDynamicScopeRefOkEvenThoughRuleRefExists()
        {
            string action = "$b::n;";
            string expecting = "((b_scope)b_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "s : b ;\n" +
                "b\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : '(' b ')' {" + action + "}\n" + // refers to current invocation's n
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Exemple #37
0
        public void TestSetAttrOfExprInMembers()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "@members {\n" +
                "%code.instr = o;" + // must not get null ptr!
                "}\n" +
                "a : ID\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            assertNoErrors( equeue );
        }
Exemple #38
0
 public void TestEscaped_InAction()
 {
     string action = "int \\$n; \"\\$in string\\$\"";
     string expecting = "int $n; \"$in string$\"";
     Grammar g = new Grammar(
         "parser grammar t;\n" +
         "@members {" + action + "}\n" +
         "a[User u, int i]\n" +
         "        : {" + action + "}\n" +
         "        ;" );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer(); // forces load of templates
     ActionTranslator translator =
         new ActionTranslator( generator,
                                   "a",
                                   new CommonToken( ANTLRParser.ACTION, action ), 0 );
     string found = translator.Translate();
     Assert.AreEqual( expecting, found );
 }
Exemple #39
0
        public void TestCannotHaveSpaceBeforeDot()
        {
            string action = "%x .y = z;";
            //String expecting = null;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
            object expectedArg = "%x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Exemple #40
0
        public void TestArgsWithInitValues()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r[32,34] ;" +
                "r[int x, int y=3] : 'a';\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_ARG_INIT_VALUES_ILLEGAL;
            object expectedArg = "y";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Exemple #41
0
 public void TestDoNotTranslateAttributeCompare()
 {
     string action = "$a.line == $b.line";
     string expecting = "(a!=null?a.getLine():0) == (b!=null?b.getLine():0)";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
             "lexer grammar a;\n" +
             "RULE:\n" +
             "     a=ID b=ID {" + action + "}" +
             "    ;\n" +
             "ID : 'id';"
     );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer();
     ActionTranslator translator = new ActionTranslator( generator,
                                                                  "RULE",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 1 );
     string found = translator.Translate();
     Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
     Assert.AreEqual( expecting, found );
 }
Exemple #42
0
        public void Test0IndexedGlobalScope()
        {
            string action = "$Symbols[0]::names.add($id.text);";
            string expecting =
                "((Symbols_scope)Symbols_stack.elementAt(0)).names.add((id!=null?id.getText():null));";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "scope Symbols {\n" +
                "  int n;\n" +
                "  List names;\n" +
                "}\n" +
                "a scope Symbols; : (id=ID ';' {" + action + "} )+\n" +
                "  ;\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }