public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression) { bool forceSpaces = false; switch (binaryOperatorExpression.Operator) { case BinaryOperatorType.Equality: case BinaryOperatorType.InEquality: forceSpaces = policy.SpaceAroundEqualityOperator; break; case BinaryOperatorType.GreaterThan: case BinaryOperatorType.GreaterThanOrEqual: case BinaryOperatorType.LessThan: case BinaryOperatorType.LessThanOrEqual: forceSpaces = policy.SpaceAroundRelationalOperator; break; case BinaryOperatorType.ConditionalAnd: case BinaryOperatorType.ConditionalOr: forceSpaces = policy.SpaceAroundLogicalOperator; break; case BinaryOperatorType.BitwiseAnd: case BinaryOperatorType.BitwiseOr: case BinaryOperatorType.ExclusiveOr: forceSpaces = policy.SpaceAroundBitwiseOperator; break; case BinaryOperatorType.Add: case BinaryOperatorType.Subtract: forceSpaces = policy.SpaceAroundAdditiveOperator; break; case BinaryOperatorType.Multiply: case BinaryOperatorType.Divide: case BinaryOperatorType.Modulus: forceSpaces = policy.SpaceAroundMultiplicativeOperator; break; case BinaryOperatorType.ShiftLeft: case BinaryOperatorType.ShiftRight: forceSpaces = policy.SpaceAroundShiftOperator; break; case BinaryOperatorType.NullCoalescing: forceSpaces = policy.SpaceAroundNullCoalescingOperator; break; } ForceSpacesAround(binaryOperatorExpression.OperatorToken, forceSpaces); base.VisitBinaryOperatorExpression(binaryOperatorExpression); // Handle line breaks in binary opeartor expression. if (binaryOperatorExpression.Left.EndLocation.Line != binaryOperatorExpression.Right.StartLocation.Line) { curIndent.Push(IndentType.Block); if (binaryOperatorExpression.OperatorToken.StartLocation.Line == binaryOperatorExpression.Right.StartLocation.Line) { FixStatementIndentation(binaryOperatorExpression.OperatorToken.StartLocation); } else { FixStatementIndentation(binaryOperatorExpression.Right.StartLocation); } curIndent.Pop(); } }
/// <summary> /// Gets the row number in the C# 4.0 spec operator precedence table. /// </summary> static int GetPrecedence(Expression expr) { // Note: the operator precedence table on MSDN is incorrect if (expr is QueryExpression) { // Not part of the table in the C# spec, but we need to ensure that queries within // primary expressions get parenthesized. return(QueryOrLambda); } UnaryOperatorExpression uoe = expr as UnaryOperatorExpression; if (uoe != null) { if (uoe.Operator == UnaryOperatorType.PostDecrement || uoe.Operator == UnaryOperatorType.PostIncrement) { return(Primary); } else { return(Unary); } } if (expr is CastExpression) { return(Unary); } BinaryOperatorExpression boe = expr as BinaryOperatorExpression; if (boe != null) { switch (boe.Operator) { case BinaryOperatorType.Multiply: case BinaryOperatorType.Divide: case BinaryOperatorType.Modulus: return(13); // multiplicative case BinaryOperatorType.Add: case BinaryOperatorType.Subtract: return(12); // additive case BinaryOperatorType.ShiftLeft: case BinaryOperatorType.ShiftRight: return(11); case BinaryOperatorType.GreaterThan: case BinaryOperatorType.GreaterThanOrEqual: case BinaryOperatorType.LessThan: case BinaryOperatorType.LessThanOrEqual: return(RelationalAndTypeTesting); case BinaryOperatorType.Equality: case BinaryOperatorType.InEquality: return(Equality); case BinaryOperatorType.BitwiseAnd: return(8); case BinaryOperatorType.ExclusiveOr: return(7); case BinaryOperatorType.BitwiseOr: return(6); case BinaryOperatorType.ConditionalAnd: return(5); case BinaryOperatorType.ConditionalOr: return(4); case BinaryOperatorType.NullCoalescing: return(3); default: throw new NotSupportedException("Invalid value for BinaryOperatorType"); } } if (expr is IsExpression || expr is AsExpression) { return(RelationalAndTypeTesting); } if (expr is ConditionalExpression) { return(Conditional); } if (expr is AssignmentExpression || expr is LambdaExpression) { return(Assignment); } // anything else: primary expression return(Primary); }
void IAstVisitor.VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression) { Visit(EnterBinaryOperatorExpression, LeaveBinaryOperatorExpression, binaryOperatorExpression); }