Skip to content
On this page

error primitive

Signature

ts
function error<T>(parser: Parser<T>, expected: string): Parser<T>

Description

error combinator allows to replace parser's error message with expected.

Usage

ts
const Parser = error(
  choice(
    string('true'),
    string('false')
  ),
  `expecting either 'true' or 'false'`
)

Success

ts
run(Parser).with('true')

{
  isOk: true,
  span: [ 0, 4 ],
  pos: 4,
  value: 'true'
}

Failure

ts
run(Parser).with('maybe')

{
  isOk: false,
  span: [ 0, 5 ],
  pos: 5,
  expected: "expecting either 'true' or 'false'"
}