pBetween

fun <I : Any, O1, O2, O3, U : Any> pBetween(open: Parser<I, O1, U>, close: Parser<I, O3, U>, inner: Parser<I, O2, U>): Parser<I, O2, U>

Returns a Parser that runs open, then inner, then close in sequence, succeeding with inner's value and discarding the delimiter values.

pBetween is sugar for wrapping a parser between two delimiters — for example, parentheses, brackets, or quotation marks.

Behaviour

ConditionResult
open failsFailure from open
open succeeds, inner failsFailure from inner
open and inner succeed, close failsFailure from close
All three succeedSuccess with inner's value; index advanced past all three

Type parameters

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

  • O1 — the output type of open; discarded.

  • O2 — the output type of inner; returned on success.

  • O3 — the output type of close; discarded.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val open = pSatisfy<Char, Unit> { it == '(' }
val close = pSatisfy<Char, Unit> { it == ')' }
val parser = pBetween(open, close, digit)

val input = ParserInput.of("(5)".toList(), Unit)
val result = parser(input) // Success('5', nextIndex=3, ...)

Return

a Parser that succeeds with inner's value.

Parameters

open

the parser for the opening delimiter; its value is discarded.

close

the parser for the closing delimiter; its value is discarded.

inner

the parser for the content between the delimiters.

See also