import type { ImplicitParjser, ParjsCombinator, Parjser } from "../../index"; import type { CombinatorInput } from "../combinated"; import { wrapImplicit } from "../wrap-implicit"; /** * Represents the given function as a Parjs combinator. * * @param f The combinator function. */ export function defineCombinator( f: (act: CombinatorInput) => Parjser ): ParjsCombinator { return (x: ImplicitParjser): Parjser => { const resolved = wrapImplicit(x); return f(resolved); }; } /** * Creates a new combinator by composing a series of functions. * * @param f1 A single function. It will be returned. */ export function composeCombinator(f1: ParjsCombinator): ParjsCombinator; /** * Creates a new combinator by composing a series of functions. * * @param f1 The first function in the series. * @param f2 The 2nd function. */ export function composeCombinator( f1: ParjsCombinator, f2: ParjsCombinator ): ParjsCombinator; /** * Creates a new combinator by composing a series of functions. * * @param f1 The first function in the series. * @param f2 The 2nd function. * @param f3 The 3rd function. */ export function composeCombinator( f1: ParjsCombinator, f2: ParjsCombinator, f3: ParjsCombinator ): ParjsCombinator; export function composeCombinator(...fs: ParjsCombinator[]) { return (x: ImplicitParjser) => { let last = x; for (const f of fs) { last = f(last); } return last; }; } /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The single combinator to apply. */ export function pipe(source: ImplicitParjser, cmb1: ParjsCombinator): Parjser; /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. */ export function pipe( source: ImplicitParjser, cmb1: ParjsCombinator, cmb2: ParjsCombinator ): Parjser; /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. * @param cmb3 The third combinator to apply. */ export function pipe( source: ImplicitParjser, cmb1: ParjsCombinator, cmb2: ParjsCombinator, cmb3: ParjsCombinator ): Parjser; /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. * @param cmb3 The third combinator to apply. * @param cmb4 The fourth combinator to apply. */ export function pipe( source: ImplicitParjser, cmb1: ParjsCombinator, cmb2: ParjsCombinator, cmb3: ParjsCombinator, cmb4: ParjsCombinator ): Parjser; /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. * @param cmb3 The third combinator to apply. * @param cmb4 The fourth combinator to apply. * @param cmb5 The fifth combinator to apply. */ export function pipe( source: ImplicitParjser, cmb1: ParjsCombinator, cmb2: ParjsCombinator, cmb3: ParjsCombinator, cmb4: ParjsCombinator, cmb5: ParjsCombinator ): Parjser; /** * The chaining or piping operator. Applies a sequence of combinators to this parser, feeding the * result of one into the input of the next. * * @param source The source parser on which to apply the combinators. * @param cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. * @param cmb3 The third combinator to apply. * @param cmb4 The fourth combinator to apply. * @param cmb5 The fifth combinator to apply. * @param cmb6 The sixth combinator to apply. */ export function pipe( source: ImplicitParjser, cmb1: ParjsCombinator, cmb2: ParjsCombinator, cmb3: ParjsCombinator, cmb4: ParjsCombinator, cmb5: ParjsCombinator, cmb6: ParjsCombinator ): Parjser; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function pipe(source: any, ...funcs: ((x: any) => any)[]) { let last = wrapImplicit(source); for (const func of funcs) { last = func(last); } return last; }