Search in sources :

Example 6 with BlockStartState

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

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?
	 */
@Override
public Handle block(BlockAST blkAST, GrammarAST ebnfRoot, 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)

Example 7 with BlockStartState

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

the class ParserATNFactory method optional.

/**
	 * From {@code (A)?} build either:
	 *
	 * <pre>
	 *  o--A-&gt;o
	 *  |     ^
	 *  o----&gt;|
	 * </pre>
	 *
	 * or, if {@code A} is a block, just add an empty alt to the end of the
	 * block
	 */
@Override
public Handle optional(GrammarAST optAST, Handle blk) {
    BlockStartState blkStart = (BlockStartState) blk.left;
    ATNState blkEnd = blk.right;
    preventEpsilonOptionalBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
    boolean greedy = ((QuantifierAST) optAST).isGreedy();
    blkStart.nonGreedy = !greedy;
    epsilon(blkStart, blk.right, !greedy);
    optAST.atnState = blk.left;
    return blk;
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) 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) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 8 with BlockStartState

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

the class ParserATNFactory method makeBlock.

protected Handle makeBlock(BlockStartState start, BlockAST blkAST, List<Handle> alts) {
    BlockEndState end = newState(BlockEndState.class, blkAST);
    start.endState = end;
    for (Handle alt : alts) {
        // hook alts up to decision block
        epsilon(start, alt.left);
        epsilon(alt.right, end);
        // no back link in ATN so must walk entire alt to see if we can
        // strip out the epsilon to 'end' state
        TailEpsilonRemover opt = new TailEpsilonRemover(atn);
        opt.visit(alt.left);
    }
    Handle h = new Handle(start, end);
    //		FASerializer ser = new FASerializer(g, h.left);
    //		System.out.println(blkAST.toStringTree()+":\n"+ser);
    blkAST.atnState = start;
    return h;
}
Also used : BlockEndState(org.antlr.v4.runtime.atn.BlockEndState)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)4 BlockStartState (org.antlr.v4.runtime.atn.BlockStartState)4 ArrayList (java.util.ArrayList)3 PlusBlockStartState (org.antlr.v4.runtime.atn.PlusBlockStartState)3 IntegerList (org.antlr.v4.runtime.misc.IntegerList)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 BasicBlockStartState (org.antlr.v4.runtime.atn.BasicBlockStartState)2 RangeTransition (org.antlr.v4.runtime.atn.RangeTransition)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)2 SetTransition (org.antlr.v4.runtime.atn.SetTransition)2 StarBlockStartState (org.antlr.v4.runtime.atn.StarBlockStartState)2 Transition (org.antlr.v4.runtime.atn.Transition)2 Rule (org.antlr.v4.tool.Rule)2 InvalidClassException (java.io.InvalidClassException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1