pNot

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

Returns a Parser that succeeds with Unit when parser would fail at the current position, and fails when parser would succeed. No input is consumed in either case.

pNot is a negative lookahead: it inverts the success/failure of parser without ever advancing the index. Use it to assert that a particular pattern does not appear at the current position before committing to another parse.

Behaviour

ConditionResult
parser failsSuccess with Unit; index unchanged
parser succeedsFailure — "Unexpected match at index \"; index unchanged

Type parameters

  • I — the token type.

  • O — the output type of parser (not used in the result).

  • U — the user context type threaded through unchanged.

Example

val notDigit = pNot(pSatisfy<Char, Unit> { it.isDigit() })

val input = ParserInput.of("a1".toList(), Unit)
val result = notDigit(input) // Success(Unit, nextIndex=0, ...) — 'a' is not a digit

val digits = ParserInput.of("1a".toList(), Unit)
val result2 = notDigit(digits) // Failure — '1' would have matched

Return

a Parser that succeeds with Unit only when parser would fail.

Parameters

parser

the parser whose failure is required.

See also