Skip to content

lazy

Takes a function that just returns a parser (a thunk). This defers the evaluation of the given parser. Useful for writing recursive parsers.

Available since: v1.0.0-alpha.3

Type declaration

const lazy: (parserThunk: () => Parser, name?: string) => Parser;

Example

In this example we are going to define a parser before it’s declaration.

import { lazy, string } from "rudus";
const parser = lazy(() => helloParser));
const helloParser = string("Hello");
const result = parser.run(`Hello`);

The result of the parser above will be:

{
"input": "Hello",
"isError": false,
"offset": 5,
"result": "Hello"
}