public ParserRuleContext Parse()
        {
            ErrorHandler = new BailErrorStrategy();
            Interpreter.PredictionMode = PredictionMode.Sll;
            var document = parse();

            return(document);
        }
Ejemplo n.º 2
0
        private int parseQuery(string query)
        {
            var t = string.Empty;
            IMySQLRecognizerCommon mySQLRecognizerCommon = new MySQLRecognizerCommon(serverVersion, sqlMode);
            var lastErrors    = new StringBuilder();
            var errorListener = new TestErrorListener(lastErrors);
            var errorStrategy = new BailErrorStrategy();
            var input         = new AntlrInputStream(query);
            var lexer         = new MySQLLexer(input, mySQLRecognizerCommon);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(errorListener);

            lexer.serverVersion = serverVersion;
            lexer.sqlMode       = sqlMode;
            lexer.charsets      = charsets;
            var tokens = new CommonTokenStream(lexer);

            var parser = new MySQLParser(tokens, mySQLRecognizerCommon);

            parser.serverVersion  = serverVersion;
            parser.sqlMode        = sqlMode;
            parser.BuildParseTree = buildParserTree;

            parser.ErrorHandler = errorStrategy;
            parser.Interpreter.PredictionMode = PredictionMode.SLL;
            parser.RemoveParseListeners();

            tokens.Fill();

            ParserRuleContext tree = default;

            try
            {
                tree = parser.query();
            }
            catch (Antlr4.Runtime.Misc.ParseCanceledException)
            {
                // If parsing was cancelled we either really have a syntax error or we need to do a second step,
                // now with the default strategy and LL parsing.
                tokens.Reset();
                parser.Reset();
                parser.ErrorHandler = new DefaultErrorStrategy();
                parser.Interpreter.PredictionMode = PredictionMode.LL;
                parser.AddErrorListener(errorListener);

                tree = parser.query();
            }

            var toks = tokens.GetTokens();

            t = input.GetText(new Antlr4.Runtime.Misc.Interval(toks[0].StartIndex, int.MaxValue));

            if (dump && buildParserTree)
            {
                if (tree == null)
                {
                    Trace.TraceInformation(@"No parse tree available");
                }
                else
                {
                    t             = tree.GetText();
                    parseTreeView = tree.ToStringTree(parser);
                    var text = $"Token count: {tokens.Size}{Environment.NewLine}{MySQLRecognizerCommon.dumpTree(tree, parser.Vocabulary)}";
                    Trace.TraceInformation(text.Trim());
                }
            }

            return(parser.NumberOfSyntaxErrors);
        }