这就是你要找的吗?
import scala.util.parsing.combinator.syntactical._
object CAQuery extends StandardTokenParsers {
lexical.reserved += ("START", "END")
lexical.delimiters += (" ")
def query:Parser[Any]= "START" ~> rep1(ident) <~ "END"
def parse(s:String) = {
val tokens = new lexical.Scanner(s)
phrase(query)(tokens)
}
}
println(CAQuery.parse("""START a END""")) //List(a)
println(CAQuery.parse("""START a b c END""")) //List(a, b, c)
如果您想了解更多详细信息,可以查看
this blog post