public static Exception AggregateArgument() { return(ParserException.CreateException("Syntax error in aggregate argument: Expecting a single column argument.")); }
public static Exception UnknownToken(string token, int position) { return(ParserException.CreateException("Cannot interpret token '{0}' at position {1}.", (object)token, (object)position)); }
public static Exception TooManyRightParentheses() { return(ParserException.CreateException("The expression has too many closing parentheses.")); }
public static Exception MissingOperand(string op) { return(ParserException.CreateException("Syntax error: Missing operand after '{0}' operator.", (object)op)); }
public static Exception SystaxError() { return(ParserException.CreateException("Syntax error in the expression.")); }
public static Exception UnsupportedOperator(string op) { return(ParserException.CreateException("The expression contains unsupported operator '{0}'.", (object)op)); }
public static Exception MissingRightParen() { return(ParserException.CreateException("The expression is missing the closing parenthesis.")); }
public static Exception MissingOperator(string token) { return(ParserException.CreateException("Missing operator before {0} operand.", (object)token)); }
/*ExpressionNode ParseAggregateArgument(string aggregate) * { * Token token = this.lexer.Read(); * if (Token.Name != token * && "*" != this.lexer.TokenString) // Allow Count(*) where the * is BinaryOp * { * throw ParserException.AggregateArgument(); * } * * string columnName = this.lexer.TokenString; * this.lexer.Read(Token.RightParen); * return new AggregateNode(aggregate, columnName); * }*/ void BuildExpression(int priority) { ExpressionNode left = null; ExpressionNode right = null; while (true) { OperatorInfo info = this.opStack.Peek(); if (info.Operator.Priority < priority) { return; } this.opStack.Pop(); switch (info.Type) { case NodeType.Unop: left = null; right = this.nodeStack.Pop(); if (Operator.IsUnary(info.Operator)) { this.nodeStack.Push(new UnaryOpNode(info.Operator, right)); } else { throw ParserException.UnsupportedOperator(info.Operator.ToString()); } break; case NodeType.Binop: right = this.nodeStack.Pop(); left = this.nodeStack.Pop(); if (Operator.IsBinary(info.Operator)) { this.nodeStack.Push(new BinaryOpNode(info.Operator, left, right)); } else if (Operator.Like == info.Operator) { this.nodeStack.Push(new LikeNode(info.Operator, left, right, this.compareFlags)); } else { throw ParserException.UnsupportedOperator(info.Operator.ToString()); } break; case NodeType.TernaryOp2: OperatorInfo opInfo1 = this.opStack.Pop(); if (opInfo1.Type != NodeType.TernaryOp) { throw ParserException.MissingOperator("Ternary operator requires 2 operators"); } if (this.nodeStack.Count < 3) { throw ParserException.MissingOperand("Ternary operator requires 3 operands"); } ExpressionNode node3 = this.nodeStack.Pop(); ExpressionNode node2 = this.nodeStack.Pop(); ExpressionNode node1 = this.nodeStack.Pop(); this.nodeStack.Push(new TernaryOpNode(opInfo1.Operator , node1 , node2 , node3)); break; case NodeType.Zop: this.nodeStack.Push(new ZeroOpNode(info.Operator)); break; case NodeType.UnopSpec: case NodeType.BinopSpec: return; default: Debug.WriteLine("BuildExpression skipped: " + info.Operator); break; } } }
private bool TryParse(bool enableExceptions, out ExpressionNode expressionNode) { expressionNode = null; Token token; int parenCount = 0; this.StartParse(); do { try { token = this.lexer.Read(); } catch (LexicalException e) { if (enableExceptions) { throw e; } return(false); } Debug.WriteLine(Utils.TokenToString(token) + " : " + this.lexer.TokenString); switch (token) { case Token.Name: case Token.Numeric: case Token.Decimal: case Token.Float: case Token.NumericHex: case Token.StringConst: case Token.Date: if (OperandType.None != this.prevOperand) { if (enableExceptions) { throw ParserException.MissingOperator(this.lexer.TokenString); } return(false); } /*if (this.opStack.Count > 0) * { * OperatorInfo info = this.opStack.Peek(); * if (info.Operator == Operator.In) * { * throw ParserException.InWithoutParentheses(); * } * }*/ this.prevOperand = OperandType.Scalar; this.nodeStack.Push(this.CreateScalarNode(token, this.lexer.TokenString)); break; case Token.ListSeparator: { if (this.prevOperand == OperandType.None) { if (enableExceptions) { throw ParserException.MissingOperandBefore(","); } return(false); } this.BuildExpression(3); OperatorInfo info = this.opStack.Peek(); if (info.Type != NodeType.Call) { if (enableExceptions) { throw ParserException.SystaxError(); } return(false); } ExpressionNode arg = this.nodeStack.Pop(); FunctionNode func = (FunctionNode)this.nodeStack.Pop(); func.AddArgument(arg); this.nodeStack.Push(func); this.prevOperand = OperandType.None; } break; case Token.LeftParen: parenCount++; if (OperandType.None != this.prevOperand) { this.BuildExpression(22); this.prevOperand = OperandType.None; ExpressionNode node = this.nodeStack.Peek(); if (!(node is NameNode)) { if (enableExceptions) { throw ParserException.SystaxError(); } return(false); } NameNode node1 = (NameNode)this.nodeStack.Pop(); ExpressionNode func; if (AggregateNode.IsAggregare(node1.Name)) { func = new AggregateNode(node1.Name); } else { func = new FunctionNode(node1.Parent, node1.Name); } this.nodeStack.Push(func); this.opStack.Push(new OperatorInfo(NodeType.Call, Operator.Proc)); } else { OperatorInfo info = this.opStack.Peek(); if (info.Type != NodeType.Binop || info.Operator != Operator.In) { this.opStack.Push(new OperatorInfo(NodeType.Paren, Operator.Proc)); } else { this.nodeStack.Push(new FunctionNode(null, "In")); this.opStack.Push(new OperatorInfo(NodeType.Call, Operator.Proc)); } } break; case Token.RightParen: { if (this.prevOperand != OperandType.None) { this.BuildExpression(3); } if (this.opStack.Count <= 1) { if (enableExceptions) { throw ParserException.TooManyRightParentheses(); } return(false); } OperatorInfo info = this.opStack.Pop(); if (this.prevOperand == OperandType.None && info.Type != NodeType.Call) { if (enableExceptions) { throw ParserException.MissingOperand(info.Operator.ToString()); } return(false); } if (info.Type == NodeType.Call) { if (this.prevOperand != OperandType.None) { ExpressionNode argument = this.nodeStack.Pop(); FunctionNode func = (FunctionNode)this.nodeStack.Pop(); func.AddArgument(argument); func.Check(); this.nodeStack.Push(func); } } else { ExpressionNode node = this.nodeStack.Pop(); node = new UnaryOpNode(Operator.Noop, node); // noop -- return value this.nodeStack.Push(node); } this.prevOperand = OperandType.Expr; parenCount--; } break; case Token.ZeroOp: if (OperandType.None != this.prevOperand) { if (enableExceptions) { throw ParserException.MissingOperator(this.lexer.TokenString); } return(false); } this.opStack.Push(new OperatorInfo(NodeType.Zop, this.lexer.Operator)); this.prevOperand = OperandType.Expr; break; case Token.UnaryOp: this.opStack.Push(new OperatorInfo(NodeType.Unop, this.lexer.Operator)); break; case Token.BinaryOp: if (OperandType.None != this.prevOperand) { this.prevOperand = OperandType.None; Operator op = this.lexer.Operator; NodeType nodeType = NodeType.Binop; if (op == Operator.And) { this.BuildExpression(Operator.BetweenAnd.Priority); // eat all ops higher than BETWEEN AND: the unary +/-; comparison ops, etc. OperatorInfo lastOp = this.opStack.Peek(); if (op == Operator.And && lastOp.Operator == Operator.Between) { op = Operator.BetweenAnd; nodeType = NodeType.TernaryOp2; } } this.BuildExpression(op.Priority); this.opStack.Push(new OperatorInfo(nodeType, op)); } else { if (Operator.Plus == this.lexer.Operator) { this.opStack.Push(new OperatorInfo(NodeType.Unop, Operator.UnaryPlus)); } else if (Operator.Minus == this.lexer.Operator) { this.opStack.Push(new OperatorInfo(NodeType.Unop, Operator.Negative)); } else { if (enableExceptions) { throw ParserException.MissingOperandBefore(this.lexer.Operator.ToString()); } return(false); } } break; case Token.TernaryOp: if (OperandType.None != this.prevOperand) { Operator op = this.lexer.Operator; this.prevOperand = OperandType.None; this.BuildExpression(op.Priority); this.opStack.Push(new OperatorInfo(NodeType.TernaryOp, op)); } break; case Token.Dot: { ExpressionNode node = this.nodeStack.Peek(); if (!(node is NameNode) && !(node is FunctionNode) && !(node is ConstNode)) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } try { token = this.lexer.Read(); } catch (LexicalException e) { if (enableExceptions) { throw e; } return(false); } if (Token.Name != token) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } ExpressionNode node1 = this.nodeStack.Pop(); //NameNode node1 = (NameNode)this.nodeStack.Pop(); //string name = node1.Name + "." + this.lexer.TokenString; this.nodeStack.Push(new NameNode(node1, this.lexer.TokenString)); } break; case Token.EOF: if (OperandType.None != this.prevOperand) { this.BuildExpression(3); if (this.opStack.Count != 1) { if (enableExceptions) { throw ParserException.MissingRightParen(); } return(false); } } else if (this.nodeStack.Count > 0) { if (enableExceptions) { throw ParserException.MissingOperand(this.opStack.Peek().Operator.ToString()); } return(false); } break; case Token.Parameter: try { token = this.lexer.Read(); } catch (LexicalException e) { if (enableExceptions) { throw e; } return(false); } if (Token.Name != token) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } NameNode parametersNode = new NameNode(null, "Parameters"); this.nodeStack.Push(new NameNode(parametersNode, this.lexer.TokenString)); this.prevOperand = OperandType.Scalar; break; default: if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } } while (Token.EOF != token); //Debug.Assert(1 == this.nodeStack.Count); if (this.nodeStack.Count != 1) { return(false); } expressionNode = this.nodeStack.Peek(); if (expressionNode == null) { return(false); } return(true); }
private void BuildExpression(int priority) { ExpressionNode expressionNode = (ExpressionNode)null; ExpressionParser.OperatorInfo operatorInfo1; while (true) { operatorInfo1 = this.opStack.Peek(); if (operatorInfo1.Operator.Priority >= priority) { this.opStack.Pop(); switch (operatorInfo1.Type) { case ExpressionParser.NodeType.Unop: expressionNode = (ExpressionNode)null; ExpressionNode right1 = this.nodeStack.Pop(); if (Operator.IsUnary(operatorInfo1.Operator)) { this.nodeStack.Push((ExpressionNode) new UnaryOpNode(operatorInfo1.Operator, right1)); continue; } goto label_7; case ExpressionParser.NodeType.UnopSpec: goto label_2; case ExpressionParser.NodeType.Binop: ExpressionNode right2 = this.nodeStack.Pop(); ExpressionNode left = this.nodeStack.Pop(); if (Operator.IsBinary(operatorInfo1.Operator)) { this.nodeStack.Push((ExpressionNode) new BinaryOpNode(operatorInfo1.Operator, left, right2, this.compareFlags)); continue; } if (Operator.Like == operatorInfo1.Operator) { this.nodeStack.Push((ExpressionNode) new LikeNode(operatorInfo1.Operator, left, right2, this.compareFlags)); continue; } goto label_12; case ExpressionParser.NodeType.BinopSpec: goto label_19; case ExpressionParser.NodeType.Zop: this.nodeStack.Push((ExpressionNode) new ZeroOpNode(operatorInfo1.Operator)); continue; case ExpressionParser.NodeType.TernaryOp2: ExpressionParser.OperatorInfo operatorInfo2 = this.opStack.Pop(); if (operatorInfo2.Type == ExpressionParser.NodeType.TernaryOp) { if (this.nodeStack.Count >= 3) { ExpressionNode op3 = this.nodeStack.Pop(); ExpressionNode op2 = this.nodeStack.Pop(); ExpressionNode op1 = this.nodeStack.Pop(); this.nodeStack.Push((ExpressionNode) new TernaryOpNode(operatorInfo2.Operator, op1, op2, op3)); continue; } goto label_16; } else { goto label_14; } default: continue; } } else { break; } } return; label_19: return; label_2: return; label_7: throw ParserException.UnsupportedOperator(operatorInfo1.Operator.ToString()); label_12: throw ParserException.UnsupportedOperator(operatorInfo1.Operator.ToString()); label_14: throw ParserException.MissingOperator("Ternary operator requires 2 operators"); label_16: throw ParserException.MissingOperand("Ternary operator requires 3 operands"); }
private bool TryParse(bool enableExceptions, out ExpressionNode expressionNode) { expressionNode = (ExpressionNode)null; int num = 0; this.StartParse(); Token token; do { try { token = this.lexer.Read(); } catch (LexicalException ex) { if (enableExceptions) { throw ex; } return(false); } switch (token) { case Token.Name: case Token.Numeric: case Token.Decimal: case Token.Float: case Token.NumericHex: case Token.StringConst: case Token.Date: if (this.prevOperand != OperandType.None) { if (enableExceptions) { throw ParserException.MissingOperator(this.lexer.TokenString); } return(false); } this.prevOperand = OperandType.Scalar; this.nodeStack.Push(this.CreateScalarNode(token, this.lexer.TokenString)); break; case Token.ListSeparator: if (this.prevOperand == OperandType.None) { if (enableExceptions) { throw ParserException.MissingOperandBefore(","); } return(false); } this.BuildExpression(3); if (this.opStack.Peek().Type != ExpressionParser.NodeType.Call) { if (enableExceptions) { throw ParserException.SystaxError(); } return(false); } ExpressionNode expressionNode1 = this.nodeStack.Pop(); FunctionNode functionNode1 = (FunctionNode)this.nodeStack.Pop(); functionNode1.AddArgument(expressionNode1); this.nodeStack.Push((ExpressionNode)functionNode1); this.prevOperand = OperandType.None; break; case Token.LeftParen: ++num; if (this.prevOperand != OperandType.None) { this.BuildExpression(22); this.prevOperand = OperandType.None; if (!(this.nodeStack.Peek() is NameNode)) { if (enableExceptions) { throw ParserException.SystaxError(); } return(false); } NameNode nameNode = (NameNode)this.nodeStack.Pop(); this.nodeStack.Push(!AggregateNode.IsAggregare(nameNode.Name) ? (ExpressionNode) new FunctionNode(nameNode.Parent, nameNode.Name) : (ExpressionNode) new AggregateNode(nameNode.Name)); this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Call, Operator.Proc)); break; } ExpressionParser.OperatorInfo operatorInfo1 = this.opStack.Peek(); if (operatorInfo1.Type != ExpressionParser.NodeType.Binop || operatorInfo1.Operator != Operator.In) { this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Paren, Operator.Proc)); break; } this.nodeStack.Push((ExpressionNode) new FunctionNode((ExpressionNode)null, "In")); this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Call, Operator.Proc)); break; case Token.RightParen: if (this.prevOperand != OperandType.None) { this.BuildExpression(3); } if (this.opStack.Count <= 1) { if (enableExceptions) { throw ParserException.TooManyRightParentheses(); } return(false); } ExpressionParser.OperatorInfo operatorInfo2 = this.opStack.Pop(); if (this.prevOperand == OperandType.None && operatorInfo2.Type != ExpressionParser.NodeType.Call) { if (enableExceptions) { throw ParserException.MissingOperand(operatorInfo2.Operator.ToString()); } return(false); } if (operatorInfo2.Type == ExpressionParser.NodeType.Call) { if (this.prevOperand != OperandType.None) { ExpressionNode expressionNode2 = this.nodeStack.Pop(); FunctionNode functionNode2 = (FunctionNode)this.nodeStack.Pop(); functionNode2.AddArgument(expressionNode2); functionNode2.Check(); this.nodeStack.Push((ExpressionNode)functionNode2); } } else { ExpressionNode right = this.nodeStack.Pop(); this.nodeStack.Push((ExpressionNode) new UnaryOpNode(Operator.Noop, right)); } this.prevOperand = OperandType.Expr; --num; break; case Token.ZeroOp: if (this.prevOperand != OperandType.None) { if (enableExceptions) { throw ParserException.MissingOperator(this.lexer.TokenString); } return(false); } this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Zop, this.lexer.Operator)); this.prevOperand = OperandType.Expr; break; case Token.UnaryOp: this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, this.lexer.Operator)); break; case Token.BinaryOp: if (this.prevOperand != OperandType.None) { this.prevOperand = OperandType.None; Operator betweenAnd = this.lexer.Operator; ExpressionParser.NodeType type = ExpressionParser.NodeType.Binop; if (betweenAnd == Operator.And) { this.BuildExpression(Operator.BetweenAnd.Priority); ExpressionParser.OperatorInfo operatorInfo3 = this.opStack.Peek(); if (betweenAnd == Operator.And && operatorInfo3.Operator == Operator.Between) { betweenAnd = Operator.BetweenAnd; type = ExpressionParser.NodeType.TernaryOp2; } } this.BuildExpression(betweenAnd.Priority); this.opStack.Push(new ExpressionParser.OperatorInfo(type, betweenAnd)); break; } if (Operator.Plus == this.lexer.Operator) { this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, Operator.UnaryPlus)); break; } if (Operator.Minus == this.lexer.Operator) { this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, Operator.Negative)); break; } if (enableExceptions) { throw ParserException.MissingOperandBefore(this.lexer.Operator.ToString()); } return(false); case Token.TernaryOp: if (this.prevOperand != OperandType.None) { Operator op = this.lexer.Operator; this.prevOperand = OperandType.None; this.BuildExpression(op.Priority); this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.TernaryOp, op)); break; } break; case Token.Dot: ExpressionNode expressionNode3 = this.nodeStack.Peek(); if (!(expressionNode3 is NameNode)) { if (!(expressionNode3 is FunctionNode)) { if (!(expressionNode3 is ConstNode)) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } } } try { token = this.lexer.Read(); } catch (LexicalException ex) { if (enableExceptions) { throw ex; } return(false); } if (Token.Name != token) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } this.nodeStack.Push((ExpressionNode) new NameNode(this.nodeStack.Pop(), this.lexer.TokenString)); break; case Token.Parameter: try { token = this.lexer.Read(); } catch (LexicalException ex) { if (enableExceptions) { throw ex; } return(false); } if (Token.Name != token) { if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } this.nodeStack.Push((ExpressionNode) new NameNode((ExpressionNode) new NameNode((ExpressionNode)null, "Parameters"), this.lexer.TokenString)); this.prevOperand = OperandType.Scalar; break; case Token.EOF: if (this.prevOperand != OperandType.None) { this.BuildExpression(3); if (this.opStack.Count != 1) { if (enableExceptions) { throw ParserException.MissingRightParen(); } return(false); } break; } if (this.nodeStack.Count > 0) { if (enableExceptions) { throw ParserException.MissingOperand(this.opStack.Peek().Operator.ToString()); } return(false); } break; default: if (enableExceptions) { throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos); } return(false); } }while (Token.EOF != token); if (this.nodeStack.Count != 1) { return(false); } expressionNode = this.nodeStack.Peek(); return(expressionNode != null); }