import { Parser, Result, Input, Output, List, Node } from '../parser'; import { always } from '../control/state'; type ResultM = List> | undefined; export function bind

(parser: Parser.IntermediateParser

, f: (nodes: List>>, Input: Parser.Input

, output: Output>) => ResultM>): P; export function bind

(parser: P, f: (nodes: List>>, Input: Parser.Input

, output: Output>) => ResultM>): P; export function bind(parser: Parser, Parser.SubParsers

>, f: (nodes: List>, Input: Parser.Input

, output: Output>) => ResultM>): P; export function bind(parser: P, f: (nodes: List>>, Input: Parser.Input

, output: Output>) => ResultM): Parser, Parser.SubParsers

>; export function bind(parser: Parser, f: (nodes: List>, Input: Input, output: Output) => ResultM): Parser { assert(parser); interface Memory { readonly position: number; } return always>>([ (input, output) => { input.memory = { position: input.position, }; output.push(); return output.context; }, parser, (input, output) => { const nodes = output.pop(); if (!output.state) return; input.range = input.position - input.memory.position; const result = f(nodes, input, output); return result ? output.import(result) : Result.fail; }, ]); }