public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { base.VisitUnaryExpression (expression); ConstantExpression operandAsConstant = expression.Operand as ConstantExpression; if (operandAsConstant != null) { // Ok, lets compute the result if (operandAsConstant.IsNullValue) return LiteralExpression.FromTypedNull(expression.ExpressionType); try { return LiteralExpression.FromTypedValue(expression.GetValue(), expression.ExpressionType); } catch (RuntimeException ex) { _errorReporter.CannotFoldConstants(ex); } } // If we getting here we return the orginal one. return expression; }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { base.VisitUnaryExpression(expression); Type operandType = expression.Operand.ExpressionType; if (operandType == null) { expression.OperatorMethod = null; } else { expression.OperatorMethod = _binder.BindOperator(expression.Op, operandType); if (expression.OperatorMethod == null) _errorReporter.CannotApplyOperator(expression.Op, operandType); } return expression; }
public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements) { UnaryExpression result = new UnaryExpression(_op, (ExpressionNode)_operand.Clone(alreadyClonedElements)); result.OperatorMethod = OperatorMethod; return result; }
private static bool VisitUnaryExpression(UnaryExpression node1, UnaryExpression node2) { return node2 != null && node1.Op == node2.Op && Visit(node1.Operand, node2.Operand); }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { _writer.Write(expression.Op.TokenText); _writer.Write(" "); bool parenthesesNeeded = NeedParentheses(expression.Operand, expression.Op.Precedence, false); if (parenthesesNeeded) _writer.Write("("); Visit(expression.Operand); if (parenthesesNeeded) _writer.Write(")"); return expression; }
private ExpressionNode ParseSubExpression(ExpressionNode left, int precedence) { if (left == null) { // No left operand, so we parse one and take care about leading unary operators if (_token.Info.UnaryOperator != null) { UnaryOperator op = _token.Info.UnaryOperator; NextToken(); ExpressionNode expr = ParseSubExpression(null, op.Precedence); left = new UnaryExpression(op, expr); } else { left = ParseSimpleExpression(); } } while (_token.Id != TokenId.Eof) { // Special handling for NOT BETWEEN, NOT IN, NOT LIKE, NOT SIMILAR TO, and NOT SOUNDSLIKE. bool negated = false; if (_token.Id == TokenId.NOT) { if (_lookahead.Id == TokenId.BETWEEN || _lookahead.Id == TokenId.IN || _lookahead.Id == TokenId.LIKE || _lookahead.Id == TokenId.SIMILAR || _lookahead.Id == TokenId.SOUNDSLIKE) { NextToken(); negated = true; } } // Special handling for the only ternary operator BETWEEN if (_token.Id == TokenId.BETWEEN) { NextToken(); ExpressionNode lowerBound = ParseSubExpression(null, Operator.BETWEEN_PRECEDENCE); Match(TokenId.AND); ExpressionNode upperBound = ParseSubExpression(null, Operator.BETWEEN_PRECEDENCE); left = new BetweenExpression(left, lowerBound, upperBound); } else { // If there is no binary operator we are finished if (_token.Info.BinaryOperator == null) break; BinaryOperator binaryOp = _token.Info.BinaryOperator; // Precedence is lower, parse it later if (binaryOp.Precedence < precedence) break; // Precedence is equal, but operator ist not right associative, parse it later if (binaryOp.Precedence == precedence && !binaryOp.IsRightAssociative) break; // Precedence is higher NextToken(); // Special handling for SIMILAR TO if (binaryOp == BinaryOperator.SimilarTo) Match(TokenId.TO); if (binaryOp == BinaryOperator.In) { // Special handling for IN InExpression inExpression = new InExpression(); inExpression.Left = left; inExpression.RightExpressions = ParseSimpleQueryExpressionList(); left = inExpression; } else if (_token.Id == TokenId.ANY || _token.Id == TokenId.SOME || _token.Id == TokenId.ALL) { // Special handling for ANY (SOME) and ALL if (binaryOp != BinaryOperator.Equal && binaryOp != BinaryOperator.NotEqual && binaryOp != BinaryOperator.Less && binaryOp != BinaryOperator.LessOrEqual && binaryOp != BinaryOperator.Greater && binaryOp != BinaryOperator.GreaterOrEqual) { _errorReporter.InvalidOperatorForAllAny(_token.Range, binaryOp); } AllAnySubselect allAnySubselect = new AllAnySubselect(); allAnySubselect.Left = left; allAnySubselect.Op = binaryOp; allAnySubselect.Type = (_token.Id == TokenId.ALL) ? AllAnySubselect.AllAnyType.All : AllAnySubselect.AllAnyType.Any; NextToken(); Match(TokenId.LeftParentheses); allAnySubselect.Query = ParseQuery(); Match(TokenId.RightParentheses); left = allAnySubselect; } else { left = new BinaryExpression(binaryOp, left, ParseSubExpression(null, binaryOp.Precedence)); } } // Special handling for negated expressions (see above) if (negated) left = new UnaryExpression(UnaryOperator.LogicalNot, left); } return left; }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { // First visit arguments base.VisitUnaryExpression(expression); if (expression.Op == UnaryOperator.LogicalNot) { // Replace "NOT NOT expr" by "expr" UnaryExpression unOp = expression.Operand as UnaryExpression; if (unOp != null) { if (unOp.Op == UnaryOperator.LogicalNot) return VisitExpression(unOp.Operand); } // Replace "NOT expr IS NULL" and "NOT expr IS NOT NULL" by // "expr IS NOT NULL" and "expr IS NULL" resp. IsNullExpression isNull = expression.Operand as IsNullExpression; if (isNull != null) { isNull.Negated = !isNull.Negated; return VisitExpression(isNull); } // Apply negation on EXISTS ExistsSubselect existsSubselect = expression.Operand as ExistsSubselect; if (existsSubselect != null) { existsSubselect.Negated = !existsSubselect.Negated; return existsSubselect; } // Apply negation on ALL/ANY subquery AllAnySubselect allAnySubselect = expression.Operand as AllAnySubselect; if (allAnySubselect != null) { allAnySubselect.Op = AstUtil.NegateBinaryOp(allAnySubselect.Op); allAnySubselect.Type = (allAnySubselect.Type == AllAnySubselect.AllAnyType.All) ? AllAnySubselect.AllAnyType.Any : AllAnySubselect.AllAnyType.All; return allAnySubselect; } // Apply De Morgan's law BinaryExpression binOp = expression.Operand as BinaryExpression; if (binOp != null) { BinaryOperator negatedOp = AstUtil.NegateBinaryOp(binOp.Op); if (negatedOp != null) { ExpressionNode newLeft; ExpressionNode newRight; if (binOp.Op == BinaryOperator.LogicalAnd || binOp.Op == BinaryOperator.LogicalOr) { newLeft = new UnaryExpression(expression.Op, binOp.Left); newRight = new UnaryExpression(expression.Op, binOp.Right); } else { newLeft = binOp.Left; newRight = binOp.Right; } binOp.Op = negatedOp; binOp.Left = newLeft; binOp.Right = newRight; return VisitExpression(binOp); } } } return expression; }
public void PushUnary(UnaryOperator unaryOperator) { ExpressionNode operand = _expressionStack.Pop(); UnaryExpression unaryExpression = new UnaryExpression(unaryOperator, operand); Push(unaryExpression); }
public virtual ExpressionNode VisitUnaryExpression(UnaryExpression expression) { expression.Operand = VisitExpression(expression.Operand); return expression; }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { EmitCall(expression.OperatorMethod, null, expression.Operand); return expression; }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { // First visit arguments base.VisitUnaryExpression(expression); if (expression.Op == UnaryOperator.LogicalNot) { // Replace "NOT NOT expr" by "expr" UnaryExpression unOp = expression.Operand as UnaryExpression; if (unOp != null) { if (unOp.Op == UnaryOperator.LogicalNot) { return(VisitExpression(unOp.Operand)); } } // Replace "NOT expr IS NULL" and "NOT expr IS NOT NULL" by // "expr IS NOT NULL" and "expr IS NULL" resp. IsNullExpression isNull = expression.Operand as IsNullExpression; if (isNull != null) { isNull.Negated = !isNull.Negated; return(VisitExpression(isNull)); } // Apply negation on EXISTS ExistsSubselect existsSubselect = expression.Operand as ExistsSubselect; if (existsSubselect != null) { existsSubselect.Negated = !existsSubselect.Negated; return(existsSubselect); } // Apply negation on ALL/ANY subquery AllAnySubselect allAnySubselect = expression.Operand as AllAnySubselect; if (allAnySubselect != null) { allAnySubselect.Op = AstUtil.NegateBinaryOp(allAnySubselect.Op); allAnySubselect.Type = (allAnySubselect.Type == AllAnySubselect.AllAnyType.All) ? AllAnySubselect.AllAnyType.Any : AllAnySubselect.AllAnyType.All; return(allAnySubselect); } // Apply De Morgan's law BinaryExpression binOp = expression.Operand as BinaryExpression; if (binOp != null) { BinaryOperator negatedOp = AstUtil.NegateBinaryOp(binOp.Op); if (negatedOp != null) { ExpressionNode newLeft; ExpressionNode newRight; if (binOp.Op == BinaryOperator.LogicalAnd || binOp.Op == BinaryOperator.LogicalOr) { newLeft = new UnaryExpression(expression.Op, binOp.Left); newRight = new UnaryExpression(expression.Op, binOp.Right); } else { newLeft = binOp.Left; newRight = binOp.Right; } binOp.Op = negatedOp; binOp.Left = newLeft; binOp.Right = newRight; return(VisitExpression(binOp)); } } } return(expression); }
public override ExpressionNode VisitUnaryExpression(UnaryExpression expression) { _xmlWriter.WriteStartElement("unaryExpression"); _xmlWriter.WriteAttributeString("operator", expression.Op.TokenText); Visit(expression.Operand); _xmlWriter.WriteEndElement(); return expression; }