到目前为止,您拥有的是一个基本的比较表达式解析器。看起来您想将其包装在处理具有子表达式支持的逻辑表达式(、等)and
的解析器中。or
我最初发布的代码是从我仍在处理的测试不佳的代码中提取的,这些代码不能处理具有多个术语的语句。我对这种ChainOperator
方法的理解显然是不完整的。
Parse.ChainOperator
是允许您指定运算符并让它们在表达式中出现 0 到多次的方法。我一直在假设它是如何工作的,结果证明是错误的。
我重写了代码并添加了一些位以使其更易于使用:
// Helpers to make access simpler
public static class Condition
{
// For testing, will fail all variable references
public static Expression<Func<object, bool>> Parse(string text)
=> ConditionParser<object>.ParseCondition(text);
public static Expression<Func<T, bool>> Parse<T>(string text)
=> ConditionParser<T>.ParseCondition(text);
public static Expression<Func<T, bool>> Parse<T>(string text, T instance)
=> ConditionParser<T>.ParseCondition(text);
}
public static class ConditionParser<T>
{
static ParameterExpression Parm = Expression.Parameter(typeof(T), "_");
public static Expression<Func<T, bool>> ParseCondition(string text)
=> Lambda.Parse(text);
static Parser<Expression<Func<T, bool>>> Lambda =>
OrTerm.End().Select(body => Expression.Lambda<Func<T, bool>>(body, Parm));
// lowest priority first
static Parser<Expression> OrTerm =>
Parse.ChainOperator(OpOr, AndTerm, Expression.MakeBinary);
static Parser<ExpressionType> OpOr = MakeOperator("or", ExpressionType.OrElse);
static Parser<Expression> AndTerm =>
Parse.ChainOperator(OpAnd, NegateTerm, Expression.MakeBinary);
static Parser<ExpressionType> OpAnd = MakeOperator("and", ExpressionType.AndAlso);
static Parser<Expression> NegateTerm =>
NegatedFactor
.Or(Factor);
static Parser<Expression> NegatedFactor =>
from negate in Parse.IgnoreCase("not").Token()
from expr in Factor
select Expression.Not(expr);
static Parser<Expression> Factor =>
SubExpression
.Or(BooleanLiteral)
.Or(BooleanVariable);
static Parser<Expression> SubExpression =>
from lparen in Parse.Char('(').Token()
from expr in OrTerm
from rparen in Parse.Char(')').Token()
select expr;
static Parser<Expression> BooleanValue =>
BooleanLiteral
.Or(BooleanVariable);
static Parser<Expression> BooleanLiteral =>
Parse.IgnoreCase("true").Or(Parse.IgnoreCase("false"))
.Text().Token()
.Select(value => Expression.Constant(bool.Parse(value)));
static Parser<Expression> BooleanVariable =>
Parse.Regex(@"[A-Za-z_][A-Za-z_\d]*").Token()
.Select(name => VariableAccess<bool>(name));
static Expression VariableAccess<TTarget>(string name)
{
MemberInfo mi = typeof(T).GetMember(name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public).FirstOrDefault();
var targetType = typeof(TTarget);
var type =
(mi is FieldInfo fi) ? fi.FieldType :
(mi is PropertyInfo pi) ? pi.PropertyType :
throw new ParseException($"Variable '{name}' not found.");
if (type != targetType)
throw new ParseException($"Variable '{name}' is type '{type.Name}', expected '{targetType.Name}'");
return Expression.MakeMemberAccess(Parm, mi);
}
// Helper: define an operator parser
static Parser<ExpressionType> MakeOperator(string token, ExpressionType type)
=> Parse.IgnoreCase(token).Token().Return(type);
}
还有一些例子:
static class Program
{
static void Main()
{
// Parser with no input
var condition1 = Condition.Parse("true and false or true");
Console.WriteLine(condition1.ToString());
var fn1 = condition1.Compile();
Console.WriteLine("\t={0}", fn1(null));
// Parser with record input
var record1 = new { a = true, b = false };
var record2 = new { a = false, b = true };
var condition2 = Condition.Parse("a and b or not a", record);
Console.WriteLine(condition2.ToString());
var fn2 = condition2.Compile();
Console.WriteLine("\t{0} => {1}", record1.ToString(), fn2(record1));
Console.WriteLine("\t{0} => {1}", record2.ToString(), fn2(record2));
}
}
您仍然需要添加自己的解析器来处理比较表达式等。BooleanValue
在现有条款之后将它们插入解析器:
static Parser<Expression> BooleanValue =>
BooleanLiteral
.Or(BooleanVariable)
.Or(SearchCondition);
我正在使用更多 C# 样式的过滤器规范做类似的事情,在解析阶段进行类型检查,并将字符串与数字的解析器分开。