Class PredictionContext
- java.lang.Object
-
- org.antlr.v4.runtime.atn.PredictionContext
-
- Direct Known Subclasses:
ArrayPredictionContext
,SingletonPredictionContext
public abstract class PredictionContext extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description int
cachedHashCode
Stores the computed hash code of thisPredictionContext
.static EmptyPredictionContext
EMPTY
Represents$
in local context prediction, which means wildcard.static int
EMPTY_RETURN_STATE
Represents$
in an array in full context mode, when$
doesn't mean wildcard:$ + x = [$,x]
.static int
globalNodeCount
int
id
-
Constructor Summary
Constructors Modifier Constructor Description protected
PredictionContext(int cachedHashCode)
-
Method Summary
-
-
-
Field Detail
-
EMPTY
public static final EmptyPredictionContext EMPTY
Represents$
in local context prediction, which means wildcard.*+x = *
.
-
EMPTY_RETURN_STATE
public static final int EMPTY_RETURN_STATE
Represents$
in an array in full context mode, when$
doesn't mean wildcard:$ + x = [$,x]
. Here,$
=EMPTY_RETURN_STATE
.- See Also:
- Constant Field Values
-
globalNodeCount
public static int globalNodeCount
-
id
public final int id
-
cachedHashCode
public final int cachedHashCode
Stores the computed hash code of thisPredictionContext
. The hash code is computed in parts to match the following reference algorithm.private int referenceHashCode() { int hash =
MurmurHash.initialize
(INITIAL_HASH
); for (int i = 0; i <size()
; i++) { hash =MurmurHash.update
(hash,getParent
(i)); } for (int i = 0; i <size()
; i++) { hash =MurmurHash.update
(hash,getReturnState
(i)); } hash =MurmurHash.finish
(hash, 2 *size()
); return hash; }
-
-
Method Detail
-
fromRuleContext
public static PredictionContext fromRuleContext(ATN atn, RuleContext outerContext)
Convert aRuleContext
tree to aPredictionContext
graph. ReturnEMPTY
ifouterContext
is empty or null.
-
size
public abstract int size()
-
getParent
public abstract PredictionContext getParent(int index)
-
getReturnState
public abstract int getReturnState(int index)
-
isEmpty
public boolean isEmpty()
This means only theEMPTY
(wildcard? not sure) context is in set.
-
hasEmptyPath
public boolean hasEmptyPath()
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public abstract boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
calculateEmptyHashCode
protected static int calculateEmptyHashCode()
-
calculateHashCode
protected static int calculateHashCode(PredictionContext parent, int returnState)
-
calculateHashCode
protected static int calculateHashCode(PredictionContext[] parents, int[] returnStates)
-
merge
public static PredictionContext merge(PredictionContext a, PredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
-
mergeSingletons
public static PredictionContext mergeSingletons(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
Merge twoSingletonPredictionContext
instances.Stack tops equal, parents merge is same; return left graph.
Same stack top, parents differ; merge parents giving array node, then remainders of those graphs. A new root node is created to point to the merged parents.
Different stack tops pointing to same parent. Make array node for the root where both element in the root point to the same (original) parent.
Different stack tops pointing to different parents. Make array node for the root where each element points to the corresponding original parent.
- Parameters:
a
- the firstSingletonPredictionContext
b
- the secondSingletonPredictionContext
rootIsWildcard
-true
if this is a local-context merge, otherwise false to indicate a full-context mergemergeCache
-
-
mergeRoot
public static PredictionContext mergeRoot(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard)
Handle case where at least one ofa
orb
isEMPTY
. In the following diagrams, the symbol$
is used to representEMPTY
.Local-Context Merges
These local-context merge operations are used when
rootIsWildcard
is true.EMPTY
is superset of any graph; returnEMPTY
.
EMPTY
and anything is#EMPTY
, so merged parent is#EMPTY
; return left graph.
Special case of last merge if local context.
Full-Context Merges
These full-context merge operations are used when
rootIsWildcard
is false.Must keep all contexts;
EMPTY
in array is a special value (and null parent).
- Parameters:
a
- the firstSingletonPredictionContext
b
- the secondSingletonPredictionContext
rootIsWildcard
-true
if this is a local-context merge, otherwise false to indicate a full-context merge
-
mergeArrays
public static PredictionContext mergeArrays(ArrayPredictionContext a, ArrayPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
Merge twoArrayPredictionContext
instances.Different tops, different parents.
Shared top, same parents.
Shared top, different parents.
Shared top, all shared parents.
Equal tops, merge parents and reduce top to
SingletonPredictionContext
.
-
combineCommonParents
protected static void combineCommonParents(PredictionContext[] parents)
Make pass over all Mparents
; merge anyequals()
ones.
-
toDOTString
public static java.lang.String toDOTString(PredictionContext context)
-
getCachedContext
public static PredictionContext getCachedContext(PredictionContext context, PredictionContextCache contextCache, java.util.IdentityHashMap<PredictionContext,PredictionContext> visited)
-
getAllContextNodes
public static java.util.List<PredictionContext> getAllContextNodes(PredictionContext context)
-
getAllContextNodes_
public static void getAllContextNodes_(PredictionContext context, java.util.List<PredictionContext> nodes, java.util.Map<PredictionContext,PredictionContext> visited)
-
toString
public java.lang.String toString(Recognizer<?,?> recog)
-
toStrings
public java.lang.String[] toStrings(Recognizer<?,?> recognizer, int currentState)
-
toStrings
public java.lang.String[] toStrings(Recognizer<?,?> recognizer, PredictionContext stop, int currentState)
-
-