Variable Rule

Rule: {
    accept: (<E, R>(value: R) => Rule<E, R>);
    as: (<T>(value: T) => (<E, R>(rule: Rule<E, R>) => Rule<E, T>));
    chain: {
        <E>(): Chain<readonly [], E>;
        <E, T>(done: Rule<E, T>): Chain<T, E>;
    };
    collect: (<E>(options?: RepOptions<E>) => (<I>(rule: Rule<E, I>) => Rule<E, I[]>));
    end: Rule<unknown, undefined>;
    first: (<F, E>(rule: Rule<E, readonly [F, unknown]>) => Rule<E, F>);
    fork: (<H, T>(head: H, ...tail: T) => Rule<ElemOf<H>, AsUnion<ResultsOf<[H, ...T[]]>>>);
    lazy: (<E, R>(rule: (() => Rule<E, R>)) => Rule<E, R>);
    log: (<E, R>(rule: Rule<E, R>) => Rule<E, R>);
    loop: (<S>(options?: RepOptions<S>) => ((rule: Rule<S, unknown>) => Rule<S, undefined>));
    map: (<I, O>(fn: ((i: I) => O)) => (<E>(rule: Rule<E, I>) => Rule<E, O>));
    nextAs: (<E, R>(fn: ((el: E) => StepResult<R>)) => Rule<E, R>);
    nextIf: (<E>(pred: ((el: E) => boolean)) => Rule<E, E>);
    nonEmpty: (<E, T>(rule: Rule<E, T>) => Rule<E, T>);
    of: (<E, R>(run: ((src: readonly E[]) => ((pos: number) => Result<R>))) => Rule<E, R>);
    optional: (<R, E>(rule: Rule<E, R>) => Rule<E, undefined | R>);
    path: ((id: string) => (<E, R>(rule: Rule<E, R>) => Rule<E, R>));
    reject: ((msg: string) => (<E, T>(_src: E[]) => ((pos: number) => Result<T>)));
    repeat: (<S>(options?: RepOptions<S>) => (<E>(rule: Rule<S, E>) => (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>)));
    slice: (<R>(rule: Rule<string, R>) => Rule<string, string[]>);
    unfinished: (<R, E>(rule: Rule<E, R>) => Rule<E, R>);
}

