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();
}
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);
}
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";
}
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";
}
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";
}
}
Aggregations