始终丢弃解析器组合器的结果

Always discard result for a parser combinator

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

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

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

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 => ...

您可以改用<~(只保留左边的顺序组合)或~>(只保留右边的顺序组合)。例如:

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

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