예제 #1
0
        static void ParseDDLClause(string input)
        {
            AntlrInputStream  inputStream = new AntlrInputStream(input);
            TSqlLexer         lexer       = new TSqlLexer(inputStream);
            CommonTokenStream tokens      = new CommonTokenStream(lexer);
            TSqlParser        parser      = new TSqlParser(tokens);

            var errorHandler = new SyntaxErrorListener();

            parser.AddErrorListener(errorHandler);

            var                        parseTree = parser.ddl_clause();
            ParseTreeWalker            walker    = new ParseTreeWalker();
            TSqlParserListenerExtended loader    = new TSqlParserListenerExtended(new SelectStatement());

            loader.TokenStream = tokens;
            walker.Walk(loader, parseTree);
            Console.WriteLine("Parse Tree:");
            Console.WriteLine(parseTree.ToStringTree(parser));

            Console.WriteLine("Errors:");
            foreach (var error in errorHandler.Errors)
            {
                Console.WriteLine(error.Message + " at position " + error.Line.ToString() + ":" + error.CharPositionInLine.ToString());
            }

            Console.Write("Press enter key to continue");
            Console.ReadLine();
        }
예제 #2
0
        static void ParseInput(string input)
        {
            AntlrInputStream  inputStream = new AntlrInputStream(input);
            TSqlLexer         lexer       = new TSqlLexer(inputStream);
            CommonTokenStream tokens      = new CommonTokenStream(lexer);
            TSqlParser        parser      = new TSqlParser(tokens);

            var errorHandler = new SyntaxErrorListener();

            parser.AddErrorListener(errorHandler);


            var                        parseTree = parser.dml_clause();
            ParseTreeWalker            walker    = new ParseTreeWalker();
            TSqlParserListenerExtended loader    = new TSqlParserListenerExtended(new SelectStatement());

            loader.TokenStream = tokens;
            walker.Walk(loader, parseTree);
            Console.WriteLine("Parse Tree:");
            Console.WriteLine(parseTree.ToStringTree(parser));

            Console.WriteLine("Errors:");
            foreach (var error in errorHandler.Errors)
            {
                Console.WriteLine(error.Message + " at position " + error.Line.ToString() + ":" + error.CharPositionInLine.ToString());
            }

            if (input.Contains("SELECT"))
            {
                var selectStatement = loader.GetStatementAsSelect();
                var text            = JsonConvert.SerializeObject(selectStatement);
                Console.WriteLine("Review Parse. Press any key to continue.");
                Console.ReadLine();
                Console.WriteLine(text);

                Console.WriteLine("Executing Generated Plan");
                GeneratePlan(selectStatement);
            }

            Console.Write("Press enter key to continue");
            Console.ReadLine();
        }