示例#1
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);
        }
示例#2
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() );
        }
示例#3
0
        public void TestCannotHaveSpaceAfterDot()
        {
            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 );
        }
        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 );
        }
示例#5
0
        public void Test3LevelImport()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string slave =
                "parser grammar T;\n" +
                "a : T ;\n";
            mkdir( tmpdir );
            writeFile( tmpdir, "T.g", slave );
            string slave2 =
                "parser grammar S;\n" + // A, B, C token type order
                "import T;\n" +
                "a : S ;\n";
            mkdir( tmpdir );
            writeFile( tmpdir, "S.g", slave2 );

            string master =
                "grammar M;\n" +
                "import S;\n" +
                "a : M ;\n";
            writeFile( tmpdir, "M.g", master );
            AntlrTool antlr = newTool( new string[] { "-lib", tmpdir } );
            CompositeGrammar composite = new CompositeGrammar();
            Grammar g = new Grammar( antlr, tmpdir + "/M.g", composite );
            composite.SetDelegationRoot( g );
            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();
            g.composite.DefineGrammarSymbols();

            string expectedTokenIDToTypeMap = "[M=6, S=5, T=4]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList = "[T, S, M]";

            assertEquals( expectedTokenIDToTypeMap,
                         realElements( g.composite.tokenIDToTypeMap ).ToElementString() );
            assertEquals( expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.ToElementString() );
            assertEquals( expectedTypeToTokenList,
                         realElements( g.composite.typeToTokenList ).ToElementString() );

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

            bool ok =
                rawGenerateAndBuildRecognizer( "M.g", master, "MParser", null, false );
            bool expecting = true; // should be ok
            assertEquals( expecting, ok );
        }
示例#6
0
        public void TestMissingAttributeAccessPreventsCodeGeneration()
        {
            string grammar =
                "grammar T;\n" +
                "options {\n" +
                "    backtrack = true; \n" +
                "}\n" +
                "// if b is rule ref, gens bad void x=null code\n" +
                "a : x=b {Object o = $x; System.out.println(\"alt1\");}\n" +
                "  | y=b\n" +
                "  ;\n" +
                "\n" +
                "b : 'a' ;\n";

            ErrorQueue errorQueue = new ErrorQueue();

            ErrorManager.SetErrorListener(errorQueue);
            bool success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);

            Assert.IsFalse(success);
            Assert.AreEqual(
                "[error(117): " + tmpdir.ToString() + Path.DirectorySeparatorChar + "T.g:6:9: missing attribute access on rule scope: x]",
                '[' + string.Join(", ", errorQueue.errors) + ']');
        }
        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);
        }
示例#8
0
        public void TestRewriteRuleAndRewriteModeOnSimpleElements() /*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: ^(A B) -> {ick}\n" +
                " | y+=INT -> {ick}\n" +
                " | x=ID -> {ick}\n" +
                " | BLORT -> {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();

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

            ErrorManager.SetErrorListener(equeue);
            string tokens =
                "';'=4\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "T.tokens", tokens);

            string importer =
                "lexer grammar B; \n" +
                "options\t{tokenVocab=T;} \n" +
                "SEMI:';' ; \n";

            writeFile(tmpdir, "B.g", importer);
            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, tmpdir + "/B.g", composite);

            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();

            string expectedTokenIDToTypeMap       = "[SEMI=4]";
            string expectedStringLiteralToTypeMap = "{';'=4}";
            string expectedTypeToTokenList        = "[SEMI]";

            Assert.AreEqual(expectedTokenIDToTypeMap,
                            realElements(g.composite.TokenIDToTypeMap).ToElementString());
            Assert.AreEqual(expectedStringLiteralToTypeMap, g.composite.StringLiteralToTypeMap.ToElementString());
            Assert.AreEqual(expectedTypeToTokenList,
                            realElements(g.composite.TypeToTokenList).ToElementString());

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
示例#10
0
        public void TestIdenticalRules() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "A : 'a' ;\n" +
                "B : 'a' ;\n");  // can't reach this
            string expecting =
                ".s0-'a'->.s1" + NewLine +
                ".s1-<EOT>->:s2=>1" + NewLine;

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            checkDecision(g, 1, expecting, new int[] { 2 });

            Assert.AreEqual(1, equeue.size(), "unexpected number of expected problems");
            Message msg = (Message)equeue.errors[0];

            Assert.IsTrue(msg is GrammarUnreachableAltsMessage, "warning must be an unreachable alt");
            GrammarUnreachableAltsMessage u = (GrammarUnreachableAltsMessage)msg;

            Assert.AreEqual("[2]", u.alts.ToElementString());
        }
示例#11
0
        public void TestCombinedImportsCombined() /*throws Exception*/
        {
            //Assert.Inconclusive( "May be failing on just my port..." );
            // for now, we don't allow combined to import combined
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            string slave =
                "grammar S;\n" + // A, B, C token type order
                "tokens { A; B; C; }\n" +
                "x : 'x' INT {System.out.println(\"S.x\");} ;\n" +
                "INT : '0'..'9'+ ;\n" +
                "WS : (' '|'\\n') {skip();} ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);

            string master =
                "grammar M;\n" +
                "import S;\n" +
                "s : x INT ;\n";

            writeFile(tmpdir, "M.g", master);
            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, tmpdir + "/M.g", composite);

            composite.SetDelegationRoot(g);
            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();

            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);
            string expectedError = "error(161): " + Regex.Replace(tmpdir.ToString(), "\\-[0-9]+", "") + "/M.g:2:8: combined grammar M cannot import combined grammar S";

            Assert.AreEqual(expectedError, Regex.Replace(equeue.errors[0].ToString(), "\\-[0-9]+", ""), "unexpected errors: " + equeue);
        }
