コード例 #1
0
        public void TestSameNameTwoStrings() /*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 { A='x'; }\n" +
                "y : A {System.out.println(\"T.y\");} ;\n";

            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, T__6=6, WS=5]";
            string expectedStringLiteralToTypeMap = "{'a'=4, 'x'=6}";
            string expectedTypeToTokenList        = "[A, WS, T__6]";

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

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

            checkGrammarSemanticsError(equeue, expectedMessage);

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

            string expectedError =
                "error(159): T.g:2:10: cannot alias A='x'; token name already assigned to 'a'";

            assertEquals(expectedError, equeue.errors[0].ToString());
        }
コード例 #2
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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void TestSyntaxErrorsInImportsNotThrownOut2() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

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

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

            string master =
                "grammar M;\n" +
                "import S;\n" +
                "s : x ;\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();

            // whole bunch of errors from bad S.g file
            assertEquals("unexpected errors: " + equeue, 3, equeue.errors.Count);
        }
コード例 #5
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);
        }
コード例 #6
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());
        }
コード例 #7
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);
        }
コード例 #8
0
        public void TestInvalidImportMechanism() /*throws Exception*/
        {
            // M.Tokens has nothing to predict tokens from S.  Should
            // not include S.Tokens alt in this case?
            string slave =
                "lexer grammar S;\n" +
                "A : 'a' {System.out.println(\"S.A\");} ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);
            string master =
                "tree grammar M;\n" +
                "import S;\n" +
                "a : A ;";

            writeFile(tmpdir, "M.g", master);

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            AntlrTool        antlr     = newTool(new string[] { "-lib", tmpdir });
            CompositeGrammar composite = new CompositeGrammar();
            Grammar          g         = new Grammar(antlr, System.IO.Path.Combine(tmpdir, "M.g"), composite);

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

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

            string expectedError =
                "error(161): " + tmpdir.ToString().replaceFirst("\\-[0-9]+", "") + "\\M.g:2:8: tree grammar M cannot import lexer grammar S";

            assertEquals(expectedError, equeue.errors[0].ToString().replaceFirst("\\-[0-9]+", ""));
        }
コード例 #9
0
        public void TestSameStringTwoNames() /*throws Exception*/
        {
            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=5, X=6]";
            string expectedStringLiteralToTypeMap = "{'a'=4}";
            string expectedTypeToTokenList        = "[A, WS, X]";

            Assert.AreEqual(expectedTokenIDToTypeMap,
                            realElements(g.composite.TokenIDToTypeMap).ToElementString());
            Assert.AreEqual(expectedStringLiteralToTypeMap, g.composite.StringLiteralToTypeMap.ToElementString());
            Assert.AreEqual(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);

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

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

            Assert.AreEqual(expectedError, equeue.errors[0].ToString());
        }
コード例 #10
0
 protected internal virtual void GenRecognizerHeaderFile(AntlrTool tool,
                                                         CodeGenerator generator,
                                                         Grammar grammar,
                                                         StringTemplate headerFileST,
                                                         string extName) // e.g., ".h"
 {
     // no header file by default
 }
コード例 #11
0
        public BuildDependencyGenerator(Tool tool, string grammarFileName)
        {
            this.tool            = tool;
            this.grammarFileName = grammarFileName;
            grammar = tool.GetRootGrammar(grammarFileName);
            string language = (string)grammar.GetOption("language");

            generator = new CodeGenerator(tool, grammar, language);
            generator.LoadTemplates(language);
        }
コード例 #12
0
        protected internal virtual void GenRecognizerFile(AntlrTool tool,
                                                          CodeGenerator generator,
                                                          Grammar grammar,
                                                          StringTemplate outputFileST)
        {
            string fileName =
                generator.GetRecognizerFileName(grammar.name, grammar.type);

            generator.Write(outputFileST, fileName);
        }
コード例 #13
0
        public void Test3LevelImport() /*throws Exception*/
        {
            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);
        }
コード例 #14
0
        public void TestDelegatesSeeSameTokenType2() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

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

            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);
            string slave2 =
                "parser grammar T;\n" +
                "tokens { C; B; A; }\n" + // reverse order
                "y : A {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" + // matches AA, which should be "aa"
                "B : 'b' ;\n" + // another order: B, A, C
                "A : 'a' ;\n" +
                "C : 'c' ;\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, B=5, C=6, WS=7]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList        = "[A, B, C, WS]";

            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);
        }
