Search in sources :

Example 11 with LoopEndState

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

the class ParserATNFactory method plus.

/**
 * From {@code (blk)+} build
 *
 * <pre>
 *   |---------|
 *   v         |
 *  [o-blk-o]-&gt;o-&gt;o
 * </pre>
 *
 * We add a decision for loop back node to the existing one at {@code blk}
 * start.
 */
@Override
public Handle plus(GrammarAST plusAST, Handle blk) {
    PlusBlockStartState blkStart = (PlusBlockStartState) blk.left;
    BlockEndState blkEnd = (BlockEndState) blk.right;
    preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
    PlusLoopbackState loop = newState(PlusLoopbackState.class, plusAST);
    loop.nonGreedy = !((QuantifierAST) plusAST).isGreedy();
    atn.defineDecisionState(loop);
    LoopEndState end = newState(LoopEndState.class, plusAST);
    blkStart.loopBackState = loop;
    end.loopBackState = loop;
    plusAST.atnState = loop;
    // blk can see loop back
    epsilon(blkEnd, loop);
    BlockAST blkAST = (BlockAST) plusAST.getChild(0);
    if (((QuantifierAST) plusAST).isGreedy()) {
        if (expectNonGreedy(blkAST)) {
            g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.getToken(), plusAST.getToken().getText());
        }
        // loop back to start
        epsilon(loop, blkStart);
        // or exit
        epsilon(loop, end);
    } else {
        // if not greedy, priority to exit branch; make it first
        // exit
        epsilon(loop, end);
        // loop back to start
        epsilon(loop, blkStart);
    }
    return new Handle(blkStart, end);
}
Also used : LoopEndState(org.antlr.v4.runtime.atn.LoopEndState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) PlusLoopbackState(org.antlr.v4.runtime.atn.PlusLoopbackState) BlockAST(org.antlr.v4.tool.ast.BlockAST) QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) BlockEndState(org.antlr.v4.runtime.atn.BlockEndState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)7 ArrayList (java.util.ArrayList)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)5 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)4 BlockAST (org.antlr.v4.tool.ast.BlockAST)4 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)4 HashMap (java.util.HashMap)3 ATNState (org.antlr.v4.runtime.atn.ATNState)3 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)3 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)3 DecisionState (org.antlr.v4.runtime.atn.DecisionState)3 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)3 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)3 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)3 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)3 StarLoopEntryState (org.antlr.v4.runtime.atn.StarLoopEntryState)3 Transition (org.antlr.v4.runtime.atn.Transition)3 IntegerList (org.antlr.v4.runtime.misc.IntegerList)3 InvalidClassException (java.io.InvalidClassException)2 LinkedHashMap (java.util.LinkedHashMap)2