/// <summary>
        /// Returns the definitions for logic operators used within the language.
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerable <GrammerDefinition> LogicalOperatorDefinitions()
        {
            return(new GrammerDefinition[]
            {
                new BinaryOperatorDefinition(
                    name: "EQ",
                    regex: @"\b(eq)\b",
                    orderOfPrecedence: 11,
                    expressionBuilder: ConvertEnumsIfRequired((left, right) => Expression.Equal(left, right))),
                new BinaryOperatorDefinition(
                    name: "NE",
                    regex: @"\b(ne)\b",
                    orderOfPrecedence: 12,
                    expressionBuilder: ConvertEnumsIfRequired((left, right) => Expression.NotEqual(left, right))),

                new BinaryOperatorDefinition(
                    name: "GT",
                    regex: @"\b(gt)\b",
                    orderOfPrecedence: 13,
                    expressionBuilder: (left, right) => Expression.GreaterThan(left, right)),
                new BinaryOperatorDefinition(
                    name: "GE",
                    regex: @"\b(ge)\b",
                    orderOfPrecedence: 14,
                    expressionBuilder: (left, right) => Expression.GreaterThanOrEqual(left, right)),

                new BinaryOperatorDefinition(
                    name: "LT",
                    regex: @"\b(lt)\b",
                    orderOfPrecedence: 15,
                    expressionBuilder: (left, right) => Expression.LessThan(left, right)),
                new BinaryOperatorDefinition(
                    name: "LE",
                    regex: @"\b(le)\b",
                    orderOfPrecedence: 16,
                    expressionBuilder: (left, right) => Expression.LessThanOrEqual(left, right)),

                new BinaryOperatorDefinition(
                    name: "AND",
                    regex: @"\b(and)\b",
                    orderOfPrecedence: 17,
                    expressionBuilder: (left, right) => Expression.And(left, right)),
                new BinaryOperatorDefinition(
                    name: "OR",
                    regex: @"\b(or)\b",
                    orderOfPrecedence: 18,
                    expressionBuilder: (left, right) => Expression.Or(left, right)),

                new UnaryOperatorDefinition(
                    name: "NOT",
                    regex: @"\b(not)\b",
                    orderOfPrecedence: 19,
                    operandPosition: RelativePosition.Right,
                    expressionBuilder: (arg) => {
                    ExpressionConversions.TryBoolean(ref arg);
                    return Expression.Not(arg);
                })
            });
        }
Beispiel #2
0
        /// <summary>
        /// Returns the definitions for logic operators used within the language.
        /// </summary>
        /// <returns></returns>
        protected override IEnumerable <GrammerDefinition> LogicalOperatorDefinitions()
        {
            return(new GrammerDefinition[]
            {
                new BinaryOperatorDefinition(
                    name: "EQ",
                    regex: @"eq",
                    orderOfPrecedence: 11,
                    expressionBuilder: ConvertEnumsIfRequired((left, right) => Expression.Equal(left, right))),
                new BinaryOperatorDefinition(
                    name: "NE",
                    regex: @"ne",
                    orderOfPrecedence: 12,
                    expressionBuilder: ConvertEnumsIfRequired((left, right) => Expression.NotEqual(left, right))),

                new BinaryOperatorDefinition(
                    name: "GT",
                    regex: @"gt",
                    orderOfPrecedence: 13,
                    expressionBuilder: (left, right) => Expression.GreaterThan(left, right)),
                new BinaryOperatorDefinition(
                    name: "GE",
                    regex: @"ge",
                    orderOfPrecedence: 14,
                    expressionBuilder: (left, right) => Expression.GreaterThanOrEqual(left, right)),

                new BinaryOperatorDefinition(
                    name: "LT",
                    regex: @"lt",
                    orderOfPrecedence: 15,
                    expressionBuilder: (left, right) => Expression.LessThan(left, right)),
                new BinaryOperatorDefinition(
                    name: "LE",
                    regex: @"le",
                    orderOfPrecedence: 16,
                    expressionBuilder: (left, right) => Expression.LessThanOrEqual(left, right)),

                new BinaryOperatorDefinition(
                    name: "AND",
                    regex: @"and",
                    orderOfPrecedence: 17,
                    expressionBuilder: (left, right) => Expression.AndAlso(left, right)),
                new BinaryOperatorDefinition(
                    name: "OR",
                    regex: @"or",
                    orderOfPrecedence: 18,
                    expressionBuilder: (left, right) => Expression.OrElse(left, right)),

                new UnaryOperatorDefinition(
                    name: "NOT",
                    regex: @"not",
                    orderOfPrecedence: 19,
                    operandPosition: RelativePosition.Right,
                    expressionBuilder: (arg) => {
                    ExpressionConversions.TryBoolean(ref arg);
                    return Expression.Not(arg);
                })
            });
        }
        /// <summary>
        /// Parses the specified text converting it into a predicate expression
        /// </summary>
        /// <typeparam name="T">The input type</typeparam>
        /// <param name="text">The text to parse.</param>
        /// <returns></returns>
        public Expression <Func <T, bool> > Parse <T>(string text)
        {
            var parameters = new[] { Expression.Parameter(typeof(T)) };
            var body       = language.Parse(text, parameters);

            ExpressionConversions.TryBoolean(ref body);

            return(Expression.Lambda <Func <T, bool> >(body, parameters));
        }