コード例 #15
0
        /** I *think* this will allow Tool subclasses to exit gracefully
         *  for GUIs etc...
         */
        public static void Panic()
        {
            Tool tool = _tool;

            if (tool == null)
            {
                // no tool registered, exit
                throw new Exception("ANTLR ErrorManager panic");
            }
            else
            {
                tool.Panic();
            }
        }
コード例 #16
0
        /** I *think* this will allow Tool subclasses to exit gracefully
         *  for GUIs etc...
         */
        public static void Panic()
        {
            Tool tool = (Tool)threadToToolMap.get(Thread.CurrentThread);

            if (tool == null)
            {
                // no tool registered, exit
                throw new Exception("ANTLR ErrorManager panic");
            }
            else
            {
                tool.Panic();
            }
        }
コード例 #17
0
        public void TestLexerDelegatorRuleOverridesDelegateLeavingNoRules() /*throws Exception*/
        {
            // M.Tokens has nothing to predict tokens from S.  Should
            // not include S.Tokens alt in this case?
            string slave =
                "lexer grammar S;\n" +
                "A : 'a' {System.out.println(\"S.A\");} ;\n";

            mkdir(tmpdir);
            writeFile(tmpdir, "S.g", slave);
            string master =
                "lexer grammar M;\n" +
                "import S;\n" +
                "A : 'a' {System.out.println(\"M.A\");} ;\n" +
                "WS : (' '|'\\n') {skip();} ;\n";

            writeFile(tmpdir, "M.g", master);

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            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();
            composite.AssignTokenTypes();
            composite.DefineGrammarSymbols();
            composite.CreateNFAs();
            g.CreateLookaheadDFAs(false);

            // predict only alts from M not S
            string expectingDFA =
                ".s0-'a'->.s1\n" +
                ".s0-{'\\n', ' '}->:s3=>2\n" +
                ".s1-<EOT>->:s2=>1\n";

            Antlr3.Analysis.DFA dfa        = g.GetLookaheadDFA(1);
            FASerializer        serializer = new FASerializer(g);
            string result = serializer.Serialize(dfa.startState);

            assertEquals(expectingDFA, result);

            // must not be a "unreachable alt: Tokens" error
            assertEquals("unexpected errors: " + equeue, 0, equeue.errors.Count);
        }
コード例 #18
0
        public void TestImportedTokenVocabWorksInRoot() /*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 tokens =
                "A=99\n";

            writeFile(tmpdir, "Test.tokens", tokens);

            string master =
                "grammar M;\n" +
                "options {tokenVocab=Test;}\n" +
                "import S;\n" +
                "s : x ;\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=99, WS=101]";
            string expectedStringLiteralToTypeMap = "{'a'=100}";
            string expectedTypeToTokenList        = "[A, 'a', WS]";

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

            assertEquals("unexpected errors: " + equeue, 0, equeue.errors.Count);
        }
コード例 #19
0
        public void TestRulesVisibleThroughMultilevelImport() /*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);
            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 x ;\n"; // x MUST BE VISIBLE TO M

            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=4, S=5, T=6]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList        = "[M, S, T]";

            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);
        }
コード例 #20
0
        protected void checkDecision(Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts)
        //throws Exception
        {
            Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
            // mimic actions of org.antlr.Tool first time for grammar g
            if (g.CodeGenerator == null)
            {
                CodeGenerator generator = new CodeGenerator(tool, g, "Java");
                g.CodeGenerator = generator;
                g.BuildNFA();
                g.CreateLookaheadDFAs(false);
            }

            DFA dfa = g.GetLookaheadDFA(decision);

            assertNotNull("unknown decision #" + decision, dfa);
            FASerializer serializer = new FASerializer(g);
            string       result     = serializer.Serialize(dfa.startState);
            //System.out.print(result);
            var nonDetAlts = dfa.UnreachableAlts;

            //System.out.println("alts w/o predict state="+nonDetAlts);

            // first make sure nondeterministic alts are as expected
            if (expectingUnreachableAlts == null)
            {
                if (nonDetAlts != null && nonDetAlts.Count != 0)
                {
                    Console.Error.WriteLine("nondeterministic alts (should be empty): " + ((IList)nonDetAlts).ToElementString());
                }
                assertEquals("unreachable alts mismatch", 0, nonDetAlts != null ? nonDetAlts.Count : 0);
            }
            else
            {
                for (int i = 0; i < expectingUnreachableAlts.Length; i++)
                {
                    assertTrue("unreachable alts mismatch",
                               nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false);
                }
            }
            assertEquals(expecting, result);
        }
コード例 #21
0
        public void TestBadGrammarOption() /*throws Exception*/
        {
            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);
        }