Type declaration

  • accept: (<E, R>(value: R) => Rule<E, R>)
      • <E, R>(value): Rule<E, R>
      • Type Parameters

        • E
        • R = unknown

        Parameters

        • value: R

        Returns Rule<E, R>

        A rule that instantly accepts, without consuming input, and produces the given value

  • as: (<T>(value: T) => (<E, R>(rule: Rule<E, R>) => Rule<E, T>))
      • <T>(value): (<E, R>(rule: Rule<E, R>) => Rule<E, T>)
      • The as function transforms the output of a rule into a fixed value, ignoring the actual result of the rule. It applies the map combinator to return a constant value (T) ignoring the original's parser result

        Type Parameters

        • T

        Parameters

        • value: T

        Returns (<E, R>(rule: Rule<E, R>) => Rule<E, T>)

  • chain: {
        <E>(): Chain<readonly [], E>;
        <E, T>(done: Rule<E, T>): Chain<T, E>;
    }
      • <E>(): Chain<readonly [], E>
      • Type Parameters

        • E

        Returns Chain<readonly [], E>

      • <E, T>(done): Chain<T, E>
      • Type Parameters

        • E
        • T extends readonly unknown[]

        Parameters

        Returns Chain<T, E>

  • collect: (<E>(options?: RepOptions<E>) => (<I>(rule: Rule<E, I>) => Rule<E, I[]>))
      • <E>(options?): (<I>(rule: Rule<E, I>) => Rule<E, I[]>)
      • The collect function creates a parser combinator that repeatedly applies a given rule and collects the results into an array. It is a high-level utility for scenarios where you need to match a repeating pattern and aggregate all matched values.

        Type Parameters

        • E

        Parameters

        • Optionaloptions: RepOptions<E>

        Returns (<I>(rule: Rule<E, I>) => Rule<E, I[]>)

  • end: Rule<unknown, undefined>
  • first: (<F, E>(rule: Rule<E, readonly [F, unknown]>) => Rule<E, F>)
      • <F, E>(rule): Rule<E, F>
      • Takes a rule that returns a non empty tuple, and returns a rule that returns said tuple's first value

        Type Parameters

        • F
        • E

        Parameters

        • rule: Rule<E, readonly [F, unknown]>

          A rule that returns a non empty tuple

        Returns Rule<E, F>

  • fork: (<H, T>(head: H, ...tail: T) => Rule<ElemOf<H>, AsUnion<ResultsOf<[H, ...T[]]>>>)
      • <H, T>(head, ...tail): Rule<ElemOf<H>, AsUnion<ResultsOf<[H, ...T[]]>>>
      • The fork function combines multiple parsing rules into a single rule. It evaluates all the provided rules at the same input position, and it selects the greediest result based on most consumed input. This allows for flexible parsing when multiple potential interpretations of the input are possible.

        Type Parameters

        • H extends Rule<never, unknown>
        • T extends readonly Rule<ElemOf<H>, unknown>[]

        Parameters

        • head: H

          First rule to be combined

        • Rest...tail: T

          Other rules to be combined

        Returns Rule<ElemOf<H>, AsUnion<ResultsOf<[H, ...T[]]>>>

  • lazy: (<E, R>(rule: (() => Rule<E, R>)) => Rule<E, R>)
      • <E, R>(rule): Rule<E, R>
      • The lazy function creates a Rule that defers the evaluation of a given rule until it is needed. It wraps a rule-generating function (rule) in a way that the rule is constructed and memoized only on the first invocation. This is useful in scenarios where rules are defined recursively or depend on other rules that may not yet be fully constructed.

        Type Parameters

        • E
        • R

        Parameters

        Returns Rule<E, R>

        A rule that is lazily evaluated

  • log: (<E, R>(rule: Rule<E, R>) => Rule<E, R>)
      • <E, R>(rule): Rule<E, R>
      • The log function is a parser combinator that wraps a given rule and adds logging behavior. It logs messages about entering the rule, whether the rule matched or rejected, and where it occurred within the input. This is primarily useful for debugging parsers, as it provides visibility into the flow of parsing and helps identify where and why parsing decisions are being made.

        Type Parameters

        • E
        • R

        Parameters

        Returns Rule<E, R>

        A rule that prints in console when a rule is being attempted, and whether it matched or not

  • loop: (<S>(options?: RepOptions<S>) => ((rule: Rule<S, unknown>) => Rule<S, undefined>))
      • <S>(options?): ((rule: Rule<S, unknown>) => Rule<S, undefined>)
      • The loop function creates a parser combinator that repeatedly applies a given rule but discards all its results. It is useful for cases where the rule needs to match a repeated pattern, but the output of the matches is not needed.

        Type Parameters

        • S

        Parameters

        • Optionaloptions: RepOptions<S>

        Returns ((rule: Rule<S, unknown>) => Rule<S, undefined>)

          • (rule): Rule<S, undefined>
          • Parameters

            Returns Rule<S, undefined>

  • map: (<I, O>(fn: ((i: I) => O)) => (<E>(rule: Rule<E, I>) => Rule<E, O>))
      • <I, O>(fn): (<E>(rule: Rule<E, I>) => Rule<E, O>)
      • The map function transforms the output of an existing parser. It takes a rule, applies it to input, and then modifies the parsed result using the provided transformation function (fn).

        Type Parameters

        • I
        • O

        Parameters

        • fn: ((i: I) => O)
            • (i): O
            • Parameters

              Returns O

        Returns (<E>(rule: Rule<E, I>) => Rule<E, O>)

        A new rule of type Rule<O, E>, which produces the transformed output

  • nextAs: (<E, R>(fn: ((el: E) => StepResult<R>)) => Rule<E, R>)
      • <E, R>(fn): Rule<E, R>
      • The nextAs function applies a predicate function (fn) to the current element in the input and returns a StepResult. If the StepResult is successful (accepted: true), it advances the position and returns the value If it fails (accepted: false), it rejects with the provided msg.

        Type Parameters

        • E
        • R

        Parameters

        Returns Rule<E, R>

  • nextIf: (<E>(pred: ((el: E) => boolean)) => Rule<E, E>)
      • <E>(pred): Rule<E, E>
      • Type Parameters

        • E

        Parameters

        • pred: ((el: E) => boolean)

          The nextIf function applies a condition (pred) to the current element in the input and advances if the condition is met. If the condition fails, it rejects the parse.

            • (el): boolean
            • Parameters

              • el: E

              Returns boolean

        Returns Rule<E, E>

  • nonEmpty: (<E, T>(rule: Rule<E, T>) => Rule<E, T>)
      • <E, T>(rule): Rule<E, T>
      • The nonEmpty function wraps an existing rule to enforce that it must consume input when it succeeds. If the wrapped rule accepts the input but fails to advance the parsing position, nonEmpty rejects the result with an appropriate error message. This helps ensure that rules behave as intended, especially in contexts where progress through the input is mandatory.

        Type Parameters

        • E
        • T

        Parameters

        Returns Rule<E, T>

  • of: (<E, R>(run: ((src: readonly E[]) => ((pos: number) => Result<R>))) => Rule<E, R>)
      • <E, R>(run): Rule<E, R>
      • Type Parameters

        • E
        • R

        Parameters

        • run: ((src: readonly E[]) => ((pos: number) => Result<R>))
            • (src): ((pos: number) => Result<R>)
            • Parameters

              • src: readonly E[]

              Returns ((pos: number) => Result<R>)

        Returns Rule<E, R>

  • optional: (<R, E>(rule: Rule<E, R>) => Rule<E, undefined | R>)
      • <R, E>(rule): Rule<E, undefined | R>
      • Type Parameters

        • R
        • E

        Parameters

        Returns Rule<E, undefined | R>

  • path: ((id: string) => (<E, R>(rule: Rule<E, R>) => Rule<E, R>))
      • (id): (<E, R>(rule: Rule<E, R>) => Rule<E, R>)
      • The path function is a parser combinator that wraps a given rule and associates it with a specific id. This can be useful for debugging or logging, as it allows the parser to track which part of the parsing process is currently being executed. The function associates the rule with a path in the context, enabling features such as error reporting and context-aware debugging. The id serves as a label for this particular parsing step, which can help identify where in the parsing process the rule is being applied.

        Parameters

        • id: string

        Returns (<E, R>(rule: Rule<E, R>) => Rule<E, R>)

        A marked rule that always includes the path segment id

  • reject: ((msg: string) => (<E, T>(_src: E[]) => ((pos: number) => Result<T>)))
      • (msg): (<E, T>(_src: E[]) => ((pos: number) => Result<T>))
      • Parameters

        • msg: string

        Returns (<E, T>(_src: E[]) => ((pos: number) => Result<T>))

        A rule that instantly fails with the given msg

          • <E, T>(_src): ((pos: number) => Result<T>)
          • Type Parameters

            • E
            • T

            Parameters

            • _src: E[]

            Returns ((pos: number) => Result<T>)

  • repeat: (<S>(options?: RepOptions<S>) => (<E>(rule: Rule<S, E>) => (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>)))
      • <S>(options?): (<E>(rule: Rule<S, E>) => (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>))
      • The repeat function is a parser combinator that allows for repeating a rule a specific number of times, optionally consuming a separator between repetitions. It takes in options that control the minimum and maximum number of repetitions, as well as the separator between elements. The rule's result is accumulated via a fold function, and an initial value is provided through init

        Type Parameters

        • S

        Parameters

        • Optionaloptions: RepOptions<S>

        Returns (<E>(rule: Rule<S, E>) => (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>))

          • <E>(rule): (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>)
          • Type Parameters

            • E

            Parameters

            Returns (<R>(init: (() => R), fold: ((el: E, result: R) => R)) => Rule<S, R>)

              • <R>(init, fold): Rule<S, R>
              • Type Parameters

                • R

                Parameters

                • init: (() => R)
                    • (): R
                    • Returns R

                • fold: ((el: E, result: R) => R)
                    • (el, result): R
                    • Parameters

                      • el: E
                      • result: R

                      Returns R

                Returns Rule<S, R>

  • slice: (<R>(rule: Rule<string, R>) => Rule<string, string[]>)
      • <R>(rule): Rule<string, string[]>
      • The slice function takes a parsing rule and transforms it into a rule that returns the slice of the input string array corresponding to the region consumed by the original rule. If the rule fails, the failure is propagated without modification. This is useful when you need to capture the matched substring(s) rather than the provided result.

        Type Parameters

        • R

        Parameters

        Returns Rule<string, string[]>

  • unfinished: (<R, E>(rule: Rule<E, R>) => Rule<E, R>)
      • <R, E>(rule): Rule<E, R>
      • The unfinished function wraps an existing rule and ensures that it only operates on valid cursor positions within the input source. If the cursor is out of range, the wrapped rule immediately fails with a rejection message. This prevents invalid accesses during parsing and helps maintain robust error handling.

        Type Parameters

        • R
        • E

        Parameters

        Returns Rule<E, R>