pRepeat

fun <I : Any, O, U : Any> pRepeat(count: Int, parser: Parser<I, O, U>): Parser<I, List<O>, U>

Returns a Parser that runs parser exactly count times in sequence, collecting each output into a List.

The runs are applied left-to-right; each run starts where the previous one left off. If any run fails the whole parser fails at that position, without consuming the tokens matched by the successful preceding runs.

A count of zero always succeeds immediately with an empty list.

Behaviour

ConditionResult
count == 0Success with an empty list; index unchanged
All count runs succeedSuccess with a list of count values; index advanced past all
Any run failsFailure from that run

Type parameters

  • I — the token type consumed by parser.

  • O — the output type of parser; each successful value is collected.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val threeDigits = pRepeat(3, digit)

val input = ParserInput.of("123x".toList(), Unit)
val result = threeDigits(input) // Success(['1','2','3'], nextIndex=3, ...)

Return

a Parser that collects exactly count values on success.

Parameters

count

the exact number of times to run parser; must be >= 0.

parser

the parser to repeat.

See also