Class Action<R,M,O>

java.lang.Object
eu.bandm.tools.ramus.runtime2.Action<R,M,O>
Type Parameters:
R - the type of parsing results attached to leaves
M - the type of diagnostic messages attached to stems
O - the type of obstructions to continuation
Direct Known Subclasses:
Blockage, Choice, Diagnosis, Success, Thunk

public abstract class Action<R,M,O> extends Object
Base class of the parsing search-tree monad.

Search trees must not share nodes internally or externally. Some operations on search trees are transitions that may induce sharing between the input and output tree. If such a method is invoked and the output is used, then the input may not be used simultaneously anymore.

  • Constructor Details

    • Action

      public Action()
  • Method Details

    • getLeafCount

      protected abstract int getLeafCount()
    • bind

      public abstract <S> Action<S,M,O> bind(Action.Continuation<? super R,S,M,O> cont)
      Applies a monadic continuation to all leaves of this search tree (transition operation).
      Type Parameters:
      S - the output parsing result type of the continuation
      Parameters:
      cont - the continuation to apply
      Returns:
      an updated search tree
    • map

      public final <S> Action<S,M,O> map(Function<? super R,? extends S> fun)
      Applies a function to all leaves of this search tree (transition operation).
      Type Parameters:
      S - the output parsing result type of the continuation
      Parameters:
      fun - the function to apply
      Returns:
      an updated search tree
    • isEmpty

      public abstract boolean isEmpty()
      Checks whether this search tree is empty.
      Returns:
      true iff this tree has no leaves
    • prune

      public abstract <Q> Action<? extends R,M,Q> prune()
      Simplifies this search tree by removing redundant choice nodes and all obstructions (transition operation).
      Type Parameters:
      Q - the new type of obstructions
      Returns:
      a simplified search tree without nested or unary choice nodes
    • pruneTo

      protected <Q> boolean pruneTo(List<? super Action<? extends R,M,Q>> alts)
      Adds a pruned variant of this search subtree to a list of choice alternatives.
      Type Parameters:
      Q - the new type of obstructions
      Parameters:
      alts - the list to add to
      Returns:
      true iff this subtree has been simplified by recursive pruning
    • fail

      public static <R, M, O> Action<R,M,O> fail()
      Creates a search tree with no alternatives.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Returns:
      a search tree with no alternatives
    • fail

      public static <R, M, O> Action<R,M,O> fail(O obstruction)
      Creates a search tree with no alternatives but an obstruction.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      obstruction - the obstruction to fail with
      Returns:
      a search tree with no alternatives
    • succeed

      public static <R, M, O> Action<R,M,O> succeed(R result)
      Creates a search tree with a single leaf.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      result - the parsing result
      Returns:
      a tree with a single leaf holding the given result
    • succeed

      public static <R, M, O> Action<R,M,O> succeed(R result, Success<?,M,O> recyclable)
      Creates a search tree with a single leaf, possibly using a recyclable leaf node.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      result - the parsing result
      recyclable - a recyclable leaf node, or null if not available
      Returns:
      a leaf node with a newly computed parsing result; either recyclable if available or new.
    • diagnosis

      @SafeVarargs public static <R, M, O> Action<R,M,O> diagnosis(Action<R,M,O> body, M... msgs)
      Creates a search tree with diagnostic message attached to the root.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      body - the search tree to attach messages to
      msgs - an array of messages to attach
      Returns:
      a search tree with the messages attached to the body
      Throws:
      IllegalArgumentException - if body == null or msgs == null
    • diagnosis

      public static <R, M, O> Action<R,M,O> diagnosis(Action<R,M,O> body, List<? extends M> msgs)
      Creates a search tree with diagnostic message attached to the root.
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      body - the search tree to attach messages to
      msgs - a list of messages to attach
      Returns:
      a search tree with the messages attached to the body
      Throws:
      IllegalArgumentException - if body == null or msgs == null
    • choice

      @SafeVarargs public static <R, M, O> Action<R,M,O> choice(Action<R,M,O>... alts)
      Creates a search tree from a choice of alternative subtrees
      Type Parameters:
      R - the type of parsing results attached to leaves
      M - the type of diagnostic messages attached to stems
      O - the type of obstructions to continuation
      Parameters:
      alts - an array of alternative subtrees
      Returns:
      a search tree with a choice root and the given alternatives
      Throws:
      IllegalArgumentException - if alts == null
    • optional

      public static <R, M, O> Action<R,M,O> optional(Optional<? extends R> opt)
    • optional

      public static <R, S, M, O> Action<R,M,O> optional(Optional<S> opt, Function<? super S,? extends R> convert)
    • host

      public abstract void host(Action.Visitor<? super R,M,O> v)
      Performs a double dispatch to the appropriate method of a visitor.
      Parameters:
      v - the visitor
    • forEachResult

      public static <R, M, O> Action.Visitor<R,M,O> forEachResult(BiConsumer<R,ReverseList<M>> op)
    • forEachObstruction

      public static <R, M, O> Action.Visitor<R,M,O> forEachObstruction(BiConsumer<O,ReverseList<M>> op)