Class Action<R,M,O>

java.lang.Object
eu.bandm.tools.ramus.ogma.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:
Action.Success

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.

  • Method Details

    • 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
      Throws:
      NullPointerException - if cont is null
    • 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
      Throws:
      NullPointerException - if fun is null
    • isEmpty

      public abstract boolean isEmpty()
      Checks whether this search tree is empty.
      Returns:
      true if this tree has no leaves, false if some success leaves are found
    • prune

      public abstract <Q> Action<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
    • 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, @Opt Action.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:
      NullPointerException - if body or msgs is 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:
      NullPointerException - if body or msgs is 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:
      NullPointerException - if alts is null
    • choice

      public static <R, M, O> Action<R,M,O> choice(List<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 - a list of alternative subtrees
      Returns:
      a search tree with a choice root and the given alternatives
      Throws:
      NullPointerException - if alts is null
    • first

      public static <R, S, M, O> Action.BiContinuation<R,S,R,M,O> first()
      Returns a binary continuation that purely selects the first argument.
      Type Parameters:
      R - the first input type of parsing results
      S - the second input type of parsing results
      M - the type of diagnostic messages
      O - the type of obstructions to continuation
      Returns:
      a binary continuation that purely selects the first argument
    • second

      public static <R, S, M, O> Action.BiContinuation<R,S,S,M,O> second()
      Returns a binary continuation that purely selects the second argument.
      Type Parameters:
      R - the first input type of parsing results
      S - the second input type of parsing results
      M - the type of diagnostic messages
      O - the type of obstructions to continuation
      Returns:
      a binary continuation that purely selects the second argument
    • 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
    • visitEachResult

      public static <R, M, O> Action.Visitor<R,M,O> visitEachResult(BiConsumer<R,List<M>> op)
      Creates a visitor that recognizes leaves.
      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:
      op - a consumer for pairs of a result at a leaf, and the messages accumulated along the path
      Returns:
      a visitor that invokes op for all leaves
      Throws:
      NullPointerException - if op is null
    • forEachResult

      public void forEachResult(BiConsumer<R,List<M>> op)
      Visits all leaves.
      Parameters:
      op - a consumer for pairs of a result at a leaf, and the messages accumulated along the path
      Throws:
      NullPointerException - if op is null
      See Also:
    • getResults

      public Collection<Map.Entry<R,List<M>>> getResults()
      Returns a collection of all leaves, each together with the messages accumulated along the respective path.
      Returns:
      a collection of map entries with the leaf result as the key and the messages accumulated on the path as the value
    • visitEachObstruction

      public static <R, M, O> Action.Visitor<R,M,O> visitEachObstruction(BiConsumer<O,List<M>> op)
      Creates a visitor that recognizes obstructions.
      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:
      op - a consumer for pairs of an obstruction, and the messages accumulates along the path
      Returns:
      a visitor that invokes op for all obstructions
      Throws:
      NullPointerException - if op is null
    • forEachObstruction

      public void forEachObstruction(BiConsumer<O,List<M>> op)
      Visits all obstructions.
      Parameters:
      op - a consumer for pairs of an obstruction, and the messages accumulates along the path
      Throws:
      NullPointerException - if op is null
      See Also: