public void load_string(string str) { var inputStream = new AntlrInputStream(str); var lexer = new TSqlLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new TSqlParser(commonTokenStream); if (!noerrorlistener) { parser.RemoveErrorListeners(); parser.AddErrorListener(new ParserErrorListener()); } Type t = parser.GetType(); MethodInfo mi = t.GetMethod(start_rulename); if (mi == null) { throw new ArgumentException("rulename " + start_rulename + " not found."); } ParserRuleContext graphContext = (ParserRuleContext)mi.Invoke(parser, new object[] { }); //ParserRuleContext graphContext = parser.tsql_file(); var token_names = parser.TokenNames; var rule_names = parser.RuleNames; var tree_parser = new TreeParser(token_names, rule_names); root = tree_parser.parse(graphContext); }
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(); }
static public void TestParsing(string sql, Func <TSqlParser, ParserRuleContext> parse) { TSqlParser parser = TSqlUtilities.GetParser(sql); parser.AddErrorListener(new ErrorListener()); var parsed = parse(parser); Assert.AreEqual(0, parser.NumberOfSyntaxErrors); Assert.AreEqual(sql.Length - 1, parsed.Stop.StopIndex); }
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(); }