示例#12
0
        public void TestNonGreedyLoopThatNeverLoops() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "DUH : (options {greedy=false;}:'x')+ ;");  // loop never matched
            string expecting =
                ":s0=>2\n";

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            checkDecision(g, 1, expecting, new int[] { 1 });

            assertEquals("unexpected number of expected problems",
                         1, equeue.size());
            Message msg = (Message)equeue.errors[0];

            assertTrue("warning must be an unreachable alt",
                       msg is GrammarUnreachableAltsMessage);
            GrammarUnreachableAltsMessage u = (GrammarUnreachableAltsMessage)msg;

            assertEquals("[1]", u.alts.ToElementString());
        }
示例#13
0
        public void TestStrayBracketRecovery()
        {
            string grammar =
                "grammar T;\n" +
                "options {output = AST;}\n" +
                "tokens{NODE;}\n" +
                "s : a=ID INT -> ^(NODE[$a]] INT);\n" +
                "ID: 'a'..'z'+;\n" +
                "INT: '0'..'9'+;\n";

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

            bool found =
                rawGenerateAndBuildRecognizer(
                    "T.g", grammar, "TParser", "TLexer", false);

            Assert.IsFalse(found);
            Assert.AreEqual(
                "[error(100): :4:27: syntax error: antlr: dangling ']'? make sure to escape with \\]]",
                '[' + string.Join(", ", errorQueue.errors) + ']');
        }
示例#14
0
        public void TestMissingAttributeAccessPreventsCodeGeneration()
        {
            string grammar =
                "grammar T;\n" +
                "options {\n" +
                "    backtrack = true; \n" +
                "}\n" +
                "// if b is rule ref, gens bad void x=null code\n" +
                "a : x=b {Object o = $x; System.out.println(\"alt1\");}\n" +
                "  | y=b\n" +
                "  ;\n" +
                "\n" +
                "b : 'a' ;\n";

            ErrorQueue errorQueue = new ErrorQueue();
            ErrorManager.SetErrorListener(errorQueue);
            bool success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
            Assert.IsFalse(success);
            Assert.AreEqual(
                "[error(117): " + tmpdir.ToString() + Path.DirectorySeparatorChar + "T.g:6:9: missing attribute access on rule scope: x]",
                '[' + string.Join(", ", errorQueue.errors) + ']');
        }
