public static void ToStringExpr(FilterTree tree) { if (tree.Type == SQLFilterLexer.STRING_EXPR || !tree.IsLeaf) { return; } FilterTree op = (FilterTree)tree.Children[0]; op.Type = StringPatternLexer.IS; if (!(op is StringTree)) { tree.SetChild(0, new StringTree(op)); } // do conversions FilterTree constant = (FilterTree)tree.Children[0].GetChild(1); constant.Type = StringPatternLexer.TEXT; constant.Text = ""; if (!(constant is StringTree)) { tree.Children[0].SetChild(1, new StringTree(constant)); } tree.Type = SQLFilterLexer.STRING_EXPR; ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty(); }
private static Expression compileEnum(FilterTree node, ParameterExpression param) { Expression property = ((FilterTree)node.Children[0]).Compile(param); string enumText = node.Children[1].Text; object parsed; try { parsed = Enum.Parse(property.Type, enumText, false); } catch { // HACK ALERT: It's kinda late for fixing the tree but whatever ((FilterTree)node.Children[1]).Text = Enum.GetNames(property.Type)[0]; parsed = Enum.GetValues(property.Type).GetValue(0); } switch (node.Type) { case SQLFilterLexer.EQUALS: return(Expression.Equal( property, Expression.Constant(parsed, property.Type))); case SQLFilterLexer.NOTEQUALS: return(Expression.NotEqual( property, Expression.Constant(parsed, property.Type))); } return(null); }
private static void changeCommonLeafNodeType(FilterTree tree, int newType, int newOp, int newConst, string newValue) { if (tree.Type == newType || !tree.IsLeaf) { return; } FilterTree op = (FilterTree)tree.Children[0]; op.Type = newOp; if (!(op is SQLTree)) { tree.SetChild(0, new SQLTree(op)); } // do conversions FilterTree constant = (FilterTree)tree.Children[0].GetChild(1); constant.Type = newConst; constant.Text = newValue; if (!(constant is SQLTree)) { tree.Children[0].SetChild(1, new SQLTree(constant)); } tree.Type = newType; ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty(); }
private void parseQuery(string newQuery) { if (root != null) { root.PropertyChanged -= rootPropertyChanged; } var stream = new ANTLRStringStream(newQuery); var lexer = new SQLFilterLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new SQLFilterParser(tokens); try { root = parser.prog().Tree; NormalizeNodes((SQLTree)root); compileTree(); query = root.ToString(); OnPropertyChanged("Tree"); OnPropertyChanged("Function"); } catch { throw; } finally { root.PropertyChanged += rootPropertyChanged; treeDirty = false; } OnPropertyChanged("Query"); }
// We want leafs to be grouped inside AND. leaf -> ^(AND leaf) private FilterTree groupify(FilterTree tree) { if (!isGroupNode(tree)) { return((FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.AND), tree)); } return(tree); }
public FilterTree(FilterTree t) : base(t) { this.SubType = t.SubType; if (t.IsLeaf) this.IsLeaf = true; if (t.IsDirty) this.IsDirty = true; if(t.Children != null) this.AddChildren(t.Children); }
private FilterTree rewriteWithNot(FilterTree tree) { switch(tree.Type) { case SQLFilterLexer.AND: tree.Token.Type = SQLFilterLexer.NOT_AND; return tree; case SQLFilterLexer.OR: tree.Token.Type = SQLFilterLexer.NOT_OR; return tree; } return (FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.NOT), tree); }
private FilterTree rewriteWithNot(FilterTree tree) { switch (tree.Type) { case SQLFilterLexer.AND: tree.Token.Type = SQLFilterLexer.NOT_AND; return(tree); case SQLFilterLexer.OR: tree.Token.Type = SQLFilterLexer.NOT_OR; return(tree); } return((FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.NOT), tree)); }
//Stolen from antlr runtime private static string printNode(FilterTree tree) { if (tree.IsNil) { return("nil"); } if (tree.Type == 0) { return("<errornode>"); } if (tree.Token == null) { return(string.Empty); } return(tree.Text); }
public FilterTree(FilterTree t) : base(t) { this.SubType = t.SubType; if (t.IsLeaf) { this.IsLeaf = true; } if (t.IsDirty) { this.IsDirty = true; } if (t.Children != null) { this.AddChildren(t.Children); } }
private FilterTree reverseOperator(FilterTree tree) { switch(tree.Type) { case SQLFilterParser.GREATER: tree.Token.Type = SQLFilterLexer.LESSER; break; case SQLFilterParser.GREATEROREQUALS: tree.Token.Type = SQLFilterParser.LESSEROREQUALS; break; case SQLFilterParser.LESSER: tree.Token.Type = SQLFilterParser.GREATER; break; case SQLFilterParser.LESSEROREQUALS: tree.Token.Type = SQLFilterParser.GREATEROREQUALS; break; } return tree; }
private static Expression compileIP(FilterTree node, ParameterExpression param) { switch (node.Type) { case SQLFilterLexer.LESSER: return(Expression.Call( typeof(IPExtension).GetMethod("LessThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) })); case SQLFilterLexer.LESSEROREQUALS: return(Expression.Call( typeof(IPExtension).GetMethod("LessThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) })); case SQLFilterLexer.GREATER: return(Expression.Call( typeof(IPExtension).GetMethod("GreaterThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) })); case SQLFilterLexer.GREATEROREQUALS: return(Expression.Call( typeof(IPExtension).GetMethod("GreaterThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) })); case SQLFilterLexer.EQUALS: return(Expression.Call( ((FilterTree)node.Children[0]).Compile(param), typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null), new Expression[] { ((FilterTree)node.Children[1]).Compile(param) })); case SQLFilterLexer.NOTEQUALS: return(Expression.Not( Expression.Call( ((FilterTree)node.Children[0]).Compile(param), typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null), new Expression[] { ((FilterTree)node.Children[1]).Compile(param) }))); } return(null); }
private FilterTree reverseOperator(FilterTree tree) { switch (tree.Type) { case SQLFilterParser.GREATER: tree.Token.Type = SQLFilterLexer.LESSER; break; case SQLFilterParser.GREATEROREQUALS: tree.Token.Type = SQLFilterParser.LESSEROREQUALS; break; case SQLFilterParser.LESSER: tree.Token.Type = SQLFilterParser.GREATER; break; case SQLFilterParser.LESSEROREQUALS: tree.Token.Type = SQLFilterParser.GREATEROREQUALS; break; } return(tree); }
public static void ToIPExpr(FilterTree tree) { changeCommonLeafNodeType(tree, SQLFilterLexer.IP_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.IPV4, "127.0.0.1"); }
public static void ToBoolExpr(FilterTree tree) { changeCommonLeafNodeType(tree, SQLFilterLexer.BOOL_EXPR, SQLFilterLexer.IS, SQLFilterLexer.TRUE, "True"); }
public StringTree(FilterTree t) : base(t) { }
//Stolen from antlr runtime private static string printNode(FilterTree tree) { if (tree.IsNil) { return "nil"; } if (tree.Type == 0) { return "<errornode>"; } if (tree.Token == null) { return string.Empty; } return tree.Text; }
public static void ToStringExpr(FilterTree tree) { if (tree.Type == SQLFilterLexer.STRING_EXPR || !tree.IsLeaf) return; FilterTree op = (FilterTree)tree.Children[0]; op.Type = StringPatternLexer.IS; if (!(op is StringTree)) tree.SetChild(0, new StringTree(op)); // do conversions FilterTree constant = (FilterTree)tree.Children[0].GetChild(1); constant.Type = StringPatternLexer.TEXT; constant.Text = ""; if (!(constant is StringTree)) tree.Children[0].SetChild(1, new StringTree(constant)); tree.Type = SQLFilterLexer.STRING_EXPR; ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty(); }
public static void ToEnumExpr(FilterTree tree, Type newType) { changeCommonLeafNodeType(tree, SQLFilterLexer.ENUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.STRING, Enum.GetName(newType,0)); }
private FilterTree makeFirstLeftSubchild(FilterTree root, ITree child) { root.Children.Insert(0, child); root.FreshenParentAndChildIndexes(); return(root); }
// Helper function private static bool isGroupNode(FilterTree node) { return node.Type == SQLFilterLexer.AND || node.Type == SQLFilterLexer.OR || node.Type == SQLFilterLexer.NOT_AND || node.Type == SQLFilterLexer.NOT_OR; }
// We want leafs to be grouped inside AND. leaf -> ^(AND leaf) private FilterTree groupify(FilterTree tree) { if (!isGroupNode(tree)) return (FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.AND), tree); return tree; }
// Mark single expressions as leafs private static void markAsLeaf(FilterTree tree) { tree.IsLeaf = true; }
private static Expression compileEnum(FilterTree node, ParameterExpression param) { Expression property = ((FilterTree)node.Children[0]).Compile(param); string enumText = node.Children[1].Text; object parsed; try { parsed = Enum.Parse(property.Type, enumText, false); } catch { // HACK ALERT: It's kinda late for fixing the tree but whatever ((FilterTree)node.Children[1]).Text = Enum.GetNames(property.Type)[0]; parsed = Enum.GetValues(property.Type).GetValue(0); } switch (node.Type) { case SQLFilterLexer.EQUALS: return Expression.Equal( property, Expression.Constant(parsed, property.Type)); case SQLFilterLexer.NOTEQUALS: return Expression.NotEqual( property, Expression.Constant(parsed, property.Type)); } return null; }
private static Expression compileIP(FilterTree node, ParameterExpression param) { switch (node.Type) { case SQLFilterLexer.LESSER: return Expression.Call( typeof(IPExtension).GetMethod("LessThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }); case SQLFilterLexer.LESSEROREQUALS: return Expression.Call( typeof(IPExtension).GetMethod("LessThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }); case SQLFilterLexer.GREATER: return Expression.Call( typeof(IPExtension).GetMethod("GreaterThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }); case SQLFilterLexer.GREATEROREQUALS: return Expression.Call( typeof(IPExtension).GetMethod("GreaterThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null), new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }); case SQLFilterLexer.EQUALS: return Expression.Call( ((FilterTree)node.Children[0]).Compile(param), typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null), new Expression[] { ((FilterTree)node.Children[1]).Compile(param) }); case SQLFilterLexer.NOTEQUALS: return Expression.Not( Expression.Call( ((FilterTree)node.Children[0]).Compile(param), typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null), new Expression[] { ((FilterTree)node.Children[1]).Compile(param) })); } return null; }
public SQLTree(FilterTree t) : base(t) { }
public static void ToNumExpr(FilterTree tree) { changeCommonLeafNodeType(tree, SQLFilterLexer.NUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.INT, "0"); }
// Helper function private static bool isGroupNode(FilterTree node) { return(node.Type == SQLFilterLexer.AND || node.Type == SQLFilterLexer.OR || node.Type == SQLFilterLexer.NOT_AND || node.Type == SQLFilterLexer.NOT_OR); }
public static void ToEnumExpr(FilterTree tree, Type newType) { changeCommonLeafNodeType(tree, SQLFilterLexer.ENUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.STRING, Enum.GetName(newType, 0)); }
private static void changeCommonLeafNodeType(FilterTree tree, int newType, int newOp, int newConst, string newValue) { if (tree.Type == newType || !tree.IsLeaf) return; FilterTree op = (FilterTree)tree.Children[0]; op.Type = newOp; if (!(op is SQLTree)) tree.SetChild(0, new SQLTree(op)); // do conversions FilterTree constant = (FilterTree)tree.Children[0].GetChild(1); constant.Type = newConst; constant.Text = newValue; if (!(constant is SQLTree)) tree.Children[0].SetChild(1, new SQLTree(constant)); tree.Type = newType; ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty(); }
private FilterTree makeFirstLeftSubchild(FilterTree root, ITree child) { root.Children.Insert(0, child); root.FreshenParentAndChildIndexes(); return root; }