org.parboiled
Class BaseActions<V>

java.lang.Object
  extended by 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 java.lang.Object
implements ContextAware<V>

Convenience context aware base class defining a number of useful helper methods.


Constructor Summary
BaseActions()
           
 
Method Summary
 java.lang.Character currentChar()
          Returns the next input character about to be matched.
 int currentIndex()
          Returns the current index in the input buffer.
 boolean drop()
          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.
 Context<V> getContext()
          The current context for use with action methods.
 boolean hasError()
          Determines whether the current rule or a sub rule has recorded a parse error.
 boolean inPredicate()
          Returns true if the current rule is running somewhere underneath a Test/TestNot rule.
 java.lang.String match()
          Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
 char matchedChar()
          Returns the first character of the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
 int matchEnd()
          Returns the end location of the rule immediately preceding the action expression that is currently being evaluated.
 int matchLength()
          Returns the number of characters matched by the rule immediately preceding the action expression that is currently being evaluated.
 java.lang.String matchOrDefault(java.lang.String defaultString)
          Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated.
 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.
 int matchStart()
          Returns the start index of the rule immediately preceding the action expression that is currently being evaluated.
 boolean nodeSuppressed()
          Returns true if the current context is for or below a rule marked @SuppressNode or below one marked @SuppressSubnodes.
 V peek()
          Returns the value at the top of the value stack without removing it.
 V 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.
 V pop()
          Removes the value at the top of the value stack and returns it.
 V pop(int down)
          Removes the value the given number of elements below the top of the value stack.
 Position position()
          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 swap()
          Swaps the top two elements of the value stack.
 boolean swap3()
          Reverses the order of the top 3 value stack elements.
 boolean swap4()
          Reverses the order of the top 4 value stack elements.
 boolean swap5()
          Reverses the order of the top 5 value stack elements.
 boolean swap6()
          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 Detail

BaseActions

public BaseActions()
Method Detail

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 java.lang.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 java.lang.String matchOrDefault(java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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 java.lang.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