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.
Optional
options: RepOptions<E>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.
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.
A rule that is lazily evaluated
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.
A rule that prints in console when a rule is being attempted, and whether it matched or not
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.
Optional
options: RepOptions<S>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).
A new rule of type Rule<O, E>, which produces the transformed output
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.
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.
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.
A marked rule that always includes the path segment id
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
Optional
options: RepOptions<S>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.
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.
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