示例#15
0
        protected void checkDecision(Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts,
                                     int[] expectingNonDetAlts,
                                     string expectingAmbigInput,
                                     int[] expectingInsufficientPredAlts,
                                     int[] expectingDanglingAlts,
                                     int expectingNumWarnings,
                                     bool hasPredHiddenByAction)
        //throws Exception
        {
            DecisionProbe.verbose = true; // make sure we get all error info
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            CodeGenerator generator = new CodeGenerator(newTool(), g, "Java");

            g.CodeGenerator = generator;
            // mimic actions of org.antlr.Tool first time for grammar g
            if (g.NumberOfDecisions == 0)
            {
                g.BuildNFA();
                g.CreateLookaheadDFAs(false);
            }

            if (equeue.size() != expectingNumWarnings)
            {
                Console.Error.WriteLine("Warnings issued: " + equeue);
            }

            Assert.AreEqual(expectingNumWarnings, equeue.size(), "unexpected number of expected problems");

            DFA          dfa        = g.GetLookaheadDFA(decision);
            FASerializer serializer = new FASerializer(g);
            string       result     = serializer.Serialize(dfa.StartState);
            //System.out.print(result);
            var unreachableAlts = dfa.UnreachableAlts;

            // make sure unreachable alts are as expected
            if (expectingUnreachableAlts != null)
            {
                BitSet s = new BitSet();
                s.AddAll(expectingUnreachableAlts);
                BitSet s2 = new BitSet();
                s2.AddAll(unreachableAlts);
                Assert.AreEqual(s, s2, "unreachable alts mismatch");
            }
            else
            {
                Assert.AreEqual(0, unreachableAlts != null ? unreachableAlts.Count : 0, "unreachable alts mismatch");
            }

            // check conflicting input
            if (expectingAmbigInput != null)
            {
                // first, find nondet message
                Message msg = getNonDeterminismMessage(equeue.warnings);
                Assert.IsNotNull(msg, "no nondeterminism warning?");
                Assert.IsTrue(msg is GrammarNonDeterminismMessage, "expecting nondeterminism; found " + msg.GetType().Name);
                GrammarNonDeterminismMessage nondetMsg =
                    getNonDeterminismMessage(equeue.warnings);
                var labels =
                    nondetMsg.probe.GetSampleNonDeterministicInputSequence(nondetMsg.problemState);
                string input = nondetMsg.probe.GetInputSequenceDisplay(labels);
                Assert.AreEqual(expectingAmbigInput, input);
            }

            // check nondet alts
            if (expectingNonDetAlts != null)
            {
                GrammarNonDeterminismMessage nondetMsg =
                    getNonDeterminismMessage(equeue.warnings);
                Assert.IsNotNull(nondetMsg, "found no nondet alts; expecting: " + str(expectingNonDetAlts));
                var nonDetAlts =
                    nondetMsg.probe.GetNonDeterministicAltsForState(nondetMsg.problemState);
                // compare nonDetAlts with expectingNonDetAlts
                BitSet s = new BitSet();
                s.AddAll(expectingNonDetAlts);
                BitSet s2 = new BitSet();
                s2.AddAll(nonDetAlts);
                Assert.AreEqual(s, s2, "nondet alts mismatch");
                Assert.AreEqual(hasPredHiddenByAction, nondetMsg.problemState.Dfa.HasPredicateBlockedByAction, "mismatch between expected hasPredHiddenByAction");
            }
            else
            {
                // not expecting any nondet alts, make sure there are none
                GrammarNonDeterminismMessage nondetMsg =
                    getNonDeterminismMessage(equeue.warnings);
                Assert.IsNull(nondetMsg, "found nondet alts, but expecting none");
            }

            if (expectingInsufficientPredAlts != null)
            {
                GrammarInsufficientPredicatesMessage insuffPredMsg =
                    getGrammarInsufficientPredicatesMessage(equeue.warnings);
                Assert.IsNotNull(insuffPredMsg, "found no GrammarInsufficientPredicatesMessage alts; expecting: " + str(expectingNonDetAlts));
                var    locations  = insuffPredMsg.altToLocations;
                var    actualAlts = locations.Keys;
                BitSet s          = new BitSet();
                s.AddAll(expectingInsufficientPredAlts);
                BitSet s2 = new BitSet();
                s2.AddAll(actualAlts);
                Assert.AreEqual(s, s2, "mismatch between insufficiently covered alts");
                Assert.AreEqual(hasPredHiddenByAction, insuffPredMsg.problemState.Dfa.HasPredicateBlockedByAction, "mismatch between expected hasPredHiddenByAction");
            }
            else
            {
                // not expecting any nondet alts, make sure there are none
                GrammarInsufficientPredicatesMessage nondetMsg =
                    getGrammarInsufficientPredicatesMessage(equeue.warnings);
                if (nondetMsg != null)
                {
                    Console.Out.WriteLine(equeue.warnings);
                }
                Assert.IsNull(nondetMsg, "found insufficiently covered alts, but expecting none");
            }

            Assert.AreEqual(expecting, result);
        }
