Ejemplo n.º 1
0
        public FileParseResult ParseProtoFile(string content)
        {
            var lexer = new GrammarLexer(new AntlrInputStream(content));

            lexer.RemoveErrorListeners();
            var tokens = new CommonTokenStream(lexer);
            var parser = new GrammarParser(tokens);

            var tree   = parser.instructions();
            var result = _mainVisitor.Visit(tree);

            return((FileParseResult)result);
        }
Ejemplo n.º 2
0
        public void TestGrammarError(String path, ErrorCode expectedError)
        {
            StreamReader pom = new System.IO.StreamReader(path);

            ErrorHandler handler = new ErrorHandler();


            AntlrInputStream inputStream = new AntlrInputStream(pom);
            GrammarLexer     lexer       = new GrammarLexer(inputStream);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new GrammarErrorListener(handler));
            CommonTokenStream c           = new CommonTokenStream(lexer);
            GrammarParser     helloParser = new GrammarParser(c);

            helloParser.RemoveErrorListeners();
            helloParser.AddErrorListener(new GrammarErrorListener(handler));

            try
            {
                IParseTree tree = helloParser.start();
                pom.Close();

                if (handler.errorsOccured())
                {
                    List <Error> grammarErrors = handler.GrammarErrors;

                    Assert.AreEqual(1, grammarErrors.Count, "More errors occured than expected");
                    Assert.AreEqual(ErrorCode.grammarError, grammarErrors[0].ErrorCode, "Invalid error type.");
                }
                else
                {
                    Assert.Fail("No grammar error occured");
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 3
0
        public void TestOutputFromFile(String path, String output)
        {
            StreamReader pom = new System.IO.StreamReader(path);

            ErrorHandler handler = new ErrorHandler();


            AntlrInputStream inputStream = new AntlrInputStream(pom);
            GrammarLexer     lexer       = new GrammarLexer(inputStream);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new GrammarErrorListener(handler));
            CommonTokenStream c           = new CommonTokenStream(lexer);
            GrammarParser     helloParser = new GrammarParser(c);

            helloParser.RemoveErrorListeners();
            helloParser.AddErrorListener(new GrammarErrorListener(handler));

            String path_file_ins = "insc1.txt";

            try
            {
                IParseTree tree = helloParser.start();
                pom.Close();

                if (handler.errorsOccured())
                {
                    Assert.Fail("Errors detected during lexical or syntactic analysis.");
                }

                Visitor visitor = new Visitor(handler);
                visitor.PrepareLibraryFunctions();
                visitor.DoInitialJmp();
                int t = visitor.Visit(tree);

                if (handler.errorsOccured())
                {
                    Assert.Fail("Errors detected during semantic analysis.");
                }

                visitor.numberInstructions();

                Program.WriteInstructions(visitor.GetInstructions(), path_file_ins);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }

            Process interpreter = new Process();

            interpreter.StartInfo.FileName               = "../../../refint_pl0_ext.exe";
            interpreter.StartInfo.Arguments              = path_file_ins + " -s -l";
            interpreter.StartInfo.UseShellExecute        = false;
            interpreter.StartInfo.RedirectStandardOutput = true;

            interpreter.Start();

            StreamReader reader = new StreamReader(interpreter.StandardOutput.BaseStream);

            if (!interpreter.WaitForExit(15000))
            {
                Assert.IsFalse(true, "Process might got stuck in infinite loop");
                interpreter.Kill();
            }


            String output_from_interpret = reader.ReadToEnd();

            Assert.AreEqual("START PL/0\r\n" + output + " END PL/0\r\n", output_from_interpret);
        }