public abstract class PredictionContext
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
int |
cachedHashCode
Stores the computed hash code of this
PredictionContext. |
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 |
| Modifier | Constructor and Description |
|---|---|
protected |
PredictionContext(int cachedHashCode) |
public static final EmptyPredictionContext EMPTY
$ in local context prediction, which means wildcard.
*+x = *.public static final int EMPTY_RETURN_STATE
$ in an array in full context mode, when $
doesn't mean wildcard: $ + x = [$,x]. Here,
$ = EMPTY_RETURN_STATE.public static int globalNodeCount
public final int id
public final int cachedHashCode
PredictionContext. 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;
}
public static PredictionContext fromRuleContext(ATN atn, RuleContext outerContext)
RuleContext tree to a PredictionContext graph.
Return EMPTY if outerContext is empty or null.public abstract int size()
public abstract PredictionContext getParent(int index)
public abstract int getReturnState(int index)
public boolean isEmpty()
EMPTY context is in set.public boolean hasEmptyPath()
public final int hashCode()
hashCode in class java.lang.Objectpublic abstract boolean equals(java.lang.Object obj)
equals in class java.lang.Objectprotected static int calculateEmptyHashCode()
protected static int calculateHashCode(PredictionContext parent, int returnState)
protected static int calculateHashCode(PredictionContext[] parents, int[] returnStates)
public static PredictionContext merge(PredictionContext a, PredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
public static PredictionContext mergeSingletons(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
SingletonPredictionContext 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.
a - the first SingletonPredictionContextb - the second SingletonPredictionContextrootIsWildcard - true if this is a local-context merge,
otherwise false to indicate a full-context mergemergeCache - public static PredictionContext mergeRoot(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard)
a or b is
EMPTY. In the following diagrams, the symbol $ is used
to represent EMPTY.
These local-context merge operations are used when rootIsWildcard
is true.
EMPTY is superset of any graph; return EMPTY.
EMPTY and anything is #EMPTY, so merged parent is
#EMPTY; return left graph.
Special case of last merge if local context.
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).
a - the first SingletonPredictionContextb - the second SingletonPredictionContextrootIsWildcard - true if this is a local-context merge,
otherwise false to indicate a full-context mergepublic static PredictionContext mergeArrays(ArrayPredictionContext a, ArrayPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
ArrayPredictionContext 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.
protected static void combineCommonParents(PredictionContext[] parents)
parents; merge any equals()
ones.public static java.lang.String toDOTString(PredictionContext context)
public static PredictionContext getCachedContext(PredictionContext context, PredictionContextCache contextCache, java.util.IdentityHashMap<PredictionContext,PredictionContext> visited)
public static java.util.List<PredictionContext> getAllContextNodes(PredictionContext context)
public static void getAllContextNodes_(PredictionContext context, java.util.List<PredictionContext> nodes, java.util.Map<PredictionContext,PredictionContext> visited)
public java.lang.String toString(Recognizer<?,?> recog)
public java.lang.String[] toStrings(Recognizer<?,?> recognizer, int currentState)
public java.lang.String[] toStrings(Recognizer<?,?> recognizer, PredictionContext stop, int currentState)