import type { ParjserDebugFunction } from "./parser"; import type { FailureInfo, ParjsResult } from "./result"; import type { ParsingState, UserState } from "./state"; import type { ImplicitParjser } from "./wrap-implicit"; /** A combinator or operator that takes a source parser that returns a new parser based on it. */ export interface ParjsCombinator { (from: ImplicitParjser): Parjser; } /** A projection on the parser result and the parser state. */ export interface ParjsProjection { (value: T, userState: UserState): TOut; } /** * A predicate on the parser result and the user state. This function must return `true` if the * input fulfills the predicate, or failure information object if it does not. Returning things * other than `true` will make it behave like a failure. */ export type ParjsValidator = ParjsProjection | true>; /** * Interface for parsers that produce result values of type {T} * * @group functional */ export interface Parjser { apply(ps: ParsingState): void; readonly expecting: string; /** * Exposes the display name of the parser. Userful when debugging. * * @group informational */ readonly type: string; /** * Applies `this` on the given input string. * * @param input The input string. * @param initialState An object containing properties that are merged with this parse * invocation's user state. * @group action */ parse(input: string, initialState?: UserState): ParjsResult; /** * 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 cmb1 The single combinator to apply. */ pipe(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 cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. */ pipe( 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 cmb1 The first combinator to apply. * @param cmb2 The second combinator to apply. * @param cmb3 The third combinator to apply. */ pipe( 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 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. */ pipe( 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 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. */ pipe( 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 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. */ pipe( cmb1: ParjsCombinator, cmb2: ParjsCombinator, cmb3: ParjsCombinator, cmb4: ParjsCombinator, cmb5: ParjsCombinator, cmb6: ParjsCombinator ): Parjser; /** * Returns a copy of this, but with the given default error message. * * @param message Description of the input. */ expects(message: string): Parjser; /** * Returns this parser unchanged, but logs the result of the parse to the console. This is * useful for debugging. */ debug(fn?: ParjserDebugFunction): Parjser; }