Class MatcherContext<V>
- java.lang.Object
-
- org.parboiled.MatcherContext<V>
-
- All Implemented Interfaces:
Context<V>
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
Matcherinstances) has been created a root MatcherContext is instantiated for the root rule (Matcher). A subsequent call torunMatcher()starts the parsing process.The MatcherContext delegates to a given
MatchHandlerto callMatcher.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 withMatcher.getSubContext(MatcherContext)and then callsrunMatcher()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 Summary
Constructors Constructor Description MatcherContext(InputBuffer inputBuffer, ValueStack<V> valueStack, java.util.List<ParseError> parseErrors, MatchHandler matchHandler, Matcher matcher, boolean fastStringMatching)Initializes a new root MatcherContext.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvanceIndex(int delta)voidcreateNode()booleanfastStringMatching()Returns true if fast string matching is enabled for this parsing run.MatcherContext<V>getBasicSubContext()chargetCurrentChar()Returns the character at the current index..intgetCurrentIndex()Returns the current index in the input buffer.chargetFirstMatchChar()Returns the first character of the input text matched by the rule immediately preceding the action expression that is currently being evaluated.InputBuffergetInputBuffer()Returns the InputBuffer the parser is currently running againstintgetIntTag()intgetLevel()Returns the current matcher level, with 0 being the root level, 1 being one level below the root and so on.java.lang.StringgetMatch()Returns the input text matched by the rule immediately preceding the action expression that is currently being evaluated.intgetMatchEndIndex()Returns the end index of the rule immediately preceding the action expression that is currently being evaluated.MatchergetMatcher()Returns the Matcher of this context or null, if this context is not valid anymore.intgetMatchLength()Returns the number of characters matched by the rule immediately preceding the action expression that is currently being evaluated.IndexRangegetMatchRange()Creates a newIndexRangeinstance covering the input text matched by the rule immediately preceding the action expression that is currently being evaluated.intgetMatchStartIndex()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.MatcherPathgetPath()Returns theMatcherPathto the currently running matcher.PositiongetPosition()Returns the current position in the underlyingInputBufferas aPositioninstance.intgetStartIndex()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.ValueStack<V>getValueStack()Returns the value stack instance used during this parsing run.booleanhasError()Determines if this context or any sub node recorded a parse error.java.lang.BooleanhasMismatched()booleaninErrorRecovery()Determines if the action calling this method is run during the resynchronization phase of an error recovery.booleaninPredicate()Determines if the current rule is running somewhere underneath a Test/TestNot rule.booleanisNodeSuppressed()Determines if the current context is for or below a rule marked @SuppressNode or below one marked @SuppressSubnodes.voidmarkError()voidmemoizeMismatch()booleanrunMatcher()voidsetCurrentIndex(int currentIndex)voidsetInErrorRecovery(boolean flag)voidsetIntTag(int intTag)voidsetMatcher(Matcher matcher)voidsetStartIndex(int startIndex)java.lang.StringtoString()
-
-
-
Constructor Detail
-
MatcherContext
public MatcherContext(InputBuffer inputBuffer, ValueStack<V> valueStack, java.util.List<ParseError> parseErrors, MatchHandler matchHandler, Matcher matcher, boolean fastStringMatching)
Initializes a new root MatcherContext.- Parameters:
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
ReportingParseRunnerandRecoveringParseRunnerimplementations only enable fast string matching during their basic first parsing run and disable it once the input has proven to contain errors.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getParent
public MatcherContext<V> getParent()
Description copied from interface:ContextReturns the parent context, i.e. the context for the currently running parent matcher.
-
getInputBuffer
public InputBuffer getInputBuffer()
Description copied from interface:ContextReturns the InputBuffer the parser is currently running against- Specified by:
getInputBufferin interfaceContext<V>- Returns:
- the InputBuffer
-
getStartIndex
public int getStartIndex()
Description copied from interface:ContextReturns the index into the underlying input buffer where the matcher of this context started its match.- Specified by:
getStartIndexin interfaceContext<V>- Returns:
- the start index
-
getMatcher
public Matcher getMatcher()
Description copied from interface:ContextReturns the Matcher of this context or null, if this context is not valid anymore.- Specified by:
getMatcherin interfaceContext<V>- Returns:
- the matcher
-
getCurrentChar
public char getCurrentChar()
Description copied from interface:ContextReturns the character at the current index..- Specified by:
getCurrentCharin interfaceContext<V>- Returns:
- the current character
-
getParseErrors
public java.util.List<ParseError> getParseErrors()
Description copied from interface:ContextReturns the list of parse errors for the entire parsing run.- Specified by:
getParseErrorsin interfaceContext<V>- Returns:
- the list of parse errors
-
getCurrentIndex
public int getCurrentIndex()
Description copied from interface:ContextReturns the current index in the input buffer.- Specified by:
getCurrentIndexin interfaceContext<V>- Returns:
- the current index
-
getPath
public MatcherPath getPath()
Description copied from interface:ContextReturns theMatcherPathto the currently running matcher.
-
getLevel
public int getLevel()
Description copied from interface:ContextReturns the current matcher level, with 0 being the root level, 1 being one level below the root and so on.
-
fastStringMatching
public boolean fastStringMatching()
Description copied from interface:ContextReturns 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
ReportingParseRunnerandRecoveringParseRunnerimplementations only enable fast string matching during their basic first parsing run and disable it once the input has proven to contain errors.- Specified by:
fastStringMatchingin interfaceContext<V>- Returns:
- true if fast string matching is enabled during the current parsing run
-
getSubNodes
public ImmutableLinkedList<Node<V>> getSubNodes()
Description copied from interface:ContextReturns the parse tree subnodes already created in the current context scope. Note that the returned list is immutable.- Specified by:
getSubNodesin interfaceContext<V>- Returns:
- the parse tree subnodes already created in the current context scope
-
inPredicate
public boolean inPredicate()
Description copied from interface:ContextDetermines if the current rule is running somewhere underneath a Test/TestNot rule.- Specified by:
inPredicatein interfaceContext<V>- Returns:
- true if the current context has a parent which corresponds to a Test/TestNot rule
-
inErrorRecovery
public boolean inErrorRecovery()
Description copied from interface:ContextDetermines if the action calling this method is run during the resynchronization phase of an error recovery.- Specified by:
inErrorRecoveryin interfaceContext<V>- Returns:
- true if the action calling this method is run during the resynchronization phase of an error recovery
-
isNodeSuppressed
public boolean isNodeSuppressed()
Description copied from interface:ContextDetermines if the current context is for or below a rule marked @SuppressNode or below one marked @SuppressSubnodes.- Specified by:
isNodeSuppressedin interfaceContext<V>- Returns:
- true or false
-
hasError
public boolean hasError()
Description copied from interface:ContextDetermines if this context or any sub node recorded a parse error.
-
getMatch
public java.lang.String getMatch()
Description copied from interface:ContextReturns 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.
-
getFirstMatchChar
public char getFirstMatchChar()
Description copied from interface:ContextReturns 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.
- Specified by:
getFirstMatchCharin interfaceContext<V>- Returns:
- the input text matched by the immediately preceding subcontext
-
getMatchStartIndex
public int getMatchStartIndex()
Description copied from interface:ContextReturns 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.
- Specified by:
getMatchStartIndexin interfaceContext<V>- Returns:
- the start index of the context immediately preceding current action
-
getMatchEndIndex
public int getMatchEndIndex()
Description copied from interface:ContextReturns 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.
- Specified by:
getMatchEndIndexin interfaceContext<V>- Returns:
- the end index of the context immediately preceding current action, i.e. the index of the character immediately following the last matched character
-
getMatchLength
public int getMatchLength()
Description copied from interface:ContextReturns 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.
- Specified by:
getMatchLengthin interfaceContext<V>- Returns:
- the number of characters matched
-
getPosition
public Position getPosition()
Description copied from interface:ContextReturns the current position in the underlying
InputBufferas aPositioninstance.- Specified by:
getPositionin interfaceContext<V>- Returns:
- the current position in the underlying inputbuffer
-
getMatchRange
public IndexRange getMatchRange()
Description copied from interface:ContextCreates a newIndexRangeinstance 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.- Specified by:
getMatchRangein interfaceContext<V>- Returns:
- a new IndexRange instance
-
getValueStack
public ValueStack<V> getValueStack()
Description copied from interface:ContextReturns the value stack instance used during this parsing run.- Specified by:
getValueStackin interfaceContext<V>- Returns:
- the value stack
-
setMatcher
public void setMatcher(Matcher matcher)
-
setStartIndex
public void setStartIndex(int startIndex)
-
setCurrentIndex
public void setCurrentIndex(int currentIndex)
-
setInErrorRecovery
public void setInErrorRecovery(boolean flag)
-
advanceIndex
public void advanceIndex(int delta)
-
getIntTag
public int getIntTag()
-
setIntTag
public void setIntTag(int intTag)
-
markError
public void markError()
-
hasMismatched
public java.lang.Boolean hasMismatched()
-
memoizeMismatch
public void memoizeMismatch()
-
createNode
public void createNode()
-
getBasicSubContext
public final MatcherContext<V> getBasicSubContext()
-
getSubContext
public final MatcherContext<V> getSubContext(Matcher matcher)
-
runMatcher
public boolean runMatcher()
-
-