Skip to content

failure

Always returns a failing parser with the given errorMessage. Typically used inside a contextual parser.

Available since: v1.0.0-alpha.10

Type declaration

const failure: (errorMessage: string, name?: string) => Parser;

Example

import { sequenceOf, string, failure } from "rudus";
const parser = sequenceOf([string("Hello"), failure("Something failed!")]);
const result = parser.run("Hello");

The result of the parser will be:

{
"input": "Hello",
"isError": true,
"errorMessage": "Something failed!",
"offset": 5,
"result": "Hello"
}