value
value(p: Parser, x: *, m?: string): Parser
Applies a parser and, if it succeeds, returns another value.
x
can be anything, so value
is one of the few combinators that can return something other than a string.
value(p, x)
is an optimized implementation of chain(p, () => always(x))
.
Example
const parser = value(string('""'), '"')
const s = parse(parser, '""')
console.log(status(s)) // "ok"
console.log(success(s)) / "\""
const f = parse(parser, '"')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// "
// ^
// Expected '""'
In this example, if the parser string('""')
succeeds, then value
returns a single character ("
). This parser could be used for detecting a quote inside a quoted string in CSV, for instance, where quotation marks are escaped by doubling them.
Parameters
p
: The parser that gets applied to the input.x
: The value forvalue
to return ifp
succeeds.m
: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if
p
succeeds. In this case, the result ofp
is discarded andx
is returned instead.
Failure
- Fails if
p
fails.
Fatal Failure
- Fails fatally if
p
fails fatally.
Throws
- Throws an error if
p
is not a parser. - Throws an error if
m
exists and is not a string.