Class Predictor<T>

java.lang.Object
eu.bandm.tools.ramus.ogma.Predictor<T>
Type Parameters:
T - the type of token types

public class Predictor<T> extends Object
Approximation of what a parser may accept for consumption.

The approximation is based on two pieces of information:

  • Whether the type of the next token to be consumed can be restricted, and if so, to which specific finite whitelist of token types.
  • Whether or not the parser may complete successfully without consuming any token.
  • Method Summary

    Modifier and Type
    Method
    Description
    <D> Predicate<Token<D,T>>
    Returns a predicate that accepts next tokens according to this predictor.
    boolean
    acceptsType(T tokenType)
    Checks whether the given next token type is acceptable for this predictor.
    Combines this and another predictor sequentially.
     
    static <T> Predictor<T>
    Returns a predictor that represents the acceptance of no token types but optional consumption.
    boolean
    static <T> Predictor<T>
    Returns a predictor that represents the acceptance of no token types but mandatory consumption.
    Returns the whitelist of types of the next acceptable token.
    int
    boolean
    Indicates whether this predictor represents mandatory consumption.
    boolean
    Indicates whether this predictor has a specific whitelist of types of the next acceptable token.
    static <T> Predictor<T>
    lookup(Map<T,Predictor<T>> alts, Predictor<T> otherwise)
    Combines several predictors in a switch-like way.
    Returns the subset of the whitelist of this predictor that is not shared with a given predictor.
    static <T> Predictor<T>
    of(Collection<? extends T> whitelist)
    Returns a predictor that represents mandatory consumption of a token of one of the given whitelist of acceptable types.
    static <T> Predictor<T>
    of(Collection<? extends T> whitelist, boolean optional)
    Returns a predictor that represents consumption of a token of one of the given whitelist of acceptable types.
    static <T> Predictor<T>
    of(T... whitelist)
    Returns a predictor that represents mandatory consumption of a token of one of the given whitelist of acceptable types.
    static <T> Predictor<T>
    optional(Collection<? extends T> whitelist)
    Returns a predictor that represents optional consumption of a token of one of the given whitelist of acceptable types.
    static <T> Predictor<T>
    optional(T... whitelist)
    Returns a predictor that represents optional consumption of a token of one of the given whitelist of acceptable types.
    orElse(Predictor<T> other)
    Combines this and another predictor disjunctively.
    boolean
     
    static <T> Predictor<T>
    Returns a predictor that represents the acceptance of all token types and mandatory consumption.
    Returns the subset of the whitelist of this predictor that is shared with a given predictor.
    static <T> Predictor<T>
    Returns a predictor that represents the acceptance of all token types and optional consumption.
    boolean
    toleratesType(T tokenType)
    Checks whether the given next token type is acceptable for this predictor.
    static <T> Predictor<T>
    union(Collection<? extends Predictor<T>> alts)
    Combines several predictors disjunctively.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isConsuming

      public boolean isConsuming()
      Indicates whether this predictor represents mandatory consumption.
      Returns:
      true if, according to this predictor, some token must always be consumed; false if the described parser may be satisfied without consuming a token
    • isSpecific

      public boolean isSpecific()
      Indicates whether this predictor has a specific whitelist of types of the next acceptable token.
      Returns:
      true if this predictor has a finite whitelist of possible types of the next acceptable token; false if the token type cannot be predicted
    • getSpecificWhitelist

      public Set<T> getSpecificWhitelist()
      Returns the whitelist of types of the next acceptable token.
      Returns:
      an unmodifiable set containing all possible types of the next acceptable token, according to this predictor
      Throws:
      UnsupportedOperationException - if no whitelist is available
    • acceptsType

      public boolean acceptsType(T tokenType)
      Checks whether the given next token type is acceptable for this predictor.

      This operation is designed for construction-time analyses. For actual use in guiding a parser, use accept() instead.

      Parameters:
      tokenType - the next token type
      Returns:
      true if, according to this predictor, the given token type is acceptable for the next consumed token; false otherwise
      See Also:
    • accept

      public <D> Predicate<Token<D,T>> accept()
      Returns a predicate that accepts next tokens according to this predictor.

      The returned predicate object may be optimized for repeated use.

      Type Parameters:
      D - the type of document identifiers
      Returns:
      a predicate that accepts exactly those tokens which have acceptable types according to this predictor
    • toleratesType

      public boolean toleratesType(T tokenType)
      Checks whether the given next token type is acceptable for this predictor.
      Parameters:
      tokenType - the next token type
      Returns:
      true if, according to this predictor, the given token type is acceptable for the next consumed token, or it is possible that the token is not consumed at all; false otherwise
      See Also:
    • star

      public static <T> Predictor<T> star()
      Returns a predictor that represents the acceptance of all token types and optional consumption.

      The parser behavior represented by this descriptor can complete successfully by consuming any number of tokens, including zero.

      Type Parameters:
      T - the type of token types
      Returns:
      a predictor that represents the acceptance of all token types and optional consumption
      See Also:
    • plus

      public static <T> Predictor<T> plus()
      Returns a predictor that represents the acceptance of all token types and mandatory consumption.

      The parser behavior represented by this descriptor can complete successfully by consuming any non-zero number of tokens.

      Type Parameters:
      T - the type of token types
      Returns:
      a predictor that represents the acceptance of all token types and mandatory consumption
      See Also:
    • epsilon

      public static <T> Predictor<T> epsilon()
      Returns a predictor that represents the acceptance of no token types but optional consumption.

      The parser behavior represented by this descriptor can only complete successfully without consuming a token.

      Type Parameters:
      T - the type of token types
      Returns:
      a predictor that represents the acceptance of no token types but optional consumption
      See Also:
    • fail

      public static <T> Predictor<T> fail()
      Returns a predictor that represents the acceptance of no token types but mandatory consumption.

      The parser behavior represented by this descriptor can never complete successfully.

      Type Parameters:
      T - the type of token types
      Returns:
      a predictor that represents the acceptance of no token types but mandatory consumption
      See Also:
    • of

      @SafeVarargs public static <T> Predictor<T> of(T... whitelist)
      Returns a predictor that represents mandatory consumption of a token of one of the given whitelist of acceptable types.

      If a token type occurs more than once in the whitelist, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      whitelist - an array of token types that are acceptable for the next token
      Returns:
      a predictor that represents mandatory consumption of a token of one of the types in whitelist
      Throws:
      NullPointerException - if whitelist is or contains null
    • optional

      @SafeVarargs public static <T> Predictor<T> optional(T... whitelist)
      Returns a predictor that represents optional consumption of a token of one of the given whitelist of acceptable types.

      If a token type occurs more than once in the whitelist, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      whitelist - an array of token types that are acceptable for the next token
      Returns:
      a predictor that represents consumption of token of one of the types in whitelist, or no token at all
      Throws:
      NullPointerException - if whitelist is or contains null
    • of

      public static <T> Predictor<T> of(Collection<? extends T> whitelist)
      Returns a predictor that represents mandatory consumption of a token of one of the given whitelist of acceptable types.

      If a token type occurs more than once in the whitelist, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      whitelist - a collection of token types that are acceptable for the next token
      Returns:
      a predictor that represents consumption of token of one of the types in whitelist
      Throws:
      NullPointerException - if whitelist is or contains null
    • optional

      public static <T> Predictor<T> optional(Collection<? extends T> whitelist)
      Returns a predictor that represents optional consumption of a token of one of the given whitelist of acceptable types.

      If a token type occurs more than once in the whitelist, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      whitelist - a collection of token types that are acceptable for the next token
      Returns:
      a predictor that represents consumption of token of one of the types in whitelist, or no token at all
      Throws:
      NullPointerException - if whitelist is or contains null
    • of

      public static <T> Predictor<T> of(Collection<? extends T> whitelist, boolean optional)
      Returns a predictor that represents consumption of a token of one of the given whitelist of acceptable types.

      If a token type occurs more than once in the whitelist, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      whitelist - a collection of token types that are acceptable for the next token
      optional - specifies whether consumption is optional
      Returns:
      a predictor that represents consumption of token of one of the types in whitelist, or no token at all
      Throws:
      NullPointerException - if whitelist is or contains null
    • andThen

      public Predictor<T> andThen(Predictor<T> other)
      Combines this and another predictor sequentially.
      Parameters:
      other - the other predictor
      Returns:
      a predictor for the sequential composition of the parser behaviors represented by this and other, respectively
      Throws:
      NullPointerException - if other is null
    • orElse

      public Predictor<T> orElse(Predictor<T> other)
      Combines this and another predictor disjunctively.
      Parameters:
      other - the other predictor
      Returns:
      a predictor for the logical disjunction of the parser behaviors represented by this and other, respectively
      Throws:
      NullPointerException - if other is null
    • union

      public static <T> Predictor<T> union(Collection<? extends Predictor<T>> alts)
      Combines several predictors disjunctively.

      If a predictor occurs more than once, this has no further effect.

      Type Parameters:
      T - the type of token types
      Parameters:
      alts - the predictors to combine
      Returns:
      a predictor for the logical disjunction of the parser behaviors represented by the elements of alts
      Throws:
      NullPointerException - if alts is or contains null
    • lookup

      public static <T> Predictor<T> lookup(Map<T,Predictor<T>> alts, Predictor<T> otherwise)
      Combines several predictors in a switch-like way.

      The resulting predictor represents a deterministic choice among the given predictors, depending on the type of the next token.

      Type Parameters:
      T - the type of token types
      Parameters:
      alts - a map of token types to predictors
      otherwise - the predictor for token types that have no entry
      Returns:
      a predictor that combines alts and otherwise in a switch-like way
      Throws:
      NullPointerException - is alts is or contains null or otherwise is null
    • sharedWith

      public Set<T> sharedWith(Predictor<T> other)
      Returns the subset of the whitelist of this predictor that is shared with a given predictor.
      Parameters:
      other - the other predictor
      Returns:
      the subset of the whitelist of this predictor that is shared with other
      Throws:
      NullPointerException - if other is null
      UnsupportedOperationException - if this predictor has no finite whitelist
    • notSharedWith

      public Set<T> notSharedWith(Predictor<T> other)
      Returns the subset of the whitelist of this predictor that is not shared with a given predictor.
      Parameters:
      other - the other predictor
      Returns:
      the subset of the whitelist of this predictor that is not shared with other
      Throws:
      NullPointerException - if other is null
      UnsupportedOperationException - if this predictor has no finite whitelist
    • overlaps

      public boolean overlaps(Predictor<T> other)
    • butConsuming

      public Predictor<T> butConsuming()