3

我正在编写一个符合跨域身份管理系统的解析器:协议过滤规范。除了“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 工作正常。但链扩展适用于二进制表达式,我需要一元。

4

1 回答 1

3

我找到了答案。这是正确的解析代码:

    Operand = (Parentheses
        .XOr(Literal.Or(AttrPath.Token()))
        .XOr(CaseInsensitiveString)).Token();

    // compareOp = "eq" / "ne" / "co" /
    //        "sw" / "ew" /
    //        "gt" / "lt" /
    //        "ge" / "le"
    Comparison = Parse.XChainOperator(Le.Or(Lt).XOr(Ge.Or(Gt)).XOr(Eq.Or(Ne)).XOr(Sw.Or(Ew)).XOr(Co).XOr(Pr), Operand, FilterExpression.Binary);

    // attrPath SP "pr"
    Presence = Operand.SelectMany(operand => Pr, (operand, pr) => FilterExpression.Unary(pr, operand));

    // attrExp = (attrPath SP "pr") /
    //   (attrPath SP compareOp SP compValue)
    AttributeExpression = Presence.Or(Comparison);

    // logExp    = FILTER SP ("and" / "or") SP FILTER
    LogicalExpression = Parse.XChainOperator(Or.Or(And), AttributeExpression, FilterExpression.Binary);
    Filter = LogicalExpression;

诀窍是先检查操作数,然后再检查操作数,然后使用 Or 而不是 XOrAttributeExpression = Presence.Or(Comparison);

于 2015-12-01T11:05:02.080 回答