/** * Checks if any configuration in {@code configs} is in a * {@link RuleStopState}. Configurations meeting this condition have reached * the end of the decision rule (local context) or end of start rule (full * context). * * @param configs the configuration set to test * @return {@code true} if any configuration in {@code configs} is in a * {@link RuleStopState}, otherwise {@code false} */ public static boolean hasConfigInRuleStopState(ATNConfigSet configs) { for (ATNConfig c : configs) { if (c.getState() instanceof RuleStopState) { return true; } } return false; }
public Set<ATNState> getStates() { Set<ATNState> states = new HashSet<ATNState>(); for (ATNConfig c : this.configs) { states.add(c.getState()); } return states; }
protected long getKey(ATNConfig e) { long key = e.getState().stateNumber; key = (key << 12) | (e.getAlt() & 0xFFF); return key; }
@Override public final ATNConfig clone() { return transform(this.getState(), false); }
/** * Checks if all configurations in {@code configs} are in a * {@link RuleStopState}. Configurations meeting this condition have reached * the end of the decision rule (local context) or end of start rule (full * context). * * @param configs the configuration set to test * @return {@code true} if all configurations in {@code configs} are in a * {@link RuleStopState}, otherwise {@code false} */ public static boolean allConfigsInRuleStopStates(@NotNull ATNConfigSet configs) { for (ATNConfig config : configs) { if (!(config.getState() instanceof RuleStopState)) { return false; } } return true; }
@Override public int compare(ATNConfig o1, ATNConfig o2) { if (o1.getAlt() != o2.getAlt()) { return o1.getAlt() - o2.getAlt(); } else if (o1.getState().stateNumber != o2.getState().stateNumber) { return o1.getState().stateNumber - o2.getState().stateNumber; } else { return o1.getSemanticContext().toString().compareTo(o2.getSemanticContext().toString()); } } });
/** * Get a map from state to alt subset from a configuration set. For each * configuration {@code c} in {@code configs}: * * <pre> * map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#getAlt getAlt()} * </pre> */ @NotNull public static Map<ATNState, BitSet> getStateToAltMap(@NotNull ATNConfigSet configs) { Map<ATNState, BitSet> m = new HashMap<ATNState, BitSet>(); for (ATNConfig c : configs) { BitSet alts = m.get(c.getState()); if ( alts==null ) { alts = new BitSet(); m.put(c.getState(), alts); } alts.set(c.getAlt()); } return m; }
protected boolean configWithAltAtStopState(@NotNull Collection<ATNConfig> configs, int alt) { for (ATNConfig c : configs) { if ( c.getAlt() == alt ) { if ( c.getState() instanceof RuleStopState ) { return true; } } } return false; }
public ATNConfig appendContext(int context, PredictionContextCache contextCache) { PredictionContext appendedContext = getContext().appendContext(context, contextCache); ATNConfig result = transform(getState(), appendedContext, false); return result; }
public ATNConfig appendContext(PredictionContext context, PredictionContextCache contextCache) { PredictionContext appendedContext = getContext().appendContext(context, contextCache); ATNConfig result = transform(getState(), appendedContext, false); return result; }
public void dumpDeadEndConfigs(@NotNull NoViableAltException nvae) { System.err.println("dead end configs: "); for (ATNConfig c : nvae.getDeadEndConfigs()) { String trans = "no edges"; if ( c.getState().getNumberOfOptimizedTransitions()>0 ) { Transition t = c.getState().getOptimizedTransition(0); if ( t instanceof AtomTransition) { AtomTransition at = (AtomTransition)t; trans = "Atom "+getTokenName(at.label); } else if ( t instanceof SetTransition ) { SetTransition st = (SetTransition)t; boolean not = st instanceof NotSetTransition; trans = (not?"~":"")+"Set "+st.set.toString(); } } System.err.println(c.toString(parser, true)+":"+trans); } }
/** * The hash code is only a function of the {@link ATNState#stateNumber} * and {@link ATNConfig#context}. */ @Override public int hashCode(ATNConfig o) { int hashCode = MurmurHash.initialize(7); hashCode = MurmurHash.update(hashCode, o.getState().stateNumber); hashCode = MurmurHash.update(hashCode, o.getContext()); hashCode = MurmurHash.finish(hashCode, 2); return hashCode; }
/** Add a new DFA state if there isn't one with this set of configurations already. This method also detects the first configuration containing an ATN rule stop state. Later, when traversing the DFA, we will know which rule to accept. */ @NotNull protected DFAState addDFAState(@NotNull ATNConfigSet configs) { /* the lexer evaluates predicates on-the-fly; by this point configs * should not contain any configurations with unevaluated predicates. */ assert !configs.hasSemanticContext(); DFAState proposed = new DFAState(atn.modeToDFA[mode], configs); DFAState existing = atn.modeToDFA[mode].states.get(proposed); if ( existing!=null ) return existing; configs.optimizeConfigs(this); DFAState newState = new DFAState(atn.modeToDFA[mode], configs.clone(true)); ATNConfig firstConfigWithRuleStopState = null; for (ATNConfig c : configs) { if ( c.getState() instanceof RuleStopState ) { firstConfigWithRuleStopState = c; break; } } if ( firstConfigWithRuleStopState!=null ) { int prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.getState().ruleIndex]; LexerActionExecutor lexerActionExecutor = firstConfigWithRuleStopState.getLexerActionExecutor(); newState.setAcceptState(new AcceptStateInfo(prediction, lexerActionExecutor)); } return atn.modeToDFA[mode].addState(newState); }
/** * Return a configuration set containing only the configurations from * {@code configs} which are in a {@link RuleStopState}. If all * configurations in {@code configs} are already in a rule stop state, this * method simply returns {@code configs}. * * @param configs the configuration set to update * @param contextCache the {@link PredictionContext} cache * * @return {@code configs} if all configurations in {@code configs} are in a * rule stop state, otherwise return a new configuration set containing only * the configurations from {@code configs} which are in a rule stop state */ @NotNull protected ATNConfigSet removeAllConfigsNotInRuleStopState(@NotNull ATNConfigSet configs, PredictionContextCache contextCache) { if (PredictionMode.allConfigsInRuleStopStates(configs)) { return configs; } ATNConfigSet result = new ATNConfigSet(); for (ATNConfig config : configs) { if (!(config.getState() instanceof RuleStopState)) { continue; } result.add(config, contextCache); } return result; }
public boolean equals(ATNConfig other) { if (this == other) { return true; } else if (other == null) { return false; } return this.getState().stateNumber==other.getState().stateNumber && this.getAlt()==other.getAlt() && this.getReachesIntoOuterContext() == other.getReachesIntoOuterContext() && this.getContext().equals(other.getContext()) && this.getSemanticContext().equals(other.getSemanticContext()) && this.isPrecedenceFilterSuppressed() == other.isPrecedenceFilterSuppressed() && this.hasPassedThroughNonGreedyDecision() == other.hasPassedThroughNonGreedyDecision() && ObjectEqualityComparator.INSTANCE.equals(this.getLexerActionExecutor(), other.getLexerActionExecutor()); }
@Override public int hashCode() { int hashCode = MurmurHash.initialize(7); hashCode = MurmurHash.update(hashCode, getState().stateNumber); hashCode = MurmurHash.update(hashCode, getAlt()); hashCode = MurmurHash.update(hashCode, getReachesIntoOuterContext() ? 1 : 0); hashCode = MurmurHash.update(hashCode, getContext()); hashCode = MurmurHash.update(hashCode, getSemanticContext()); hashCode = MurmurHash.update(hashCode, hasPassedThroughNonGreedyDecision() ? 1 : 0); hashCode = MurmurHash.update(hashCode, getLexerActionExecutor()); hashCode = MurmurHash.finish(hashCode, 7); return hashCode; }