Search in sources :

Example 21 with Nullable

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

the class LL1Analyzer method LOOK.

/**
 * Compute set of tokens that can follow {@code s} in the ATN in the
 * specified {@code ctx}.
 *
 * <p>If {@code ctx} is {@code null} and the end of the rule containing
 * {@code s} is reached, {@link Token#EPSILON} is added to the result set.
 * If {@code ctx} is not {@code PredictionContext#EMPTY_LOCAL} and the end of the outermost rule is
 * reached, {@link Token#EOF} is added to the result set.</p>
 *
 * @param s the ATN state
 * @param stopState the ATN state to stop at. This can be a
 * {@link BlockEndState} to detect epsilon paths through a closure.
 * @param ctx the complete parser context, or {@code null} if the context
 * should be ignored
 *
 * @return The set of tokens that can follow {@code s} in the ATN in the
 * specified {@code ctx}.
 */
@NotNull
public IntervalSet LOOK(@NotNull ATNState s, @Nullable ATNState stopState, @NotNull PredictionContext ctx) {
    IntervalSet r = new IntervalSet();
    // ignore preds; get all lookahead
    final boolean seeThruPreds = true;
    final boolean addEOF = true;
    _LOOK(s, stopState, ctx, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, addEOF);
    return r;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 22 with Nullable

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

the class ATN method getExpectedTokens.

/**
 * Computes the set of input symbols which could follow ATN state number
 * {@code stateNumber} in the specified full {@code context}. This method
 * considers the complete parser context, but does not evaluate semantic
 * predicates (i.e. all predicates encountered during the calculation are
 * assumed true). If a path in the ATN exists from the starting state to the
 * {@link RuleStopState} of the outermost context without matching any
 * symbols, {@link Token#EOF} is added to the returned set.
 *
 * <p>If {@code context} is {@code null}, it is treated as
 * {@link ParserRuleContext#EMPTY}.</p>
 *
 * <p>Note that this does NOT give you the set of all tokens that could
 * appear at a given token position in the input phrase.  In other words, it
 * does not answer:</p>
 *
 * <quote>"Given a specific partial input phrase, return the set of all
 * tokens that can follow the last token in the input phrase."</quote>
 *
 * <p>The big difference is that with just the input, the parser could land
 * right in the middle of a lookahead decision. Getting all
 * <em>possible</em> tokens given a partial input stream is a separate
 * computation. See https://github.com/antlr/antlr4/issues/1428</p>
 *
 * <p>For this function, we are specifying an ATN state and call stack to
 * compute what token(s) can come next and specifically: outside of a
 * lookahead decision. That is what you want for error reporting and
 * recovery upon parse error.</p>
 *
 * @param stateNumber the ATN state number
 * @param context the full parse context
 * @return The set of potentially valid input symbols which could follow the
 * specified state in the specified context.
 * @throws IllegalArgumentException if the ATN does not contain a state with
 * number {@code stateNumber}
 */
@NotNull
public IntervalSet getExpectedTokens(int stateNumber, @Nullable RuleContext context) {
    if (stateNumber < 0 || stateNumber >= states.size()) {
        throw new IllegalArgumentException("Invalid state number.");
    }
    RuleContext ctx = context;
    ATNState s = states.get(stateNumber);
    IntervalSet following = nextTokens(s);
    if (!following.contains(Token.EPSILON)) {
        return following;
    }
    IntervalSet expected = new IntervalSet();
    expected.addAll(following);
    expected.remove(Token.EPSILON);
    while (ctx != null && ctx.invokingState >= 0 && following.contains(Token.EPSILON)) {
        ATNState invokingState = states.get(ctx.invokingState);
        RuleTransition rt = (RuleTransition) invokingState.transition(0);
        following = nextTokens(rt.followState);
        expected.addAll(following);
        expected.remove(Token.EPSILON);
        ctx = ctx.parent;
    }
    if (following.contains(Token.EPSILON)) {
        expected.add(Token.EOF);
    }
    return expected;
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5 Nullable (org.antlr.v4.runtime.misc.Nullable)5 Token (org.antlr.v4.runtime.Token)4 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 ATNState (org.antlr.v4.runtime.atn.ATNState)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Nullable (javax.annotation.Nullable)2 ClassNode (kalang.ast.ClassNode)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 NotNull (org.antlr.v4.runtime.misc.NotNull)2 GraqlLexer (ai.grakn.graql.internal.antlr.GraqlLexer)1 GraqlParser (ai.grakn.graql.internal.antlr.GraqlParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Verify (com.google.common.base.Verify)1 Cache (com.google.common.cache.Cache)1 AbstractIterator (com.google.common.collect.AbstractIterator)1 ImmutableList (com.google.common.collect.ImmutableList)1