buntil
buntil(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 bbetween 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.
If p fails before e succeeds and input was consumed, the state will backtrack to where it was before p was applied the first time.
Example
const parser = buntil(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)) // "fail"
console.log(failure(t)) // Parse error at (line 1, column 1):
//
// ab12>
// ^
// The parser backtracked after:
//
// 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
pfails beforeesucceeds. If any input was consumed, backtracking will occur and an additional error message will be provided that details the circumstances of the backtracking.
Fatal Failure
- Fails fatally if either
porefail fatally.
Throws
- Throws an error if either
poreare not parsers. - Throws an error if
mexists and is not a string.