What is the correct way to parse a list consuming all but the last element using scala parser-combinators? For example, if I want to parse "spam spam spam spam spam and eggs" into List("spam", "spam", "spam", "spam") ~ "spam and eggs".
If I just use "spam".+, then all the spam is consumed before I get to match "spam and eggs". Naturally, I could match on "and eggs" but in practice I might already have a complicated "spam and eggs" parser that I'd rather not alter.
("spam and eggs" | "spam").+would work for this case, if accepting "spam and eggs spam" etc. is acceptable. If not and nobody does earlier, I'll try to answer tomorrow.def allButOne[T](p:Parser[T]):Parser[List[T]] = ((p ~ guard(p) ~ allButOne(p)) ^^ { case a ~ b ~ c => List(a) ++ c })|(p <~ guard(p) ^^ { a => List(a) })but wondered if there was a more natural pattern.