Class Parser<D,T>

java.lang.Object
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
All Implemented Interfaces:
Action.Continuation<Input<D,T>,Output<D,T>,SimpleMessage<D>,Input<D,T>>, Function<Input<D,T>,Action<Output<D,T>,SimpleMessage<D>,Input<D,T>>>

@Generated(generator="eu.bandm.tools.expander", version="1.3.1", timestamp="2026-08-26T09:14:17") public abstract class Parser<D,T> extends Object implements Action.Continuation<Input<D,T>,Output<D,T>,SimpleMessage<D>,Input<D,T>>
Combinatorial nondeterministic parser building blocks.

Every parser building block is essentially a transition step, a function taking Input to Output, with computational behavior specified by Action and diagnostic messages of type SimpleMessage.

Subclasses must implement this behavior by overriding the abstract method Action<Output<D, T>, SimpleMessage<D>, Input<D, T>> apply(Input<D, T>, Success<?, SimpleMessage<D>, Input<D, T>>).

Configurational and Operational Methods

The methods of this class are divided into a configurational and an operational phase:

  • Configurational methods create new parser objects, often by combining existing ones, or query invariant properties. They make invoke other configurational methods on parser objects, but no operational methods. They must not change the observable state of parser objects.
  • Operational methods give access to the parsing behavior. They should not to change the observable state of parser objects.

Operational methods are explicitly marked in this documentation; all others are configurational.

Behavioral Equivalence

