Search in sources :

Example 16 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator 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 17 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class DefaultOutputModelFactory method rulePostamble.

@Override
public List<SrcOp> rulePostamble(RuleFunction function, Rule r) {
    if (r.namedActions.containsKey("after") || r.namedActions.containsKey("finally")) {
        // See OutputModelController.buildLeftRecursiveRuleFunction
        // and Parser.exitRule for other places which set stop.
        CodeGenerator gen = getGenerator();
        STGroup codegenTemplates = gen.getTemplates();
        ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
        Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST);
        List<SrcOp> ops = new ArrayList<SrcOp>(1);
        ops.add(setStopTokenAction);
        return ops;
    }
    return super.rulePostamble(function, r);
}
Also used : ST(org.stringtemplate.v4.ST) Action(org.antlr.v4.codegen.model.Action) SrcOp(org.antlr.v4.codegen.model.SrcOp) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList)

Example 18 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class GoTarget method getVisitorFileName.

/** A given grammar T, return the visitor name such as
	 *  TVisitor.java, if we're using the Java target.
 	 */
public String getVisitorFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g.name != null;
    return g.name.toLowerCase() + "_visitor.go";
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Example 19 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class GoTarget method getListenerFileName.

/** A given grammar T, return the listener name such as
	 *  TListener.java, if we're using the Java target.
 	 */
public String getListenerFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g.name != null;
    return g.name.toLowerCase() + "_listener.go";
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Example 20 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class GoTarget method getRecognizerFileName.

public String getRecognizerFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g != null;
    String name;
    switch(g.getType()) {
        case ANTLRParser.PARSER:
            name = g.name.endsWith("Parser") ? g.name.substring(0, g.name.length() - 6) : g.name;
            return name.toLowerCase() + "_parser.go";
        case ANTLRParser.LEXER:
            // trim off "lexer"
            name = g.name.endsWith("Lexer") ? g.name.substring(0, g.name.length() - 5) : g.name;
            return name.toLowerCase() + "_lexer.go";
        case ANTLRParser.COMBINED:
            return g.name.toLowerCase() + "_parser.go";
        default:
            return "INVALID_FILE_NAME";
    }
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Aggregations

Grammar (org.antlr.v4.tool.Grammar)15 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)13 ST (org.stringtemplate.v4.ST)10 STGroup (org.stringtemplate.v4.STGroup)8 ATNFactory (org.antlr.v4.automata.ATNFactory)7 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)7 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)7 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 ArrayList (java.util.ArrayList)3 Action (org.antlr.v4.codegen.model.Action)3 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 SrcOp (org.antlr.v4.codegen.model.SrcOp)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 ActionAST (org.antlr.v4.tool.ast.ActionAST)2 PredAST (org.antlr.v4.tool.ast.PredAST)2