Well, even if this functionality is not available now, you can write a function that applies ~
a given number of times:
def repExactly(parser: Parser[Unit])(times: Int): Parser[Unit] =
Iterator.iterate(parser)(_ ~ parser).drop(times - 1).next()
Here is a small test:
object Main extends App {
import fastparse._
def repExactly(parser: Parser[Unit])(times: Int): Parser[Unit] =
Iterator.iterate(parser)(_ ~ parser).drop(times - 1).next()
val hexDigit = P( CharIn('0'to'9', 'a'to'f', 'A'to'F') )
def fiveHexDigits = repExactly(hexDigit)(5) ~ End
println(fiveHexDigits.parse("123a"))
println(fiveHexDigits.parse("123ab"))
println(fiveHexDigits.parse("123abc"))
}
And the output is
Failure(hexDigit:4 / CharIn("0123456789abcdefABCDEF"):4 ..."", false)
Success((), 5)
Failure(End:5 ..."c", false)
And here is a generic way to implement this functionality as an operator *
of Parser
(The original implementation of rep
does something quite convoluted, so my implementation may not account for some cases. Also, I didn't test how it works with arguments that have cuts):
object Main extends App {
import fastparse._
implicit class ParserExtension[T](parser: Parser[T]) {
def *[R] (times: Int)(implicit ev: Implicits.Repeater[T, R]): Parser[R] = {
assert(times >= 1)
Iterator.iterate(parser map { t =>
val acc = ev.initial
ev.accumulate(t, acc)
acc
}){ prev: Parser[ev.Acc] =>
(prev ~ parser) map {
case (acc, t) =>
ev.accumulate(t, acc)
acc
}
}.drop(times - 1).next() map (acc => ev.result(acc))
}
}
val hexDigit = P( CharIn('0'to'9', 'a'to'f', 'A'to'F') )
val fiveDigitsSeq = (hexDigit.! * 5) ~ End
println(fiveDigitsSeq.parse("123a")) // Failure ...
println(fiveDigitsSeq.parse("123ab")) // Success(ArrayBuffer(1, 2, 3, a, b), 5)
println(fiveDigitsSeq.parse("123abc")) // Failure ...
println()
val fiveDigitsStr = (hexDigit * 5).! ~ End
println(fiveDigitsStr.parse("123a")) // Failure ...
println(fiveDigitsStr.parse("123ab")) // Success(123ab, 5)
println(fiveDigitsStr.parse("123abc")) // Failure ...
}