The results of configurational methods are typically specified only in terms of equivalent behavior. Two parsers are behaviorally equivalent if replacing one by the other in the following procedure does not change resulting search trees.

  • Constructor Details

    • Parser

      protected Parser(ParserFactory factory)
      Creates a new instance.
      Parameters:
      factory - the factory that has been responsible for creating this parser.
      Throws:
      NullPointerException - if factory is null
  • Method Details

    • apply

      public abstract Action<Output<D,T>,SimpleMessage<D>,Input<D,T>> apply(Input<D,T> in, Action.Success<?,SimpleMessage<D>,Input<D,T>> recyclable)
      Applies this continuation, possibly using a recyclable leaf node. Operational method.
      Specified by:
      apply in interface Action.Continuation<Input<D,T>,Output<D,T>,SimpleMessage<D>,Input<D,T>>
      Parameters:
      in - the input parsing result
      recyclable - a recyclable leaf node, or null if not available
      Returns:
      a leaf node with a newly computed parsing result; either recyclable if available or new.
      See Also:
    • getPredictor

      public final Predictor<T> getPredictor()
      Returns the predictor for this parser.

      This method must not be called before the constructor has returned.

      Returns:
      the predictor for this parser
    • initializePredictor

      protected Predictor<T> initializePredictor()
      Creates the predictor for this parser.

      This method should not be called explicitly.

      The default implementation returns Predictor.star(), the predictor that contains no information.

      Returns:
      the new predictor for this parser
    • isConsuming

      public final boolean isConsuming()
      Checks whether this parser can only succeed by consuming input.
      Returns:
      true if this parser only succeeds after having consumed one or more input tokens (including the case that it fails always); false if this parser can succeed without consuming input.
    • getFactory

      public final ParserFactory getFactory()
      Returns the factory responsible for creating this parser.

      Mixing parsers created by different factories, and hence with possibly different configuration settings, is possible but not recommended.

      Returns:
      the factory responsible for creating this parser
    • getLocationTree

      public @Opt LocationTree<String,Parser<?,?>> getLocationTree()
      Returns the location tree for this parser if available.

      If caller locations are available, this tree identifies the client code that has created each building block of this parser.

      Returns:
      the location tree for this parser, or null if not available
    • identity

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> identity()
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> succeed(Update<SimpleMessage<D>> effect)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> constant(Object value)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> startCollection()
      Deprecated.
      Returns a parser that succeeds and sets the result to the empty sequence, consuming no tokens.

      This operation is useful in conjunction with collecting().

      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> fail()
      Deprecated.
      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 Parser<D,T> negate()
      Returns a parser that succeeds if this parser fails, and vice versa.

      The resulting parser speculatively executes the behavior of this parser. 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.

      Returns:
      a parser that succeeds with no effect if this parser fails and vice versa.
      See Also:
    • startInterval

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> startInterval()
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> endInterval()
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Parser<D,T> lookbehind(T... types)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> lookbehind(Set<T> types)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> lookbehind(Predicate<? super Token<D,T>> match)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> terminal()
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> terminal(Predicate<Token<D,T>> match)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> terminal(T type)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <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:
    • diagnose

      @SafeVarargs public final Parser<D,T> diagnose(SimpleMessage<D>... msgs)
      Add diagnostic messages to the behavior of this parser.
      Parameters:
      msgs - the messages to add
      Returns:
      a parser that behaves equivalently to this parser, but annotates the transition output with the given messages.
    • diagnose

      public final Parser<D,T> diagnose(Function<? super Input<D,T>,? extends List<? extends SimpleMessage<D>>> msgs)
      Add diagnostic messages to the behavior of this parser.
      Parameters:
      msgs - a function that computes the messages to add from the current input
      Returns:
      a parser that behaves equivalently to this parser, but annotates the transition output with the computed messages.
    • map

      public final Parser<D,T> map(UnaryOperator<Data> transform)
      Adds a final transformation of the result value to the behavior of this parser.
      Parameters:
      transform - the transformation to apply to the result value
      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect transforms the result value by the specified function.
      Throws:
      NullPointerException - if transform is null
    • assigning

      public final Parser<D,T> assigning(Object key)
      Adds the assignment of the result value to a semantic variable to the behavior of this parser.
      Parameters:
      key - the key identifying the semantic variable to assign to
      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect also assigns the result value to the specified variable.
    • collecting

      public final Parser<D,T> collecting()
      Adds the collecting of the result value in a sequence to the behavior of this parser.

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

      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect then appends the result value to the preceding one, if that is a sequence, or otherwise fails.
      See Also:
    • withScope

      public final Parser<D,T> withScope(Object key, Data initializer)
      Wraps the behavior of this 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 inializes the specified variable before, and restores the previous value afterwards.

      Parameters:
      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 this parser, but whose semantic effect executes in a context where the specified variable is locally scoped.
      Throws:
      NullPointerException - if key or initializer is null
    • withScope

      public final Parser<D,T> withScope(Map<?,? extends Data> initializers)
      Wraps the behavior of this parser 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 values afterwards.

      Parameters:
      initializers - the map of keys to initial value of the variable in its local scope
      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
      Throws:
      NullPointerException - if initializers is or contains null
    • withScope

      @SafeVarargs public final Parser<D,T> withScope(Fragment<?,?,?>... fragments)
      Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.

      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 values afterwards.

      This method creates a local scope for the target variables of all given fragments that are not marked as imported, and initializes them with the specified values.

      Parameters:
      fragments - the list of grammar fragments
      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
      Throws:
      NullPointerException - if fragments is or contains null
      IllegalArgumentException - if the list of fragments contains duplicate target variable keys.
      See Also:
    • withScope

      public final Parser<D,T> withScope(List<? extends Fragment<?,?,?>> fragments)
      Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.

      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 values afterwards.

      This method creates a local scope for the target variables of all given fragments that are not marked as imported, and initializes them with the specified values.

      Parameters:
      fragments - the list of grammar fragments
      Returns:
      a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
      Throws:
      NullPointerException - if fragments is or contains null
      IllegalArgumentException - if the list of fragments contains duplicate target variable keys.
      See Also:
    • postprocess

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

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

      Parameters:
      effect - the semantic effect to append
      Returns:
      a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given effect.
      Throws:
      NullPointerException - if effect is null
    • postprocess

      public final Parser<D,T> postprocess(Action.Continuation<State,State,SimpleMessage<D>,State> cont)
      Appends an ad-hoc continuation to the semantic effect of this parser.
      Parameters:
      cont - the semantic continuation to append
      Returns:
      a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
    • postprocess

      public final Parser<D,T> postprocess(Action.Continuation<State,State,SimpleMessage<D>,State> cont, String description)
      Appends an ad-hoc continuation to the semantic effect of this parser.
      Parameters:
      cont - the semantic continuation to append
      description - a user-friendly description of the continuation
      Returns:
      a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
    • postprocess

      public final Parser<D,T> postprocess(Action.Continuation<State,State,SimpleMessage<D>,State> cont, Supplier<String> description)
      Appends an ad-hoc continuation to the semantic effect of this parser.
      Parameters:
      cont - the semantic continuation to append
      description - generates a user-friendly description of the continuation
      Returns:
      a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
    • ignoringResult

      public final Parser<D,T> ignoringResult()
      Removes the setting of the result value from the semantic effect of this parser. All other effects, such as diagnostics and assignments to state variables, are unaffected.
      Returns:
      a parser that behaves equivalently to this parser, but where the previous result value is restored.
    • whenFollowedBy

      protected Parser<D,T> whenFollowedBy(Predictor<T> following, Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser if consumed input is followed by the specified token types.

      The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the specified kind. Otherwise, behavior is undefined.

      Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts.

      Parameters:
      following - a specification of the possible following token
      pragmas - the pragmas to apply
      Returns:
      a parser that is equivalent to this for all inputs where the consumed tokens are immediately followed by a token for which following.accept would return true
      Throws:
      NullPointerException - if following is null
      See Also:
    • whenFollowedBy

      protected Parser<D,T> whenFollowedBy(Predictor<T> following, Rewriting<Parser<D,T>,Predictor<T>,Parser.Pragma[]>.Context context)
    • closeAndTerminate

      public final Parser<D,T> closeAndTerminate(T endType, Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser if consumed input is followed by the specified token type.

      The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the specified type. Otherwise, behavior is undefined.

      Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts. Use this operation only on top-level parsers that consume a unit of input terminated by a distinguished end token.

      Parameters:
      endType - the type of the token that follows and terminates the consumed input
      pragmas - the pragmas to apply
      Returns:
      a parser that is equivalent to this for all inputs where the consumed tokens are immediately followed by a token of type endType
      Throws:
      NullPointerException - if endType is null
      See Also:
    • closeAndRepeat

      public final Parser<D,T> closeAndRepeat(Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser if consumed input is followed by more input of the same syntax.

      The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the expected type. Otherwise, behavior is undefined.

      Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts. Use this operation only on top-level parsers that lazily consume a unit of input from a potentially infinite stream.

      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that is equivalent to this for all inputs where the consumed tokens are immediately followed by input of the same syntax
      See Also:
    • andThen

      public final Parser<D,T> andThen(Parser<D,T> other)
      Follows the behavior of this parser sequentially with another.
      Parameters:
      other - the subsequent parser
      Returns:
      a parser that behaves equivalently to this parser, sequentially followed by other.
      Throws:
      NullPointerException - if other is null
    • andThen

      public final Parser<D,T> andThen(Parser<D,T> other, Parser.Pragma... pragmas)
      Follows the behavior of this parser sequentially with another.

      This method supports the following pragmas:

      • Disjoint – throw an exception if the type of the next token does not determine unambiguously whether it is to be consumed by this or the other parser.
      Parameters:
      other - the subsequent parser
      Returns:
      a parser that behaves equivalently to this parser, sequentially followed by other.
      Throws:
      NullPointerException - if other is null
      UnsupportedOperationException - if there is a type of tokens that could both be consumed by this, or by other after this succeeds without consuming any tokens
    • andThenCombining

      public final Parser<D,T> andThenCombining(Parser<D,T> other, Action.BiContinuation<Data,Data,Data,SimpleMessage<D>,State> combiner, Parser.Pragma... pragmas)
      Follows the behavior of this parser sequentially with another, combining the result values.

      This method supports the following pragmas:

      • Disjoint – throw an exception if the type of the next token does not determine unambiguously whether it is to be consumed by this or the other parser.
      Parameters:
      other - the subsequent parser
      combiner - a binary continuation to combine the result values
      Returns:
      a parser that behaves equivalently to this parser, sequentially followed by other, followed by the combination of result values.
      Throws:
      NullPointerException - if other or combiner is null
      UnsupportedOperationException - if there is a type of tokens that could both be consumed by this, or by other after this succeeds without consuming any tokens
    • sequence

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Parser<D,T> sequence(Parser<D,T>... elems)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> sequence(List<? extends Parser<D,T>> elems)
      Deprecated.
      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:
    • orElse

      public final Parser<D,T> orElse(Parser<D,T> other, Parser.Pragma... pragmas)
      Alternates the behavior of this parser with another in nondeterministic choice.

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

      This method supports the following pragmas:

      • Productive – throw an exception if both alternatives may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      • Disjoint – return a result only if the choice between the two alternatives can be made deterministically by lookahead; throw an exception otherwise.
      Parameters:
      other - the alternative parser
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to this parser, (non)deterministically branching to other if necessary.
      Throws:
      NullPointerException - if other is null
    • ifThenElse

      @Deprecated(forRemoval=false, since="1.0") public static <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:
    • optional

      public final Parser<D,T> optional(Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser, or alternatively does nothing.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of this parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to this parser, or alternatively to the parser identity().
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • optionalTagged

      public final Parser<D,T> optionalTagged(Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser, or alternatively does nothing, setting a corresponding result value.

      The semantic effect of the returned parser wraps the result value of this parser with Data.just(eu.bandm.tools.ramus.ogma.Data) if the corresponding branch has been chosen, or sets the result value to Data.none() otherwise.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of this parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to this parser, or alternatively to the parser identity(), setting a corresponding result value.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • optionalTagged

      public final Parser<D,T> optionalTagged(Parser<D,T> defaultBranch, Parser.Pragma... pragmas)
      Returns a parser that behaves like this parser, or some alternative without result value.

      The semantic effect of the returned parser wraps the result value of this parser with Data.just(eu.bandm.tools.ramus.ogma.Data) if the corresponding branch has been chosen, or sets the result value to Data.none() otherwise.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of this parser if the next token is of an acceptable type. Note that the alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      defaultBranch - the parser that specifies the alternative
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to this parser, or alternatively to defaultBranch, setting a corresponding result value.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • choice

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Parser<D,T> choice(Parser<D,T>... alts)
      Deprecated.
      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(eu.bandm.tools.ramus.ogma.Parser<D, T>), 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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> choice(List<? extends Parser<D,T>> alts)
      Deprecated.
      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(eu.bandm.tools.ramus.ogma.Parser<D, T>), 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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> lookup(Map<T,Parser<D,T>> alts, Parser.Pragma... pragmas)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <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

      @Deprecated(forRemoval=false, since="1.0") public static <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 recursion step 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.
    • star

      public final Parser<D,T> star(Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, zero or more times. Decreasing numbers of iterations are tried as nondeterministic alternatives if something fails downstream.

      This method is a shorthand for plus().optional().

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of zero or more sequential iterations of this parser.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.mayBeEpsilon() returns true.
      See Also:
    • plus

      public final Parser<D,T> plus(Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, one or more times. Decreasing numbers of iterations are tried as nondeterministic alternatives if something fails downstream.

      This method is a shorthand for fix(p -> this.andThen(p.optional())).

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of one or more sequential iterations of this parser.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • starSequence

      public final Parser<D,T> starSequence(Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, zero or more times, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.

      This method is a shorthand for startCollection().andThen(this.collecting().star(pragmas)).

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of zero or more sequential iterations of this parser.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • plusSequence

      public final Parser<D,T> plusSequence(Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, one or more times, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.

      This method is a shorthand for startCollection().andThen(this.collecting().plus(pragmas)).

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of one or more sequential iterations of this parser.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and this.isConsuming() returns false.
      See Also:
    • starSequence

      public final Parser<D,T> starSequence(Parser<D,T> separator, Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, zero or more times, interspersed with a separator parser, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser and the separator may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      separator - the parser to invoke between consecutive iterations
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of zero or more sequential iterations of this parser, interspersed with separator.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and both this.isConsuming() and separator.isConsuming() return false.
      See Also:
    • plusSequence

      public final Parser<D,T> plusSequence(Parser<D,T> separator, Parser.Pragma... pragmas)
      Returns a parser that behaves as the sequential iteration of this parser, one or more times, interspersed with a separator parser, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser and the separator may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      separator - the parser to invoke between consecutive iterations
      pragmas - the pragmas to apply
      Returns:
      a parser that behaves equivalently to the choice of any number of one or more sequential iterations of this parser, interspersed with separator.
      Throws:
      UnsupportedOperationException - optionally if the Productive pragma is specified, and both this.mayBeEpsilon() and {separator.mayBeEpsilon()} return true.
      See Also:
    • guard

      public final Parser<D,T> guard(Predicate<State> condition, Function<State,List<SimpleMessage<D>>> error)
      Returns a parser that extends the behavior of this parser with a mandatory check and conditional error diagnostics.
      Parameters:
      condition - the condition to check on the post state
      error - a function to provide diagnostics for the post state if the condition fails
      Returns:
      a parser that behaves equivalently to this parser, but then only succeeds if condition returns true for the post state; otherwise diagnostics created by applying error to the same state are attached, and the state is rejected
      Throws:
      NullPointerException - if condition or error is null
    • process

      public @Opt Action<State,SimpleMessage<D>,State> process(Input<D,T> input, Action.Visitor<Output<D,T>,SimpleMessage<D>,Input<D,T>> errorVisitor)
      Processes an input stream with this parser and the default initial semantic state.

      This method either returns a non-null search tree with at least one successful parsing result, or invokes errorVisitor to collect diagnostics in case of failure, but not both.

      Parameters:
      input - the input stream to process
      errorVisitor - a visitor for processing errors if parsing has failed
      Returns:
      a search tree of parsing results if there is at least one success, or null otherwise
      Throws:
      NullPointerException - if input or errorVisitor is null
    • process

      public @Opt Action<State,SimpleMessage<D>,State> process(Input<D,T> input, Action.Visitor<Output<D,T>,SimpleMessage<D>,Input<D,T>> errorVisitor, State initialState)
      Processes an input stream with this parser and a given initial semantic state.

      This method either returns a non-null search tree with at least one successful parsing result, or invokes errorVisitor to collect diagnostics in case of failure, but not both.

      Parameters:
      input - the input stream to process
      errorVisitor - a visitor for processing errors if parsing has failed
      initialState - the initial state for semantic analysis
      Returns:
      a search tree of parsing results if there is at least one success, or null otherwise
      Throws:
      NullPointerException - if input, errorVisitor or initialState is null
    • sideEffect

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> sideEffect(Runnable effect)
      Deprecated.
      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

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Parser<D,T> sideEffect(Consumer<Input<D,T>> effect)
      Deprecated.
      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 Parser<D,T> withSideEffects(Consumer<Input<D,T>> pre, Consumer<Input<D,T>> post)
      Adds side effects to this 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.

      Parameters:
      pre - the side effect to perform before parsing with the current input
      post - the side effect to perform after parsing with the remaining input
      Returns:
      a parser that behaves equivalently to this, but additionally invokes pre with its current input directly before parsing, and post with the same input directly after parsing
      Throws:
      NullPointerException - if pre or post is null
    • withOtherTokenSource

      public final <K> Parser<D,T> withOtherTokenSource(LookaheadTokenMultiplexer<K,D,T,?> multi, K newKey)
      Extends the behavior of this parser by switching input token sources.

      Note that token source switching is only supported in situations where no lookahead tokens have been acquired. IllegalStateException will be thrown if lookahead tokens are present when the switch is performed.

      Type Parameters:
      K - the type of token multiplexer keys
      Parameters:
      multi - the token multiplexer to use for switching
      newKey - the key to switch the token source to during parsing with the extended parser
      Returns:
      a parser that behaves equivalently to this parser, but switches to the token source associated with newKey before parsing, and switches back afterwards, on both success and failure
      Throws:
      NullPointerException - if multi or newKey is null