Class PredictionContext
- java.lang.Object
-
- org.antlr.v4.runtime.atn.PredictionContext
-
- Direct Known Subclasses:
ArrayPredictionContext
,SingletonPredictionContext
public abstract class PredictionContext extends Object
-
-
Field Summary
Fields Modifier and Type Field Description int
cachedHashCode
Stores the computed hash code of thisPredictionContext
.static int
EMPTY_RETURN_STATE
Represents$
in an array in full context mode, when$
doesn't mean wildcard:$ + x = [$,x]
.int
id
-
Constructor Summary
Constructors Modifier Constructor Description protected
PredictionContext(int cachedHashCode)
-
Method Summary
-
-
-
Field Detail
-
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
-
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. ReturnEmptyPredictionContext.Instance
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 theEmptyPredictionContext.Instance
(wildcard? not sure) context is in set.
-
hasEmptyPath
public boolean hasEmptyPath()
-
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
isEmptyPredictionContext.Instance
. In the following diagrams, the symbol$
is used to representEmptyPredictionContext.Instance
.Local-Context Merges
These local-context merge operations are used when
rootIsWildcard
is true.EmptyPredictionContext.Instance
is superset of any graph; returnEmptyPredictionContext.Instance
.
EmptyPredictionContext.Instance
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;
EmptyPredictionContext.Instance
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 String toDOTString(PredictionContext context)
-
getCachedContext
public static PredictionContext getCachedContext(PredictionContext context, PredictionContextCache contextCache, IdentityHashMap<PredictionContext,PredictionContext> visited)
-
getAllContextNodes
public static List<PredictionContext> getAllContextNodes(PredictionContext context)
-
getAllContextNodes_
public static void getAllContextNodes_(PredictionContext context, List<PredictionContext> nodes, Map<PredictionContext,PredictionContext> visited)
-
toString
public String toString(Recognizer<?,?> recog)
-
toStrings
public String[] toStrings(Recognizer<?,?> recognizer, int currentState)
-
toStrings
public String[] toStrings(Recognizer<?,?> recognizer, PredictionContext stop, int currentState)
-
-