pSatisfy
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
| Condition | Result |
|---|---|
| Input is exhausted | Failure — "Unexpected end of input" |
predicate(current) is true | Success with the matched token; index advances by 1 |
predicate(current) is false | Failure — "Unexpected \ |
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, ...)Content copied to clipboard
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