我正在编写一个符合跨域身份管理系统的解析器:协议过滤规范。除了“pr”运算符,我几乎可以用 Sprache 解析任何表达式。无法理解如何使其正常工作。
这是主要的解析内容:
{
And = Operator("and", "And");
Or = Operator("or", "Or");
Le = Operator("le", "LessThanOrEqual");
Lt = Operator("lt", "LessThan");
Gt = Operator("gt", "GreaterThan");
Ge = Operator("ge", "GreaterThanOrEqual");
Eq = Operator("eq", "Equal");
Ne = Operator("ne", "NotEqual");
Co = Operator("co", "Contains");
Sw = Operator("sw", "StartsWith");
Ew = Operator("ew", "EndsWith");
Pr = Operator("pr", "Present");
...
Operand = (Parentheses
.XOr(Literal.Or(AttrPath.Token()))
.XOr(CaseInsensitiveString)).Token();
//compareOp = "eq" / "ne" / "co" /
// "sw" / "ew" /
// "gt" / "lt" /
// "ge" / "le"
CompareExpression = Parse.XChainOperator(Le.Or(Lt).XOr(Ge.Or(Gt)).XOr(Eq.Or(Ne)).XOr(Sw.Or(Ew)).XOr(Co).XOr(Pr), Operand, FilterExpression.Binary);
PresentsExpression = Parse.XChainRightOperator(Pr, Operand, FilterExpression.Binary).Or(CompareExpression);
// logExp = FILTER SP ("and" / "or") SP FILTER
AndExpression = Parse.XChainOperator(And, CompareExpression, FilterExpression.Binary);
// logExp = FILTER SP ("and" / "or") SP FILTER
OrExpression = Parse.XChainOperator(Or, AndExpression, FilterExpression.Binary);
Filter = OrExpression;
}
private static Parser<string> Operator(string op, string opName)
{
return Parse.String(op).Token().Return(opName);
}
这是我添加“pr”解析的地方:
Parse.XChainRightOperator(Pr, Operand, FilterExpression.Binary).Or(CompareExpression);
此过滤器失败:title pr and addresses[type eq \"work\"].streetAddress eq \"workStreet\"
这没关系:
addresses[type eq \"work\"].streetAddress eq \"workStreet\"
and (userType eq \"Employee\" or userType eq \"ParttimeEmployee\")
也许有人可以帮我解决这个问题。
编辑:也许我应该在某个时候将运算符与 XChainRightOperator 链接起来,因为 pr title 工作正常。但链扩展适用于二进制表达式,我需要一元。