Пример #1
0
        private static ASTNode ParseInput(string inputPath)
        {
            var input = new AntlrFileStream(inputPath);
            var lexer = new CoolLexer(input);


            var errors = new List <string>();

            lexer.RemoveErrorListeners();
            //lexer.AddErrorListener(new LexerErrorListener(errors));

            var tokens = new CommonTokenStream(lexer);

            var parser = new CoolParser(tokens);

            //parser.RemoveErrorListeners();
            //parser.AddErrorListener(new ParserErrorListener(errors));

            IParseTree tree = parser.program();

            if (errors.Any())
            {
                Console.WriteLine();
                foreach (var item in errors)
                {
                    Console.WriteLine(item);
                }
                return(null);
            }

            var     astBuilder = new ASTGenerator();
            ASTNode ast        = astBuilder.Visit(tree);

            return(ast);
        }
Пример #2
0
        public static CommonTokenStream Tokens(string inputPath)
        {
            var code  = new AntlrFileStream(inputPath);
            var lexer = new CoolLexer(code);

            var errors = new List <string>();

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener(errors));

            if (errors.Any())
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                foreach (var item in errors)
                {
                    Console.WriteLine(item);
                }
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine();
                return(null);
            }
            var tokens = new CommonTokenStream(lexer);

            return(tokens);
        }
Пример #3
0
        public List <string> ParsingFile(string file)
        {
            var input  = new AntlrFileStream(file);
            var lexer  = new CoolLexer(input);
            var errors = new List <string>();

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener(errors));

            tokens = new CommonTokenStream(lexer);
            parser = new CoolParser(tokens);

            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ParserErrorListener(errors));

            tree = parser.program();

            return(errors);
        }