pAnd

fun <I : Any, O1, O2, U : Any> pAnd(first: Parser<I, O1, U>, second: Parser<I, O2, U>): Parser<I, Pair<O1, O2>, U>

Returns a Parser that runs first and then second in sequence, combining their outputs into a Pair.

If either parser fails the combined parser fails immediately, without consuming any input beyond what the failing parser had already consumed.

Behaviour

ConditionResult
first failsFailure propagated from first
first succeeds, second failsFailure propagated from second
Both succeedSuccess with Pair(first.value, second.value); index advanced past both

Type parameters

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

  • O1 — the output type of first.

  • O2 — the output type of second.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val letter = pSatisfy<Char, Unit> { it.isLetter() }
val digitThenLetter = pAnd(digit, letter)

val input = ParserInput.of("1a".toList(), Unit)
val result = digitThenLetter(input) // Success(Pair('1', 'a'), nextIndex=2, ...)

Return

a Parser that produces a Pair of both outputs on success.

Parameters

first

the parser to run first.

second

the parser to run after first succeeds.

See also