until
until(p: Parser, e: Parser, m?: string): Parser
Applies a content parser zero or more times as long as an end parser continues to fail. Once the end parser succeeds, the content parser's successes are collected into an array and returned.
This parser differs from between in that the e parser is applied first; because of this, there is no possibility of content parser p consuming whatever e is supposed to match and thereby causing the combinator to fail.
The operation of this parser in EBNF is (!e p)* e.
Example
const parser = until(letter(), char('>'))
const s = parse(parser, 'abcd>')
console.log(status(s)) // "ok"
console.log(success(s)) // ["a", "b", "c", "d"]
const f = parse(parser, '1234>')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// 1234>
// ^
// Expected a letter
const t = parse(parser, 'ab12>')
console.log(status(t)) // "fatal"
console.log(failure(t)) // Parse error at (line 1, column 3):
//
// ab12>
// ^
// Expected a letter
Parameters
p: The content parser. Afteresucceeds, the prior results of this parser are returned as an array.e: The end parser.pwill continue to be applied untilesucceeds. Its result is discarded.m: The optional expected error message that will take the place of the default error message.
Success
- Succeeds as long as
esucceeds beforepfails. Returns the results ofpas an array. Ifesucceeds on the first attempt,pis never applied and the returned array is empty.
Failure
- Fails if
pandeboth fail initially. - Fails if
pfails beforeesucceeds and prior successes frompdid not consume any input.
Fatal Failure
- Fails fatally if either
porefail fatally. - Fails fatally if
pfails beforeesucceeds and prior successes frompconsumed some input.
Throws
- Throws an error if either
poreare not parsers. - Throws an error if
mexists and is not a string.