Class Variable<A>

java.lang.Object
eu.bandm.tools.paisley.Pattern<A>
eu.bandm.tools.paisley.Variable<A>
All Implemented Interfaces:
Extractor<A>, Serializable, Cloneable

public final class Variable<A> extends Pattern<A> implements Extractor<A>
Class of pattern variables.

A pattern variable is deterministic, matches any object and binds itself.

See Also:
  • Constructor Details

    • Variable

      public Variable()
      Creates a new anonymous variable.
    • Variable

      public Variable(String name)
      Creates a new named variable. Variable names are for user-level annotation purposes only, they have no effect on matching behaviour.
      Parameters:
      name - the variable name.
  • Method Details

    • getName

      public String getName()
      Returns the name of this variable.
      Returns:
      the name of this variable, or null if this variable is anonymous.
    • getValue

      public A getValue()
      Returns the current value bound to this variable. The value is initially null. It is set to a meaningful value by a successful match with a pattern that guarantees to bind the variable. It may be set to an unspecified value by a match with a pattern that does not guarantee to preserve the variable.
      Specified by:
      getValue in interface Extractor<A>
      Returns:
      the current value bound to this variable.
      See Also:
    • clear

      public void clear(boolean recursively)
      Disposes of all internal state related to results and nondeterminism.

      The default implementation does nothing. This implementation resets the variable value to null.

      Overrides:
      clear in class Pattern<A>
      Parameters:
      recursively - true if all subpatterns should be deactivated, false otherwise.
    • match

      public boolean match(A target)
      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. This implementation sets the variable value to target.
      Specified by:
      match in class Pattern<A>
      Parameters:
      target - the object to match against
      Returns:
      true
      See Also:
    • binds

      public boolean binds(Variable<?> variable)
      Description copied from class: Pattern
      Checks whether a variable is bound by this pattern.

      The default implementation returns false.

      Overrides:
      binds in class Pattern<A>
      Parameters:
      variable - a pattern variable
      Returns:
      true if the variable is guaranteed to have a meaningful value when this pattern is open, false otherwise.
    • preserves

      public boolean preserves(Variable<?> variable, boolean success)
      Description copied from class: Pattern
      Checks whether a variable is preserved by this pattern, conditionally on success or failure.

      The default implementation returns false.

      Overrides:
      preserves in class Pattern<A>
      Parameters:
      variable - a pattern variable
      success - specifies preservation on success or failure
      Returns:
      true if the variable value is guaranteed to be unaffected by a matching attempt with this pattern that returns a value equal to success.
    • preserves

      public boolean preserves(Variable<?> variable)
      Description copied from class: Pattern
      Checks whether a variable is preserved by this pattern, unconditionally.

      The default implementation returns false.

      Overrides:
      preserves in class Pattern<A>
      Parameters:
      variable - a pattern variable
      Returns:
      true if the variable value is guaranteed to be unaffected by any matching attempt with this pattern.
    • toString

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

      public Variable<A> clone()
      Subpatterns that are instances of Variable must be shared. This implies that clone() must return this.
      Overrides:
      clone in class Pattern<A>
      See Also:
    • lambda

      public <B> Motif<A,B> lambda(Pattern<B> body)
    • plus

      public Pattern<A> plus(Pattern<? super A> root)
      Returns the "plus" variant of the Kleene fixpoint of a pattern as a function of this variable.

      Conceptually, x.star(p) is equivalent to the pattern arising from p.clone() by substituting x.star(p) for x. Note that this cannot be used naively for implementation, since the ensuing recursive substitution would not terminate. The actual implementation guarantees to produce only as many new subpatterns as dynamically necessary.

      See Also:
    • star

      public Pattern<A> star(Pattern<? super A> root)
      Returns the "star" variant of the Kleene fixpoint of a pattern as a function of this variable.

      Conceptually, x.star(p) is equivalent to either(x, x.plus(p)).

      See Also:
    • compileMatchAgain

      protected void compileMatchAgain(CompilationContext context)
      Overrides:
      compileMatchAgain in class Pattern<A>
    • compileCut

      protected void compileCut(CompilationContext context)
      Overrides:
      compileCut in class Pattern<A>
    • compileCut

      protected void compileCut(CompilationContext context, boolean recursively)
      Overrides:
      compileCut in class Pattern<A>