Search in sources :

Example 6 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)4 ArrayList (java.util.ArrayList)3 ATNState (org.antlr.v4.runtime.atn.ATNState)3 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 HashMap (java.util.HashMap)2 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)2 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)2 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)2 DecisionState (org.antlr.v4.runtime.atn.DecisionState)2 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)2 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)2 StarLoopEntryState (org.antlr.v4.runtime.atn.StarLoopEntryState)2 Transition (org.antlr.v4.runtime.atn.Transition)2 IntegerList (org.antlr.v4.runtime.misc.IntegerList)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 BlockAST (org.antlr.v4.tool.ast.BlockAST)2 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)2