コード例 #22
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);
        }
コード例 #23
0
        public void TestImportedTokenVocabIgnoredWithWarning() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

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

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

            string master =
                "grammar M;\n" +
                "import S;\n" +
                "s : x ;\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();

            object expectedArg   = "S";
            int    expectedMsgID = ErrorManager.MSG_TOKEN_VOCAB_IN_DELEGATE;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);

            checkGrammarSemanticsWarning(equeue, expectedMessage);

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

            string expectedError =
                "warning(160): S.g:2:10: tokenVocab option ignored in imported grammar S";

            assertEquals(expectedError, equeue.warnings[0].ToString());
        }
コード例 #24
0
ファイル: TestLexer.cs プロジェクト: guojianbin/antlrcs
        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);
        }
コード例 #25
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);
        }
コード例 #26
0
ファイル: TestTemplates.cs プロジェクト: zhangzhouzhe/antlrcs
        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);
        }
コード例 #27
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);
        }
コード例 #28
0
ファイル: TestTemplates.cs プロジェクト: zhangzhouzhe/antlrcs
        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);
        }
コード例 #29
0
        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);
        }
コード例 #30
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();

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

            assertEquals("unexpected errors: " + equeue, expectedError, equeue.errors[0].ToString().replaceFirst("\\-[0-9]+", ""));
        }
コード例 #31
0
ファイル: Grammar.cs プロジェクト: bszafko/antlrcs
 /** Used for testing and Interp/RandomPhrase.  Only useful on
  *  noncomposite grammars.
  */
 public Grammar( Tool tool, string grammarString )
     : this()
 {
     Tool = tool;
     FileName = "<string>";
     StringReader r = new StringReader( grammarString );
     ParseAndBuildAST( r );
     composite.AssignTokenTypes();
     DefineGrammarSymbols();
     CheckNameSpaceAndActions();
 }
コード例 #32
0
ファイル: Target.cs プロジェクト: bszafko/antlrcs
 protected internal virtual void GenRecognizerFile( AntlrTool tool,
                                  CodeGenerator generator,
                                  Grammar grammar,
                                  StringTemplate outputFileST )
 {
     string fileName =
         generator.GetRecognizerFileName( grammar.name, grammar.type );
     generator.Write( outputFileST, fileName );
 }
コード例 #33
0
ファイル: Target.cs プロジェクト: bszafko/antlrcs
 // e.g., ".h"
 protected internal virtual void GenRecognizerHeaderFile( AntlrTool tool,
                                        CodeGenerator generator,
                                        Grammar grammar,
                                        StringTemplate headerFileST,
                                        string extName )
 {
     // no header file by default
 }
コード例 #34
0
ファイル: CodeGenerator.cs プロジェクト: bszafko/antlrcs
        public CodeGenerator( AntlrTool tool, Grammar grammar, string language )
        {
            this.tool = tool;
            this.grammar = grammar;
            this.language = language;

            acyclicDFAGenerator = new ACyclicDFACodeGenerator( this );

            LoadLanguageTarget( language );
        }
コード例 #35
0
ファイル: Grammar.cs プロジェクト: jamilgeor/antlrcs
        /** Create a grammar from file name.  */
        public Grammar( Tool tool, string fileName, CompositeGrammar composite )
        {
            nameSpaceChecker = new NameSpaceChecker( this );
            ll1Analyzer = new LL1Analyzer( this );
            sanity = new GrammarSanity( this );

            this.composite = composite;
            Tool = tool;
            FileName = fileName;
            // ensure we have the composite set to something
            if ( composite.DelegateGrammarTreeRoot == null )
            {
                composite.SetDelegationRoot( this );
            }
            else
            {
                defaultRuleModifier = composite.DelegateGrammarTreeRoot.Grammar.DefaultRuleModifier;
            }

            TemplateGroup lexerGrammarTemplateGroup = new TemplateGroupString(lexerGrammarTemplateText);
            _lexerGrammarTemplate = lexerGrammarTemplateGroup.GetInstanceOf("grammar");
            target = CodeGenerator.LoadLanguageTarget((string)GetOption("language"), tool.TargetsDirectory);
        }
