Beispiel #1
0
        public static void Parse(string src, Action <sphereScript99Parser> parserAction)
        {
            AntlrInputStream      inputStream   = new AntlrInputStream(src);
            sphereScript99Lexer   lexer         = new sphereScript99Lexer(inputStream);
            CommonTokenStream     tokenStream   = new CommonTokenStream(lexer);
            sphereScript99Parser  parser        = new sphereScript99Parser(tokenStream);
            FailTestErrorListener errorListener = new FailTestErrorListener();

            parser.AddErrorListener(errorListener);

            parserAction(parser);
        }
Beispiel #2
0
        private ParsingResult <T> Parse <T>(string src, Func <sphereScript99Parser, T> parseAction)
            where T : IParseTree
        {
            var inputStream   = new AntlrInputStream(src);
            var lexer         = new sphereScript99Lexer(inputStream);
            var tokenStream   = new CommonTokenStream(lexer);
            var parser        = new sphereScript99Parser(tokenStream);
            var errorListener = new MemoryErrorListener();

            parser.AddErrorListener(errorListener);

            var result = parseAction(parser);

            return(new ParsingResult <T>(result, errorListener.Errors));
        }
Beispiel #3
0
        protected void Parse(string src, Action <sphereScript99Parser> parserAction)
        {
            try
            {
                AntlrInputStream inputStream = new AntlrInputStream(src);
                var lexer         = new sphereScript99Lexer(inputStream);
                var tokenStream   = new CommonTokenStream(lexer);
                var parser        = new sphereScript99Parser(tokenStream);
                var errorListener = new FailTestErrorListener();
                parser.AddErrorListener(errorListener);

                parserAction(parser);

                if (parser.InputStream.Index + 1 < parser.InputStream.Size)
                {
                    Assert.Fail($"Input stream not fully parsed index: {parser.InputStream.Index}, size: {parser.InputStream.Size}");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Testing '{src.Substring(0, Math.Min(src.Length, 40))}'\n\nMessage: {ex.Message}\n\n{ex}");
            }
        }
Beispiel #4
0
        private void RoundtripFile(string inputFileName, string outputFileName)
        {
            if (inputFileName.Equals(outputFileName, StringComparison.OrdinalIgnoreCase))
            {
                throw new CommandLineException("Cannot parse and write to the same file.");
            }

            var inputStream = new AntlrInputStream(File.OpenRead(inputFileName));
            var lexer       = new sphereScript99Lexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new sphereScript99Parser(tokenStream);

            parser.AddErrorListener(new ConsoleErrorListener());

            System.Console.WriteLine($"Parsing {inputFileName}");
            var file = parser.file();

            System.Console.WriteLine($"Writing {outputFileName}");
            var generator = new RoundtripGenerator();

            generator.Visit(file);

            File.WriteAllText(outputFileName, generator.Output);
        }