Question

Suppose I have a snippet in a StandardTokenParser:

lazy val validWords = Set("param value","param2 value2")

lazy val paramNameCollectionToken = paramNameToken ~ rep(paramNameToken) ^^ {
     case head ~ rest => (head :: rest).mkString(" ")
}

How can I produce the list only if the result of head::rest is contained inside validWords Set?

Was it helpful?

Solution

A simple way to do this is using filter.

val validToken: Parser[String] = paramNameCollection.filter(validWords)

If the filter predicate fails, the parser failure message will be generic, such as "Input doesn't match filter: ..."

The source of filter should contain some clues about how to customise this message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top