Closed aborg0 closed 1 month ago
Workaround: introduce a new method for the rep
+ map
like this (haven't checked with this example):
def expression[_: P]: P[Int] = (P("(" ~
multiExpression ~ ")") | integer).flatMap(c => Pass(c))
def multiExpression[_: P]: P[Int] = expression.rep(1, ";").map(_.head)
I hit this same issue (v2.2.4). Thanks for the workaround @aborg0 !
Here's my scenario:
// doesn't work
def parseInt[_: P]: P[Int] =
CharsWhileIn("0-9").rep.!.flatMapX { str =>
Try(str.toInt) match {
case Success(i) => Pass(i)
case _ => Fail
}
}
// works
def parseInt[_: P]: P[Int] = {
def parseIntStr = CharsWhileIn("0-9").rep.!
parseIntStr.flatMapX { str =>
Try(str.toInt) match {
case Success(i) => Pass(i)
case _ => Fail
}
}
}
I get the same issue, thanks for the workarounds!
This still occurs in Scala 2.13.14 and FastParse 3.1.1, but seems to be fixed in Scala 3.4.2, so I'm going to call it done
Code to reproduce with fastparse 2.1.0:
It compile without
flatMap
:and also compiles without the inner repeats:
Full error message:
Scastie reproduction: https://scastie.scala-lang.org/KTzjwsYsQLyccDCG6gZVgA