Class BaseActions<V>
- Type Parameters:
V
- the type of the parser values
- All Implemented Interfaces:
ContextAware<V>
- Direct Known Subclasses:
BaseParser
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the next input character about to be matched.int
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.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
Returns true if the current rule is running somewhere underneath a Test/TestNot rule.match()
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
matchEnd()
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 newIndexRange
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.peek()
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
Replaces the element the given number of elements below the current top of the value stack.boolean
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.position()
Returns the current position in the underlyingInputBuffer
as aPosition
instance.boolean
Inserts the given value a given number of elements below the current top of the value stack.boolean
Pushes the given value onto the value stack.boolean
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.
-
Constructor Details
-
BaseActions
public BaseActions()
-
-
Method Details
-
getContext
The current context for use with action methods. Updated immediately before action calls.- Returns:
- the current context
-
setContext
ContextAware interface implementation.- Specified by:
setContext
in interfaceContextAware<V>
- Parameters:
context
- the context
-
currentIndex
public int currentIndex()Returns the current index in the input buffer.- Returns:
- the current index
-
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
Creates a newIndexRange
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
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
Returns the current position in the underlying
InputBuffer
as aPosition
instance.- Returns:
- the current position in the underlying inputbuffer
-
push
Pushes the given value onto the value stack. Equivalent to push(0, value).- Parameters:
value
- the value to push- Returns:
- true
-
push
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
Pushes all given elements onto the value stack (in the order as given).- Parameters:
firstValue
- the first valuemoreValues
- the other values- Returns:
- true
-
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
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
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
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
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
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
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
-