Search in sources :

Example 31 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method adaptivePredict.

public int adaptivePredict(@NotNull TokenStream input, int decision, @Nullable ParserRuleContext outerContext, boolean useContext) {
    DFA dfa = atn.decisionToDFA[decision];
    assert dfa != null;
    if (optimize_ll1 && !dfa.isPrecedenceDfa() && !dfa.isEmpty()) {
        int ll_1 = input.LA(1);
        if (ll_1 >= 0 && ll_1 <= Short.MAX_VALUE) {
            int key = (decision << 16) + ll_1;
            Integer alt = atn.LL1Table.get(key);
            if (alt != null) {
                return alt;
            }
        }
    }
    this.dfa = dfa;
    if (force_global_context) {
        useContext = true;
    } else if (!always_try_local_context) {
        useContext |= dfa.isContextSensitive();
    }
    userWantsCtxSensitive = useContext || (predictionMode != PredictionMode.SLL && outerContext != null && !atn.decisionToState.get(decision).sll);
    if (outerContext == null) {
        outerContext = ParserRuleContext.emptyContext();
    }
    SimulatorState state = null;
    if (!dfa.isEmpty()) {
        state = getStartState(dfa, input, outerContext, useContext);
    }
    if (state == null) {
        if (outerContext == null)
            outerContext = ParserRuleContext.emptyContext();
        if (debug)
            System.out.println("ATN decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
        state = computeStartState(dfa, outerContext, useContext);
    }
    int m = input.mark();
    int index = input.index();
    try {
        int alt = execDFA(dfa, input, index, state);
        if (debug)
            System.out.println("DFA after predictATN: " + dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
        return alt;
    } finally {
        this.dfa = null;
        input.seek(index);
        input.release(m);
    }
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Example 32 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method addDFAEdge.

@NotNull
protected DFAState addDFAEdge(@NotNull DFA dfa, @NotNull DFAState fromState, int t, IntegerList contextTransitions, @NotNull ATNConfigSet toConfigs, PredictionContextCache contextCache) {
    assert contextTransitions == null || contextTransitions.isEmpty() || dfa.isContextSensitive();
    DFAState from = fromState;
    DFAState to = addDFAState(dfa, toConfigs, contextCache);
    if (contextTransitions != null) {
        for (int context : contextTransitions.toArray()) {
            if (context == PredictionContext.EMPTY_FULL_STATE_KEY) {
                if (from.configs.isOutermostConfigSet()) {
                    continue;
                }
            }
            from.setContextSensitive(atn);
            from.setContextSymbol(t);
            DFAState next = from.getContextTarget(context);
            if (next != null) {
                from = next;
                continue;
            }
            next = addDFAContextState(dfa, from.configs, context, contextCache);
            assert context != PredictionContext.EMPTY_FULL_STATE_KEY || next.configs.isOutermostConfigSet();
            from.setContextTarget(context, next);
            from = next;
        }
    }
    if (debug)
        System.out.println("EDGE " + from + " -> " + to + " upon " + getTokenName(t));
    addDFAEdge(from, t, to);
    if (debug)
        System.out.println("DFA=\n" + dfa.toString(parser != null ? parser.getVocabulary() : VocabularyImpl.EMPTY_VOCABULARY, parser != null ? parser.getRuleNames() : null));
    return to;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 33 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class Parser method getExpectedTokensWithinCurrentRule.

@NotNull
public IntervalSet getExpectedTokensWithinCurrentRule() {
    ATN atn = getInterpreter().atn;
    ATNState s = atn.states.get(getState());
    return atn.nextTokens(s);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 34 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class Parser method getATNWithBypassAlts.

/**
 * The ATN with bypass alternatives is expensive to create so we create it
 * lazily.
 *
 * @throws UnsupportedOperationException if the current parser does not
 * implement the {@link #getSerializedATN()} method.
 */
@NotNull
public ATN getATNWithBypassAlts() {
    String serializedAtn = getSerializedATN();
    if (serializedAtn == null) {
        throw new UnsupportedOperationException("The current parser does not support an ATN with bypass alternatives.");
    }
    synchronized (bypassAltsAtnCache) {
        ATN result = bypassAltsAtnCache.get(serializedAtn);
        if (result == null) {
            ATNDeserializationOptions deserializationOptions = new ATNDeserializationOptions();
            deserializationOptions.setGenerateRuleBypassTransitions(true);
            result = new ATNDeserializer(deserializationOptions).deserialize(serializedAtn.toCharArray());
            bypassAltsAtnCache.put(serializedAtn, result);
        }
        return result;
    }
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ATNDeserializationOptions(org.antlr.v4.runtime.atn.ATNDeserializationOptions) ATN(org.antlr.v4.runtime.atn.ATN) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 35 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class DefaultErrorStrategy method reportUnwantedToken.

/**
 * This method is called to report a syntax error which requires the removal
 * of a token from the input stream. At the time this method is called, the
 * erroneous symbol is current {@code LT(1)} symbol and has not yet been
 * removed from the input stream. When this method returns,
 * {@code recognizer} is in error recovery mode.
 *
 * <p>This method is called when {@link #singleTokenDeletion} identifies
 * single-token deletion as a viable recovery strategy for a mismatched
 * input error.</p>
 *
 * <p>The default implementation simply returns if the handler is already in
 * error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
 * enter error recovery mode, followed by calling
 * {@link Parser#notifyErrorListeners}.</p>
 *
 * @param recognizer the parser instance
 */
protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String msg = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer.getVocabulary());
    recognizer.notifyErrorListeners(t, msg, null);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Aggregations

NotNull (org.antlr.v4.runtime.misc.NotNull)40 ATNState (org.antlr.v4.runtime.atn.ATNState)14 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)14 DFAState (org.antlr.v4.runtime.dfa.DFAState)12 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 ArrayList (java.util.ArrayList)7 Token (org.antlr.v4.runtime.Token)7 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)4 DFA (org.antlr.v4.runtime.dfa.DFA)4 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)4 List (java.util.List)3 ATN (org.antlr.v4.runtime.atn.ATN)3 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)3 Transition (org.antlr.v4.runtime.atn.Transition)3 WildcardTransition (org.antlr.v4.runtime.atn.WildcardTransition)3 Interval (org.antlr.v4.runtime.misc.Interval)3 Nullable (org.antlr.v4.runtime.misc.Nullable)3