bchain
bchain(p: Parser, fn: function, m?: string): Parser
Applies a parser to the input, passes its result to a function, and then applies the parser that function returns to the input.
If p
succeeds but the parser that is returned by fn
fails, the state will backtrack to the location where p
was originally applied and bchain
will fail non-fatally.
Example
const parser = bchain(any, c => char(c))
const s = parse(parser, 'aabbcc')
console.log(status(s)) // "ok"
console.log(success(s)) // "a"
const f = parse(parser, '')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
//
// ^
// Expected any character
// Note: failure occurred at the end of input
const t = parse(parser, 'abc')
console.log(status(t)) // "fail"
console.log(failure(t)) // Parse error at (line 1, column 1):
//
// abc
// ^
// The parser backtracked after:
//
// Parse error at (line 1, column 2):
//
// abc
// ^
// Expected 'a'
Parameters
p
: The parser which is applied first and whose result is passed intofn
.fn
: A function which, when passed the result ofp
, should return a second parser to be applied to the input.m
: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if both
p
and the parser returned byfn
succeed. Returns the result of the parser returned byfn
.
Failure
- Fails if either
p
or the parser returned byfn
fails. 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
p
or the parser returned byfn
fail fatally.
Throws
- Throws an error if
p
is not a parser. - Throws an error if
fn
is not a non-parser function. - Throws an error if
m
exists and is not a string. - Throws an error if
p
succeeds and the value returned byfn
is not a parser.