Beispiel #1
0
        public ParseResult Run([NotNull] string code, [CanBeNull] string gold)
        {
            if (_parserHost == null)
            {
                _parserHost       = new ParserHost();
                _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules);
            }
            var source = new SourceSnapshot(code);

            if (StartRule == null)
            {
                return(null);
            }

            var timer = System.Diagnostics.Stopwatch.StartNew();

            try
            {
                var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule);
                this.Exception = null;
                this.TestTime  = timer.Elapsed;
                return(res);
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                this.TestTime  = timer.Elapsed;
                return(null);
            }
        }
        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]+", ""));
        }
        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());
        }
Beispiel #4
0
        public ParseResult Run([NotNull] string code, [CanBeNull] string gold, RecoveryStrategy recoveryStrategy)
        {
            if (_parserHost == null)
            {
                _parserHost       = new ParserHost();
                _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules);
            }
            var source = new SourceSnapshot(code);

            if (StartRule == null)
            {
                return(null);
            }

            try
            {
                var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, recoveryStrategy);
                this.Exception = null;
                return(res);
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                return(null);
            }
        }
        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);
        }
        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());
        }
        public void SetUp()
        {
            _theAddress = null;

            grammar = new CompositeGrammar();
            expression = new ObjectConstructionExpression<Address>(grammar);

            theStep = new Step();
        }
        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);
        }
        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);
        }
Beispiel #10
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);
        }
Beispiel #11
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);
        }
        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);
        }
Beispiel #13
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());
        }
        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);
        }
Beispiel #15
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]+", ""));
        }
Beispiel #16
0
        private static bool IsInsideToken(SCG.Dictionary <ParseRecord, bool> memoization, CompositeGrammar compositeGrammar, ParseRecord record)
        {
            bool res;

            if (memoization.TryGetValue(record, out res))
            {
                return(res);
            }

            if (record.Sequence.ParsingSequence.SequenceInfo is SequenceInfo.Ast)
            {
                var parser = record.Sequence.ParsingSequence.SequenceInfo.Parser;
                res = compositeGrammar.Tokens.ContainsKey(parser) || compositeGrammar.VoidTokens.ContainsKey(parser);
                memoization[record] = res;
                if (res)
                {
                    return(res);
                }
            }

            foreach (var caller in record.Sequence.Callers)
            {
                res = IsInsideToken(memoization, compositeGrammar, caller);
                if (res)
                {
                    memoization[record] = true;
                    return(true);
                }
            }

            memoization[record] = false;
            return(false);
        }
Beispiel #17
0
        public ParseResult Run([NotNull] string code, [CanBeNull] string gold, RecoveryStrategy recoveryStrategy)
        {
            if (_parserHost == null)
              {
            _parserHost = new ParserHost();
            _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules);
              }
              var source = new SourceSnapshot(code);

              if (StartRule == null)
            return null;

              try
              {
            var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, recoveryStrategy);
            this.Exception = null;
            return res;
              }
              catch (Exception ex)
              {
            this.Exception = ex;
            return null;
              }
        }
 public void SetUp()
 {
     grammar = new CompositeGrammar("an action");
     step = new Step("some grammar");
     TheAddress = null;
 }
Beispiel #19
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);
        }
Beispiel #20
0
 public GrammarRenderer(CompositeGrammar compositeGrammar)
 {
     _compositeGrammar = compositeGrammar;
 }
Beispiel #21
0
    private static bool IsInsideToken(SCG.Dictionary<ParseRecord, bool> memoization, CompositeGrammar compositeGrammar, ParseRecord record)
    {
      bool res;
      if (memoization.TryGetValue(record, out res))
        return res;

      if (record.Sequence.ParsingSequence.SequenceInfo is SequenceInfo.Ast)
      {
        var parser = record.Sequence.ParsingSequence.SequenceInfo.Parser;
        res = compositeGrammar.Tokens.ContainsKey(parser) || compositeGrammar.VoidTokens.ContainsKey(parser);
        memoization[record] = res;
        if(res)
          return res;
      }

      foreach (var caller in record.Sequence.Callers)
      {
        res = IsInsideToken(memoization, compositeGrammar, caller);
        if (res)
        {
          memoization[record] = true;
          return true;
        }
      }

      memoization[record] = false;
      return false;
    }
        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=4, B=5, C=6, M=7, S=8, T=9]";
            string expectedStringLiteralToTypeMap = "{}";
            string expectedTypeToTokenList        = "[A, B, C, 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);

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

            Assert.AreEqual(expecting, ok);
        }
Beispiel #23
0
 public GrammarRenderer(CompositeGrammar compositeGrammar)
 {
   _compositeGrammar = compositeGrammar;
 }
Beispiel #24
0
        public ParseResult Run([NotNull] string code, [CanBeNull] string gold)
        {
            if (_parserHost == null)
              {
            _parserHost = new ParserHost();
            _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules);
              }
              var source = new SourceSnapshot(code);

              if (StartRule == null)
            return null;

              var timer = System.Diagnostics.Stopwatch.StartNew();
              try
              {
            var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, parseToEndOfString: true);
            this.Exception = null;
            this.TestTime = timer.Elapsed;
            return res;
              }
              catch (Exception ex)
              {
            this.Exception = ex;
            this.TestTime = timer.Elapsed;
            return null;
              }
        }