示例#1
0
        static void Try(string input, int server_version = 0)
        {
            //var str = new AntlrInputStream(input);
            var str   = new AntlrInputStream(input);
            var lexer = new MySQLLexer(str);

            lexer.serverVersion = server_version;
            var tokens = new CommonTokenStream(lexer);
            var parser = new MySQLParser(tokens);

            parser.serverVersion = server_version;
            var listener = new ErrorListener <IToken>(parser, lexer, tokens);

            parser.AddErrorListener(listener);
            var tree = parser.queries();

            if (listener.had_error)
            {
                System.Console.WriteLine("error in parse.");
            }
            else
            {
                System.Console.WriteLine("parse completed.");
            }
            System.Console.WriteLine(tokens.OutputTokens());
            System.Console.WriteLine(tree.OutputTree(tokens));
        }
示例#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);
        }