/// <summary>
        /// Parses the given VBA expression.
        /// </summary>
        /// <param name="expression">The expression to parse. NOTE: Call statements are not supported.</param>
        /// <returns>The root of the parse tree.</returns>
        public ParserRuleContext Parse(string expression)
        {
            var stream = new AntlrInputStream(expression);
            var lexer  = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            ParserRuleContext tree = null;

            try
            {
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                tree = parser.expression();
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, "SLL mode failed for {0}. Retrying using LL.", expression);
                tokens.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                tree = parser.expression();
            }
            return(tree);
        }
        public IParseTree Parse(string moduleName, string moduleCode, IParseTreeListener[] listeners, out ITokenStream outStream)
        {
            var stream = new AntlrInputStream(moduleCode);
            var lexer  = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            ParserRuleContext tree = null;

            try
            {
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                tree = parser.startRule();
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, "SLL mode failed in module {0}. Retrying using LL.", moduleName);
                tokens.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                tree = parser.startRule();
            }
            foreach (var listener in listeners)
            {
                ParseTreeWalker.Default.Walk(listener, tree);
            }
            outStream = tokens;
            return(tree);
        }
Beispiel #3
0
        public IParseTree Parse(string moduleName, CommonTokenStream moduleTokens, IParseTreeListener[] listeners, BaseErrorListener errorListener, out ITokenStream outStream)
        {
            moduleTokens.Reset();
            var parser = new VBAParser(moduleTokens);

            parser.AddErrorListener(errorListener);
            ParserRuleContext tree;

            try
            {
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                tree = parser.startRule();
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, "SLL mode failed in module {0}. Retrying using LL.", moduleName);
                moduleTokens.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                tree = parser.startRule();
            }
            foreach (var listener in listeners)
            {
                ParseTreeWalker.Default.Walk(listener, tree);
            }
            outStream = moduleTokens;
            return(tree);
        }
        protected override IParseTree Parse(ITokenStream tokenStream, PredictionMode predictionMode, IParserErrorListener errorListener)
        {
            var parser = new VBAParser(tokenStream);

            parser.Interpreter.PredictionMode = predictionMode;
            parser.AddErrorListener(errorListener);
            return(parser.startRule());
        }
Beispiel #5
0
        private IParseTree Parse(string code, out TokenStreamRewriter outRewriter)
        {
            var input  = new AntlrInputStream(code);
            var lexer  = new VBALexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            outRewriter = new TokenStreamRewriter(tokens);

            var result = parser.startRule();

            return(result);
        }
        private Tuple <VBAParser, ParserRuleContext> Parse(string code)
        {
            var stream = new AntlrInputStream(code);
            var lexer  = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            // Don't remove this line otherwise we won't get notified of parser failures.
            parser.AddErrorListener(new ExceptionErrorListener());
            // If SLL fails we want to get notified ASAP so we can fix it, that's why we don't retry using LL.
            parser.Interpreter.PredictionMode = PredictionMode.Sll;
            var tree = parser.startRule();

            return(Tuple.Create <VBAParser, ParserRuleContext>(parser, tree));
        }
        private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream)
        {
            var stream = new AntlrInputStream(code);
            var lexer  = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            foreach (var l in listeners)
            {
                parser.AddParseListener(l);
            }

            outStream = tokens;
            return(parser.startRule());
        }
Beispiel #8
0
        public (IParseTree tree, ITokenStream tokenStream) Parse(string moduleName, CommonTokenStream moduleTokens, IParseTreeListener[] listeners, BaseErrorListener errorListener)
        {
            moduleTokens.Reset();
            var parser = new VBAParser(moduleTokens);

            parser.AddErrorListener(errorListener);
            ParserRuleContext tree;

            try
            {
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                tree = parser.startRule();
            }
            catch (ParsePassSyntaxErrorException syntaxErrorException)
            {
                var parsePassText = syntaxErrorException.ParsePass == ParsePass.CodePanePass
                    ? "code pane"
                    : "exported";
                Logger.Warn($"SLL mode failed while parsing the {parsePassText} version of module {moduleName} at symbol {syntaxErrorException.OffendingSymbol.Text} at L{syntaxErrorException.LineNumber}C{syntaxErrorException.Position}. Retrying using LL.");
                Logger.Debug(syntaxErrorException, "SLL mode exception");
                moduleTokens.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                tree = parser.startRule();
            }
            catch (Exception exception)
            {
                Logger.Warn($"SLL mode failed while parsing module {moduleName}. Retrying using LL.");
                Logger.Debug(exception, "SLL mode exception");
                moduleTokens.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                tree = parser.startRule();
            }
            foreach (var listener in listeners)
            {
                ParseTreeWalker.Default.Walk(listener, tree);
            }
            return(tree, moduleTokens);
        }
        public IParseTree Parse(string code, out ITokenStream outStream)
        {
            var input = new AntlrInputStream(code);
            var lexer = new VBALexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);
            parser.AddErrorListener(new ExceptionErrorListener());
            outStream = tokens;

            var result = parser.startRule();
            return result;
        }
        private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream)
        {
            var stream = new AntlrInputStream(code);
            var lexer = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            foreach (var l in listeners)
            {
                parser.AddParseListener(l);
            }

            outStream = tokens;
            return parser.startRule();
        }