Example #1
0
        /// <summary>
        /// Returns a relational expression from 2 operands and the operator (with appropriate error handling).
        /// </summary>
        public static Expression Build(IList <Expression> operands, string relationalOperator)
        {
            if (operands.Count == 1)
            {
                return(operands[0]);
            }

            if (operands.Count != 2)
            {
                return(null);
            }

            ExpressionOperand parameterAsExpression        = operands[0] as ExpressionOperand;
            ExpressionOperand equivalenceClassAsExpression = operands[1] as ExpressionOperand;

            if ((parameterAsExpression == null) ||
                (equivalenceClassAsExpression == null))
            {
                return(null);
            }

            return(new ExpressionRelational
            {
                ParameterName = parameterAsExpression.Operands[0],
                EquivalenceClassNames = equivalenceClassAsExpression.Operands,
                RelationalOperator = relationalOperator
            });
        }
        /// <summary>
        /// Parses and returns the operand (or parenthesized expression) as an expression.
        /// </summary>
        private static Expression ParseOperand(IList <string> expressionTokens, ref int index)
        {
            string token = Token(expressionTokens, index, false);

            if (token == ParenthesisLeft)
            {
                index++;
                Expression expression = ParseOr(expressionTokens, ref index);
                if ((expression == null) || (Token(expressionTokens, index, false) != ParenthesisRight))
                {
                    return(null);
                }

                index++;
                return(expression);
            }

            if (token == BracketLeft)
            {
                index++;
                IList <string> alternatives = new List <string>();
                string         alternative  = Token(expressionTokens, index, false);
                while (alternative != BracketRight)
                {
                    alternatives.Add(alternative);
                    index++;

                    alternative = Token(expressionTokens, index, false);
                    if (alternative == Comma)
                    {
                        index++;
                        alternative = Token(expressionTokens, index, false);
                    }
                }

                index++;
                return(ExpressionOperand.Build(alternatives));
            }

            index++;
            return(ExpressionOperand.Build(token));
        }