Exemple #1
0
            public override bool Evaluate(TSource source)
            {
                foreach (IEvaluate <TSource> expr in Expressions)
                {
                    if (!JavaScriptHelpers.IsTruthy(expr.Evaluate(source)))
                    {
                        return(false);
                    }
                }

                return(true);
            }
Exemple #2
0
 public override bool Evaluate(TSource source)
 {
     return(JavaScriptHelpers.IsTruthy(Token.GetValue(source)));
 }
Exemple #3
0
 public override bool Evaluate(TSource source)
 {
     return(JavaScriptHelpers.IsTruthy(Inner.Evaluate(source)));
 }
Exemple #4
0
        internal static LogicalExpression <TSource> CompileLogicalExpression <TSource, TToken>(string text, Func <string, TToken> pathToken)
            where TToken : Token <TSource>
        {
            LogicalExpression <TSource> result = null;

            var whereMatch = parser.Match(text);

            if (whereMatch == null || !whereMatch.Success)
            {
                throw new ArgumentException("Invalid expression \"" + text + "\".");
            }

            var exprCaptures = whereMatch.Groups["expr"].Captures;
            var nextCaptures = whereMatch.Groups["next"].Captures;

            AndExpressionGroup <TSource> andGroup = null;

            for (var i = 0; i < exprCaptures.Count; i++)
            {
                var exprText = exprCaptures[i].Value.Trim();
                var nextText = nextCaptures[i].Value.Trim();

                IEvaluate <TSource> expr;

                if (Regex.IsMatch(exprText, @"^\(.*\)$"))
                {
                    expr = CompileLogicalExpression <TSource, TToken>(exprText.Substring(1, exprText.Length - 2), pathToken);
                }
                else
                {
                    var exprMatch = exprParser.Match(exprText);

                    if (exprMatch == null || !exprMatch.Success)
                    {
                        throw new Exception("Invalid expression \"" + exprText + "\".");
                    }

                    var leftText  = exprMatch.Groups["left"].Value.Trim();
                    var opText    = exprMatch.Groups["op"].Value.Trim();
                    var rightText = exprMatch.Groups["right"].Value.Trim();

                    if (string.IsNullOrEmpty(opText))
                    {
                        // No operator, so this is a truthy property or constant check.

                        object value;
                        if (JavaScriptHelpers.TryParseConstant(leftText, out value))
                        {
                            expr = new TruthyConstantExpression <TSource>(new ConstantToken <TSource>(value));
                        }
                        else
                        {
                            expr = new PathExpression <TSource>(pathToken(leftText));
                        }
                    }
                    else
                    {
                        // Parse the comparison operator.
                        LogicalOperator op = ParseLogicalOperator(opText);

                        // Parse the left-hand token.
                        Token <TSource> leftToken;
                        object          leftValue;
                        if (JavaScriptHelpers.TryParseConstant(leftText, out leftValue))
                        {
                            leftToken = new ConstantToken <TSource>(leftValue);
                        }
                        else
                        {
                            leftToken = pathToken(leftText);
                        }

                        // Parse the right-hand token.
                        Token <TSource> rightToken;
                        object          rightValue;
                        if (JavaScriptHelpers.TryParseConstant(rightText, out rightValue))
                        {
                            rightToken = new ConstantToken <TSource>(rightValue);
                        }
                        else
                        {
                            rightToken = pathToken(rightText);
                        }

                        // Create the expression from "left op right".
                        expr = new CompareExpression <TSource>(leftToken, op, rightToken);
                    }
                }

                if (nextText == "&&" && andGroup == null)
                {
                    // There is currently no active and group and the next expression
                    // will be "ANDed", so start a new and group, beginning with this expression.
                    andGroup = AndExpressionGroup <TSource> .Begin(expr);
                }
                else if (andGroup != null)
                {
                    // There is an active and group expression, so add this expression to it.
                    andGroup.And(expr);
                }
                else if (result != null)
                {
                    // There is currently a result, so or it with this expression.
                    result = result.Or(expr);
                }
                else
                {
                    // There is currently no result, so use this expression as the result.
                    result = new TruthyExpressionWrapper <TSource>(expr);
                }

                // Add the existing group if we have reached the end of the expression, or the next expression will be "ORed".
                if ((string.IsNullOrEmpty(nextText) || nextText == "||") && andGroup != null)
                {
                    if (result == null)
                    {
                        result = andGroup;
                    }
                    else
                    {
                        result = result.Or(andGroup);
                    }
                    andGroup = null;
                }
            }

            return(result);
        }