Esempio n. 1
0
        public static void GrammarWarning(int msgID,
                                          Grammar g,
                                          IToken token,
                                          Object arg,
                                          Object arg2)
        {
            GetErrorState().warnings++;
            Message msg = new GrammarSemanticsMessage(msgID, g, token, arg, arg2);

            GetErrorState().warningMsgIDs.Add(msgID);
            GetErrorListener().Warning(msg);
        }
        public void TestBadGrammarOption()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue ); // unique listener per thread
            AntlrTool antlr = newTool();
            Grammar g = new Grammar( antlr,
                                    "grammar t;\n" +
                                    "options {foo=3; language=Java;}\n" +
                                    "a : 'a';\n" );

            object expectedArg = "foo";
            int expectedMsgID = ErrorManager.MSG_ILLEGAL_OPTION;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkGrammarSemanticsError( equeue, expectedMessage );
        }
Esempio n. 3
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 );
        }
Esempio n. 4
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 );
        }
Esempio n. 5
0
        public void TestUnqualifiedRuleScopeAttribute()
        {
            string action = "$n;"; // must be qualified
            string expecting = "$n;";

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

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
            object expectedArg = "n";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 6
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 );
        }
Esempio n. 7
0
        public void TestTreeRuleStopAttributeIsInvalid()
        {
            string action = "$r.x; $r.start; $r.stop";
            string expecting = "(r!=null?r.x:0); (r!=null?((CommonTree)r.start):null); $r.stop";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar t;\n" +
                "options {ASTLabelType=CommonTree;}\n" +
                "a returns [int x]\n" +
                "  :\n" +
                "  ;\n" +
                "b : r=a {###" + action + "!!!}\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 );

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE;
            object expectedArg = "a";
            object expectedArg2 = "stop";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            Console.Out.WriteLine( "equeue:" + equeue );
            checkError( equeue, expectedMessage );
        }
Esempio n. 8
0
        public void TestRuleLabelWithoutOutputOption()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar T;\n" +
                "s : x+=a ;" +
                "a : 'a';\n" +
                "b : 'b';\n" +
                "WS : ' '|'\n';\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_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT;
            object expectedArg = "x";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 9
