A
- the target type (of objects to match against)public abstract class Pattern<A> extends Object implements Cloneable, Serializable
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 clone()
method.
Subclasses should override the isDeterministic()
, binds(Variable)
and preserves(Variable,boolean)
methods if appropriate to improve
runtime information on pattern behavior.
Modifier | Constructor and Description |
---|---|
protected |
Pattern()
Creates a new pattern.
|
Modifier and Type | Method and Description |
---|---|
static <A> Pattern<A> |
all(Iterable<? extends Pattern<? super A>> p) |
static <A> Pattern<A> |
all(Pattern<? super A>... p)
Combines many patterns conjunctively.
|
static <A> Pattern<A> |
all(Pattern<? super A> p1,
Pattern<? super A> p2)
Combines many patterns conjunctively.
|
static <A> Pattern<A> |
all(Pattern<? super A> p1,
Pattern<? super A> p2,
Pattern<? super A> p3)
Combines many patterns conjunctively.
|
static <A> Pattern<A> |
all(Pattern<? super A> p1,
Pattern<? super A> p2,
Pattern<? super A> p3,
Pattern<? super A> p4)
Combines many patterns conjunctively.
|
<B extends A> |
and(Pattern<? super B> p)
Combines this patterns conjunctively with another pattern.
|
Pattern<A> |
andThen(Consumer<? super A> c) |
Pattern<A> |
andThen(Runnable r) |
static <A> Pattern<A> |
any()
Returns a pattern that matches anything.
|
static <A> Pattern<A> |
any(Pattern<? super A>... p)
Deprecated.
Name conflicts with
any() , use some(Pattern...)
instead. |
static <A> Pattern<A> |
aside(Runnable r) |
boolean |
binds(Variable<?> var)
Checks whether a variable is bound by this pattern.
|
static <A> Pattern<A> |
both(Pattern<? super A> left,
Pattern<? super A> right)
Combines two patterns conjunctively.
|
void |
clear()
Disposes of all internal state related to results and
nondeterminism.
|
void |
clear(boolean recursively)
Disposes of all internal state related to results and
nondeterminism.
|
Pattern<A> |
clone()
Makes a matching-state copy if this pattern.
|
static <A extends Comparable<? super A>> |
compareTo(A other,
Pattern<? super Integer> body)
Deprecated.
Use the method
PrimitivePatterns.compareTo(Comparable, Pattern) instead. |
Pattern<A> |
compile() |
protected void |
compileClear(eu.bandm.tools.paisley.CompilationContext context) |
protected void |
compileClear(eu.bandm.tools.paisley.CompilationContext context,
boolean recursively) |
protected void |
compileCut(eu.bandm.tools.paisley.CompilationContext context) |
protected void |
compileCut(eu.bandm.tools.paisley.CompilationContext context,
boolean recursively) |
protected void |
compileIsDeterministic(eu.bandm.tools.paisley.CompilationContext context) |
protected void |
compileMatch(eu.bandm.tools.paisley.CompilationContext context) |
protected void |
compileMatchAgain(eu.bandm.tools.paisley.CompilationContext context) |
protected eu.bandm.tools.lljava.live.VariableContext.Variable |
compileSubPattern(eu.bandm.tools.paisley.CompilationContext context) |
protected eu.bandm.tools.lljava.live.VariableContext.Variable |
compileThis(eu.bandm.tools.paisley.CompilationContext context) |
<B> Motif<B,A> |
compileUpTo(Variable<B> hole) |
void |
cut()
Disposes of all internal state related to nondeterminism.
|
void |
cut(boolean recursively)
Disposes of all internal state related to nondeterminism.
|
void |
DEBUG(String msg) |
static <A> Pattern<A> |
either(Pattern<? super A> p,
Pattern<? super A> q)
Combines two patterns disjunctively.
|
<B extends A> |
enPassant() |
static <A> Pattern<A> |
eq(A a)
Deprecated.
Use the method
PrimitivePatterns.eq(A) instead. |
static <A> Pattern<A> |
eqNull()
Deprecated.
Use the method
PrimitivePatterns.eqNull() instead. |
static <A> Pattern<A> |
equal(A a)
Deprecated.
Use the method
PrimitivePatterns.equal(A) instead. |
Pattern<A> |
filter(Predicate<? super A> pred)
Deprecated.
Use the method
FunctionPatterns.filter(java.util.function.Predicate, Pattern)
instead. |
static <A> Pattern<A> |
flatten(Pattern<A> p) |
Pattern<Object> |
forInstancesOf(Class<? extends A> cls)
Deprecated.
Use the method
ReflectionPatterns.forInstancesOf(Class, Pattern) instead. |
boolean |
isDeterministic()
Checks whether this pattern is deterministic.
|
Pattern<A> |
limit(int n)
Returns a pattern that stops after a given number of matches.
|
static void |
main(String[] args) |
abstract boolean |
match(A target)
Attempts to match this pattern against a given object.
|
boolean |
matchAgain()
Attempts to re-match this pattern against a previously matched
object.
|
boolean |
matchOnce(A target)
Attempts to match this pattern at most once against a given
object.
|
boolean |
matchVar(Extractor<? extends A> var) |
<B extends A> |
narrow()
Returns an equivalent pattern with narrower type parameter.
|
static <A> Pattern<A> |
neq(A a)
Deprecated.
Use the method
PrimitivePatterns.neq(A) instead. |
static <A> Pattern<A> |
neqNull()
Deprecated.
Use the method
PrimitivePatterns.neqNull() instead. |
static <A> Pattern<A> |
newAll(Pattern<? super A>... ps) |
Pattern<A> |
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.
|
<B extends A> |
or(Pattern<? super B> p)
Combines this patterns disjunctively with another pattern.
|
Pattern<A> |
orElse(Consumer<? super A> c) |
Pattern<A> |
orElse(Runnable r) |
boolean |
preserves(Variable<?> var)
Checks whether a variable is preserved by this pattern,
unconditionally.
|
boolean |
preserves(Variable<?> var,
boolean success)
Checks whether a variable is preserved by this pattern,
conditionally on success or failure.
|
static <A> Pattern<A> |
repeat() |
static <A> Pattern<A> |
some(Pattern<? super A>... p)
Combines many patterns disjunctively.
|
static <A> Pattern<A> |
some(Pattern<? super A> p1,
Pattern<? super A> p2)
Combines many patterns disjunctively.
|
static <A> Pattern<A> |
some(Pattern<? super A> p1,
Pattern<? super A> p2,
Pattern<? super A> p3)
Combines many patterns disjunctively.
|
static <A> Pattern<A> |
some(Pattern<? super A> p1,
Pattern<? super A> p2,
Pattern<? super A> p3,
Pattern<? super A> p4)
Combines many patterns disjunctively.
|
Pattern<A> |
someMatch() |
protected void |
startCompile(eu.bandm.tools.paisley.CompilationContext context) |
static <A> Pattern<A> |
test(Predicate<? super A> pred)
Deprecated.
Use the method
FunctionPatterns.test(java.util.function.Predicate) instead. |
<B> Pattern<B> |
transform(Function<? super B,? extends A> fun)
Deprecated.
Use the method
FunctionPatterns.transform(java.util.function.Function, Pattern)
instead. |
Pattern<A> |
uniquely()
Returns a pattern that matches if and only if this pattern has a unique
match.
|
static <A> Variable<A> |
variable()
Creates a new anonymous pattern variable.
|
static <A> Variable<A> |
variable(String name)
Creates a new named pattern variable.
|
public abstract boolean match(A target)
target
- the object to match againsttrue
if the matching is successful, false
if it fails. Which Variable
s are bound by a
successful or failed match depends.binds(Variable)
,
preserves(Variable, boolean)
public boolean matchAgain()
Patterns classes that do not override this method are
deterministic. Pattern classes that overwrite this method to
provide additional matches should also override isDeterministic()
.
true
if the matching is successful, false
if it fails. Which Variable
s are bound by a
successful match depends. The default implementation
always returns false
.binds(Variable)
,
preserves(Variable, boolean)
public final void cut()
matchAgain()
should
fail.
Invokes cut(true)
.public final void clear()
matchAgain()
should fail. Bound variables are reset to initial
values.
Invokes clear(true)
.public void cut(boolean recursively)
The default implementation does nothing. Subclasses that override
this method should also override clear(boolean)
and
ensure that it implies the effect of cut(false)
.
recursively
- true
if all subpatterns should be
deactivated, false
otherwise.public void clear(boolean recursively)
The default implementation does nothing.
recursively
- true
if all subpatterns should be
deactivated, false
otherwise.public final boolean matchOnce(A target)
Closes the pattern immediately by invoking cut()
.
public boolean binds(Variable<?> var)
The default implementation returns false
.
var
- a pattern variabletrue
if the variable is guaranteed to have a
meaningful value when this pattern is open, false
otherwise.public boolean preserves(Variable<?> var, boolean success)
The default implementation returns false
.
var
- a pattern variablesuccess
- specifies preservation on success or failuretrue
if the variable value is guaranteed to be
unaffected by a matching attempt with this pattern that returns a
value equal to success
.public boolean preserves(Variable<?> var)
The default implementation returns false
.
var
- a pattern variabletrue
if the variable value is guaranteed to be
unaffected by any matching attempt with this pattern.public boolean isDeterministic()
The default implementation returns true
.
true
if this pattern is guaranteed to match any
object at most once, false
otherwise.matchAgain()
public <B extends A> Pattern<B> narrow()
Note that, by contrast, casting Variable<A>
to Variable<B>
, where B
is a subtype of A
, is
not type safe.
public Pattern<A> clone()
Subclasses must override this method if necessary to honour the following rules for all transitively reachable patterns:
Variable
must
be shared. This implies that Variable.clone()
must
return this
.match(A)
, matchAgain()
, cut()
or clear()
) 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.this
.public static <A> Variable<A> variable()
public final Pattern<A> uniquely()
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.
public final Pattern<A> limit(int n)
n
- the maximal number of matches.IllegalArgumentException
- if n < 0
.public static final <A> Pattern<A> any()
public static final <A> Pattern<A> none()
A pattern returned by this method is vacuously deterministic and binds all variables.
public static final <A> Pattern<A> repeat()
@Deprecated public static <A> Pattern<A> equal(A a)
PrimitivePatterns.equal(A)
instead.Object.equals(java.lang.Object)
if an object
is given, or by ==
if null
is given instead.
A pattern returned by this method is deterministic and binds no variables.
a
- an object to compare with or null
.@Deprecated public static <A> Pattern<A> eq(A a)
PrimitivePatterns.eq(A)
instead.==
.
A pattern returned by this method is deterministic and binds no variables.
a
- an object to compare with or null
.@Deprecated public static <A> Pattern<A> neq(A a)
PrimitivePatterns.neq(A)
instead.!=
.
A pattern returned by this method is deterministic and binds no variables.
a
- an object to compare with or null
.@Deprecated public static <A extends Comparable<? super A>> Pattern<A> compareTo(A other, Pattern<? super Integer> body)
PrimitivePatterns.compareTo(Comparable, Pattern)
instead.Comparable.compareTo
to a given object matches a given
int
patterns.
A pattern returned by this method inherits determinism and variable bindings from the given pattern.
other
- an object to compare with or null
.body
- a pattern to match the comparison result.@Deprecated public static <A> Pattern<A> eqNull()
PrimitivePatterns.eqNull()
instead.null
reference.
A pattern returned by this method is deterministic and binds no variables.
@Deprecated public static <A> Pattern<A> neqNull()
PrimitivePatterns.neqNull()
instead.null
reference.
A pattern returned by this method is deterministic and binds no variables.
@SafeVarargs public static <A> Pattern<A> all(Pattern<? super A>... p)
both(Pattern, Pattern)
and any()
.public static <A> Pattern<A> all(Pattern<? super A> p1, Pattern<? super A> p2)
both(Pattern, Pattern)
and any()
.public static <A> Pattern<A> all(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3)
both(Pattern, Pattern)
and any()
.public static <A> Pattern<A> all(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3, Pattern<? super A> p4)
both(Pattern, Pattern)
and any()
.@Deprecated public static <A> Pattern<A> any(Pattern<? super A>... p)
any()
, use some(Pattern...)
instead.either(Pattern, Pattern)
and none()
.public static <A> Pattern<A> some(Pattern<? super A>... p)
either(Pattern, Pattern)
and none()
.public static <A> Pattern<A> some(Pattern<? super A> p1, Pattern<? super A> p2)
either(Pattern, Pattern)
and none()
.public static <A> Pattern<A> some(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3)
either(Pattern, Pattern)
and none()
.public static <A> Pattern<A> some(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3, Pattern<? super A> p4)
either(Pattern, Pattern)
and none()
.public <B extends A> Pattern<B> and(Pattern<? super B> p)
both(this, p)
.public <B extends A> Pattern<B> or(Pattern<? super B> p)
either(this, p)
.public static final <A> Pattern<A> both(Pattern<? super A> left, Pattern<? super A> right)
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.
left
- an argument patternright
- another argument patternpublic static final <A> Pattern<A> either(Pattern<? super A> p, Pattern<? super A> q)
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.
p
- an argument patternq
- another argument pattern@Deprecated public <B> Pattern<B> transform(Function<? super B,? extends A> fun)
FunctionPatterns.transform(java.util.function.Function, Pattern)
instead.@Deprecated public Pattern<A> filter(Predicate<? super A> pred)
FunctionPatterns.filter(java.util.function.Predicate, Pattern)
instead.@Deprecated public static <A> Pattern<A> test(Predicate<? super A> pred)
FunctionPatterns.test(java.util.function.Predicate)
instead.@Deprecated public Pattern<Object> forInstancesOf(Class<? extends A> cls)
ReflectionPatterns.forInstancesOf(Class, Pattern)
instead.cls
- the class to be checked against.cls
,
determined by invoking Class.isInstance(java.lang.Object)
.public Pattern<A> noMatch()
A pattern returned by this method is deterministic and binds no variables.
protected void startCompile(eu.bandm.tools.paisley.CompilationContext context)
protected eu.bandm.tools.lljava.live.VariableContext.Variable compileThis(eu.bandm.tools.paisley.CompilationContext context)
protected void compileMatch(eu.bandm.tools.paisley.CompilationContext context)
protected void compileMatchAgain(eu.bandm.tools.paisley.CompilationContext context)
protected void compileCut(eu.bandm.tools.paisley.CompilationContext context)
protected void compileCut(eu.bandm.tools.paisley.CompilationContext context, boolean recursively)
protected void compileClear(eu.bandm.tools.paisley.CompilationContext context)
protected void compileClear(eu.bandm.tools.paisley.CompilationContext context, boolean recursively)
protected void compileIsDeterministic(eu.bandm.tools.paisley.CompilationContext context)
protected eu.bandm.tools.lljava.live.VariableContext.Variable compileSubPattern(eu.bandm.tools.paisley.CompilationContext context)
public static void main(String[] args)
public void DEBUG(String msg)
see also the complete user documentation .