Class PredictionContext
- Direct Known Subclasses:
ArrayPredictionContext
,SingletonPredictionContext
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int
Stores the computed hash code of thisPredictionContext
.static final EmptyPredictionContext
Represents$
in local context prediction, which means wildcard.static final int
Represents$
in an array in full context mode, when$
doesn't mean wildcard:$ + x = [$,x]
.static int
final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static int
protected static int
calculateHashCode
(PredictionContext[] parents, int[] returnStates) protected static int
calculateHashCode
(PredictionContext parent, int returnState) protected static void
combineCommonParents
(PredictionContext[] parents) Make pass over all Mparents
; merge anyequals()
ones.abstract boolean
static PredictionContext
fromRuleContext
(ATN atn, RuleContext outerContext) Convert aRuleContext
tree to aPredictionContext
graph.static List
<PredictionContext> getAllContextNodes
(PredictionContext context) static void
getAllContextNodes_
(PredictionContext context, List<PredictionContext> nodes, Map<PredictionContext, PredictionContext> visited) static PredictionContext
getCachedContext
(PredictionContext context, PredictionContextCache contextCache, IdentityHashMap<PredictionContext, PredictionContext> visited) abstract PredictionContext
getParent
(int index) abstract int
getReturnState
(int index) boolean
final int
hashCode()
boolean
isEmpty()
This means only theEMPTY
(wildcard? not sure) context is in set.static PredictionContext
merge
(PredictionContext a, PredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache) static PredictionContext
mergeArrays
(ArrayPredictionContext a, ArrayPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache) Merge twoArrayPredictionContext
instances.static PredictionContext
mergeRoot
(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard) static PredictionContext
mergeSingletons
(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache) Merge twoSingletonPredictionContext
instances.abstract int
size()
static String
toDOTString
(PredictionContext context) toString
(Recognizer<?, ?> recog) String[]
toStrings
(Recognizer<?, ?> recognizer, int currentState) String[]
toStrings
(Recognizer<?, ?> recognizer, PredictionContext stop, int currentState)
-
Field Details
-
EMPTY
Represents$
in local context prediction, which means wildcard.*+x = *
. -
EMPTY_RETURN_STATE
public static final int EMPTY_RETURN_STATERepresents$
in an array in full context mode, when$
doesn't mean wildcard:$ + x = [$,x]
. Here,$
=EMPTY_RETURN_STATE
.- See Also:
-
globalNodeCount
public static int globalNodeCount -
id
public final int id -
cachedHashCode
public final int cachedHashCodeStores 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; }
-
-
Constructor Details
-
PredictionContext
protected PredictionContext(int cachedHashCode)
-
-
Method Details
-
fromRuleContext
Convert aRuleContext
tree to aPredictionContext
graph. ReturnEMPTY
ifouterContext
is empty or null. -
size
public abstract int size() -
getParent
-
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() -
equals
-
calculateEmptyHashCode
protected static int calculateEmptyHashCode() -
calculateHashCode
-
calculateHashCode
-
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
Make pass over all Mparents
; merge anyequals()
ones. -
toDOTString
-
getCachedContext
public static PredictionContext getCachedContext(PredictionContext context, PredictionContextCache contextCache, IdentityHashMap<PredictionContext, PredictionContext> visited) -
getAllContextNodes
-
getAllContextNodes_
public static void getAllContextNodes_(PredictionContext context, List<PredictionContext> nodes, Map<PredictionContext, PredictionContext> visited) -
toString
-
toStrings
-
toStrings
-