Top Fields Constructors Methods
org.antlr.v4.runtime.atn

public abstract Class PredictionContext

extends Object
Class Inheritance
Known Direct Subclasses
org.antlr.v4.runtime.atn.SingletonPredictionContext, org.antlr.v4.runtime.atn.ArrayPredictionContext
Imports
org.antlr.v4.runtime.ParserRuleContext, .Recognizer, .RuleContext, org.antlr.v4.runtime.misc.DoubleKeyMap, .MurmurHash, java.util.*, java.util.concurrent.atomic.AtomicInteger

Field Summary

Modifier and TypeField and Description
public final int
cachedHashCode

Stores the computed hash code of this PredictionContext.

public static final int
EMPTY_RETURN_STATE

Represents $ in an array in full context mode, when $ doesn't mean wildcard: $ + x = [$,x].

private static final AtomicInteger
public final int
private static final int

Constructor Summary

AccessConstructor and Description
protected
PredictionContext(int cachedHashCode)

Method Summary

Modifier and TypeMethod and Description
protected static int
protected static int
calculateHashCode(PredictionContext parent, int returnState)

protected static int
calculateHashCode(PredictionContext[] parents, int[] returnStates)

protected static void
combineCommonParents(PredictionContext[] parents)

Make pass over all M parents; merge any equals() ones.

public abstract boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public static PredictionContext
fromRuleContext(ATN atn, RuleContext outerContext)

Convert a RuleContext tree to a PredictionContext graph.

public static List<PredictionContext>
public static void
public static PredictionContext
public abstract PredictionContext
getParent(int index)

public abstract int
getReturnState(int index)

public boolean
public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public boolean
isEmpty()

This means only the EmptyPredictionContext#Instance (wildcard?

public static PredictionContext
public static PredictionContext
public static PredictionContext
mergeRoot(SingletonPredictionContext a, SingletonPredictionContext b, boolean
true if this is a local-context merge, otherwise false to indicate a full-context merge
rootIsWildcard
)

Handle case where at least one of a or b is EmptyPredictionContext#Instance.

public static PredictionContext
mergeSingletons(SingletonPredictionContext a, SingletonPredictionContext b, boolean
true if this is a local-context merge, otherwise false to indicate a full-context merge
rootIsWildcard
,
DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache)

Merge two SingletonPredictionContext instances.

public abstract int
size()

public static String
public String
toString(Recognizer<?, ?> recog)

public String[]
toStrings(Recognizer<?, ?> recognizer, int currentState)

public String[]
toStrings(Recognizer<?, ?> recognizer, PredictionContext stop, int currentState)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedHashCodeback to summary
public final int cachedHashCode

Stores the computed hash code of this 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;
 }
EMPTY_RETURN_STATEback to summary
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.

globalNodeCountback to summary
private static final AtomicInteger globalNodeCount
idback to summary
public final int id
INITIAL_HASHback to summary
private static final int INITIAL_HASH

Constructor Detail

PredictionContextback to summary
protected PredictionContext(int cachedHashCode)

Method Detail

calculateEmptyHashCodeback to summary
protected static int calculateEmptyHashCode()
calculateHashCodeback to summary
protected static int calculateHashCode(PredictionContext parent, int returnState)
calculateHashCodeback to summary
protected static int calculateHashCode(PredictionContext[] parents, int[] returnStates)
combineCommonParentsback to summary
protected static void combineCommonParents(PredictionContext[] parents)

Make pass over all M parents; merge any equals() ones.

equalsback to summary
public abstract boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fromRuleContextback to summary
public static PredictionContext fromRuleContext(ATN atn, RuleContext outerContext)

Convert a RuleContext tree to a PredictionContext graph. Return EmptyPredictionContext#Instance if outerContext is empty or null.

getAllContextNodesback to summary
public static List<PredictionContext> getAllContextNodes(PredictionContext context)
getAllContextNodes_back to summary
public static void getAllContextNodes_(PredictionContext context, List<PredictionContext> nodes, Map<PredictionContext, PredictionContext> visited)
getCachedContextback to summary
public static PredictionContext getCachedContext(PredictionContext context, PredictionContextCache contextCache, IdentityHashMap<PredictionContext, PredictionContext> visited)
getParentback to summary
public abstract PredictionContext getParent(int index)
getReturnStateback to summary
public abstract int getReturnState(int index)
hasEmptyPathback to summary
public boolean hasEmptyPath()
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

This means only the EmptyPredictionContext#Instance (wildcard? not sure) context is in set.

mergeback to summary
public static PredictionContext merge(PredictionContext a, PredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache)
mergeArraysback to summary
public static PredictionContext mergeArrays(ArrayPredictionContext a, ArrayPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache)

Merge two 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.

mergeRootback to summary
public static PredictionContext mergeRoot(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard)

Handle case where at least one of a or b is EmptyPredictionContext#Instance. In the following diagrams, the symbol $ is used to represent EmptyPredictionContext#Instance.

Local-Context Merges

These local-context merge operations are used when rootIsWildcard is true.

EmptyPredictionContext#Instance is superset of any graph; return EmptyPredictionContext#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:SingletonPredictionContext

the first SingletonPredictionContext

b:SingletonPredictionContext

the second SingletonPredictionContext

rootIsWildcard:boolean

true if this is a local-context merge, otherwise false to indicate a full-context merge

mergeSingletonsback to summary
public static PredictionContext mergeSingletons(SingletonPredictionContext a, SingletonPredictionContext b, boolean rootIsWildcard, DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext> mergeCache)

Merge two 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.

Parameters
a:SingletonPredictionContext

the first SingletonPredictionContext

b:SingletonPredictionContext

the second SingletonPredictionContext

rootIsWildcard:boolean

true if this is a local-context merge, otherwise false to indicate a full-context merge

sizeback to summary
public abstract int size()
toDOTStringback to summary
public static String toDOTString(PredictionContext context)
toStringback to summary
public String toString(Recognizer<?, ?> recog)
toStringsback to summary
public String[] toStrings(Recognizer<?, ?> recognizer, int currentState)
toStringsback to summary
public String[] toStrings(Recognizer<?, ?> recognizer, PredictionContext stop, int currentState)