始终丢弃解析器组合器的结果(Always discard result for a parser combinator)

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

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

如何在当前版本的库中实现...即只是简单地做

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

In earlier versions of scala the discard method existed to throw away the results of a parser:

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

How may that be achieved in current versions of the library .. i.e while simply doing

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

最满意答案

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

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

甚至:

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

You can simply reference the extra parser result but not use it:

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

or even:

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

更多推荐