bcount
bcount(p: Parser, n: number, m?: string): Parser
Applies a parser a certain number of times, collecting the results into an array to return.
The parser p must succeed the full n times for bcount to succeed. Any fewer successes results in failure. Any input that is consumed in the process will be backtracked.
Example
const parser = bcount(letter(), 3)
const s = parse(parser, 'abc')
console.log(status(s)) // "ok"
console.log(success(s)) // ["a", "b", "c"]
const f = parse(parser, '123')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// 123
// ^
// Expected a letter
const t = parse(parser, 'ab3')
console.log(status(t)) // "fail"
console.log(failure(t)) // Parse error at (line 1, column 1):
//
// ab3
// ^
// The parser backtracked after:
//
// Parse error at (line 1, column 3):
//
// ab3
// ^
// Expected a letter
Parameters
p: The parser to apply. Its results are returned in an array.n: The number of times thatpis applied.m: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if
psucceedsntimes. The results are collected into an array and returned.
Failure
- Fails if
pfails before it has succeededntimes. 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
pfails fatally before it has succeededntimes.
Throws
- Throws an error if
pis not a parser. - Throws an error if
nis not a number. - Throws an error if
mexists and is not a string.