Search in sources :

Example 1 with RuleStopState

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

the class ParserATNFactory method rule.

/* start->ruleblock->end */
@NotNull
@Override
public Handle rule(@NotNull GrammarAST ruleAST, @NotNull String name, @NotNull Handle blk) {
    Rule r = g.getRule(name);
    RuleStartState start = atn.ruleToStartState[r.index];
    epsilon(start, blk.left);
    RuleStopState stop = atn.ruleToStopState[r.index];
    epsilon(blk.right, stop);
    Handle h = new Handle(start, stop);
    // ATNPrinter ser = new ATNPrinter(g, h.left);
    // System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
    ruleAST.atnState = start;
    return h;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 2 with RuleStopState

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

the class ParserATNFactory method createRuleStartAndStopATNStates.

/**
 * Define all the rule begin/end ATNStates to solve forward reference
 *  issues.
 */
void createRuleStartAndStopATNStates() {
    atn.ruleToStartState = new RuleStartState[g.rules.size()];
    atn.ruleToStopState = new RuleStopState[g.rules.size()];
    for (Rule r : g.rules.values()) {
        RuleStartState start = newState(RuleStartState.class, r.ast);
        RuleStopState stop = newState(RuleStopState.class, r.ast);
        start.stopState = stop;
        start.isPrecedenceRule = r instanceof LeftRecursiveRule;
        start.setRuleIndex(r.index);
        stop.setRuleIndex(r.index);
        atn.ruleToStartState[r.index] = start;
        atn.ruleToStopState[r.index] = stop;
    }
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Example 3 with RuleStopState

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

the class LexerATNSimulator method addDFAState.

/**
 * 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.
 */
protected DFAState addDFAState(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(configs);
    ATNConfig firstConfigWithRuleStopState = null;
    for (ATNConfig c : configs) {
        if (c.state instanceof RuleStopState) {
            firstConfigWithRuleStopState = c;
            break;
        }
    }
    if (firstConfigWithRuleStopState != null) {
        proposed.isAcceptState = true;
        proposed.lexerActionExecutor = ((LexerATNConfig) firstConfigWithRuleStopState).getLexerActionExecutor();
        proposed.prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex];
    }
    DFA dfa = decisionToDFA[mode];
    synchronized (dfa.states) {
        DFAState existing = dfa.states.get(proposed);
        if (existing != null)
            return existing;
        DFAState newState = proposed;
        newState.stateNumber = dfa.states.size();
        configs.setReadonly(true);
        newState.configs = configs;
        dfa.states.put(newState, newState);
        return newState;
    }
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 4 with RuleStopState

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

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>
 *
 * 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:
 *
 *   "Given a specific partial input phrase, return the set of all tokens
 *    that can follow the last token in the input phrase."
 *
 * The big difference is that with just the input, the parser could
 * land right in the middle of a lookahead decision. Getting
 * all *possible* tokens given a partial input stream is a separate
 * computation. See https://github.com/antlr/antlr4/issues/1428
 *
 * 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.
 *
 * @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}
 */
public IntervalSet getExpectedTokens(int stateNumber, 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 : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) RuleContext(org.antlr.v4.runtime.RuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Example 5 with RuleStopState

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

the class LeftRecursionDetector method check.

/**
 * From state s, look for any transition to a rule that is currently
 *  being traced.  When tracing r, visitedPerRuleCheck has r
 *  initially.  If you reach a rule stop state, return but notify the
 *  invoking rule that the called rule is nullable. This implies that
 *  invoking rule must look at follow transition for that invoking state.
 *
 *  The visitedStates tracks visited states within a single rule so
 *  we can avoid epsilon-loop-induced infinite recursion here.  Keep
 *  filling the cycles in listOfRecursiveCycles and also, as a
 *  side-effect, set leftRecursiveRules.
 */
public boolean check(Rule enclosingRule, ATNState s, Set<ATNState> visitedStates) {
    if (s instanceof RuleStopState)
        return true;
    if (visitedStates.contains(s))
        return false;
    visitedStates.add(s);
    // System.out.println("visit "+s);
    int n = s.getNumberOfTransitions();
    boolean stateReachesStopState = false;
    for (int i = 0; i < n; i++) {
        Transition t = s.transition(i);
        if (t instanceof RuleTransition) {
            RuleTransition rt = (RuleTransition) t;
            Rule r = g.getRule(rt.ruleIndex);
            if (rulesVisitedPerRuleCheck.contains((RuleStartState) t.target)) {
                addRulesToCycle(enclosingRule, r);
            } else {
                // must visit if not already visited; mark target, pop when done
                rulesVisitedPerRuleCheck.add((RuleStartState) t.target);
                // send new visitedStates set per rule invocation
                boolean nullable = check(r, t.target, new HashSet<ATNState>());
                // we're back from visiting that rule
                rulesVisitedPerRuleCheck.remove((RuleStartState) t.target);
                if (nullable) {
                    stateReachesStopState |= check(enclosingRule, rt.followState, visitedStates);
                }
            }
        } else if (t.isEpsilon()) {
            stateReachesStopState |= check(enclosingRule, t.target, visitedStates);
        }
    // else ignore non-epsilon transitions
    }
    return stateReachesStopState;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) Transition(org.antlr.v4.runtime.atn.Transition) Rule(org.antlr.v4.tool.Rule) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)12 ATNState (org.antlr.v4.runtime.atn.ATNState)6 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)6 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)6 Transition (org.antlr.v4.runtime.atn.Transition)6 Rule (org.antlr.v4.tool.Rule)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)5 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)4 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)4 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)4 SetTransition (org.antlr.v4.runtime.atn.SetTransition)4 NotNull (org.antlr.v4.runtime.misc.NotNull)4 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)4 ArrayList (java.util.ArrayList)3 DFAState (org.antlr.v4.runtime.dfa.DFAState)3 InvalidClassException (java.io.InvalidClassException)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 UUID (java.util.UUID)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2