Package org.parboiled

Class BaseActions<V>

java.lang.Object
org.parboiled.BaseActions<V>
Type Parameters:
V - the type of the parser values
All Implemented Interfaces:
ContextAware<V>
Direct Known Subclasses:
BaseParser

public abstract class BaseActions<V> extends Object implements ContextAware<V>
Convenience context aware base class defining a number of useful helper methods.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the next input character about to be matched.
    int
    Returns the current index in the input buffer.
    boolean
    Removes the value at the top of the value stack.
    boolean
    drop(int down)
    Removes the value the given number of elements below the top of the value stack.
    boolean
    dup()
    Duplicates the top value of the value stack.
    The current context for use with action methods.
    boolean
    Determines whether the current rule or a sub rule has recorded a parse error.
    boolean
    Returns true if the current rule is running somewhere underneath a Test/TestNot rule.
    Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
    char
    Returns the first character of the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
    int
    Returns the end location of the rule immediately preceding the action expression that is currently being evaluated.
    int
    Returns the number of characters matched by the rule immediately preceding the action expression that is currently being evaluated.
    matchOrDefault(String defaultString)
    Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
    Creates a new IndexRange instance covering the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
    int
    Returns the start index of the rule immediately preceding the action expression that is currently being evaluated.
    boolean
    Returns true if the current context is for or below a rule marked @SuppressNode or below one marked @SuppressSubnodes.
    Returns the value at the top of the value stack without removing it.
    peek(int down)
    Returns the value the given number of elements below the top of the value stack without removing it.
    boolean
    poke(int down, V value)
    Replaces the element the given number of elements below the current top of the value stack.
    boolean
    poke(V value)
    Replaces the current top value of the value stack with the given value.
    pop()
    Removes the value at the top of the value stack and returns it.
    pop(int down)
    Removes the value the given number of elements below the top of the value stack.
    Returns the current position in the underlying InputBuffer as a Position instance.
    boolean
    push(int down, V value)
    Inserts the given value a given number of elements below the current top of the value stack.
    boolean
    push(V value)
    Pushes the given value onto the value stack.
    boolean
    pushAll(V firstValue, V... moreValues)
    Pushes all given elements onto the value stack (in the order as given).
    void
    setContext(Context<V> context)
    ContextAware interface implementation.
    boolean
    Swaps the top two elements of the value stack.
    boolean
    Reverses the order of the top 3 value stack elements.
    boolean
    Reverses the order of the top 4 value stack elements.
    boolean
    Reverses the order of the top 5 value stack elements.
    boolean
    Reverses the order of the top 6 value stack elements.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BaseActions

      public BaseActions()
  • Method Details

    • getContext

      public Context<V> getContext()
      The current context for use with action methods. Updated immediately before action calls.
      Returns:
      the current context
    • setContext

      public void setContext(Context<V> context)
      ContextAware interface implementation.
      Specified by:
      setContext in interface ContextAware<V>
      Parameters:
      context - the context
    • currentIndex

      public int currentIndex()
      Returns the current index in the input buffer.
      Returns:
      the current index
    • match

      public String match()

      Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      Returns:
      the input text matched by the immediately preceding subrule
    • matchRange

      public IndexRange matchRange()
      Creates a new IndexRange instance covering the input text matched by the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.
      Returns:
      a new IndexRange instance
    • matchOrDefault

      public String matchOrDefault(String defaultString)

      Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated. If the matched input text is empty the given default string is returned. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      Parameters:
      defaultString - the default string to return if the matched input text is empty
      Returns:
      the input text matched by the immediately preceding subrule or the default string
    • matchedChar

      public char matchedChar()

      Returns the first character of the input text matched by the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      If the immediately preceding rule did not match anything this method throws a GrammarException. If you need to able to handle that case use the getMatch() method.

      Returns:
      the first input char of the input text matched by the immediately preceding subrule or null, if the previous rule matched nothing
    • matchStart

      public int matchStart()

      Returns the start index of the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      Returns:
      the start index of the context immediately preceding current action
    • matchEnd

      public int matchEnd()

      Returns the end location of the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      Returns:
      the end index of the context immediately preceding current action, i.e. the index of the character immediately following the last matched character
    • matchLength

      public int matchLength()

      Returns the number of characters matched by the rule immediately preceding the action expression that is currently being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first position in this Sequence.

      Returns:
      the number of characters matched
    • position

      public Position position()

      Returns the current position in the underlying InputBuffer as a Position instance.

      Returns:
      the current position in the underlying inputbuffer
    • push

      public boolean push(V value)
      Pushes the given value onto the value stack. Equivalent to push(0, value).
      Parameters:
      value - the value to push
      Returns:
      true
    • push

      public boolean push(int down, V value)
      Inserts the given value a given number of elements below the current top of the value stack.
      Parameters:
      down - the number of elements to skip before inserting the value (0 being equivalent to push(value))
      value - the value
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack does not contain enough elements to perform this operation
    • pushAll

      public boolean pushAll(V firstValue, V... moreValues)
      Pushes all given elements onto the value stack (in the order as given).
      Parameters:
      firstValue - the first value
      moreValues - the other values
      Returns:
      true
    • pop

      public V pop()
      Removes the value at the top of the value stack and returns it.
      Returns:
      the current top value
      Throws:
      IllegalArgumentException - if the stack is empty
    • pop

      public V pop(int down)
      Removes the value the given number of elements below the top of the value stack.
      Parameters:
      down - the number of elements to skip before removing the value (0 being equivalent to pop())
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the stack does not contain enough elements to perform this operation
    • drop

      public boolean drop()
      Removes the value at the top of the value stack.
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack is empty
    • drop

      public boolean drop(int down)
      Removes the value the given number of elements below the top of the value stack.
      Parameters:
      down - the number of elements to skip before removing the value (0 being equivalent to drop())
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack does not contain enough elements to perform this operation
    • peek

      public V peek()
      Returns the value at the top of the value stack without removing it.
      Returns:
      the current top value
      Throws:
      IllegalArgumentException - if the stack is empty
    • peek

      public V peek(int down)
      Returns the value the given number of elements below the top of the value stack without removing it.
      Parameters:
      down - the number of elements to skip (0 being equivalent to peek())
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the stack does not contain enough elements to perform this operation
    • poke

      public boolean poke(V value)
      Replaces the current top value of the value stack with the given value. Equivalent to poke(0, value).
      Parameters:
      value - the value
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack is empty
    • poke

      public boolean poke(int down, V value)
      Replaces the element the given number of elements below the current top of the value stack.
      Parameters:
      down - the number of elements to skip before replacing the value (0 being equivalent to poke(value))
      value - the value to replace with
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack does not contain enough elements to perform this operation
    • dup

      public boolean dup()
      Duplicates the top value of the value stack. Equivalent to push(peek()).
      Returns:
      true
      Throws:
      IllegalArgumentException - if the stack is empty
    • swap

      public boolean swap()
      Swaps the top two elements of the value stack.
      Returns:
      true
      Throws:
      GrammarException - if the stack does not contain at least two elements
    • swap3

      public boolean swap3()
      Reverses the order of the top 3 value stack elements.
      Returns:
      true
      Throws:
      GrammarException - if the stack does not contain at least 3 elements
    • swap4

      public boolean swap4()
      Reverses the order of the top 4 value stack elements.
      Returns:
      true
      Throws:
      GrammarException - if the stack does not contain at least 4 elements
    • swap5

      public boolean swap5()
      Reverses the order of the top 5 value stack elements.
      Returns:
      true
      Throws:
      GrammarException - if the stack does not contain at least 5 elements
    • swap6

      public boolean swap6()
      Reverses the order of the top 6 value stack elements.
      Returns:
      true
      Throws:
      GrammarException - if the stack does not contain at least 6 elements
    • currentChar

      public Character currentChar()
      Returns the next input character about to be matched.
      Returns:
      the next input character about to be matched
    • inPredicate

      public boolean inPredicate()
      Returns true if the current rule is running somewhere underneath a Test/TestNot rule. Useful for example for making sure actions are not run inside of a predicate evaluation: return Sequence( ..., inPredicate() || actions.doSomething() );
      Returns:
      true if in a predicate
    • nodeSuppressed

      public boolean nodeSuppressed()
      Returns true if the current context is for or below a rule marked @SuppressNode or below one marked @SuppressSubnodes.
      Returns:
      true or false
    • hasError

      public boolean hasError()
      Determines whether the current rule or a sub rule has recorded a parse error. Useful for example for making sure actions are not run on erroneous input: return Sequence( ..., !hasError() && actions.doSomething() );
      Returns:
      true if either the current rule or a sub rule has recorded a parse error