pSatisfy

fun <I : Any, U : Any> pSatisfy(predicate: (I) -> Boolean): Parser<I, I, U>

Returns a Parser that consumes exactly one token if predicate returns true for it, and fails otherwise.

pSatisfy is the fundamental building block for token-level parsers. All character and token matchers are typically implemented in terms of it.

Behaviour

ConditionResult
Input is exhaustedFailure — "Unexpected end of input"
predicate(current) is trueSuccess with the matched token; index advances by 1
predicate(current) is falseFailure — "Unexpected \ at index \"

Type parameters

  • I — the token type.

  • U — the user context type threaded through unchanged. Use Unit when no context is needed.

Example

val isDigit: Parser<Char, Char, Unit> = pSatisfy { it.isDigit() }

val input = ParserInput.of("42".toList(), Unit)
val result = isDigit(input) // Success('4', nextIndex=1, ...)

Return

a Parser that succeeds with the matching token or fails with a diagnostic message.

Parameters

predicate

a function that tests each token; should be side-effect-free.

See also

pChar