Skip to main content
Module

x/pazza/src/misc.ts

Parser combinators library designed for Deno, but also works on browsers and Node.js.
Go to Latest
File
import type { IParser, Input, Result } from "./core.ts";
/** * Construct a parser only when it's needed. * This is useful when building a recursive parser. * * // We don't call the `parse` method, * // so the `recursiveParser` won't be constructed. * lazy(() => recursiveParser()); * * @param fn Function that returns a parser. */export function lazy<O, E, I extends Input>( fn: () => IParser<O, E, I>,): IParser<O, E, I> { function parse<C>( input: I, context: C = Object.create(null), ): Result<I, O, E, C> { parse.parser ??= parse.fn();
return parse.parser(input, context); } parse.fn = fn; parse.parser = null as IParser<O, E, I> | null;
return parse;}