示例#16
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);
        }
示例#17
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 );
        }
示例#18
0
        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);
        }
示例#19
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 );
        }
示例#20
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 );
 }
示例#21
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 );
 }
示例#22
0
        public void TestCStyleReturnInitValue()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r ;\n" +
                "r returns [int (*x)()=NULL] : 'a' ;\n" );
            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);

            Rule r = g.GetRule( "r" );
            AttributeScope retScope = r.ReturnScope;
            var parameters = retScope.Attributes;
            Assert.IsNotNull(parameters, "missing return action");
            Assert.AreEqual( 1, parameters.Count );
            string found = parameters.ElementAt( 0 ).ToString();
            string expecting = "int (*)() x=NULL";
            Assert.AreEqual( expecting, found );
        }
示例#23
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);
        }
示例#24
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);
        }
示例#25
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 );
        }
示例#26
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);
        }
示例#27
0
        public void TestIdenticalRules()
        {
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "A : 'a' ;\n" +
                "B : 'a' ;\n" ); // can't reach this
            string expecting =
                ".s0-'a'->.s1" + NewLine +
                ".s1-<EOT>->:s2=>1" + NewLine;

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

            checkDecision( g, 1, expecting, new int[] { 2 } );

            Assert.AreEqual(1, equeue.size(), "unexpected number of expected problems");
            Message msg = (Message)equeue.errors[0];
            Assert.IsTrue( msg is GrammarUnreachableAltsMessage, "warning must be an unreachable alt" );
            GrammarUnreachableAltsMessage u = (GrammarUnreachableAltsMessage)msg;
            Assert.AreEqual( "[2]", u.alts.ToElementString() );
        }
        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 );
        }
示例#29
0
 // S U P P O R T
 private void assertNoErrors( ErrorQueue equeue )
 {
     Assert.AreEqual(equeue.errors.Count, 0, "unexpected errors: " + equeue);
 }
示例#30
0
        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);
        }
示例#31
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 );
        }
示例#32
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 );
        }
示例#33
0
 // S U P P O R T
 private void assertNoErrors(ErrorQueue equeue)
 {
     assertTrue("unexpected errors: " + equeue, equeue.errors.Count == 0);
 }
示例#34
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 );
 }
示例#35
0
 // S U P P O R T
 private void assertNoErrors(ErrorQueue equeue)
 {
     Assert.AreEqual(equeue.errors.Count, 0, "unexpected errors: " + equeue);
 }
示例#36
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);
        }
示例#37
0
        public void TestSameStringTwoNames() /*throws Exception*/
        {
            Assert.Inconclusive("May be failing on just my port...");
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            string slave =
                "parser grammar S;\n" +
                "tokens { A='a'; }\n" +
                "x : A {System.out.println(\"S.x\");} ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);
            string slave2 =
                "parser grammar T;\n" +
                "tokens { X='a'; }\n" +
                "y : X {System.out.println(\"T.y\");} ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "T.g", slave2);

            string master =
                "grammar M;\n" +
                "import S,T;\n" +
                "s : x y ;\n" +
                "WS : (' '|'\\n') {skip();} ;\n";

            writeFile(tmpdir, "M.g", master);
            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, tmpdir + "/M.g", composite);

            composite.SetDelegationRoot(g);
            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();

            string expectedTokenIDToTypeMap       = "[A=4, WS=6, X=5]";
            string expectedStringLiteralToTypeMap = "{'a'=4}";
            string expectedTypeToTokenList        = "[A, X, WS]";

            assertEquals(expectedTokenIDToTypeMap,
                         realElements(g.composite.tokenIDToTypeMap).ToElementString());
            assertEquals(expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.ToString());
            assertEquals(expectedTypeToTokenList,
                         realElements(g.composite.typeToTokenList).ToElementString());

            object expectedArg   = "X='a'";
            object expectedArg2  = "A";
            int    expectedMsgID = ErrorManager.MSG_TOKEN_ALIAS_CONFLICT;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);

            checkGrammarSemanticsError(equeue, expectedMessage);

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

            string expectedError =
                "error(158): T.g:2:10: cannot alias X='a'; string already assigned to A";

            assertEquals(expectedError, equeue.errors[0].ToString());
        }
