public class MatcherContext<V> extends java.lang.Object implements Context<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
Modifier and Type | Field and Description |
---|---|
private char |
currentChar |
private int |
currentIndex |
private boolean |
fastStringMatching |
private boolean |
hasError |
private boolean |
inErrorRecovery |
private InputBuffer |
inputBuffer |
private int |
intTag |
private int |
level |
private Matcher |
matcher |
private MatchHandler |
matchHandler |
private java.util.Set<MatcherPosition> |
memoizedMismatches |
private Node<V> |
node |
private boolean |
nodeSuppressed |
private MatcherContext<V> |
parent |
private java.util.List<ParseError> |
parseErrors |
private MatcherPath |
path |
private int |
startIndex |
private MatcherContext<V> |
subContext |
private ImmutableLinkedList<Node<V>> |
subNodes |
private ValueStack<V> |
valueStack |
Modifier | Constructor and Description |
---|---|
|
MatcherContext(InputBuffer inputBuffer,
ValueStack<V> valueStack,
java.util.List<ParseError> parseErrors,
MatchHandler matchHandler,
Matcher matcher,
boolean fastStringMatching)
Initializes a new root MatcherContext.
|
private |
MatcherContext(InputBuffer inputBuffer,
ValueStack<V> valueStack,
java.util.List<ParseError> parseErrors,
MatchHandler matchHandler,
MatcherContext<V> parent,
int level,
boolean fastStringMatching,
java.util.Set<MatcherPosition> memoizedMismatches) |
Modifier and Type | Method and Description |
---|---|
void |
advanceIndex(int delta) |
private void |
checkActionContext() |
void |
createNode() |
boolean |
fastStringMatching()
Returns true if fast string matching is enabled for this parsing run.
|
MatcherContext<V> |
getBasicSubContext() |
char |
getCurrentChar()
Returns the character 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 |
getIntTag() |
int |
getLevel()
Returns the current matcher level, with 0 being the root level, 1 being one level below the root and so on.
|
java.lang.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.
|
Node<V> |
getNode() |
MatcherContext<V> |
getParent()
Returns the parent context, i.e.
|
java.util.List<ParseError> |
getParseErrors()
Returns the list of parse errors for the entire parsing run.
|
MatcherPath |
getPath()
Returns the
MatcherPath to the currently running matcher. |
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) |
ImmutableLinkedList<Node<V>> |
getSubNodes()
Returns the parse tree subnodes already created in the current context scope.
|
private static <V> ImmutableLinkedList<Node<V>> |
getSubNodes(ImmutableLinkedList<Node<V>> remaining,
ImmutableLinkedList<Node<V>> tail) |
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.
|
java.lang.Boolean |
hasMismatched() |
boolean |
inErrorRecovery()
Determines if the action calling this method is run during the resynchronization phase of an error recovery.
|
boolean |
inPredicate()
Determines if the current rule is running somewhere underneath a Test/TestNot rule.
|
boolean |
isNodeSuppressed()
Determines if the current context is for or below a rule marked @SuppressNode or below one
marked @SuppressSubnodes.
|
void |
markError() |
void |
memoizeMismatch() |
boolean |
runMatcher() |
void |
setCurrentIndex(int currentIndex) |
void |
setInErrorRecovery(boolean flag) |
void |
setIntTag(int intTag) |
void |
setMatcher(Matcher matcher) |
void |
setStartIndex(int startIndex) |
java.lang.String |
toString() |
private final InputBuffer inputBuffer
private final ValueStack<V> valueStack
private final java.util.List<ParseError> parseErrors
private final MatchHandler matchHandler
private final MatcherContext<V> parent
private final int level
private final boolean fastStringMatching
private final java.util.Set<MatcherPosition> memoizedMismatches
private MatcherContext<V> subContext
private int startIndex
private int currentIndex
private char currentChar
private Matcher matcher
private ImmutableLinkedList<Node<V>> subNodes
private MatcherPath path
private int intTag
private boolean hasError
private boolean nodeSuppressed
private boolean inErrorRecovery
public MatcherContext(InputBuffer inputBuffer, ValueStack<V> valueStack, java.util.List<ParseError> parseErrors, MatchHandler matchHandler, Matcher matcher, boolean fastStringMatching)
inputBuffer
- the InputBuffer for the parsing runvalueStack
- the ValueStack instance to use for the parsing runparseErrors
- the parse error list to create ParseError objects inmatchHandler
- the MatcherHandler to use for the parsing runmatcher
- the root matcherfastStringMatching
- Fast string matching "short-circuits" the default practice of treating string rules
as simple Sequence of character rules. When fast string matching is enabled strings are
matched at once, without relying on inner CharacterMatchers. Even though this can lead
to significant increases of parsing performance it does not play well with error
reporting and recovery, which relies on character level matches.
Therefore the ReportingParseRunner
and RecoveringParseRunner
implementations only enable fast string matching during their basic first parsing run
and disable it once the input has proven to contain errors.
private MatcherContext(InputBuffer inputBuffer, ValueStack<V> valueStack, java.util.List<ParseError> parseErrors, MatchHandler matchHandler, MatcherContext<V> parent, int level, boolean fastStringMatching, java.util.Set<MatcherPosition> memoizedMismatches)
public java.lang.String toString()
toString
in class java.lang.Object
public MatcherContext<V> getParent()
Context
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 java.util.List<ParseError> getParseErrors()
Context
getParseErrors
in interface Context<V>
public int getCurrentIndex()
Context
getCurrentIndex
in interface Context<V>
public MatcherPath getPath()
Context
MatcherPath
to the currently running matcher.public int getLevel()
Context
public boolean fastStringMatching()
Context
Returns true if fast string matching is enabled for this parsing run.
Fast string matching "short-circuits" the default practice of treating string rules as simple Sequence of
character rules. When fast string matching is enabled strings are matched at once, without relying on inner
CharacterMatchers. Even though this can lead to significant increases of parsing performance it does not play
well with error reporting and recovery, which relies on character level matches.
Therefore the ReportingParseRunner
and RecoveringParseRunner
implementations only enable fast
string matching during their basic first parsing run and disable it once the input has proven to contain errors.
fastStringMatching
in interface Context<V>
public ImmutableLinkedList<Node<V>> getSubNodes()
Context
getSubNodes
in interface Context<V>
private static <V> ImmutableLinkedList<Node<V>> getSubNodes(ImmutableLinkedList<Node<V>> remaining, ImmutableLinkedList<Node<V>> tail)
public boolean inPredicate()
Context
inPredicate
in interface Context<V>
public boolean inErrorRecovery()
Context
inErrorRecovery
in interface Context<V>
public boolean isNodeSuppressed()
Context
isNodeSuppressed
in interface Context<V>
public boolean hasError()
Context
public java.lang.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>
private void checkActionContext()
public ValueStack<V> getValueStack()
Context
getValueStack
in interface Context<V>
public void setMatcher(Matcher matcher)
public void setStartIndex(int startIndex)
public void setCurrentIndex(int currentIndex)
public void setInErrorRecovery(boolean flag)
public void advanceIndex(int delta)
public int getIntTag()
public void setIntTag(int intTag)
public void markError()
public java.lang.Boolean hasMismatched()
public void memoizeMismatch()
public void createNode()
public final MatcherContext<V> getBasicSubContext()
public final MatcherContext<V> getSubContext(Matcher matcher)
public boolean runMatcher()