Search in sources :

Example 1 with PlusBlockAST

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

the class LeftFactoringRuleTransformer method expandOptionalQuantifiersForAlt.

protected GrammarAST expandOptionalQuantifiersForAlt(GrammarAST alt) {
    if (alt.getChildCount() == 0) {
        return null;
    }
    if (alt.getChild(0).getType() == ANTLRParser.OPTIONAL) {
        GrammarAST root = adaptor.nil();
        GrammarAST alt2 = alt.dupTree();
        alt2.deleteChild(0);
        if (alt2.getChildCount() == 0) {
            adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
        }
        alt.setChild(0, alt.getChild(0).getChild(0));
        if (alt.getChild(0).getType() == ANTLRParser.BLOCK && alt.getChild(0).getChildCount() == 1 && alt.getChild(0).getChild(0).getType() == ANTLRParser.ALT) {
            GrammarAST list = adaptor.nil();
            for (Object tree : ((GrammarAST) alt.getChild(0).getChild(0)).getChildren()) {
                adaptor.addChild(list, tree);
            }
            adaptor.replaceChildren(alt, 0, 0, list);
        }
        adaptor.addChild(root, alt);
        adaptor.addChild(root, alt2);
        return root;
    } else if (alt.getChild(0).getType() == ANTLRParser.CLOSURE) {
        GrammarAST root = adaptor.nil();
        GrammarAST alt2 = alt.dupTree();
        alt2.deleteChild(0);
        if (alt2.getChildCount() == 0) {
            adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
        }
        PlusBlockAST plusBlockAST = new PlusBlockAST(ANTLRParser.POSITIVE_CLOSURE, adaptor.createToken(ANTLRParser.POSITIVE_CLOSURE, "+"), null);
        for (int i = 0; i < alt.getChild(0).getChildCount(); i++) {
            plusBlockAST.addChild(alt.getChild(0).getChild(i));
        }
        alt.setChild(0, plusBlockAST);
        adaptor.addChild(root, alt);
        adaptor.addChild(root, alt2);
        return root;
    }
    return alt;
}
Also used : PlusBlockAST(org.antlr.v4.tool.ast.PlusBlockAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1 PlusBlockAST (org.antlr.v4.tool.ast.PlusBlockAST)1