UNPKG

@optique/core

Version:

Type-safe combinatorial command-line interface parser

852 lines (850 loc) 89.5 kB
import { Message } from "./message.cjs"; import { OptionName, Usage } from "./usage.cjs"; import { DocFragments, DocPage } from "./doc.cjs"; import { ValueParser, ValueParserResult } from "./valueparser.cjs"; //#region src/parser.d.ts /** * Represents the state passed to getDocFragments. * Can be either the actual parser state or an explicit indicator * that no state is available. * @template TState The type of the actual state when available. * @since 0.3.0 */ type DocState<TState> = { readonly kind: "available"; readonly state: TState; } | { readonly kind: "unavailable"; }; /** * Parser interface for command-line argument parsing. * @template TValue The type of the value returned by the parser. * @template TState The type of the state used during parsing. */ interface Parser<TValue, TState> { /** * A type tag for the result value of this parser, used for type inference. * Usually this is an empty array at runtime, but it does not matter * what it contains. * @internal */ readonly $valueType: readonly TValue[]; /** * A type tag for the state of this parser, used for type inference. * Usually this is an empty array at runtime, but it does not matter * what it contains. * @internal */ readonly $stateType: readonly TState[]; /** * The priority of this parser, which determines the order in which * parsers are applied when multiple parsers are available. The greater * the number, the higher the priority. */ readonly priority: number; /** * The usage information for this parser, which describes how * to use it in command-line interfaces. */ readonly usage: Usage; /** * The initial state for this parser. This is used to initialize the * state when parsing starts. */ readonly initialState: TState; /** * Parses the input context and returns a result indicating * whether the parsing was successful or not. * @param context The context of the parser, which includes the input buffer * and the current state. * @returns A result object indicating success or failure. */ parse(context: ParserContext<TState>): ParserResult<TState>; /** * Transforms a {@link TState} into a {@link TValue}, if applicable. * If the transformation is not applicable, it should return * a `ValueParserResult` with `success: false` and an appropriate error * message. * @param state The current state of the parser, which may contain accumulated * data or context needed to produce the final value. * @returns A result object indicating success or failure of * the transformation. If successful, it should contain * the parsed value of type {@link TValue}. If not applicable, * it should return an error message. */ complete(state: TState): ValueParserResult<TValue>; /** * Generates a documentation fragment for this parser, which can be used * to describe the parser's usage, description, and default value. * @param state The current state of the parser, wrapped in a DocState * to indicate whether the actual state is available or not. * @param defaultValue An optional default value that can be used * to provide a default value in the documentation. * @returns {@link DocFragments} object containing documentation * fragments for this parser. */ getDocFragments(state: DocState<TState>, defaultValue?: TValue): DocFragments; } /** * The context of the parser, which includes the input buffer and the state. * @template TState The type of the state used during parsing. */ interface ParserContext<TState> { /** * The array of input strings that the parser is currently processing. */ readonly buffer: readonly string[]; /** * The current state of the parser, which is used to track * the progress of parsing and any accumulated data. */ readonly state: TState; /** * A flag indicating whether no more options should be parsed and instead * the remaining input should be treated as positional arguments. * This is typically set when the parser encounters a `--` in the input, * which is a common convention in command-line interfaces to indicate * that no further options should be processed. */ readonly optionsTerminated: boolean; } /** * A discriminated union type representing the result of a parser operation. * It can either indicate a successful parse with the next state and context, * or a failure with an error message. * @template TState The type of the state after parsing. It should match with * the `TState` type of the {@link Parser} interface. */ type ParserResult<TState> = { /** * Indicates that the parsing operation was successful. */ readonly success: true; /** * The next context after parsing, which includes the updated input buffer. */ readonly next: ParserContext<TState>; /** * The input elements consumed by the parser during this operation. */ readonly consumed: readonly string[]; } | { /** * Indicates that the parsing operation failed. */ readonly success: false; /** * The number of the consumed input elements. */ readonly consumed: number; /** * The error message describing why the parsing failed. */ readonly error: Message; }; /** * Creates a parser that always succeeds without consuming any input and * produces a constant value of the type {@link T}. * @template T The type of the constant value produced by the parser. */ declare function constant<const T>(value: T): Parser<T, T>; /** * Options for the {@link option} parser. */ interface OptionOptions { /** * The description of the option, which can be used for help messages. */ readonly description?: Message; } /** * Creates a parser for various styles of command-line options that take an * argument value, such as `--option=value`, `-o value`, or `/option:value`. * @template T The type of value this parser produces. * @param args The {@link OptionName}s to parse, followed by * a {@link ValueParser} that defines how to parse the value of * the option. If no value parser is provided, the option is * treated as a boolean flag. * @returns A {@link Parser} that can parse the specified options and their * values. */ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>]): Parser<T, ValueParserResult<T> | undefined>; /** * Creates a parser for various styles of command-line options that take an * argument value, such as `--option=value`, `-o value`, or `/option:value`. * @template T The type of value this parser produces. * @param args The {@link OptionName}s to parse, followed by * a {@link ValueParser} that defines how to parse the value of * the option, and an optional {@link OptionOptions} object * that allows you to specify a description or other metadata. * @returns A {@link Parser} that can parse the specified options and their * values. */ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>, OptionOptions]): Parser<T, ValueParserResult<T> | undefined>; /** * Creates a parser for various styles of command-line options that do not * take an argument value, such as `--option`, `-o`, or `/option`. * @param optionNames The {@link OptionName}s to parse. * @return A {@link Parser} that can parse the specified options as Boolean * flags, producing `true` if the option is present. */ declare function option(...optionNames: readonly OptionName[]): Parser<boolean, ValueParserResult<boolean> | undefined>; /** * Creates a parser for various styles of command-line options that take an * argument value, such as `--option=value`, `-o value`, or `/option:value`. * @param args The {@link OptionName}s to parse, followed by * an optional {@link OptionOptions} object that allows you to * specify a description or other metadata. * @returns A {@link Parser} that can parse the specified options and their * values. */ declare function option(...args: readonly [...readonly OptionName[], OptionOptions]): Parser<boolean, ValueParserResult<boolean> | undefined>; /** * Options for the {@link flag} parser. */ interface FlagOptions { /** * The description of the flag, which can be used for help messages. */ readonly description?: Message; } /** * Creates a parser for command-line flags that must be explicitly provided. * Unlike {@link option}, this parser fails if the flag is not present, making * it suitable for required boolean flags that don't have a meaningful default. * * The key difference from {@link option} is: * - {@link option} without a value parser: Returns `false` when not present * - {@link flag}: Fails parsing when not present, only produces `true` * * This is useful for dependent options where the presence of a flag changes * the shape of the result type. * * @param args The {@link OptionName}s to parse, followed by an optional * {@link FlagOptions} object that allows you to specify * a description or other metadata. * @returns A {@link Parser} that produces `true` when the flag is present * and fails when it is not present. * * @example * ```typescript * // Basic flag usage * const parser = flag("-f", "--force"); * // Succeeds with true: parse(parser, ["-f"]) * // Fails: parse(parser, []) * * // With description * const verboseFlag = flag("-v", "--verbose", { * description: "Enable verbose output" * }); * ``` */ declare function flag(...args: readonly [...readonly OptionName[], FlagOptions] | readonly OptionName[]): Parser<true, ValueParserResult<true> | undefined>; /** * Options for the {@link argument} parser. */ interface ArgumentOptions { /** * The description of the argument, which can be used for help messages. */ readonly description?: Message; } /** * Creates a parser that expects a single argument value. * This parser is typically used for positional arguments * that are not options or flags. * @template T The type of the value produced by the parser. * @param valueParser The {@link ValueParser} that defines how to parse * the argument value. * @param options Optional configuration for the argument parser, * allowing you to specify a description or other metadata. * @returns A {@link Parser} that expects a single argument value and produces * the parsed value of type {@link T}. */ declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOptions): Parser<T, ValueParserResult<T> | undefined>; /** * Creates a parser that makes another parser optional, allowing it to succeed * without consuming input if the wrapped parser fails to match. * If the wrapped parser succeeds, this returns its value. * If the wrapped parser fails, this returns `undefined` without consuming input. * @template TValue The type of the value returned by the wrapped parser. * @template TState The type of the state used by the wrapped parser. * @param parser The {@link Parser} to make optional. * @returns A {@link Parser} that produces either the result of the wrapped parser * or `undefined` if the wrapped parser fails to match. */ declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>; /** * Creates a parser that makes another parser use a default value when it fails * to match or consume input. This is similar to {@link optional}, but instead * of returning `undefined` when the wrapped parser doesn't match, it returns * a specified default value. * @template TValue The type of the value returned by the wrapped parser. * @template TState The type of the state used by the wrapped parser. * @template TDefault The type of the default value. * @param parser The {@link Parser} to wrap with default behavior. * @param defaultValue The default value to return when the wrapped parser * doesn't match or consume input. Can be a value of type * {@link TDefault} or a function that returns such a value. * @returns A {@link Parser} that produces either the result of the wrapped parser * or the default value if the wrapped parser fails to match (union type {@link TValue} | {@link TDefault}). */ declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>; /** * Creates a parser that transforms the result value of another parser using * a mapping function. This enables value transformation while preserving * the original parser's parsing logic and state management. * * The `map()` function is useful for: * - Converting parsed values to different types * - Applying transformations like string formatting or boolean inversion * - Computing derived values from parsed input * - Creating reusable transformations that can be applied to any parser * * @template T The type of the value produced by the original parser. * @template U The type of the value produced by the mapping function. * @template TState The type of the state used by the original parser. * @param parser The {@link Parser} whose result will be transformed. * @param transform A function that transforms the parsed value from type T to type U. * @returns A {@link Parser} that produces the transformed value of type U * while preserving the original parser's state type and parsing behavior. * * @example * ```typescript * // Transform boolean flag to its inverse * const parser = object({ * disallow: map(option("--allow"), b => !b) * }); * * // Transform string to uppercase * const upperParser = map(argument(string()), s => s.toUpperCase()); * * // Transform number to formatted string * const prefixedParser = map(option("-n", integer()), n => `value: ${n}`); * ``` */ declare function map<T, U, TState>(parser: Parser<T, TState>, transform: (value: T) => U): Parser<U, TState>; /** * Options for the {@link multiple} parser. */ interface MultipleOptions { /** * The minimum number of occurrences required for the parser to succeed. * If the number of occurrences is less than this value, * the parser will fail with an error. * @default `0` */ readonly min?: number; /** * The maximum number of occurrences allowed for the parser. * If the number of occurrences exceeds this value, * the parser will fail with an error. * @default `Infinity` */ readonly max?: number; } /** * Creates a parser that allows multiple occurrences of a given parser. * This parser can be used to parse multiple values of the same type, * such as multiple command-line arguments or options. * @template TValue The type of the value that the parser produces. * @template TState The type of the state used by the parser. * @param parser The {@link Parser} to apply multiple times. * @param options Optional configuration for the parser, * allowing you to specify the minimum and maximum number of * occurrences allowed. * @returns A {@link Parser} that produces an array of values * of type {@link TValue} and an array of states * of type {@link TState}. */ declare function multiple<TValue, TState>(parser: Parser<TValue, TState>, options?: MultipleOptions): Parser<readonly TValue[], readonly TState[]>; /** * Creates a parser that combines multiple parsers into a single object parser. * Each parser in the object is applied to parse different parts of the input, * and the results are combined into an object with the same structure. * @template T A record type where each value is a {@link Parser}. * @param parsers An object containing named parsers that will be combined * into a single object parser. * @returns A {@link Parser} that produces an object with the same keys as * the input, where each value is the result of the corresponding * parser. */ declare function object<T extends { readonly [key: string | symbol]: Parser<unknown, unknown>; }>(parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>; /** * Creates a labeled parser that combines multiple parsers into a single * object parser with an associated label for documentation or error reporting. * @template T A record type where each value is a {@link Parser}. * @param label A descriptive label for this parser group, used for * documentation and error messages. * @param parsers An object containing named parsers that will be combined * into a single object parser. * @returns A {@link Parser} that produces an object with the same keys as * the input, where each value is the result of the corresponding * parser. */ declare function object<T extends { readonly [key: string | symbol]: Parser<unknown, unknown>; }>(label: string, parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>; /** * Creates a parser that combines multiple parsers into a sequential tuple parser. * The parsers are applied in the order they appear in the array, and all must * succeed for the tuple parser to succeed. * @template T A readonly array type where each element is a {@link Parser}. * @param parsers An array of parsers that will be applied sequentially * to create a tuple of their results. * @returns A {@link Parser} that produces a readonly tuple with the same length * as the input array, where each element is the result of the * corresponding parser. */ declare function tuple<const T extends readonly Parser<unknown, unknown>[]>(parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>; /** * Creates a labeled parser that combines multiple parsers into a sequential * tuple parser with an associated label for documentation or error reporting. * @template T A readonly array type where each element is a {@link Parser}. * @param label A descriptive label for this parser group, used for * documentation and error messages. * @param parsers An array of parsers that will be applied sequentially * to create a tuple of their results. * @returns A {@link Parser} that produces a readonly tuple with the same length * as the input array, where each element is the result of the * corresponding parser. */ declare function tuple<const T extends readonly Parser<unknown, unknown>[]>(label: string, parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>; /** * Creates a parser that combines two mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @returns A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. */ declare function or<TA, TB, TStateA, TStateB>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>): Parser<TA | TB, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>]>; /** * Creates a parser that combines three mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. */ declare function or<TA, TB, TC, TStateA, TStateB, TStateC>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>): Parser<TA | TB | TC, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>]>; /** * Creates a parser that combines four mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. */ declare function or<TA, TB, TC, TD, TStateA, TStateB, TStateC, TStateD>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>): Parser<TA | TB | TC | TD, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>]>; /** * Creates a parser that combines five mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. */ declare function or<TA, TB, TC, TD, TE, TStateA, TStateB, TStateC, TStateD, TStateE>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>): Parser<TA | TB | TC | TD | TE, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>]>; /** * Creates a parser that combines six mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TF The type of the value returned by the sixth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @template TStateF The type of the state used by the sixth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @param f The sixth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. * @since 0.3.0 */ declare function or<TA, TB, TC, TD, TE, TF, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>): Parser<TA | TB | TC | TD | TE | TF, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>]>; /** * Creates a parser that combines seven mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TF The type of the value returned by the sixth parser. * @template TG The type of the value returned by the seventh parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @template TStateF The type of the state used by the sixth parser. * @template TStateG The type of the state used by the seventh parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @param f The sixth {@link Parser} to try. * @param g The seventh {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. * @since 0.3.0 */ declare function or<TA, TB, TC, TD, TE, TF, TG, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>): Parser<TA | TB | TC | TD | TE | TF | TG, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>]>; /** * Creates a parser that combines eight mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TF The type of the value returned by the sixth parser. * @template TG The type of the value returned by the seventh parser. * @template TH The type of the value returned by the eighth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @template TStateF The type of the state used by the sixth parser. * @template TStateG The type of the state used by the seventh parser. * @template TStateH The type of the state used by the eighth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @param f The sixth {@link Parser} to try. * @param g The seventh {@link Parser} to try. * @param h The eighth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. * @since 0.3.0 */ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>): Parser<TA | TB | TC | TD | TE | TF | TG | TH, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>]>; /** * Creates a parser that combines nine mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TF The type of the value returned by the sixth parser. * @template TG The type of the value returned by the seventh parser. * @template TH The type of the value returned by the eighth parser. * @template TI The type of the value returned by the ninth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @template TStateF The type of the state used by the sixth parser. * @template TStateG The type of the state used by the seventh parser. * @template TStateH The type of the state used by the eighth parser. * @template TStateI The type of the state used by the ninth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @param f The sixth {@link Parser} to try. * @param g The seventh {@link Parser} to try. * @param h The eighth {@link Parser} to try. * @param i The ninth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. * @since 0.3.0 */ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>]>; /** * Creates a parser that combines ten mutually exclusive parsers into one. * The resulting parser will try each of the provided parsers in order, * and return the result of the first successful parser. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TF The type of the value returned by the sixth parser. * @template TG The type of the value returned by the seventh parser. * @template TH The type of the value returned by the eighth parser. * @template TI The type of the value returned by the ninth parser. * @template TJ The type of the value returned by the tenth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @template TStateF The type of the state used by the sixth parser. * @template TStateG The type of the state used by the seventh parser. * @template TStateH The type of the state used by the eighth parser. * @template TStateI The type of the state used by the ninth parser. * @template TStateJ The type of the state used by the tenth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @param f The sixth {@link Parser} to try. * @param g The seventh {@link Parser} to try. * @param h The eighth {@link Parser} to try. * @param i The ninth {@link Parser} to try. * @param j The tenth {@link Parser} to try. * @return A {@link Parser} that tries to parse using the provided parsers * in order, returning the result of the first successful parser. * @since 0.3.0 */ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TJ, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI, TStateJ>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>, j: Parser<TJ, TStateJ>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI | TJ, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>] | [9, ParserResult<TStateJ>]>; /** * Creates a parser that combines two mutually exclusive parsers into one, * selecting the parser that consumes the most tokens. * The resulting parser will try both parsers and return the result * of the parser that consumed more input tokens. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @returns A {@link Parser} that tries to parse using both parsers * and returns the result of the parser that consumed more tokens. * @since 0.3.0 */ declare function longestMatch<TA, TB, TStateA, TStateB>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>): Parser<TA | TB, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>]>; /** * Creates a parser that combines three mutually exclusive parsers into one, * selecting the parser that consumes the most tokens. * The resulting parser will try all parsers and return the result * of the parser that consumed the most input tokens. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @returns A {@link Parser} that tries to parse using all parsers * and returns the result of the parser that consumed the most tokens. * @since 0.3.0 */ declare function longestMatch<TA, TB, TC, TStateA, TStateB, TStateC>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>): Parser<TA | TB | TC, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>]>; /** * Creates a parser that combines four mutually exclusive parsers into one, * selecting the parser that consumes the most tokens. * The resulting parser will try all parsers and return the result * of the parser that consumed the most input tokens. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @returns A {@link Parser} that tries to parse using all parsers * and returns the result of the parser that consumed the most tokens. * @since 0.3.0 */ declare function longestMatch<TA, TB, TC, TD, TStateA, TStateB, TStateC, TStateD>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>): Parser<TA | TB | TC | TD, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>]>; /** * Creates a parser that combines five mutually exclusive parsers into one, * selecting the parser that consumes the most tokens. * The resulting parser will try all parsers and return the result * of the parser that consumed the most input tokens. * @template TA The type of the value returned by the first parser. * @template TB The type of the value returned by the second parser. * @template TC The type of the value returned by the third parser. * @template TD The type of the value returned by the fourth parser. * @template TE The type of the value returned by the fifth parser. * @template TStateA The type of the state used by the first parser. * @template TStateB The type of the state used by the second parser. * @template TStateC The type of the state used by the third parser. * @template TStateD The type of the state used by the fourth parser. * @template TStateE The type of the state used by the fifth parser. * @param a The first {@link Parser} to try. * @param b The second {@link Parser} to try. * @param c The third {@link Parser} to try. * @param d The fourth {@link Parser} to try. * @param e The fifth {@link Parser} to try. * @returns A {@link Parser} that tries to parse using all parsers * and returns the result of the parser that consumed the most tokens. * @since 0.3.0 */ declare function longestMatch<TA, TB, TC, TD, TE, TStateA, TStateB, TStateC, TStateD, TStateE>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>): Parser<TA | TB | TC | TD | TE, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>]>; /** * Helper type to check if all members of a union are object-like. * This allows merge() to work with parsers like withDefault() that produce union types. */ type AllObjectLike<T> = T extends readonly unknown[] ? never : T extends Record<string | symbol, unknown> ? T : never; /** * Helper type to extract object-like types from parser value types, * including union types where all members are objects. */ type ExtractObjectTypes<P> = P extends Parser<infer V, unknown> ? [AllObjectLike<V>] extends [never] ? never : V : never; /** * Merges multiple {@link object} parsers into a single {@link object} parser. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>>(a: ExtractObjectTypes<TA> extends never ? never : TA, b: ExtractObjectTypes<TB> extends never ? never : TB): Parser<ExtractObjectTypes<TA> & ExtractObjectTypes<TB>, Record<string | symbol, unknown>>; /** * Merges multiple {@link object} parsers into a single {@link object} parser * with a label for documentation and help text organization. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @param label A descriptive label for this merged group, used for * documentation and help messages. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>>(label: string, a: ExtractObjectTypes<TA> extends never ? never : TA, b: ExtractObjectTypes<TB> extends never ? never : TB): Parser<ExtractObjectTypes<TA> & ExtractObjectTypes<TB>, Record<string | symbol, unknown>>; /** * Merges multiple {@link object} parsers into a single {@link object} parser. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @template TC The type of the third parser. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @param c The third {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>, TC extends Parser<unknown, unknown>>(a: ExtractObjectTypes<TA> extends never ? never : TA, b: ExtractObjectTypes<TB> extends never ? never : TB, c: ExtractObjectTypes<TC> extends never ? never : TC): Parser<ExtractObjectTypes<TA> & ExtractObjectTypes<TB> & ExtractObjectTypes<TC>, Record<string | symbol, unknown>>; /** * Merges multiple {@link object} parsers into a single {@link object} parser * with a label for documentation and help text organization. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @template TC The type of the third parser. * @param label A descriptive label for this merged group, used for * documentation and help messages. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @param c The third {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>, TC extends Parser<unknown, unknown>>(label: string, a: TA, b: TB, c: TC): ExtractObjectTypes<TA> extends never ? never : ExtractObjectTypes<TB> extends never ? never : ExtractObjectTypes<TC> extends never ? never : Parser<ExtractObjectTypes<TA> & ExtractObjectTypes<TB> & ExtractObjectTypes<TC>, Record<string | symbol, unknown>>; /** * Merges multiple {@link object} parsers into a single {@link object} parser. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @template TC The type of the third parser. * @template TD The type of the fourth parser. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @param c The third {@link object} parser to merge. * @param d The fourth {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>, TC extends Parser<unknown, unknown>, TD extends Parser<unknown, unknown>>(a: TA, b: TB, c: TC, d: TD): ExtractObjectTypes<TA> extends never ? never : ExtractObjectTypes<TB> extends never ? never : ExtractObjectTypes<TC> extends never ? never : ExtractObjectTypes<TD> extends never ? never : Parser<ExtractObjectTypes<TA> & ExtractObjectTypes<TB> & ExtractObjectTypes<TC> & ExtractObjectTypes<TD>, Record<string | symbol, unknown>>; /** * Merges multiple {@link object} parsers into a single {@link object} parser * with a label for documentation and help text organization. * It is useful for combining multiple {@link object} parsers so that * the unified parser produces a single object containing all the values * from the individual parsers while separating the fields into multiple * groups. * @template TA The type of the first parser. * @template TB The type of the second parser. * @template TC The type of the third parser. * @template TD The type of the fourth parser. * @param label A descriptive label for this merged group, used for * documentation and help messages. * @param a The first {@link object} parser to merge. * @param b The second {@link object} parser to merge. * @param c The third {@link object} parser to merge. * @param d The fourth {@link object} parser to merge. * @return A new {@link object} parser that combines the values and states * of the two parsers into a single object. */ declare function merge<TA extends Parser<unknown, unknown>, TB extends Parser<unknown, unknown>, TC extends Parser<unknown, unknown>, TD extends Parser<unknown, unknown>>(label: string, a: TA, b: TB, c: TC, d: TD): ExtractObjectTypes<TA> extends never ? ne