Class Predictor<T>
- Type Parameters:
T- the type of token types
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 TypeMethodDescriptionaccept()Returns a predicate that accepts next tokens according to this predictor.booleanacceptsType(T tokenType) Checks whether the given next token type is acceptable for this predictor.Combines this and another predictor sequentially.static <T> Predictor<T> epsilon()Returns a predictor that represents the acceptance of no token types but optional consumption.booleanstatic <T> Predictor<T> fail()Returns a predictor that represents the acceptance of no token types but mandatory consumption.Returns the whitelist of types of the next acceptable token.inthashCode()booleanIndicates whether this predictor represents mandatory consumption.booleanIndicates whether this predictor has a specific whitelist of types of the next acceptable token.static <T> Predictor<T> Combines several predictors in a switch-like way.notSharedWith(Predictor<T> other) 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.Combines this and another predictor disjunctively.booleanstatic <T> Predictor<T> plus()Returns a predictor that represents the acceptance of all token types and mandatory consumption.sharedWith(Predictor<T> other) Returns the subset of the whitelist of this predictor that is shared with a given predictor.static <T> Predictor<T> star()Returns a predictor that represents the acceptance of all token types and optional consumption.booleantoleratesType(T tokenType) Checks whether the given next token type is acceptable for this predictor.toString()static <T> Predictor<T> union(Collection<? extends Predictor<T>> alts) Combines several predictors disjunctively.
-
Method Details
-
hashCode
public int hashCode() -
equals
-
toString
-
isConsuming
public boolean isConsuming()Indicates whether this predictor represents mandatory consumption.- Returns:
trueif, according to this predictor, some token must always be consumed;falseif 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:
trueif this predictor has a finite whitelist of possible types of the next acceptable token;falseif the token type cannot be predicted
-
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
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:
trueif, according to this predictor, the given token type is acceptable for the next consumed token;falseotherwise- See Also:
-
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
Checks whether the given next token type is acceptable for this predictor.- Parameters:
tokenType- the next token type- Returns:
trueif, 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;falseotherwise- See Also:
-
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
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
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
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
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- ifwhitelistis or contains null
-
optional
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- ifwhitelistis or contains null
-
of
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- ifwhitelistis or contains null
-
optional
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- ifwhitelistis or contains null
-
of
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 tokenoptional- 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- ifwhitelistis or contains null
-
andThen
Combines this and another predictor sequentially.- Parameters:
other- the other predictor- Returns:
- a predictor for the sequential composition of the parser behaviors
represented by
thisandother, respectively - Throws:
NullPointerException- ifotheris null
-
orElse
Combines this and another predictor disjunctively.- Parameters:
other- the other predictor- Returns:
- a predictor for the logical disjunction of the parser behaviors
represented by
thisandother, respectively - Throws:
NullPointerException- ifotheris null
-
union
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- ifaltsis or contains null
-
lookup
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 predictorsotherwise- the predictor for token types that have no entry- Returns:
- a predictor that combines
altsandotherwisein a switch-like way - Throws:
NullPointerException- isaltsis or contains null orotherwiseis null
-
overlaps
-
butConsuming
-