コード例 #36
0
ファイル: Grammar.cs プロジェクト: jamilgeor/antlrcs
        /** Useful for when you are sure that you are not part of a composite
         *  already.  Used in Interp/RandomPhrase and testing.
         */
        public Grammar(Tool tool)
        {
            nameSpaceChecker = new NameSpaceChecker( this );
            ll1Analyzer = new LL1Analyzer( this );
            sanity = new GrammarSanity( this );

            builtFromString = true;
            composite = new CompositeGrammar( this );
            Tool = tool;

            TemplateGroup lexerGrammarTemplateGroup = new TemplateGroupString(lexerGrammarTemplateText);
            _lexerGrammarTemplate = lexerGrammarTemplateGroup.GetInstanceOf("grammar");
            string targetsDirectory = Path.Combine(AntlrTool.ToolPathRoot, "Targets");
            target = CodeGenerator.LoadLanguageTarget((string)GetOption("language"), targetsDirectory);
        }
コード例 #37
0
ファイル: Grammar.cs プロジェクト: jamilgeor/antlrcs
 /** Used for testing and Interp/RandomPhrase.  Only useful on
  *  noncomposite grammars.
  */
 public Grammar( Tool tool, string grammarString )
     : this(tool)
 {
     Tool = tool;
     FileName = "<string>";
     StringReader r = new StringReader( grammarString );
     ParseAndBuildAST( r );
     composite.AssignTokenTypes();
     //composite.TranslateLeftRecursiveRules();
     AddRulesForSyntacticPredicates();
     composite.DefineGrammarSymbols();
     //composite.CreateNFAs();
     CheckNameSpaceAndActions();
 }
コード例 #38
0
        //throws Exception
        protected void checkDecision( Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts )
        {
            Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
            // mimic actions of org.antlr.Tool first time for grammar g
            if ( g.CodeGenerator == null )
            {
                CodeGenerator generator = new CodeGenerator( tool, g, "Java" );
                g.CodeGenerator = generator;
                g.BuildNFA();
                g.CreateLookaheadDFAs( false );
            }

            DFA dfa = g.GetLookaheadDFA( decision );
            Assert.IsNotNull(dfa, "unknown decision #" + decision);
            FASerializer serializer = new FASerializer( g );
            string result = serializer.Serialize( dfa.StartState );
            //System.out.print(result);
            var nonDetAlts = dfa.UnreachableAlts;
            //System.out.println("alts w/o predict state="+nonDetAlts);

            // first make sure nondeterministic alts are as expected
            if ( expectingUnreachableAlts == null )
            {
                if ( nonDetAlts != null && nonDetAlts.Count != 0 )
                {
                    Console.Error.WriteLine( "nondeterministic alts (should be empty): " + ( (IList)nonDetAlts ).ToElementString() );
                }
                Assert.AreEqual(0, nonDetAlts != null ? nonDetAlts.Count : 0, "unreachable alts mismatch");
            }
            else
            {
                for ( int i = 0; i < expectingUnreachableAlts.Length; i++ )
                {
                    Assert.IsTrue(nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false, "unreachable alts mismatch");
                }
            }
            Assert.AreEqual( expecting, result );
        }
コード例 #39
0
 public BuildDependencyGenerator( Tool tool, string grammarFileName )
 {
     this.tool = tool;
     this.grammarFileName = grammarFileName;
     grammar = tool.GetRootGrammar( grammarFileName );
     string language = (string)grammar.GetOption( "language" );
     generator = new CodeGenerator( tool, grammar, language );
     generator.LoadTemplates( language );
 }
コード例 #40
0
ファイル: Grammar.cs プロジェクト: bszafko/antlrcs
        /** Create a grammar from file name.  */
        public Grammar( Tool tool, string fileName, CompositeGrammar composite )
        {
            nameSpaceChecker = new NameSpaceChecker( this );
            ll1Analyzer = new LL1Analyzer( this );
            sanity = new GrammarSanity( this );

            this.composite = composite;
            Tool = tool;
            FileName = fileName;
            // ensure we have the composite set to something
            if ( composite.delegateGrammarTreeRoot == null )
            {
                composite.SetDelegationRoot( this );
            }
            else
            {
                defaultRuleModifier = composite.delegateGrammarTreeRoot.grammar.DefaultRuleModifier;
            }
        }
コード例 #41
0
ファイル: ErrorManager.cs プロジェクト: JSchofield/antlrcs
 public static void SetTool( Tool tool )
 {
     threadToToolMap[Thread.CurrentThread] = tool;
 }
コード例 #42
0
ファイル: ErrorManager.cs プロジェクト: mahanteshck/antlrcs
 public static void SetTool( Tool tool )
 {
     _tool = tool;
 }