Exemplo n.º 1
0
        //[Test]
        public void BigFile2()
        {
            Console.WriteLine("Starting BigFile");
            var code  = LoadTestCodeRaw();
            var lexer = new MalinaLexer(new AntlrInputStream(code));
            //lexer.RemoveErrorListeners();
            //lexer.ErrorListeners.Clear();
            var    t1    = Environment.TickCount;
            var    i     = 0;
            IToken token = null;

            for (token = lexer.NextToken(); token.Type != -1; token = lexer.NextToken())
            {
                i++;
            }
            var t2 = Environment.TickCount;

            Console.WriteLine("GetAllTokens Time: {0}", t2 - t1);
            Console.WriteLine("Token Number: {0}", i);
            Assert.Less(t2 - t1, 7000);

            lexer.Reset();
            var parser = MalinaParser.Create(new CommonTokenStream(lexer));

            parser.Interpreter.PredictionMode = PredictionMode.Sll;
            var malinaListener = new MalinaParserListener();

            //var parserErrorListener = new ErrorListener<IToken>();
            //parser.AddErrorListener(parserErrorListener);
            //parser.AddParseListener(malinaListener);
            parser.BuildParseTree = false;
            t1 = Environment.TickCount;
            //var module = parser.module();
            t2 = Environment.TickCount;

            Console.WriteLine("Parse Time: {0}", t2 - t1);
            Assert.Less(t2 - t1, 15000);
            //Assert.IsFalse(parserErrorListener.HasErrors);


            lexer.Reset();
            parser.Reset();
            parser.AddParseListener(malinaListener);
            t1 = Environment.TickCount;
            parser.module();
            lexer.Reset();
            parser.Reset();

            t2 = Environment.TickCount;
            Console.WriteLine("DOM Time: {0}", t2 - t1);
            Assert.Less(t2 - t1, 20000);
            //Assert.IsFalse(parserErrorListener.HasErrors);

            //t1 = Environment.TickCount;
            //var visitor = new DOMPrinterVisitor();
            //visitor.Visit(malinaListener.CompileUnit);
            //t2 = Environment.TickCount;
            //Console.WriteLine("Visitor Time: {0}", t2 - t1);
        }
Exemplo n.º 2
0
        public static void PerformTest()
        {
            var code = LoadTestCode();

            PrintCode(code);

            //Testing Lexer
            MalinaLexer         lexer;
            ErrorListener <int> lexerErrors;
            IList <IToken>      tokens = GetTokens(code, out lexer, out lexerErrors);

            var printedTokens = PrintedTokens(tokens);

            Console.WriteLine("");
            Console.WriteLine("Tokens:");
            Console.WriteLine(printedTokens);


            string recorded = LoadRecordedTest(printedTokens);
            string serialLexerErrors;
            var    recordedLexerErros = LoadLexerErrors(lexerErrors, out serialLexerErrors);

            if (recordedLexerErros != null)
            {
                Console.WriteLine("Lexer Errors:");
                Console.WriteLine(recordedLexerErros);
            }

            if (lexerErrors.HasErrors)
            {
                PrintErrors(lexerErrors.Errors, "Lexer Errors: ");
            }

            //Testing Parse Tree
            lexer.Reset();
            lexer.ErrorListeners.Clear();
            var parser = MalinaParser.Create(new CommonTokenStream(lexer));

            parser.Interpreter.PredictionMode = PredictionMode.Sll;
            var malinaListener      = new MalinaParserListener();
            var parserErrorListener = new ErrorListener <IToken>();

            parser.AddErrorListener(parserErrorListener);
            parser.AddParseListener(malinaListener);

            //parser.AddErrorListener(new DiagnosticErrorListener());
            //parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;

            var module = parser.module();

            int nCount = 0;
            int tCount = 0;

            Console.WriteLine();
            Console.WriteLine("ParseTree:");
            Console.WriteLine();

            var sb = new StringBuilder();

            PrintTree(module, 0, sb, ref nCount, ref tCount);
            var parseTree = sb.ToString();

            var    isParseTreeRecordedTest = IsParseTreeRecordedTest();
            var    isParseTreeRecordTest   = IsParseTreeRecordTest(); //Overwrites existing recording
            string recordedParseTree       = null;

            if (isParseTreeRecordedTest || isParseTreeRecordTest)
            {
                if (isParseTreeRecordedTest)
                {
                    recordedParseTree = LoadRecordedParseTreeTest();
                }
                if (recordedParseTree == null || isParseTreeRecordTest)
                {
                    SaveRecordedParseTreeTest(parseTree);
                }
            }

            //Parser Errors
            string serialParserErrors;
            var    recordedParserErros = LoadParserErrors(parserErrorListener, out serialParserErrors);

            if (recordedParserErros != null)
            {
                Console.WriteLine("Parser Errors:");
                Console.WriteLine(recordedParserErros);
            }

            Console.WriteLine(parseTree);

            Console.WriteLine();


            //Testing DOM generation
            Console.WriteLine("DOM:");
            Console.WriteLine();

            var printerVisitor = new DOMPrinterVisitor();

            printerVisitor.Visit(malinaListener.CompileUnit);


            Console.WriteLine(printerVisitor.Text);

            var    isDomRecordedTest = IsDomRecordedTest();
            var    isDomRecordTest   = IsDomRecordTest(); //Overwrites existing recording
            string recordedDom       = null;

            if (isDomRecordedTest || isDomRecordTest)
            {
                if (isDomRecordedTest)
                {
                    recordedDom = LoadRecordedDomTest();
                }
                if (recordedDom == null || isDomRecordTest)
                {
                    SaveRecordedDomTest(printerVisitor.Text);
                }
            }

            PrintCode(code);

            //LEXER Assertions
            if (recordedLexerErros != null)
            {
                Assert.AreEqual(recordedLexerErros, serialLexerErrors);
            }
            else
            {
                Assert.AreEqual(false, lexerErrors.HasErrors, "LexerErrorListener has errors");
            }


            Assert.AreEqual(0, lexer.InvalidTokens.Count);

            if (recorded != null)
            {
                Assert.AreEqual(recorded, printedTokens, "LEXER assertion failed");
            }

            //Parse Tree Assertions
            if (recordedParseTree != null)
            {
                Assert.AreEqual(recordedParseTree, parseTree, "PARSE TREE assertion failed");
            }

            if (recordedParserErros != null)
            {
                Assert.AreEqual(recordedParserErros, serialParserErrors);
            }
            else
            {
                if (parserErrorListener.HasErrors)
                {
                    PrintErrors(parserErrorListener.Errors, "Parser Errors:");
                }

                Assert.AreEqual(false, parserErrorListener.HasErrors, "ParserErrorListener has errors");
            }


            //DOM Assertions
            if (recordedDom != null)
            {
                Assert.AreEqual(recordedDom, printerVisitor.Text.Replace("\r\n", "\n"), "DOM assertion failed");
            }
        }