/// <summary> /// Removes a token using the enhanced token stream class. /// </summary> /// <param name="sql"></param> /// <param name="position"></param> /// <returns></returns> private CommonTokenStream RemoveToken(string sql, SnapshotPoint snapPos) { MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); //ANTLRInputStream input = new ANTLRInputStream(ms); Version ver = LanguageServiceUtil.GetVersion(LanguageServiceUtil.GetConnection().ServerVersion); MySQLLexer lexer = new MySQLLexer(input); lexer.MySqlVersion = ver; TokenStreamRemovable tokens = new TokenStreamRemovable(lexer); IToken tr = null; int position = snapPos.Position; tokens.Fill(); if (!char.IsWhiteSpace(snapPos.GetChar())) { foreach (IToken t in tokens.GetTokens()) { if ((t.StartIndex <= position) && (t.StopIndex >= position)) { tr = t; break; } } tokens.Remove(tr); } return tokens; }
public static MySQL51Parser.program_return ParseSql(string sql, bool expectErrors, out StringBuilder sb, Version version ) { // The grammar supports upper case only MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql/*.ToUpper() */)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); //ANTLRInputStream input = new ANTLRInputStream(ms); MySQLLexer lexer = new MySQLLexer(input); lexer.MySqlVersion = version; CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); parser.MySqlVersion = version; sb = new StringBuilder(); TextWriter tw = new StringWriter(sb); parser.TraceDestination = tw; MySQL51Parser.program_return r = parser.program(); if (!expectErrors) { if (0 != parser.NumberOfSyntaxErrors) Assert.AreEqual("", sb.ToString()); //Assert.AreEqual( 0, parser.NumberOfSyntaxErrors); } else { Assert.AreNotEqual(0, parser.NumberOfSyntaxErrors); } return r; }
internal static MySQL51Parser.program_return ParseSql( string sql, bool expectErrors, out StringBuilder sb, out CommonTokenStream tokensOutput) { DbConnection con = GetConnection(); // The grammar supports upper case only MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); //ANTLRInputStream input = new ANTLRInputStream(ms); MySQLLexer lexer = new MySQLLexer(input); lexer.MySqlVersion = GetVersion( con.ServerVersion ); CommonTokenStream tokens = new CommonTokenStream(lexer); tokensOutput = tokens; return DoParse(tokens, expectErrors, out sb, lexer.MySqlVersion); }
public void TestTokenRemove() { string sql = "select *, a, c, d from table1 where a is null"; MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); //ANTLRInputStream input = new ANTLRInputStream(ms); MySQLLexer lexer = new MySQLLexer(input); MySql.Parser.TokenStreamRemovable tsr = new MySql.Parser.TokenStreamRemovable(lexer); tsr.Fill(); List<IToken> tokens = tsr.GetTokens(); IToken removed = null; foreach( IToken t in tokens ) { if (t.Text == "d") { removed = t; break; } } tsr.Remove(removed); tokens = tsr.GetTokens(); }
public static string GetRoutineName(string sql) { MySQL51Parser.program_return r = new MySQL51Parser.program_return(); StringBuilder sb; bool expectErrors = false; CommonTokenStream cts; MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); MySQLLexer lexer = new MySQLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); sb = new StringBuilder(); TextWriter tw = new StringWriter(sb); parser.TraceDestination = tw; try { r = parser.program(); } catch (RewriteEmptyStreamException e) { sb.AppendLine(); sb.Append(e.Message); } cts = tokens; if (!expectErrors && sb.Length != 0) { throw new DebugSyntaxException(sb.ToString()); } CommonTree t = (CommonTree)r.Tree; if (t.IsNil) t = (CommonTree)t.GetChild(0); return GetRoutineName(t); }
public MySQL51Parser.program_return ParseSql(string sql, bool expectErrors, out StringBuilder sb, out CommonTokenStream cts) { Version ver = ParserUtils.GetVersion( _connection.ServerVersion ); // The grammar supports upper case only MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql)); CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms); MySQLLexer lexer = new MySQLLexer(input); lexer.MySqlVersion = ver; CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); parser.MySqlVersion = ver; sb = new StringBuilder(); TextWriter tw = new StringWriter(sb); parser.TraceDestination = tw; MySQL51Parser.program_return r = new MySQL51Parser.program_return(); r.Tree = null; try { r = parser.program(); } catch (RewriteEmptyStreamException e) { sb.AppendLine(); sb.Append(e.Message); } cts = tokens; if (!expectErrors && sb.Length != 0) { throw new DebugSyntaxException(sb.ToString()); } return r; }