map
map(p: Parser, fn: function, m?: string): Parser
Applies a parser and returns the value that a function returns when passed the parser's result.
fn can return anything, so map is one of the few combinators that can return something other than a string.
map corresponds to fmap in Haskell (often simply map in other languages), which is a member of the Functor type class. It is often written <$> in languages that support custom operators.
Example
const parser = map(lower(), c => c.toUpperCase())
const s = parse(parser, 'abc')
console.log(status(s)) // "ok"
console.log(success(s)) // "A"
const f = parse(parser, 'ABC')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// ABC
// ^
// Expected a lowercase letter
In this example, a lower parser is used to read a lowercase letter. That character is passed into a function that uppercases it, then that uppercase letter is returned.
Parameters
p: A parser to apply to the input.fn: A function of one argument which gets passed the result ofp. Its return value becomesmap's result.m: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if
psucceeds. Returns the value thatfnreturns when passed the result ofp.
Failure
- Fails if
pfails. In this casefnis never invoked.
Fatal Failure
- Fails fatally if
pfails fatally. In this casefnis never invoked.
Throws
- Throws an error if
pis not a parser. - Throws an error if
fnis not a non-parser function. - Throws an error if
mexists and is not a string.