pMany

fun <I : Any, O, U : Any> pMany(parser: Parser<I, O, U>): Parser<I, List<O>, U>

Returns a Parser that runs parser repeatedly until it fails, collecting each output into a List. Always succeeds, returning an empty list if parser fails on the first attempt.

pMany is the zero-or-more repetition combinator. For one-or-more, pair it with an initial required match using pAnd or pBind.

Note: parser must consume at least one token on each success. If it succeeds without advancing the index, pMany stops immediately to prevent an infinite loop.

Behaviour

ConditionResult
parser fails on first attemptSuccess with an empty list; index unchanged
parser succeeds N times then failsSuccess with N values; index advanced past all
parser succeeds without advancingSuccess with values collected so far; loop stops

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 letter = pSatisfy<Char, Unit> { it.isLetter() }
val word = pMany(letter)

val input = ParserInput.of("abc!".toList(), Unit)
val result = word(input) // Success(['a','b','c'], nextIndex=3, ...)

Return

a Parser that always succeeds with a (possibly empty) list of values.

Parameters

parser

the parser to run repeatedly.

See also