Search in sources :

Example 36 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.

the class ATN method nextTokens.

/**
 * Compute the set of valid tokens that can occur starting in state {@code s}.
 *  If {@code ctx} is null, the set of tokens will not include what can follow
 *  the rule surrounding {@code s}. In other words, the set will be
 *  restricted to tokens reachable staying within {@code s}'s rule.
 */
public IntervalSet nextTokens(ATNState s, RuleContext ctx) {
    LL1Analyzer anal = new LL1Analyzer(this);
    IntervalSet next = anal.LOOK(s, ctx);
    return next;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Example 37 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.

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 null} 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}.
 */
public IntervalSet LOOK(ATNState s, ATNState stopState, RuleContext ctx) {
    IntervalSet r = new IntervalSet();
    // ignore preds; get all lookahead
    boolean seeThruPreds = true;
    PredictionContext lookContext = ctx != null ? PredictionContext.fromRuleContext(s.atn, ctx) : null;
    _LOOK(s, stopState, lookContext, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, true);
    return r;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet)

Example 38 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.

the class LL1Analyzer method getDecisionLookahead.

/**
 * Calculates the SLL(1) expected lookahead set for each outgoing transition
 * of an {@link ATNState}. The returned array has one element for each
 * outgoing transition in {@code s}. If the closure from transition
 * <em>i</em> leads to a semantic predicate before matching a symbol, the
 * element at index <em>i</em> of the result will be {@code null}.
 *
 * @param s the ATN state
 * @return the expected symbols for each outgoing transition of {@code s}.
 */
public IntervalSet[] getDecisionLookahead(ATNState s) {
    // System.out.println("LOOK("+s.stateNumber+")");
    if (s == null) {
        return null;
    }
    IntervalSet[] look = new IntervalSet[s.getNumberOfTransitions()];
    for (int alt = 0; alt < s.getNumberOfTransitions(); alt++) {
        look[alt] = new IntervalSet();
        Set<ATNConfig> lookBusy = new HashSet<ATNConfig>();
        // fail to get lookahead upon pred
        boolean seeThruPreds = false;
        _LOOK(s.transition(alt).target, null, PredictionContext.EMPTY, look[alt], lookBusy, new BitSet(), seeThruPreds, false);
        // or we had a predicate when we !seeThruPreds
        if (look[alt].size() == 0 || look[alt].contains(HIT_PRED)) {
            look[alt] = null;
        }
    }
    return look;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Example 39 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.

the class LexerATNFactory method range.

@Override
public Handle range(GrammarAST a, GrammarAST b) {
    ATNState left = newState(a);
    ATNState right = newState(b);
    int t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText());
    int t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText());
    checkRange(a, b, t1, t2);
    left.addTransition(CodePointTransitions.createWithCodePointRange(right, t1, t2));
    a.atnState = left;
    b.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 40 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.

the class LexerATNFactory method tokenRef.

@Override
public Handle tokenRef(TerminalAST node) {
    // Ref to EOF in lexer yields char transition on -1
    if (node.getText().equals("EOF")) {
        ATNState left = newState(node);
        ATNState right = newState(node);
        left.addTransition(new AtomTransition(right, IntStream.EOF));
        return new Handle(left, right);
    }
    return _ruleRef(node);
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)94 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)34 ATN (org.antlr.v4.runtime.atn.ATN)25 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)23 Rule (org.antlr.v4.tool.Rule)22 Transition (org.antlr.v4.runtime.atn.Transition)21 NotNull (org.antlr.v4.runtime.misc.NotNull)19 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)18 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)17 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)16 SetTransition (org.antlr.v4.runtime.atn.SetTransition)16 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)15 DecisionState (org.antlr.v4.runtime.atn.DecisionState)11 DOTGenerator (org.antlr.v4.tool.DOTGenerator)10 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)10 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)9 ArrayList (java.util.ArrayList)8 EpsilonTransition (org.antlr.v4.runtime.atn.EpsilonTransition)8 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)8