ParserATNFactory.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:codebuff 作者:
/**
 * From {@code (blk)*} build {@code ( blk+ )?} with *two* decisions, one for
 * entry and one for choosing alts of {@code blk}.
 *
 * <pre>
 *   |-------------|
 *   v             |
 *   o--[o-blk-o]-&gt;o  o
 *   |                ^
 *   -----------------|
 * </pre>
 *
 * Note that the optional bypass must jump outside the loop as
 * {@code (A|B)*} is not the same thing as {@code (A|B|)+}.
 */

@Override
public Handle star(GrammarAST starAST, Handle elem) {
    StarBlockStartState blkStart = (StarBlockStartState)elem.left;
    BlockEndState blkEnd = (BlockEndState)elem.right;
    preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));

    StarLoopEntryState entry = newState(StarLoopEntryState.class, starAST);
    entry.nonGreedy = !((QuantifierAST)starAST).isGreedy();
    atn.defineDecisionState(entry);
    LoopEndState end = newState(LoopEndState.class, starAST);
    StarLoopbackState loop = newState(StarLoopbackState.class, starAST);
    entry.loopBackState = loop;
    end.loopBackState = loop;

    BlockAST blkAST = (BlockAST)starAST.getChild(0);
    if ( ((QuantifierAST)starAST).isGreedy() ) {
        if (expectNonGreedy(blkAST)) {
            g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, starAST.getToken(), starAST.getToken().getText());
        }

        epsilon(entry, blkStart);   // loop enter edge (alt 1)
        epsilon(entry, end);        // bypass loop edge (alt 2)
    }
    else {
        // if not greedy, priority to exit branch; make it first
        epsilon(entry, end);        // bypass loop edge (alt 1)
        epsilon(entry, blkStart);   // loop enter edge (alt 2)
    }
    epsilon(blkEnd, loop);      // block end hits loop back
    epsilon(loop, entry);       // loop back to entry/exit decision

    starAST.atnState = entry;   // decision is to enter/exit; blk is its own decision
    return new Handle(entry, end);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号