bapply
bapply(p: Parser, q: Parser, m?: string): Parser
Applies two parsers, the second of which must return a function. Returns the result of that function when the result of the other parser is passed to it.
If p
succeeds but q
fails, the parser will backtrack to the location where p
was originally applied and bapply
will fail non-fatally.
Example
const parser = bapply(left(letter, digit), always(c => c.toUpperCase()))
const s = parse(parser, 'a1')
console.log(status(s)) // "ok"
console.log(success(s)) // "A"
const f = parse(parser, '1a')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// 1a
// ^
// Expected a letter
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
: A parser whose result is passed to the function that results fromq
.q
: A parser that returns a function.m
: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if both
p
andq
succeed. Returns the result of the function returned byq
when applied to the result ofp
.
Failure
- Fails if
p
orq
fails.
Fatal Failure
- Fails fatally if either
p
orq
fails fatally.
Throws
- Throws an error if either
p
orq
are not parsers. - Throws an error if
m
exists and is not a string. - Throws an error if
q
succeeds but does not return a non-parser function.