/// <summary>
        /// Reads a conditional expression.
        /// </summary>
        /// <param name="leftHandSide">The expression on the left hand side of the operator.</param>
        /// <param name="previousPrecedence">The precedence of the expression just before this one.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private ConditionalExpression GetConditionalExpression(
            Expression leftHandSide, ExpressionPrecedence previousPrecedence, bool unsafeCode)
        {
            Param.AssertNotNull(leftHandSide, "leftHandSide");
            Param.Ignore(unsafeCode);
            Param.Ignore(previousPrecedence);

            ConditionalExpression expression = null;

            if (CheckPrecedence(previousPrecedence, ExpressionPrecedence.Conditional))
            {
                var expressionReference = new Reference<ICodePart>();

                // Get the first operator.
                this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalQuestionMark, expressionReference));

                // Get the expression on the right-hand side of the operator.
                Expression trueValue = this.GetOperatorRightHandExpression(ExpressionPrecedence.Conditional, expressionReference, unsafeCode);

                // Get the next operator and make sure it is the correct type.
                this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalColon, expressionReference));

                // Get the expression on the right-hand side of the operator.
                Expression falseValue = this.GetOperatorRightHandExpression(ExpressionPrecedence.None, expressionReference, unsafeCode);

                // Create the partial token list for the expression.
                CsTokenList partialTokens = new CsTokenList(this.tokens, leftHandSide.Tokens.First, this.tokens.Last);

                // Create and return the expression.
                expression = new ConditionalExpression(partialTokens, leftHandSide, trueValue, falseValue);
                expressionReference.Target = expression;
            }

            return expression;
        }
Example #2
0
 private ConditionalExpression GetConditionalExpression(Expression leftHandSide, ExpressionPrecedence previousPrecedence, bool unsafeCode)
 {
     ConditionalExpression expression = null;
     if (CheckPrecedence(previousPrecedence, ExpressionPrecedence.Conditional))
     {
         this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalQuestionMark));
         Expression operatorRightHandExpression = this.GetOperatorRightHandExpression(ExpressionPrecedence.Conditional, unsafeCode);
         this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalColon));
         Expression falseValue = this.GetOperatorRightHandExpression(ExpressionPrecedence.None, unsafeCode);
         CsTokenList tokens = new CsTokenList(this.tokens, leftHandSide.Tokens.First, this.tokens.Last);
         expression = new ConditionalExpression(tokens, leftHandSide, operatorRightHandExpression, falseValue);
     }
     return expression;
 }