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
| Condition | Result |
|---|---|
| open fails | Failure from open |
| open succeeds, inner fails | Failure from inner |
| open and inner succeed, close fails | Failure from close |
| All three succeed | Success with inner's value; index advanced past all three |
Type parameters
I — the shared token type consumed by all parsers.
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, ...)Content copied to clipboard
Return
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.