0
        public void TestInvalidRuleLabelAccessesScopeAttribute()
        {
            string action = "$r.n";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a\n" +
                "scope { int n; }\n" +
                "  :\n" +
                "  ;\n" +
                "b : r=a[3] {" + action + "}\n" +
                "  ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            int expectedMsgID = ErrorManager.MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF;
            object expectedArg = "a";
            object expectedArg2 = "n";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 10
0
        public void TestInvalidReturnValues()
        {
            string action = "$x";
            string expecting = action;

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

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
            object expectedArg = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Esempio n. 11
0
        public void TestIllegalAssignToOwnRulenameAttr()
        {
            string action = "$rule.stop = 0;";
            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 rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            object expectedArg = "rule";
            object expectedArg2 = "stop";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 12
0
        public void TestIllegalAssignToLocalAttr()
        {
            string action = "$tree = null; $st = null; $start = 0; $stop = 0; $text = 0;";
            string expecting = "retval.tree = null; retval.st = 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 rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            var expectedErrors = new List<object>( 3 );
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, "start", "" );
            expectedErrors.Add( expectedMessage );
            GrammarSemanticsMessage expectedMessage2 =
                new GrammarSemanticsMessage( expectedMsgID, g, null, "stop", "" );
            expectedErrors.Add( expectedMessage2 );
            GrammarSemanticsMessage expectedMessage3 =
            new GrammarSemanticsMessage( expectedMsgID, g, null, "text", "" );
            expectedErrors.Add( expectedMessage3 );
            checkErrors( equeue, expectedErrors );

            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();
            Assert.AreEqual( expecting, found );
        }
Esempio n. 13
0
 //throws Exception
 // S U P P O R T
 protected void checkError( ErrorQueue equeue,
                           GrammarSemanticsMessage expectedMessage )
 {
     /*
     System.out.println(equeue.infos);
     System.out.println(equeue.warnings);
     System.out.println(equeue.errors);
     */
     Message foundMsg = null;
     for ( int i = 0; i < equeue.errors.Count; i++ )
     {
         Message m = (Message)equeue.errors[i];
         if ( m.msgID == expectedMessage.msgID )
         {
             foundMsg = m;
         }
     }
     assertTrue( "no error; " + expectedMessage.msgID + " expected", equeue.errors.Count > 0 );
     assertNotNull( "couldn't find expected error: " + expectedMessage.msgID + " in " + equeue, foundMsg );
     assertTrue( "error is not a GrammarSemanticsMessage",
                foundMsg is GrammarSemanticsMessage );
     assertEquals( expectedMessage.arg, foundMsg.arg );
     assertEquals( expectedMessage.arg2, foundMsg.arg2 );
 }
Esempio n. 14
0
        public void TestUnknownDynamicAttribute()
        {
            string action = "$a::x";
            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
            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();
            assertEquals( expecting, found );

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
            object expectedArg = "a";
            object expectedArg2 = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 15
0
        public void TestNonDynamicAttributeOutsideRule()
        {
            string action = "[TestMethod] public void foo() { $x; }";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "@members {'+action+'}\n" +
                "a : ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         null,
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 0 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE;
            object expectedArg = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Esempio n. 16
0
        public void TestArgsOnToken()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : ID[32,34] ;" +
                "ID : '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_ARGS_ON_TOKEN_REF;
            object expectedArg = "ID";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 17
0
        public void TestIsolatedRefToRule()
        {
            string action = "$x;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : x=b {" + action + "}\n" +
                "  ;\n" +
                "b : 'b' ;\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_SCOPE;
            object expectedArg = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Esempio n. 18
0
        public void TestArgsOnTokenInLexerRuleOfCombined()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : R;\n" +
                "R : 'z' ID[32] ;\n" +
                "ID : 'a';\n" );

            string lexerGrammarStr = g.GetLexerGrammar();
            System.IO.StringReader sr = new System.IO.StringReader( lexerGrammarStr );
            Grammar lexerGrammar = new Grammar();
            lexerGrammar.FileName = "<internally-generated-lexer>";
            lexerGrammar.ImportTokenVocabulary( g );
            lexerGrammar.ParseAndBuildAST( sr );
            lexerGrammar.DefineGrammarSymbols();
            lexerGrammar.CheckNameSpaceAndActions();
            sr.Close();

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

            int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
            object expectedArg = "ID";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, lexerGrammar, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 19
0
 public void TestListAndRuleLabelTypeMismatch()
 {
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
         "grammar t;\n" +
         "options {output=AST;}\n" +
         "a : bs+=b bs=b\n" +
         "  ;\n" +
         "b : 'b';\n" );
     int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT;
     object expectedArg = "bs";
     object expectedArg2 = "rule!=rule-list";
     GrammarSemanticsMessage expectedMessage =
         new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
     checkError( equeue, expectedMessage );
 }
Esempio n. 20
0
        public void TestUnknownGlobalDynamicAttribute()
        {
            string action = "$Symbols::x";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "scope Symbols {\n" +
                "  int n;\n" +
                "}\n" +
                "a : {'+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);

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
            object expectedArg = "Symbols";
            object expectedArg2 = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 21
0
        public void TestMissingArgsInLexer()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "A : R ;" +
                "R[int i] : '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_MISSING_RULE_ARGS;
            object expectedArg = "R";
            object expectedArg2 = null;
            // getting a second error @1:12, probably from nextToken
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 22
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 );
        }
Esempio n. 23
0
        public void TestAmbiguousTokenRefWithProp()
        {
            string action = "$ID.text;";
            //String expecting = "";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : ID ID {" + action + "};" +
                "ID : '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_NONUNIQUE_REF;
            object expectedArg = "ID";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Esempio n. 24
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 );
        }
Esempio n. 25
0
        public void TestMissingUnlabeledRuleAttribute()
        {
            string action = "$a";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a returns [int x]:\n" +
                "  ;\n" +
                "b : a {" + action + "}\n" +
                "  ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE;
            object expectedArg = "a";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Esempio n. 26
0
 //throws Exception
 protected void checkError( ErrorQueue equeue,
                           GrammarSemanticsMessage expectedMessage )
 {
     /*
     System.out.println(equeue.infos);
     System.out.println(equeue.warnings);
     System.out.println(equeue.errors);
     */
     Message foundMsg = null;
     for ( int i = 0; i < equeue.errors.Count; i++ )
     {
         Message m = (Message)equeue.errors[i];
         if ( m.msgID == expectedMessage.msgID )
         {
             foundMsg = m;
         }
     }
     Assert.IsTrue(equeue.errors.Count > 0, "no error; " + expectedMessage.msgID + " expected");
     Assert.IsTrue(equeue.errors.Count <= 1, "too many errors; " + equeue.errors);
     Assert.IsTrue(foundMsg != null, "couldn't find expected error: " + expectedMessage.msgID);
     Assert.IsTrue(foundMsg is GrammarSemanticsMessage, "error is not a GrammarSemanticsMessage");
     Assert.AreEqual( expectedMessage.arg, foundMsg.arg );
     Assert.AreEqual( expectedMessage.arg2, foundMsg.arg2 );
 }
Esempio n. 27
0
        public void TestNonDynamicAttributeOutsideRule2()
        {
            string action = "[TestMethod] public void foo() { $x.y; }";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "@members {'+action+'}\n" +
                "a : ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         null,
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 0 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE;
            object expectedArg = "x";
            object expectedArg2 = "y";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Esempio n. 28
0
 public static void GrammarWarning( int msgID,
                                   Grammar g,
                                   IToken token,
                                   Object arg,
                                   Object arg2 )
 {
     GetErrorState().warnings++;
     Message msg = new GrammarSemanticsMessage( msgID, g, token, arg, arg2 );
     GetErrorState().warningMsgIDs.Add( msgID );
     GetErrorListener().Warning( msg );
 }
Esempio n. 29
0
 public void TestArgReturnValueMismatch()
 {
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
         "grammar t;\n" +
         "a[int i] returns [int x, int i]\n" +
         "  : \n" +
         "  ;\n" +
         "b : ;\n" );
     int expectedMsgID = ErrorManager.MSG_ARG_RETVAL_CONFLICT;
     object expectedArg = "i";
     object expectedArg2 = "a";
     GrammarSemanticsMessage expectedMessage =
         new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
     checkError( equeue, expectedMessage );
 }
Esempio n. 30
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() );
        }
Esempio n. 31
0
 public void TestRuleAndTokenLabelTypeMismatch()
 {
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
         "grammar t;\n" +
         "a : id='foo' id=b\n" +
         "  ;\n" +
         "b : ;\n" );
     int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT;
     object expectedArg = "id";
     object expectedArg2 = "rule!=token";
     GrammarSemanticsMessage expectedMessage =
         new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
     checkError( equeue, expectedMessage );
 }