pSequence

fun <I : Any, O, U : Any> pSequence(parsers: List<Parser<I, O, U>>): Parser<I, List<O>, U>

Returns a Parser that runs each parser in parsers in order, collecting every output into a List.

Unlike pRepeat, which applies a single parser count times, pSequence applies a different parser at each position. The runs proceed left-to-right; each run starts where the previous one left off. If any parser fails the whole combined parser fails immediately at that position.

An empty parsers list always succeeds with an empty list.

Behaviour

ConditionResult
parsers is emptySuccess with an empty list; index unchanged
All parsers succeedSuccess with a list of values; index advanced past all
Any parser failsFailure from that parser

Type parameters

  • I — the shared token type consumed by all parsers.

  • O — the shared output type; all parsers must produce the same type.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val letter = pSatisfy<Char, Unit> { it.isLetter() }
val parser = pSequence(listOf(digit, letter, digit))

val input = ParserInput.of("1a2".toList(), Unit)
val result = parser(input) // Success(['1','a','2'], nextIndex=3, ...)

Return

a Parser that collects the output of each parser on success.

Parameters

parsers

the ordered list of parsers to run.

See also