Search in sources :

Example 96 with ATNState

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

the class ParserATNFactory method epsilon.

/**
 * From an empty alternative build {@code o-e->o}.
 */
@NotNull
@Override
public Handle epsilon(@NotNull GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    epsilon(left, right);
    node.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 97 with ATNState

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

the class ParserATNFactory method epsilon.

protected void epsilon(ATNState a, @NotNull ATNState b, boolean prepend) {
    for (Transition t : a.getTransitions()) {
        if (t.getSerializationType() != Transition.EPSILON) {
            continue;
        }
        if (t.target == b && ((EpsilonTransition) t).outermostPrecedenceReturn() == -1) {
            // This transition was already added
            return;
        }
    }
    if (a != null) {
        int index = prepend ? 0 : a.getNumberOfTransitions();
        a.addTransition(index, new EpsilonTransition(b));
    }
}
Also used : NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition)

Example 98 with ATNState

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

the class ParserATNFactory method addEOFTransitionToStartRules.

/**
 * Add an EOF transition to any rule end ATNState that points to nothing
 *  (i.e., for all those rules not invoked by another rule).  These
 *  are start symbols then.
 *
 *  Return the number of grammar entry points; i.e., how many rules are
 *  not invoked by another rule (they can only be invoked from outside).
 *  These are the start rules.
 */
public int addEOFTransitionToStartRules() {
    int n = 0;
    // one unique EOF target for all rules
    ATNState eofTarget = newState(null);
    for (Rule r : g.rules.values()) {
        ATNState stop = atn.ruleToStopState[r.index];
        if (stop.getNumberOfTransitions() > 0)
            continue;
        n++;
        Transition t = new AtomTransition(eofTarget, Token.EOF);
        stop.addTransition(t);
    }
    return n;
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 99 with ATNState

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

the class ParserATNFactory method createATN.

@NotNull
@Override
public ATN createATN() {
    _createATN(g.rules.values());
    assert atn.maxTokenType == g.getMaxTokenType();
    addRuleFollowLinks();
    addEOFTransitionToStartRules();
    ATNOptimizer.optimize(g, atn);
    for (Tuple3<? extends Rule, ? extends ATNState, ? extends ATNState> pair : preventEpsilonClosureBlocks) {
        LL1Analyzer analyzer = new LL1Analyzer(atn);
        ATNState blkStart = pair.getItem2();
        ATNState blkStop = pair.getItem3();
        IntervalSet lookahead = analyzer.LOOK(blkStart, blkStop, PredictionContext.EMPTY_LOCAL);
        if (lookahead.contains(org.antlr.v4.runtime.Token.EPSILON)) {
            ErrorType errorType = pair.getItem1() instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
            g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST) pair.getItem1().ast.getChild(0)).getToken(), pair.getItem1().name);
        }
    }
    optionalCheck: for (Tuple3<? extends Rule, ? extends ATNState, ? extends ATNState> pair : preventEpsilonOptionalBlocks) {
        int bypassCount = 0;
        for (int i = 0; i < pair.getItem2().getNumberOfTransitions(); i++) {
            ATNState startState = pair.getItem2().transition(i).target;
            if (startState == pair.getItem3()) {
                bypassCount++;
                continue;
            }
            LL1Analyzer analyzer = new LL1Analyzer(atn);
            if (analyzer.LOOK(startState, pair.getItem3(), PredictionContext.EMPTY_LOCAL).contains(org.antlr.v4.runtime.Token.EPSILON)) {
                g.tool.errMgr.grammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST) pair.getItem1().ast.getChild(0)).getToken(), pair.getItem1().name);
                continue optionalCheck;
            }
        }
        if (bypassCount != 1) {
            throw new UnsupportedOperationException("Expected optional block with exactly 1 bypass alternative.");
        }
    }
    return atn;
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LL1Analyzer(org.antlr.v4.runtime.atn.LL1Analyzer) ErrorType(org.antlr.v4.tool.ErrorType) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Tuple3(org.antlr.v4.runtime.misc.Tuple3) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 100 with ATNState

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

the class ParserATNFactory method block.

/**
 * From {@code A|B|..|Z} alternative block build
 *
 * <pre>
 *  o-&gt;o-A-&gt;o-&gt;o (last ATNState is BlockEndState pointed to by all alts)
 *  |          ^
 *  |-&gt;o-B-&gt;o--|
 *  |          |
 *  ...        |
 *  |          |
 *  |-&gt;o-Z-&gt;o--|
 * </pre>
 *
 * So start node points at every alternative with epsilon transition and
 * every alt right side points at a block end ATNState.
 * <p>
 * Special case: only one alternative: don't make a block with alt
 * begin/end.
 * <p>
 * Special case: if just a list of tokens/chars/sets, then collapse to a
 * single edged o-set-&gt;o graph.
 * <p>
 * TODO: Set alt number (1..n) in the states?
 */
@NotNull
@Override
public Handle block(@NotNull BlockAST blkAST, @NotNull GrammarAST ebnfRoot, @NotNull List<Handle> alts) {
    if (ebnfRoot == null) {
        if (alts.size() == 1) {
            Handle h = alts.get(0);
            blkAST.atnState = h.left;
            return h;
        }
        BlockStartState start = newState(BasicBlockStartState.class, blkAST);
        if (alts.size() > 1)
            atn.defineDecisionState(start);
        return makeBlock(start, blkAST, alts);
    }
    switch(ebnfRoot.getType()) {
        case ANTLRParser.OPTIONAL:
            BlockStartState start = newState(BasicBlockStartState.class, blkAST);
            atn.defineDecisionState(start);
            Handle h = makeBlock(start, blkAST, alts);
            return optional(ebnfRoot, h);
        case ANTLRParser.CLOSURE:
            BlockStartState star = newState(StarBlockStartState.class, ebnfRoot);
            if (alts.size() > 1)
                atn.defineDecisionState(star);
            h = makeBlock(star, blkAST, alts);
            return star(ebnfRoot, h);
        case ANTLRParser.POSITIVE_CLOSURE:
            PlusBlockStartState plus = newState(PlusBlockStartState.class, ebnfRoot);
            if (alts.size() > 1)
                atn.defineDecisionState(plus);
            h = makeBlock(plus, blkAST, alts);
            return plus(ebnfRoot, h);
    }
    return null;
}
Also used : PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) StarBlockStartState(org.antlr.v4.runtime.atn.StarBlockStartState) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) NotNull(org.antlr.v4.runtime.misc.NotNull)

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