Interface Input<D,T>

Type Parameters:
D - the type of source document identifiers
T - the type of token types; typically an enum type
All Superinterfaces:
Comparable<Input<D,T>>

public interface Input<D,T> extends Comparable<Input<D,T>>
Interface of immutable parser input states. Each state is a pointer into an immutable infinite sequence of tokens.

For actual, finite input the token sequence should be extended by a potentially infinite repetition of distinguished end tokens (typically of a reserved type EOF) carrying no further information.

  • Method Summary

    Modifier and Type
    Method
    Description
    default int
    compareTo(Input<D,T> other)
    Compare two inputs for temporal order.
    Returns a new input that is advanced by one token.
    void
    Ensures that this input has at least one stored lookahead token.
    long
    Returns the number of tokens that have been consumed from this input.
    Returns an object that identifies the token source.
    boolean
    Checks whether one or more lookahead tokens have been generated ahead.
    boolean
    Checks whether this input is at the start and has not yet consumed any tokens.
    lookahead(int k)
    Returns the token that lies a given number of steps ahead.
    Returns the most recently consumed token.
    static <D, T> Input<D,T>
    of(Supplier<? extends Token<D,T>> gen)
    Returns a parser input state lazily backed by a generator.
    default Output<D,T>
    Returns an output specifying the given effect and this input as the remainder.
  • Method Details

    • lookahead

      Token<D,T> lookahead(int k)
      Returns the token that lies a given number of steps ahead.

      Numbering starts from zero; k = 0 selects the next token.

      Parameters:
      k - the number of steps to look ahead
      Returns:
      the token that lies k steps ahead from this state.
      Throws:
      IllegalArgumentException - if k < 0.
    • hasLookahead

      boolean hasLookahead()
      Checks whether one or more lookahead tokens have been generated ahead.

      Modifications to the token source do only take effect immediately if this method returns false.

      Returns:
      true if one or more lookahead tokens have been generated ahead; false if this input is fully synchronous with the token source
      See Also:
    • isStart

      boolean isStart()
      Checks whether this input is at the start and has not yet consumed any tokens.

      The method lookbehind() may only be called if this method returns false.

      Returns:
      true if this input is at the start; false if some tokens have already been consumed
      See Also:
    • lookbehind

      Token<D,T> lookbehind()
      Returns the most recently consumed token.
      Returns:
      the most recently consumed token
      Throws:
      IllegalStateException - if this state points to the start of the token sequence
      See Also:
    • consume

      Input<D,T> consume()
      Returns a new input that is advanced by one token.
      Returns:
      an input state pointing one step ahead in the token sequence
    • transitionBy

      default Output<D,T> transitionBy(Update<SimpleMessage<D>> effect)
      Returns an output specifying the given effect and this input as the remainder.
      Parameters:
      effect - the effect of parsing
      Returns:
      an output specifying effect, and this as the remainder
      Throws:
      NullPointerException - if effect is null
    • getSourceIdentifier

      Object getSourceIdentifier()
      Returns an object that identifies the token source.

      The resulting object should be used only in equality comparisons, to determine whether two inputs are snapshots of the consumption of the same source.

      Returns:
      an object that identifies the token source
    • getIndex

      long getIndex()
      Returns the number of tokens that have been consumed from this input.
      Returns:
      the number of tokens that have been consumed
    • compareTo

      default int compareTo(Input<D,T> other)
      Compare two inputs for temporal order.

      Inputs are compared by the number of consumed tokens, if they have the same token source. Otherwise, they are incomparable.

      Specified by:
      compareTo in interface Comparable<D>
      Parameters:
      other - the other input to compare to
      Returns:
      an integer that represents the comparison of the respective number of consumed tokens
      Throws:
      NullPointerException - if other is null
      IllegalArgumentException - if this and other have different token sources
      See Also:
    • force

      void force()
      Ensures that this input has at least one stored lookahead token.

      If there is no lookahead token, the token source is queried. Otherwise, nothing happens.

      See Also:
    • of

      static <D, T> Input<D,T> of(Supplier<? extends Token<D,T>> gen)
      Returns a parser input state lazily backed by a generator.

      The returned input state represent the potentially infinite sequence of tokens obtained by successive invocations of the method Supplier.get() of the given generator.

      The sequence is created lazily; new tokens are demanded only when required by lookahead(int) or consume(). Thus concurrent shared use of the generator may result in nondeterministic behavior.

      The implementation is thread-safe: concurrent shared uses of the resulting Input object observe consistent token sequences. However, the underlying supplier is per definition not thread-safe, and must not be used concurrently.

      Type Parameters:
      D - the type of source document identifiers
      T - the type of token types; typically an enum type
      Parameters:
      gen - a functional object that generates tokens on demand
      Returns:
      an input state lazily backed by the given generator.