Class ParserFactory

java.lang.Object
eu.bandm.tools.ramus.ogma.Factory
eu.bandm.tools.ramus.ogma.ParserFactory

@Generated(generator="eu.bandm.tools.expander", version="1.3.1", timestamp="2026-08-26T09:14:17") public class ParserFactory extends Factory
Configurable operations for constructing parsers.

Parser factories have various configuration parameters that are fixed at construction time. A new parser factory can be derived from an existing one by changing a specific configuration parameter.

Configuration parameters control features of the parsers constructed by the factory, but only those that are not part of the main functionality. As a consequence, parsers constructed by different factories are interoperable; the observable input–output behavior is not affected by the choice of factory. Nevertheless, for uniform non-functional behavior such as the availability of metadata, it is recommended to construct all parsers that make up a grammar with the same factory.

  • Method Details

    • getDefault

      public static ParserFactory getDefault()
      Returns a parser factory with default configuration (factory settings).
      Returns:
      a parser factory with default configuration
    • derive

      Creates a new variant of this factory with a modified configuration.
      Parameters:
      evolution - the modification to apply to the configuration
      Returns:
      a new factory with a modified configuration
      Throws:
      NullPointerException - if evolution is null
    • identity

      public final <D, T> Parser<D,T> identity()
      Returns a parser that succeeds with no effect, consuming no tokens.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that succeeds with no effect, consuming no tokens.
    • succeed

      public final <D, T> Parser<D,T> succeed(Update<SimpleMessage<D>> effect)
      Returns a parser that succeeds with the given effect, consuming no tokens.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      effect - the desired semantic effect
      Returns:
      a parser that succeeds with the given effect, consuming no tokens.
      Throws:
      NullPointerException - if effect is null
    • succeedGivenInput

      public final <D, T> Parser<D,T> succeedGivenInput(Function<? super Input<D,T>,? extends Update<SimpleMessage<D>>> effect)
      Returns a parser that succeeds with the given state-dependent effect, consuming no tokens.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      effect - a function object that computes the desired semantic effect, given the current parser input state
      Returns:
      a parser that succeeds with the computed effect, consuming no tokens.
      Throws:
      NullPointerException - if effect is null
    • constant

      public final <D, T> Parser<D,T> constant(Object value)
      Returns a parser that succeeds and sets the result to the given value, consuming no tokens.

      This method is a shorthand for succeed(Update.setValue(Data.atomic(value))).

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      value - the desired semantic value
      Returns:
      a parser that succeeds with the effect of setting the result to the given value, consuming no tokens.
      Throws:
      NullPointerException - if value is null
      See Also:
    • startCollection

      public final <D, T> Parser<D,T> startCollection()
      Returns a parser that succeeds and sets the result to the empty sequence, consuming no tokens.

      This operation is useful in conjunction with collecting(eu.bandm.tools.ramus.ogma.Parser<D, T>).

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that succeeds with the effect of setting the result to the empty sequence, consuming no tokens.
      See Also:
    • fail

      public final <D, T> Parser<D,T> fail()
      Returns a parser that fails immediately.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that fails with no effect, consuming no tokens.
    • negate

      public final <D, T> Parser<D,T> negate(Parser<D,T> body)
      Returns a parser that succeeds if the given parser fails, and vice versa.

      The resulting parser speculatively executes the behavior of body. If this leads to one or more (nondeterministic) successes, the resulting parser fails; speculatively applied semantic effects have no bearing further computations. Otherwise, if the speculative execution fails, the resulting parser succeeds with no effect, consuming no tokens.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to negate
      Returns:
      a parser that succeeds with no effect if body fails and vice versa
      Throws:
      NullPointerException - if body is null
      See Also:
    • startInterval

      public final <D, T> Parser<D,T> startInterval()
      Returns a parser that sets the result value to the token location at the current input state.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that sets the result value to the token location at the current input state.
    • endInterval

      public final <D, T> Parser<D,T> endInterval()
      Returns a parser that sets the result value to the token location before the current input state.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that sets the result value to the token location before the current input state.
    • lookbehind

      @SafeVarargs public final <D, T> Parser<D,T> lookbehind(T... types)
      Returns a parser that succeeds if and only if the preceding token is of one of the given types.

      If there is no preceding token, or its type is not among the given ones, then the parser fails.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      types - an array of token types
      Returns:
      a parser that succeeds if and only if the preceding token has a type contained in types.
      Throws:
      NullPointerException - if types is null
    • lookbehind

      public final <D, T> Parser<D,T> lookbehind(Set<T> types)
      Returns a parser that succeeds if and only if the preceding token is of one of the given types.

      If there is no preceding token, or its type is not among the given ones, then the parser fails.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      types - a set of token types
      Returns:
      a parser that succeeds if and only if the preceding token has a type contained in types.
      Throws:
      NullPointerException - if types is null
    • lookbehind

      public final <D, T> Parser<D,T> lookbehind(Predicate<? super Token<D,T>> match)
      Returns a parser that succeeds if and only if the preceding token matches a given predicate.

      If there is no preceding token, or its type is not among the given ones, then the parser fails.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      match - a predicate to match
      Returns:
      a parser that succeeds if and only if match returns true when applied to the preceding token.
      Throws:
      NullPointerException - if types is null
    • terminal

      public final <D, T> Parser<D,T> terminal()
      Returns a parser that consumes any single token.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a parser that succeeds by consuming the next token and storing it in the accumulator of the semantic state
      See Also:
    • terminal

      public final <D, T> Parser<D,T> terminal(Predicate<Token<D,T>> match)
      Returns a parser that consumes a single token matching a given predicate.

      This method is a shorthand for terminal(match, Predictor.plus(), null); use that method directly if more specific information is available.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      match - a functional object that recognizes acceptable tokens
      Returns:
      a parser that succeeds by consuming the next token and storing it in the accumulator of the semantic state, if the token is accepted by match; otherwise parsing fails and rejects the token
      Throws:
      NullPointerException - if match is null
      See Also:
    • terminal

      public final <D, T> Parser<D,T> terminal(T type)
      Returns a parser that consumes a single token of a given type.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      type - the type of tokens to match
      Returns:
      a parser that succeeds by consuming the next token and storing it in the accumulator of the semantic state, if the token has the type type; otherwise parsing fails and rejects the token
      Throws:
      NullPointerException - if type is null
      See Also:
    • terminal

      public final <D, T> Parser<D,T> terminal(T type, Predicate<? super String> text)
      Returns a parser that consumes a single token of a given type and text.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      type - the type of tokens to match
      text - a predicate that specifies acceptable text
      Returns:
      a parser that succeeds by consuming the next token and storing it in the accumulator of the semantic state, if the token has the type type and text accepted by text; otherwise parsing fails and rejects the token
      Throws:
      NullPointerException - if type or text is null
      See Also:
    • diagnoseUnexpectedToken

      public static <D, T> Action<Output<D,T>,SimpleMessage<D>,Input<D,T>> diagnoseUnexpectedToken(Action<Output<D,T>,SimpleMessage<D>,Input<D,T>> body, @Opt Location<D> location, @Opt String description)
      Extends a search tree with an error message regarding an unexpected token.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the search tree to extend
      location - the location of the unexpected token, or null if not available
      description - a user-friendly description of expected tokens, or null if not available
      Returns:
      a search tree where all successes have been extended with an error message using the given information
      Throws:
      NullPointerException - if body is null
    • keyword

      public <D, T> Predicate<Token<D,T>> keyword(String text)
      Returns a predicate that matches a single token with a given text, regardless of type.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      text - the text to match
      Returns:
      a predicate that matches a token if and only if it has text equal to text.
      Throws:
      NullPointerException - if text is null
      See Also:
    • diagnose

      @SafeVarargs public final <D, T> Parser<D,T> diagnose(Parser<D,T> body, SimpleMessage<D>... msgs)
      Adds diagnostic messages to the behavior of a given parser.

      Adding zero messages has no effect.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      msgs - the messages to add
      Returns:
      a parser that behaves equivalently to body, but annotates the all successes of the search tree with msgs
      Throws:
      NullPointerException - if body, msgs or any element is null
      See Also:
    • diagnose

      public final <D, T> Parser<D,T> diagnose(Parser<D,T> body, Function<? super Input<D,T>,? extends List<? extends SimpleMessage<D>>> msgs)
      Add diagnostic messages to the behavior of a given parser.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      msgs - a function that computes the messages to add from the current input
      Returns:
      a parser that behaves equivalently to body, but annotates the all successes of the search tree with the result of msgs applied to the current input
      Throws:
      NullPointerException - if body or msgs is null
    • map

      public final <D, T> Parser<D,T> map(Parser<D,T> body, UnaryOperator<Data> transform)
      Adds a final transformation of the result value to the behavior of a given parser.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      transform - the transformation to apply to the result value
      Returns:
      a parser that behaves equivalently to body, but whose semantic effect transforms the result value by transform
      Throws:
      NullPointerException - if body or transform is null
      See Also:
    • assigning

      public final <D, T> Parser<D,T> assigning(Parser<D,T> body, Object key)
      Adds the assignment of the accumulator value to a semantic variable to the behavior of a given parser.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      key - the key identifying the semantic variable to assign to
      Returns:
      a parser that behaves equivalently to body, but whose semantic effect also assigns the accumulator value to key
      Throws:
      NullPointerException - if body or key is null
      See Also:
    • collecting

      public final <D, T> Parser<D,T> collecting(Parser<D,T> body)
      Makes a given parser collect results in a sequence.

      The resulting parser assumes that the accumulator value of its pre-state is set to a sequence. It then performs the effect of this parser, and appends its accumulator value to the preceding sequence, which becomes the overall accumulator value. FIXME (exception handling): If the preceding accumulator value is not a sequence, the result parser fails with a failure message.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      Returns:
      a parser that behaves equivalently to body, but whose semantic effect then appends the result value to the preceding one, if that is a sequence, or otherwise fails
      Throws:
      NullPointerException - if body is null
      See Also:
    • withScope

      public final <D, T> Parser<D,T> withScope(Parser<D,T> body, Object key, Data initializer)
      Wraps the behavior of a given parser in a local scope for a semantic variable.

      The semantic effect of the returned parser embeds the effect of this parser in a context that initializes the specified variable before, and restores the previous environment afterwards.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      key - the key identifying the semantic variable to scope
      initializer - the initial value of the variable in its local scope
      Returns:
      a parser that behaves equivalently to body, but whose semantic effect executes in a context where the specified variable is locally scoped
      Throws:
      NullPointerException - if body, key or initializer is null
    • withScope

      public final <D, T> Parser<D,T> withScope(Parser<D,T> body, Map<?,? extends Data> initializers)
      Wraps the behavior of a given in a local scope for a set of semantic variables.

      The semantic effect of the returned parser embeds the effect of this parser in a context that inializes the specified variables before, and restores the previous environment afterwards.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      initializers - the map of keys to initial value of the variable in its local scope
      Returns:
      a parser that behaves equivalently to body, but whose semantic effect executes in a context where the specified variables are locally scoped.
      Throws:
      NullPointerException - if body, initializers or any map key or value is null
    • postprocess

      public final <D, T> Parser<D,T> postprocess(Parser<D,T> body, Update<SimpleMessage<D>> effect)
      Appends to the semantic effect of a given parser.

      This method is an equivalent shorthand for body.andThen(succeed(cons)), but the result of this method may be slightly more efficient.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to extend
      effect - the semantic effect to append
      Returns:
      a parser that behaves equivalently to body, but where the semantic effect of this parser is sequentially followed by effect.
      Throws:
      NullPointerException - if body or effect is null
    • ignoringResult

      public final <D, T> Parser<D,T> ignoringResult(Parser<D,T> body)
      Removes the setting of the accumulator value from the semantic effect of a given parser. All other effects, such as diagnostics and assignments to state variables, are unaffected.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      body - the parser to modify
      Returns:
      a parser that behaves equivalently to body, but where the previous accumulator value is restored.
      See Also:
    • andThenCombining

      public final <D, T> Parser<D,T> andThenCombining(Parser<D,T> first, Parser<D,T> second, Action.BiContinuation<Data,Data,Data,SimpleMessage<D>,State> combiner, Parser.Pragma... pragmas)
      Follows the behavior of one parser sequentially with another, combining the result values.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      first - the first parser
      second - the subsequent parser
      combiner - a binary continuation to combine the result values
      Returns:
      a parser that behaves equivalently to first, sequentially followed by second, followed by the combination of result values.
      Throws:
      NullPointerException - if first, second or combiner is null
    • sequence

      @SafeVarargs public final <D, T> Parser<D,T> sequence(Parser<D,T>... elems)
      Combines the behavior of several parsers sequentially.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      elems - the array of parsers to combine sequentially
      Returns:
      a parser that behaves equivalently to all of the given parsers combined sequentially, or to identity() if the array is empty.
      Throws:
      NullPointerException - if elems is or contains null
      See Also:
    • sequence

      public final <D, T> Parser<D,T> sequence(List<? extends Parser<D,T>> elems)
      Combines the behavior of several parsers sequentially.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      elems - the list of parsers to combine sequentially
      Returns:
      a parser that behaves equivalently to all of the given parsers combined sequentially, or to identity() if the list is empty.
      Throws:
      NullPointerException - if elems is or contains null
      See Also:
    • ifThenElse

      public final <D, T> Parser<D,T> ifThenElse(Predicate<Token<D,T>> condition, Parser<D,T> thenBranch, Parser<D,T> elseBranch)
      Alternates the behavior of two parsers in deterministic choice, depending on the next input token.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      condition - a predicate to apply to the next input token
      thenBranch - the branch to choose if the next input token matches the condition
      elseBranch - the branch to choose if the next input token does not match the condition
      Returns:
      a parser that behaves equivalently to either given branch, depending on whether the next input token matches the condition.
      Throws:
      NullPointerException - if condition, thenBranch or elseBranch is null
      See Also:
    • choice

      @SafeVarargs public final <D, T> Parser<D,T> choice(Parser<D,T>... alts)
      Alternates the behavior of several parsers in nondeterministic choice.

      This method may exploit available syntactic information to reduce the choice to a case for the deterministic variant lookup.

      In contrast to the dual operation sequence, which can be conceived as the moral equivalent of an iteration of the binary operation andThen, this method

      • is strongly preferrable to an iteration of the binary operation orElse, where more than two alternatives are concerned;
      • but may not preserve the order in which alternative branches are tried.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      alts - the alternative branches
      Returns:
      a parser that behaves equivalent to the choice of all alternative branches, tried in some arbitrary given order, nondeterministically if necessary.
      See Also:
    • choice

      public final <D, T> Parser<D,T> choice(List<? extends Parser<D,T>> alts)
      Alternates the behavior of several parsers in nondeterministic choice.

      This method may exploit available syntactic information to reduce the choice to a case for the deterministic variant lookup.

      In contrast to the dual operation sequence, which can be conceived as the moral equivalent of an iteration of the binary operation andThen, this method

      • is strongly preferrable to an iteration of the binary operation orElse, where more than two alternatives are concerned;
      • but may not preserve the order in which alternative branches are tried.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      alts - the alternative branches
      Returns:
      a parser that behaves equivalent to the choice of all alternative branches, tried in some arbitrary given order, nondeterministically if necessary.
      See Also:
    • lookup

      public final <D, T> Parser<D,T> lookup(Map<T,Parser<D,T>> alts, Parser.Pragma... pragmas)
      Alternates the behavior of several parsers in deterministic choice, depending on the next input token.

      This method supports the following pragmas:

      • Reachable – throw an exception if any of the given parsers is known to never succeed if the next input token is of the associated type.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      alts - a map of token types to parsers, to be chosen if the next token is of the respective type
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the parser mapped to the type of the next input token, or to a parser that fails with a diagnostic hint if there is none.
      Throws:
      UnsupportedOperationException - if a parser is found in the map alts such that is predictor rules out the associated key
      See Also:
    • lookup

      public final <D, T> Parser<D,T> lookup(Map<T,Parser<D,T>> alts, Parser<D,T> otherwise, Parser.Pragma... pragmas)
      Alternates the behavior of several parsers in deterministic choice, depending on the next input token.

      This method supports the following pragmas:

      • Reachable – throw an exception if any of the given parsers is known to never succeed if the next input token is of the associated type.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      alts - a map of token types to parsers, to be chosen if the next input token is of the respective type
      otherwise - the parser to be chosen if the next input token is of an unmapped type
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the parser mapped to the type of the next input token, or to otherwise if there is none.
      Throws:
      UnsupportedOperationException - if a parser is found in the map alts such that is predictor rules out the associated key
      See Also:
    • fix

      public final <D, T> Parser<D,T> fix(UnaryOperator<Parser<D,T>> loop, Parser.Pragma... pragmas)
      Computes the fixpoint of a recursive parser definition.

      This method supports the following pragmas:

      • Productive – throw an exception if the recursive may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      loop - a function object that returns a parser, assuming that it receives a behaviorally equivalent parser as input
      pragmas - the pragmas to apply
      Returns:
      a parser p that is behaviorally equivalent to loop.apply(p).
      Throws:
      NullPointerException - if loop is or returns null
      UnsupportedOperationException - optionally if the Productive pragma is specified, and isConsuming() method of the parser returned by loop returns false.
    • fix

      public final <D, T> Parser<D,T> fix(UnaryOperator<Parser<D,T>> loop, UnaryOperator<Parser<D,T>> polish, Parser.Pragma... pragmas)
    • sideEffect

      public final <D, T> Parser<D,T> sideEffect(Runnable effect)
      Returns a parser that always succeeds without semantic effect, and performs the given side effect directly.

      Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      effect - the side effect to perform
      Returns:
      a parser that always succeeds without semantic effect, and invokes effect directly
      Throws:
      NullPointerException - if effect is null
    • sideEffect

      public final <D, T> Parser<D,T> sideEffect(Consumer<Input<D,T>> effect)
      Returns a parser that always succeeds without semantic effect, and performs the given side effect directly.

      Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      effect - the side effect to perform with the current input
      Returns:
      a parser that always succeeds without semantic effect, and invokes effect with the current input directly
      Throws:
      NullPointerException - if effect is null
    • withSideEffects

      public final <D, T> Parser<D,T> withSideEffects(Consumer<Input<D,T>> pre, Parser<D,T> body, Consumer<Input<D,T>> post)
      Adds side effects to a given parser to be performed directly before and after, respectively.

      Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      pre - the side effect to perform before parsing with the current input
      body - the parser to extend
      post - the side effect to perform after parsing with the remaining input
      Returns:
      a parser that behaves equivalently to body, but additionally invokes pre with its current input directly before parsing, and post with the same input directly after parsing
      Throws:
      NullPointerException - if pre, body or post is null
    • getLocator

      public <D, T> ParserFactory.Locator<D,T> getLocator()
      Returns a location tracking service.
      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Returns:
      a location tracking service