Class ATNConfig

java.lang.Object
org.antlr.v4.runtime.atn.ATNConfig
Direct Known Subclasses:
ATNConfig.ActionATNConfig, ATNConfig.SemanticContextATNConfig

public class ATNConfig extends Object
Represents a location with context in an ATN. The location is identified by the following values:
  • The current ATN state
  • The predicted alternative
  • The semantic context which must be true for this configuration to be enabled
  • The syntactic context, which is represented as a graph-structured stack whose path(s) lead to the root of the rule invocations leading to this state.

In addition to these values, ATNConfig stores several properties about paths taken to get to the location which were added over time to help with performance, correctness, and/or debugging.

Due to the use of a graph-structured stack, a single ATNConfig is capable of representing many individual ATN configurations which reached the same location in an ATN by following different paths.

PERF: To conserve memory, ATNConfig is split into several different concrete types. ATNConfig itself stores the minimum amount of information typically used to define an ATNConfig instance. Various derived types provide additional storage space for cases where a non-default value is used for some of the object properties. The create(org.antlr.v4.runtime.atn.ATNState, int, org.antlr.v4.runtime.atn.PredictionContext) and transform(org.antlr.v4.runtime.atn.ATNState, boolean) methods automatically select the smallest concrete type capable of representing the unique information for any given ATNConfig.

  • Field Details

    • SUPPRESS_PRECEDENCE_FILTER

      private static final int SUPPRESS_PRECEDENCE_FILTER
      This field stores the bit mask for implementing the isPrecedenceFilterSuppressed() property as a bit within the existing altAndOuterContextDepth field.
      See Also:
    • state

      @NotNull private final ATNState state
      The ATN state associated with this configuration
    • altAndOuterContextDepth

      private int altAndOuterContextDepth
      This is a bit-field currently containing the following values.
      • 0x00FFFFFF: Alternative
      • 0x7F000000: Outer context depth
      • 0x80000000: Suppress precedence filter
    • context

      @NotNull private PredictionContext context
      The stack of invoking states leading to the rule/states associated with this config. We track only those contexts pushed during execution of the ATN simulator.
    • cachedHashCode

      private int cachedHashCode
      Lazily cached hashCode() result. Zero means "not yet computed" (or invalidated after a mutable field change). The hash algorithm is biased away from zero when the raw MurmurHash finishes at zero so a legitimate hash of zero cannot thrash the cache sentinel.

      PERF: Lexer OrderedATNConfigSet keys configs by hashCode(), parser epsilon-closure busy sets hash configs on every right-recursion / EOF* guard, and DFA state lookup hashes entire config sets. Caching avoids repeating MurmurHash over the same immutable-after-first-use field set on these hot paths.

  • Constructor Details

  • Method Details

    • create

      public static ATNConfig create(@NotNull ATNState state, int alt, @Nullable PredictionContext context)
    • create

      public static ATNConfig create(@NotNull ATNState state, int alt, @Nullable PredictionContext context, @NotNull SemanticContext semanticContext)
    • create

      public static ATNConfig create(@NotNull ATNState state, int alt, @Nullable PredictionContext context, @NotNull SemanticContext semanticContext, LexerActionExecutor lexerActionExecutor)
    • getState

      @NotNull public final ATNState getState()
      Gets the ATN state associated with this configuration
    • getAlt

      public final int getAlt()
      What alt (or lexer rule) is predicted by this configuration
    • getContext

      @NotNull public final PredictionContext getContext()
    • setContext

      public void setContext(@NotNull PredictionContext context)
    • getReachesIntoOuterContext

      public final boolean getReachesIntoOuterContext()
    • getOuterContextDepth

      public final int getOuterContextDepth()
      We cannot execute predicates dependent upon local context unless we know for sure we are in the correct context. Because there is no way to do this efficiently, we simply cannot evaluate dependent predicates unless we are in the rule that initially invokes the ATN simulator.

      closure() tracks the depth of how far we dip into the outer context: depth > 0. Note that it may not be totally accurate depth since I don't ever decrement. TODO: make it a boolean then

    • setOuterContextDepth

      public void setOuterContextDepth(int outerContextDepth)
    • getLexerActionExecutor

      @Nullable public LexerActionExecutor getLexerActionExecutor()
    • getSemanticContext

      @NotNull public SemanticContext getSemanticContext()
    • hasPassedThroughNonGreedyDecision

      public boolean hasPassedThroughNonGreedyDecision()
    • clone

      public final ATNConfig clone()
      Overrides:
      clone in class Object
    • transform

      public final ATNConfig transform(@NotNull ATNState state, boolean checkNonGreedy)
    • transform

      public final ATNConfig transform(@NotNull ATNState state, @NotNull SemanticContext semanticContext, boolean checkNonGreedy)
    • transform

      public final ATNConfig transform(@NotNull ATNState state, @Nullable PredictionContext context, boolean checkNonGreedy)
    • transform

      public final ATNConfig transform(@NotNull ATNState state, LexerActionExecutor lexerActionExecutor, boolean checkNonGreedy)
    • transform

      private ATNConfig transform(@NotNull ATNState state, @Nullable PredictionContext context, @NotNull SemanticContext semanticContext, boolean checkNonGreedy, LexerActionExecutor lexerActionExecutor)
    • checkNonGreedyDecision

      private static boolean checkNonGreedyDecision(ATNConfig source, ATNState target)
    • appendContext

      public ATNConfig appendContext(int context, PredictionContextCache contextCache)
    • appendContext

      public ATNConfig appendContext(PredictionContext context, PredictionContextCache contextCache)
    • contains

      public boolean contains(ATNConfig subconfig)
      Determines if this ATNConfig fully contains another ATNConfig.

      An ATN configuration represents a position (including context) in an ATN during parsing. Since ATNConfig stores the context as a graph, a single ATNConfig instance is capable of representing many ATN configurations which are all in the same "location" but have different contexts. These ATNConfig instances are again merged when they are added to an ATNConfigSet. This method supports ATNConfigSet.contains(java.lang.Object) by evaluating whether a particular ATNConfig contains all of the ATN configurations represented by another ATNConfig.

      An ATNConfig a contains another ATNConfig b if all of the following conditions are met:

      • The configurations are in the same state (getState())
      • The configurations predict the same alternative (getAlt())
      • The semantic context of a implies the semantic context of b (this method performs a weaker equality check)
      • Joining the prediction contexts of a and b results in the prediction context of a

      This method implements a conservative approximation of containment. As a result, when this method returns {code true} it is known that parsing from subconfig can only recognize a subset of the inputs which can be recognized starting at the current ATNConfig. However, due to the imprecise evaluation of implication for the semantic contexts, no assumptions can be made about the relationship between the configurations when this method returns false.

      Parameters:
      subconfig - The sub configuration.
      Returns:
      true if this configuration contains subconfig; otherwise, false.
    • isPrecedenceFilterSuppressed

      public final boolean isPrecedenceFilterSuppressed()
    • setPrecedenceFilterSuppressed

      public final void setPrecedenceFilterSuppressed(boolean value)
    • equals

      public boolean equals(Object o)
      An ATN configuration is equal to another if both have the same state, they predict the same alternative, and syntactic/semantic contexts are the same.
      Overrides:
      equals in class Object
    • equals

      public boolean equals(ATNConfig other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toDotString

      public String toDotString()
      Returns a graphical representation of the current ATNConfig in Graphviz format. The graph can be stored to a .dot file and then rendered to an image using Graphviz.
      Returns:
      A Graphviz graph representing the current ATNConfig.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(@Nullable Recognizer<?,?> recog, boolean showAlt)
    • toString

      public String toString(@Nullable Recognizer<?,?> recog, boolean showAlt, boolean showContext)