“Sprache”解析器`Present`语义

时间:2015-11-30 22:23:20

标签: c# parsing sprache

我正在编写一个符合System for Cross-domain Identity Management: Protocol Filtering规范的解析器。我能用Sprache解析几乎所有的表达,除了" pr"运营商。无法解决如何使其正常工作的问题。

这是主要的解析内容:

   {
        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);
    }

这是我添加&#34; pr&#34;解析:

 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工作正常。但链扩展用于二进制表达式,我需要一元。

1 个答案:

答案 0 :(得分:1)

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

    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;

Trick首先检查操作数并在其后执行操作并在AttributeExpression = Presence.Or(Comparison);中使用Or而不是XOr