【问题标题】:Always discard result for a parser combinator始终丢弃解析器组合器的结果
【发布时间】:2015-11-15 21:13:11
【问题描述】:

在早期版本的 scala 中,存在丢弃方法以丢弃解析器的结果:

lazy val throwThisAway: Parser[String] = (ows ~> discard(comma | EOF | EOL)) <~ ows

如何在当前版本的库中实现这一点......即在简单地做的时候

otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ b // only 2, not 3, parser results

【问题讨论】:

    标签: scala parser-combinators


    【解决方案1】:

    您可以简单地引用额外的解析器结果但不使用它:

    otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ x ~ b => // eg.: SomeCaseClass(a,b)
    

    甚至:

    otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ _ ~ b => ...
    

    【讨论】:

    • 好吧,我猜这就是我们所得到的。旧方法有时更方便。
    【解决方案2】:

    您可以改用&lt;~(仅保留左侧的顺序合成)或~&gt;(仅保留右侧的顺序合成)。例如:

    (otherParser <~ throwThisAway) ~ anotherParser ^^ { case a ~ b => ... }
    

    或者

    otherParser ~ (throwThisAway ~> anotherParser) ^^ { case a ~ b => ... }
    

    【讨论】:

    • 谢谢.. 但这就是我的解析器中已经的全部内容。请注意,您必须将括号放在它周围。这个问题的目的是获得一种避免这种情况的方法。
    • 对不起,我没有注意到...如果你定义一个解析器,比如otherParser &lt;~ throwThisAway,然后在最终的解析器中使用它,你可以避免括号。但不幸的是,这不太清楚......
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多