pLookAhead

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

Returns a Parser that runs parser and, if it succeeds, returns its value without consuming any input.

pLookAhead peeks ahead in the input: the index is always reset to its original position after a successful match. A failure from parser is propagated unchanged, also without consuming input.

Behaviour

ConditionResult
parser succeedsSuccess with parser's value; index unchanged
parser failsFailure propagated from parser; index unchanged

Type parameters

  • I — the token type consumed by parser.

  • O — the output type of parser.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val peek = pLookAhead(digit)

val input = ParserInput.of("1a".toList(), Unit)
val result = peek(input) // Success('1', nextIndex=0, ...) — index stays at 0

Return

a Parser that succeeds without advancing the index.

Parameters

parser

the parser to run as a lookahead.

See also