Search in sources :

Example 6 with BlockAST

use of org.antlr.v4.tool.ast.BlockAST in project antlr4 by antlr.

the class ParserFactory method getChoiceBlock.

@Override
public Choice getChoiceBlock(BlockAST blkAST, List<CodeBlockForAlt> alts, GrammarAST labelAST) {
    int decision = ((DecisionState) blkAST.atnState).decision;
    Choice c;
    if (!g.tool.force_atn && AnalysisPipeline.disjoint(g.decisionLOOK.get(decision))) {
        c = getLL1ChoiceBlock(blkAST, alts);
    } else {
        c = getComplexChoiceBlock(blkAST, alts);
    }
    if (labelAST != null) {
        // for x=(...), define x or x_list
        String label = labelAST.getText();
        Decl d = getTokenLabelDecl(label);
        c.label = d;
        getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), d);
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            String listLabel = gen.getTarget().getListLabel(label);
            TokenListDecl l = new TokenListDecl(this, listLabel);
            getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), l);
        }
    }
    return c;
}
Also used : Choice(org.antlr.v4.codegen.model.Choice) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) DecisionState(org.antlr.v4.runtime.atn.DecisionState)

Example 7 with BlockAST

use of org.antlr.v4.tool.ast.BlockAST 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)

Example 8 with BlockAST

use of org.antlr.v4.tool.ast.BlockAST 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

BlockAST (org.antlr.v4.tool.ast.BlockAST)5 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)3 Rule (org.antlr.v4.tool.Rule)3 AltAST (org.antlr.v4.tool.ast.AltAST)3 ATNState (org.antlr.v4.runtime.atn.ATNState)2 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)2 PlusBlockStartState (org.antlr.v4.runtime.atn.PlusBlockStartState)2 StarBlockStartState (org.antlr.v4.runtime.atn.StarBlockStartState)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)2 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)2 ArrayList (java.util.ArrayList)1 CommonToken (org.antlr.runtime.CommonToken)1 Choice (org.antlr.v4.codegen.model.Choice)1 Decl (org.antlr.v4.codegen.model.decl.Decl)1 RuleContextDecl (org.antlr.v4.codegen.model.decl.RuleContextDecl)1 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)1 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)1 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)1 BasicBlockStartState (org.antlr.v4.runtime.atn.BasicBlockStartState)1