public final class DefaultMatcherContext<V> extends Object implements MatcherContext<V>
The Context implementation orchestrating most of the matching process.
The parsing process works as following:
After the rule tree (which is in fact a directed and potentially even
cyclic graph of Matcher
instances) has been created a root
MatcherContext is instantiated for the root rule (Matcher). A subsequent call
to runMatcher()
starts the parsing process.
The MatcherContext delegates to a given MatchHandler
to call
Matcher.match(MatcherContext)
, passing itself to the Matcher which
executes its logic, potentially calling sub matchers. For each sub matcher
the matcher creates/initializes a subcontext with Matcher.getSubContext(MatcherContext)
and then calls runMatcher()
on it.
This basically creates a stack of MatcherContexts, each corresponding to their rule matchers. The MatcherContext instances serve as companion objects to the matchers, providing them with support for building the parse tree nodes, keeping track of input locations and error recovery.
At each point during the parsing process the matchers and action
expressions have access to the current MatcherContext and all "open" parent
MatcherContexts through the getParent()
chain.
For performance reasons subcontext instances are reused instead of being
recreated. If a MatcherContext instance returns null on a getMatcher()
call it has been retired (is invalid) and is waiting to be
reinitialized with a new Matcher by its parent
Constructor and Description |
---|
DefaultMatcherContext(InputBuffer inputBuffer,
ValueStack<V> valueStack,
MatchHandler matchHandler,
Matcher matcher)
Initializes a new root MatcherContext.
|
Modifier and Type | Method and Description |
---|---|
void |
advanceIndex(int delta) |
MatcherContext<V> |
getBasicSubContext() |
char |
getCurrentChar()
Returns the character at the current index..
|
int |
getCurrentCodePoint()
Return the code point at the current index
|
int |
getCurrentIndex()
Returns the current index in the input buffer.
|
char |
getFirstMatchChar()
Returns the first character of the input text matched by the rule
immediately preceding the action expression that is currently being
evaluated.
|
InputBuffer |
getInputBuffer()
Returns the InputBuffer the parser is currently running against
|
int |
getLevel()
Returns the current matcher level, with 0 being the root level, 1 being
one level below the root and so on.
|
String |
getMatch()
Returns the input text matched by the rule immediately preceding the
action expression that is currently being evaluated.
|
int |
getMatchEndIndex()
Returns the end index of the rule immediately preceding the action
expression that is currently being evaluated.
|
Matcher |
getMatcher()
Returns the Matcher of this context or null, if this context is not valid
anymore.
|
int |
getMatchLength()
Returns the number of characters matched by the rule immediately
preceding the action expression that is currently being evaluated.
|
IndexRange |
getMatchRange()
Creates a new
IndexRange instance covering the input text matched
by the rule immediately preceding the action expression that is currently
being evaluated. |
int |
getMatchStartIndex()
Returns the start index of the rule immediately preceding the action
expression that is currently being evaluated.
|
MatcherContext<V> |
getParent() |
Position |
getPosition()
Returns the current position in the underlying
InputBuffer as
a Position instance. |
int |
getStartIndex()
Returns the index into the underlying input buffer where the matcher of
this context started its match.
|
MatcherContext<V> |
getSubContext(Matcher matcher) |
ValueStack<V> |
getValueStack()
Returns the value stack instance used during this parsing run.
|
boolean |
hasError()
Determines if this context or any sub node recorded a parse error.
|
boolean |
inPredicate()
Determines if the current rule is running somewhere underneath a
Test/TestNot rule.
|
boolean |
runMatcher() |
void |
setCurrentIndex(int currentIndex) |
void |
setMatcher(Matcher matcher) |
void |
setStartIndex(int startIndex) |
String |
toString() |
public DefaultMatcherContext(@Nonnull InputBuffer inputBuffer, @Nonnull ValueStack<V> valueStack, @Nonnull MatchHandler matchHandler, @Nonnull Matcher matcher)
inputBuffer
- the InputBuffer for the parsing runvalueStack
- the ValueStack instance to use for the parsing runmatchHandler
- the MatcherHandler to use for the parsing runmatcher
- the root matcherpublic MatcherContext<V> getParent()
getParent
in interface MatcherContext<V>
@Nonnull public InputBuffer getInputBuffer()
Context
getInputBuffer
in interface Context<V>
public int getStartIndex()
Context
getStartIndex
in interface Context<V>
public Matcher getMatcher()
Context
getMatcher
in interface Context<V>
public char getCurrentChar()
Context
getCurrentChar
in interface Context<V>
public int getCurrentCodePoint()
Context
getCurrentCodePoint
in interface Context<V>
InputBuffer.codePointAt(int)
public int getCurrentIndex()
Context
getCurrentIndex
in interface Context<V>
public int getLevel()
Context
public boolean inPredicate()
Context
inPredicate
in interface Context<V>
public boolean hasError()
Context
public String getMatch()
Context
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.
public char getFirstMatchChar()
Context
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.
getFirstMatchChar
in interface Context<V>
public int getMatchStartIndex()
Context
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.
getMatchStartIndex
in interface Context<V>
public int getMatchEndIndex()
Context
Returns the end 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.
getMatchEndIndex
in interface Context<V>
public int getMatchLength()
Context
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.
getMatchLength
in interface Context<V>
public Position getPosition()
Context
Returns the current position in the underlying InputBuffer
as
a Position
instance.
getPosition
in interface Context<V>
public IndexRange getMatchRange()
Context
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.getMatchRange
in interface Context<V>
public ValueStack<V> getValueStack()
Context
getValueStack
in interface Context<V>
public void setMatcher(Matcher matcher)
setMatcher
in interface MatcherContext<V>
public void setStartIndex(int startIndex)
setStartIndex
in interface MatcherContext<V>
public void setCurrentIndex(int currentIndex)
setCurrentIndex
in interface MatcherContext<V>
public void advanceIndex(int delta)
advanceIndex
in interface MatcherContext<V>
public MatcherContext<V> getBasicSubContext()
getBasicSubContext
in interface MatcherContext<V>
public MatcherContext<V> getSubContext(Matcher matcher)
getSubContext
in interface MatcherContext<V>
public boolean runMatcher()
runMatcher
in interface MatcherContext<V>