I just want to parse the following string up until the END token then ignore the rest:
val input = """
0)
blah1
blah2
blah3
1)
blah4
blah5
END
blah6
"""
Using
object Pars extends RegexParsers {
def strings: Parser[List[String]] = rep(str) <~ end
def str: Parser[String] = ".*".r
def end: Parser[String] = "END" <~ rep(".*".r)
}
Pars.parseAll(Pars.strings, input)
goes into an infinite loop and an OutOfMemoryError. What am I doing wrong, and how to fix this?
Just do not use
parseAll. Useparseinstead.As for the problem you have, you are saying the same thing twice in a few places:
repand*both mean “any number of repetitions”. Now,.*matches the empty string, sorepthen proceed to match an infinite number of empty strings.Here’s how I’d rewrite it: