Search in sources :

Example 86 with GrammarAST

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

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

the class ParserATNFactory method wildcard.

/** Build an atom with all possible values in its label. */
@Override
public Handle wildcard(GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    left.addTransition(new WildcardTransition(right));
    node.atnState = left;
    return new Handle(left, right);
}
Also used : WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 88 with GrammarAST

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

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

the class CodeGenPipeline method process.

public void process() {
    if (!CodeGenerator.targetExists(g.getOptionString("language")))
        return;
    CodeGenerator gen = new CodeGenerator(g);
    IntervalSet idTypes = new IntervalSet();
    idTypes.add(ANTLRParser.ID);
    idTypes.add(ANTLRParser.RULE_REF);
    idTypes.add(ANTLRParser.TOKEN_REF);
    List<GrammarAST> idNodes = g.ast.getNodesWithType(idTypes);
    for (GrammarAST idNode : idNodes) {
        if (gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode)) {
            g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD, g.fileName, idNode.getToken(), idNode.getText());
        }
    }
    // all templates are generated in memory to report the most complete
    // error information possible, but actually writing output files stops
    // after the first error is reported
    int errorCount = g.tool.errMgr.getNumErrors();
    if (g.isLexer()) {
        if (gen.getTarget().needsHeader()) {
            // Header file if needed.
            ST lexer = gen.generateLexer(true);
            if (g.tool.errMgr.getNumErrors() == errorCount) {
                writeRecognizer(lexer, gen, true);
            }
        }
        ST lexer = gen.generateLexer(false);
        if (g.tool.errMgr.getNumErrors() == errorCount) {
            writeRecognizer(lexer, gen, false);
        }
    } else {
        if (gen.getTarget().needsHeader()) {
            ST parser = gen.generateParser(true);
            if (g.tool.errMgr.getNumErrors() == errorCount) {
                writeRecognizer(parser, gen, true);
            }
        }
        ST parser = gen.generateParser(false);
        if (g.tool.errMgr.getNumErrors() == errorCount) {
            writeRecognizer(parser, gen, false);
        }
        if (g.tool.gen_listener) {
            if (gen.getTarget().needsHeader()) {
                ST listener = gen.generateListener(true);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeListener(listener, true);
                }
            }
            ST listener = gen.generateListener(false);
            if (g.tool.errMgr.getNumErrors() == errorCount) {
                gen.writeListener(listener, false);
            }
            if (gen.getTarget().needsHeader()) {
                ST baseListener = gen.generateBaseListener(true);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeBaseListener(baseListener, true);
                }
            }
            if (gen.getTarget().wantsBaseListener()) {
                ST baseListener = gen.generateBaseListener(false);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeBaseListener(baseListener, false);
                }
            }
        }
        if (g.tool.gen_visitor) {
            if (gen.getTarget().needsHeader()) {
                ST visitor = gen.generateVisitor(true);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeVisitor(visitor, true);
                }
            }
            ST visitor = gen.generateVisitor(false);
            if (g.tool.errMgr.getNumErrors() == errorCount) {
                gen.writeVisitor(visitor, false);
            }
            if (gen.getTarget().needsHeader()) {
                ST baseVisitor = gen.generateBaseVisitor(true);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeBaseVisitor(baseVisitor, true);
                }
            }
            if (gen.getTarget().wantsBaseVisitor()) {
                ST baseVisitor = gen.generateBaseVisitor(false);
                if (g.tool.errMgr.getNumErrors() == errorCount) {
                    gen.writeBaseVisitor(baseVisitor, false);
                }
            }
        }
    }
    gen.writeVocabFile();
}
Also used : ST(org.stringtemplate.v4.ST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 90 with GrammarAST

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

the class RuleFunction method getDeclForAltElement.

public List<Decl> getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList, boolean optional) {
    List<Decl> decls = new ArrayList<Decl>();
    if (t.getType() == RULE_REF) {
        Rule rref = factory.getGrammar().getRule(t.getText());
        String ctxName = factory.getGenerator().getTarget().getRuleFunctionContextStructName(rref);
        if (needList) {
            if (factory.getGenerator().getTarget().supportsOverloadedMethods())
                decls.add(new ContextRuleListGetterDecl(factory, refLabelName, ctxName));
            decls.add(new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName));
        } else {
            decls.add(new ContextRuleGetterDecl(factory, refLabelName, ctxName, optional));
        }
    } else {
        if (needList) {
            if (factory.getGenerator().getTarget().supportsOverloadedMethods())
                decls.add(new ContextTokenListGetterDecl(factory, refLabelName));
            decls.add(new ContextTokenListIndexedGetterDecl(factory, refLabelName));
        } else {
            decls.add(new ContextTokenGetterDecl(factory, refLabelName, optional));
        }
    }
    return decls;
}
Also used : ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ArrayList(java.util.ArrayList) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) StructDecl(org.antlr.v4.codegen.model.decl.StructDecl) AttributeDecl(org.antlr.v4.codegen.model.decl.AttributeDecl) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) Rule(org.antlr.v4.tool.Rule) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5