Interface Pattern<A>
- Type Parameters:
A- the target type of objects to match against
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
AbstractPattern,Adaptive,All,Atomic,Binary,Both,Both.Backtrack,Both.NoBacktrack,CachedTransform,Constraint,Either,EnumBranch,Filter,FlatMultiTransform,IntBranch,Lazy,MultiTransform,Proxy,RestrictedTransform,Search,Theme,TotalTransform,Transform,Unary,Variable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Subclasses must implement the match(A) method.
Nondeterministic subclasses must override the matchAgain()
method. Subclasses with local matching state and/or subpatterns must
override the fork() method.
Subclasses should override the isDeterministic(), binds(Variable) and preserves(Variable,boolean) methods, if
appropriate, to improve runtime information on pattern behavior.
Serialization
This base interface extends Serializable. Subclasses
should not add fields that may hold references to potentially
non-serializable objects in their persistent state. However, such references
may be held in temporary state during a nondeterministic matching session;
the corresponding fields should be declared transient.
Deserialized pattern objects are generally in an initial state, as after a
call to clear(true).
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> Pattern<A> Combines several patterns conjunctively.static <A> Pattern<A> Combines several patterns conjunctively.Combines this patterns conjunctively with another pattern.Returns a pattern that matches like this pattern, and invokes a given action for each success.Returns a pattern that matches like this pattern, and invokes a given consumer on the target for each success.static <A> Pattern<A> any()Returns a pattern that matches anything.Returns a deterministic pattern that matches any target and executes an action.default booleanChecks whether a variable is bound by this pattern.static <A> Pattern<A> Combines two patterns conjunctively.default voidclear()Disposes of all temporary internal state related to results and nondeterminism.default voidclear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.default voidcut()Disposes of all temporary internal state related to nondeterminism.default voidcut(boolean recursively) Disposes of all temporary internal state related to nondeterminism.static <A> Pattern<A> Combines two patterns disjunctively.Returns a motif that matches this pattern against each target and then binds the whole target.flatten()Returns a possibly simplified variant of this pattern.fork()Makes a copy of this pattern with independent internal state.static <A> Pattern<A> ifThenElse(Predicate<? super A> condition, Pattern<? super A> thenBody, Pattern<? super A> elseBody) Returns a pattern that delegates to either of two subpatterns, depending on a given condition.default booleanChecks whether this pattern is deterministic.limit(int n) Returns a pattern that stops after a given number of matches.booleanAttempts to match this pattern against a given object.default booleanAttempts to re-match this pattern against a previously matched object.default BooleanSupplierdefault booleanAttempts to match this pattern at most once against a given object.default booleanFIXME.narrow()Returns an equivalent pattern with narrower type parameter.noMatch()Returns a pattern that matches if and only if this pattern has no match.static <A> Pattern<A> none()Returns a pattern that matches nothing.Combines this pattern disjunctively with another pattern.Returns a pattern that matches like this pattern, but invokes a given action if there is no match.Returns a pattern that matches like this pattern, but invokes a given consumer on the target if there is no match.default booleanChecks whether a variable is preserved by this pattern unconditionally.default booleanChecks whether a variable is preserved by this pattern, conditionally on success or failure.static <A> Pattern<A> repeat()Returns a pattern that matches any target an indefinite number of times.static <A> Pattern<A> Combines several patterns disjunctively.static <A> Pattern<A> Combines several patterns disjunctively.Returns a pattern matches this pattern, but at most once per target.uniquely()Returns a pattern that matches if and only if this pattern has a unique match.Returns a pattern that matches like this pattern against non-null targets.static <A> Variable<A> variable()Creates a new anonymous pattern variable.static <A> Variable<A> Creates a new named pattern variable.
-
Field Details
-
none
A pattern that matches nothing. This pattern may be shared and reused concurrently.
-
-
Method Details
-
match
Attempts to match this pattern against a given object.If the class of this pattern imposes a specific order on matches, the first one is chosen.
The
nulltarget is generally allowed.- Parameters:
target- the object to match against- Returns:
trueif the matching is successful,falseif it fails. WhichVariables are bound by a successful or failed match depends.- See Also:
-
matchAgain
default boolean matchAgain()Attempts to re-match this pattern against a previously matched object.If the class of this pattern imposes a specific order on matches, they are chosen in that order, beginning with the second one. The behaviour of this method is unspecified if the last matching attempt of this pattern has not been successful.
Patterns classes that do not override this method are deterministic. Pattern classes that overwrite this method to provide additional matches should also override
isDeterministic().- Returns:
trueif the matching is successful,falseif it fails. WhichVariables are bound by a successful match depends. The default implementation always returnsfalse.- See Also:
-
matchVar
FIXME.- Parameters:
variable- FIXME- Returns:
- FIXME
-
cut
default void cut()Disposes of all temporary internal state related to nondeterminism. Subsequent matching attempts withmatchAgain()should fail.The default implementation invokes
cut(true). -
clear
default void clear()Disposes of all temporary internal state related to results and nondeterminism. Subsequent matching attempts withmatchAgain()should fail. Bound variables are reset to initial values.The default implementation invokes
clear(true). -
cut
default void cut(boolean recursively) Disposes of all temporary internal state related to nondeterminism.The default implementation does nothing. Subclasses that override this method should also override
clear(boolean)and ensure that it implies the effect ofcut(false).- Parameters:
recursively-trueif all subpatterns should traversed,falseotherwise.
-
clear
default void clear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.The default implementation just invokes
cut(false). Subclasses that override this method should either invoke this overridden implementation, or ensure by other means that the effect ofcut(false)is implied.- Parameters:
recursively-trueif all subpatterns should be traversed,falseotherwise.
-
matchOnce
Attempts to match this pattern at most once against a given object.Disposes of internal state of the pattern immediately by invoking
cut().- Parameters:
target- the object to match against- Returns:
trueif the matching is successful,falseif it fails. WhichVariables are bound by a successful or failed match depends.
-
binds
Checks whether a variable is bound by this pattern.The default implementation returns
false.- Parameters:
variable- a pattern variable- Returns:
trueif the variable is guaranteed to have a meaningful value when this pattern is open,falseotherwise.- Throws:
NullPointerException- ifvariableis null
-
preserves
Checks whether a variable is preserved by this pattern, conditionally on success or failure.The default implementation returns
false.- Parameters:
variable- a pattern variablesuccess- specifies preservation on success or failure- Returns:
trueif the variable value is guaranteed to be unaffected by a matching attempt with this pattern that returns a value equal tosuccess.- Throws:
NullPointerException- ifvariableis null
-
preserves
Checks whether a variable is preserved by this pattern unconditionally.The default implementation returns
false.- Parameters:
variable- a pattern variable- Returns:
trueif the variable value is guaranteed to be unaffected by any matching attempt with this pattern.- Throws:
NullPointerException- ifvariableis null
-
isDeterministic
default boolean isDeterministic()Checks whether this pattern is deterministic.The default implementation returns
false.- Returns:
trueif this pattern is guaranteed to match any object at most once,falseotherwise.- See Also:
-
narrow
Returns an equivalent pattern with narrower type parameter. The default implementation returns this pattern and relies on the class invariants for dynamic type safety.Note that, by contrast, casting
Variable<A>toVariable<B>, whereBis a subtype ofA, is not type safe.- Type Parameters:
B- the narrower target type of objects to match against- Returns:
- a pattern that is equivalent to this pattern, but only applicable
to targets of type
B
-
fork
Makes a copy of this pattern with independent internal state.Subclasses must override this method if necessary to honour the following rules for all transitively reachable patterns:
- Subpatterns that are instances of
Variablemust be shared. This implies thatVariable.fork()must returnthis. - Fields that do not refer to subpatterns, but to matching
state (changed by invocations of
match(A),matchAgain(),cut()orclear()) must be duplicated. If matching state changes are effected by modification of other objects, these must be duplicated. In summary, matching operations on this pattern and its copy must not interfere, with the exception of variable bindings. - Fields neither referring to subpatterns nor to matching state may be either shared or duplicated. The usage of such fields in a modifiable way (such that the distinction is relevant) is strongly deprecated.
- Patterns
whose fields need not be duplicated need not be duplicated themselves, but
may return
this.
- Returns:
- a copy of this pattern that has independent internal state, except for variables
- Subpatterns that are instances of
-
variable
Creates a new anonymous pattern variable.- Type Parameters:
A- the target type of objects to match against- Returns:
- a new anonymous variable
-
variable
Creates a new named pattern variable.- Type Parameters:
A- the target type of objects to match against- Parameters:
name- the name of the variable, ornullif not applicable- Returns:
- a new variable with the given name
-
uniquely
Returns a pattern that matches if and only if this pattern has a unique match.Note that the resulting pattern invokes this pattern twice, since determining that there is no second match may clobber the state of the first match. Hence nesting such patterns is strongly discouraged.
A pattern of this class is deterministic by construction.
- Returns:
- a pattern that matches if and only if this pattern has a unique match
-
limit
Returns a pattern that stops after a given number of matches.- Parameters:
n- the maximal number of matches- Returns:
- a pattern that matches any target like this patterns, but at most
ntimes - Throws:
IllegalArgumentException- ifn < 0
-
any
Returns a pattern that matches anything.A pattern returned by this method is deterministic and binds no variables.
- Type Parameters:
A- the target type of objects to match against- Returns:
- a pattern that matches any target deterministically and without side effects
-
none
Returns a pattern that matches nothing.A pattern returned by this method is vacuously deterministic and binds all variables.
- Type Parameters:
A- the target type of objects to match against- Returns:
- a pattern that matches no target
-
repeat
Returns a pattern that matches any target an indefinite number of times.A pattern returned by this method is nondeterministic and binds no variables.
- Type Parameters:
A- the target type of objects to match against- Returns:
- a pattern that matches the target indenfinitely often
-
all
Combines several patterns conjunctively.The given patterns are tried on each target in the given order. If there is no pattern to try, then the resulting pattern succeeds vacuously.
- Type Parameters:
A- the target type of objects to match against- Parameters:
patterns- the patterns to combine- Returns:
- a pattern that tries the given patterns in order, and only succeeds if they all succeed
- Throws:
NullPointerException- ifpatternsis null or contains a null element- See Also:
-
all
Combines several patterns conjunctively.The given patterns are tried on each target in the given order. If there is no pattern to try, then the resulting pattern succeeds vacuously.
- Type Parameters:
A- the target type of objects to match against- Parameters:
patterns- the patterns to combine- Returns:
- a pattern that tries the given patterns in order, and only succeeds if they all succeed
- Throws:
NullPointerException- ifpatternsis null or contains a null element- See Also:
-
some
Combines several patterns disjunctively.The given patterns are tried on each target in the given order. If there is no pattern to try, then the resulting pattern fails vacuously.
- Type Parameters:
A- the target type of objects to match against- Parameters:
patterns- the patterns to combine- Returns:
- a pattern that tries the given patterns in order, and succeeds nondeterministically whenever one of them succeeds
- Throws:
NullPointerException- ifpatternsis null or contains a null element- See Also:
-
some
Combines several patterns disjunctively.The given patterns are tried on each target in the given order. If there is no pattern to try, then the resulting pattern fails vacuously.
- Type Parameters:
A- the target type of objects to match against- Parameters:
patterns- the patterns to combine- Returns:
- a pattern that tries the given patterns in order, and succeeds nondeterministically whenever one of them succeeds
- Throws:
NullPointerException- ifpatternsis null or contains a null element- See Also:
-
and
Combines this patterns conjunctively with another pattern.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by either argument pattern.
Since the matches of the second argument pattern are repeated for each successful match of the first, it is more efficient to give arguments in decreasing order of matching costs.
This method calls
both(this, other).- Type Parameters:
B- the joint target type of objects to match against- Parameters:
other- the other pattern- Returns:
- a pattern that has a successful match for each combination of successful matches of the argument patterns. If both argument patterns are ordered, the resulting order is the lexical order of the pairs.
- Throws:
NullPointerException- ifotheris null
-
or
Combines this pattern disjunctively with another pattern.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic and mutually exclusive. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by both argument patterns.
- Type Parameters:
B- the joint target type of objects to match against- Parameters:
other- the other pattern- Returns:
- a pattern that has a successful match for each successful match of either argument pattern. If both argument patterns are ordered, the resulting order is the concatenation.
- Throws:
NullPointerException- ifotheris null- See Also:
-
both
Combines two patterns conjunctively.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by either argument pattern.
Since the matches of the second argument pattern are repeated for each successful match of the first, it is more efficient to give arguments in decreasing order of matching costs.
- Type Parameters:
A- the target type of objects to match against- Parameters:
left- an argument patternright- another argument pattern- Returns:
- a pattern that has a successful match for each combination of successful matches of the argument patterns. If both argument patterns are ordered, the resulting order is the lexical order of the pairs.
- Throws:
NullPointerException- ifleftorrightis null
-
either
Combines two patterns disjunctively.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic and mutually exclusive. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by both argument patterns.
- Type Parameters:
A- the target type of objects to match against- Parameters:
left- an argument patternright- another argument pattern- Returns:
- a pattern that has a successful match for each successful match of either argument pattern. If both argument patterns are ordered, the resulting order is the concatenation.
- Throws:
NullPointerException- ifleftorrightis null
-
noMatch
Returns a pattern that matches if and only if this pattern has no match.A pattern returned by this method is deterministic and binds no variables.
- Returns:
- a pattern that matches if and only if this pattern has no match
-
someMatch
Returns a pattern matches this pattern, but at most once per target.Further matches are discarded by invoking
this.cut().The
matchAgain()method of the resulting pattern always returnsfalse.The resulting pattern is deterministic by construction.
- Returns:
- a pattern matches this pattern, but at most once per target
-
enPassant
Returns a motif that matches this pattern against each target and then binds the whole target.Matches are combined conjunctively.
- Type Parameters:
B- the restricted target type of objects to match against- Returns:
- a motif that matches this pattern against each target and then binds it
- See Also:
-
flatten
Returns a possibly simplified variant of this pattern.The default implementation flattens conjunctions. Use this method if the original pattern uses deep conjunction and is prone to run out of stack.
- Returns:
- a possibly simplified variant of this pattern
- See Also:
-
aside
Returns a deterministic pattern that matches any target and executes an action.The resulting patterns are useful to embed in deeply nested patterns for tracing.
- Parameters:
action- the action to be executed upon match- Returns:
- a deterministic pattern that matches any target and executes
action
-
andThen
Returns a pattern that matches like this pattern, and invokes a given action for each success.- Parameters:
action- the action to invoke- Returns:
- a pattern that matches like
this, and invokesactionfor each success - Throws:
NullPointerException- ifactionis null
-
andThen
Returns a pattern that matches like this pattern, and invokes a given consumer on the target for each success.- Parameters:
consumer- the consumer to invoke- Returns:
- a pattern that matches like
this, and invokesconsumerfor each success - Throws:
NullPointerException- ifconsumeris null
-
orElse
Returns a pattern that matches like this pattern, but invokes a given action if there is no match.The action is only invoked if there no match at all for the given target, i.e., when
match(target)fails. It is not invoked whenmatchAgainfails.- Parameters:
action- the action to invoke- Returns:
- a pattern that matches like
this, but invokesactionif there is no match - Throws:
NullPointerException- ifactionis null
-
orElse
Returns a pattern that matches like this pattern, but invokes a given consumer on the target if there is no match.The consumer is only invoked if there no match at all for the given target, i.e., when
match(target)fails. It is not invoked whenmatchAgainfails.- Parameters:
consumer- the consumer to invoke- Returns:
- a pattern that matches like
this, but invokesconsumerif there is no match - Throws:
NullPointerException- ifconsumeris null
-
ifThenElse
static <A> Pattern<A> ifThenElse(Predicate<? super A> condition, Pattern<? super A> thenBody, Pattern<? super A> elseBody) Returns a pattern that delegates to either of two subpatterns, depending on a given condition.- Type Parameters:
A- the target type of objects to match against- Parameters:
condition- the condition that selects a subpattern for each targetthenBody- the subpattern to match ifconditionreturnstrueelseBody- the subpattern to match ifconditionreturnsfalse- Returns:
- a pattern that matches each target like
thenBodyifconditionreturnstrue, or likeelseBodyifconditionreturnsfalse - Throws:
NullPointerException- ifcondition,thenBodyorelseBodyis null
-
unlessNull
Returns a pattern that matches like this pattern against non-null targets. The resulting pattern simply fails for null targets.- Returns:
- a pattern that matches like this pattern if
targetis non-null, or otherwise fails
-
matches
-