示例#38
0
        public void TestNonGreedyLoopThatNeverLoops()
        {
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "DUH : (options {greedy=false;}:'x')+ ;" ); // loop never matched
            string expecting =
                ":s0=>2" + NewLine;

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

            checkDecision( g, 1, expecting, new int[] { 1 } );

            Assert.AreEqual(1, equeue.size(), "unexpected number of expected problems");
            Message msg = (Message)equeue.errors[0];
            Assert.IsTrue(msg is GrammarUnreachableAltsMessage, "warning must be an unreachable alt");
            GrammarUnreachableAltsMessage u = (GrammarUnreachableAltsMessage)msg;
            Assert.AreEqual( "[1]", u.alts.ToElementString() );
        }
示例#39
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);
        }
示例#40
0
        public void TestNestedComposite()
        {
            // Wasn't compiling. http://www.antlr.org/jira/browse/ANTLR-438
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            string gstr =
                "lexer grammar L;\n" +
                "T1: '1';\n" +
                "T2: '2';\n" +
                "T3: '3';\n" +
                "T4: '4';\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "L.g", gstr);
            gstr =
                "parser grammar G1;\n" +
                "s: a | b;\n" +
                "a: T1;\n" +
                "b: T2;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "G1.g", gstr);

            gstr =
                "parser grammar G2;\n" +
                "import G1;\n" +
                "a: T3;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "G2.g", gstr);
            string G3str =
                "grammar G3;\n" +
                "import G2;\n" +
                "b: T4;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "G3.g", G3str);

            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, tmpdir + "/G3.g", composite);

            composite.SetDelegationRoot(g);
            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();
            g.composite.DefineGrammarSymbols();

            string expectedTokenIDToTypeMap       = "[T1=4, T2=5, T3=6, T4=7]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList        = "[T1, T2, T3, T4]";

            Assert.AreEqual(expectedTokenIDToTypeMap, realElements(g.composite.tokenIDToTypeMap).ToElementString());
            Assert.AreEqual(expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.ToElementString());
            Assert.AreEqual(expectedTypeToTokenList, realElements(g.composite.typeToTokenList).ToElementString());

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

            bool ok =
                rawGenerateAndBuildRecognizer("G3.g", G3str, "G3Parser", null, false);
            bool expecting = true; // should be ok

            assertEquals(expecting, ok);
        }
示例#41
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);
        }
示例#42
0
        public void TestBigTreeOfImports() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            string slave =
                "parser grammar T;\n" +
                "x : T ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "T.g", slave);
            slave =
                "parser grammar S;\n" +
                "import T;\n" +
                "y : S ;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);

            slave =
                "parser grammar C;\n" +
                "i : C ;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "C.g", slave);
            slave =
                "parser grammar B;\n" +
                "j : B ;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "B.g", slave);
            slave =
                "parser grammar A;\n" +
                "import B,C;\n" +
                "k : A ;\n";
            mkdir(tmpdir);
            writeFile(tmpdir, "A.g", slave);

            string master =
                "grammar M;\n" +
                "import S,A;\n" +
                "a : M ;\n";

            writeFile(tmpdir, "M.g", master);
            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, tmpdir + "/M.g", composite);

            composite.SetDelegationRoot(g);
            g.ParseAndBuildAST();
            g.composite.AssignTokenTypes();
            g.composite.DefineGrammarSymbols();

            string expectedTokenIDToTypeMap       = "[A=8, B=6, C=7, M=9, S=5, T=4]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList        = "[T, S, B, C, A, M]";

            assertEquals(expectedTokenIDToTypeMap,
                         realElements(g.composite.tokenIDToTypeMap).ToElementString());
            assertEquals(expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.ToElementString());
            assertEquals(expectedTypeToTokenList,
                         realElements(g.composite.typeToTokenList).ToElementString());

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

            bool ok =
                rawGenerateAndBuildRecognizer("M.g", master, "MParser", null, false);
            bool expecting = true; // should be ok

            assertEquals(expecting, ok);
        }
示例#43
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);
        }