lazy val exprBad: Parser[Int] = eParensBad | eLiteralInt
lazy val eParensBad: Parser[Int] = char('(') *> many(whitespace) *> exprBad <* many(whitespace) <* char(')')
lazy val exprGood: Parser[Int] = eParensGood | eLiteralInt
lazy val eParensGood: Parser[Int] = {
for {
_ <- char('(')
_ <- many(whitespace)
e <- exprGood
_ <- many(whitespace)
_ <- char(')')
} yield (e)
}
lazy val eLiteralInt: Parser[Int] = int
def makeItCrash = exprBad.parse("5").done
def worksFine = exprGood.parse("5").done
scala> worksFine
res0: atto.ParseResult[Int] = Done(,5)
scala> makeItCrash
java.lang.StackOverflowError
<stacktrace bouncing between exprBad and eParensBad>
Current import:
"org.tpolecat" %% "atto-core" % "0.4.2"Example code: