Use accessor methods for CodeGenerator.templates and CodeGenerator.target

This commit is contained in:
Sam Harwell 2013-02-23 16:51:26 -06:00
parent b372d4d88d
commit 371bcc83b2
24 changed files with 78 additions and 70 deletions

View File

@ -106,7 +106,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
// use codegen to get correct language templates; that's it though
CodeGenerator gen = new CodeGenerator(tool, null, language);
codegenTemplates = gen.templates;
codegenTemplates = gen.getTemplates();
}
@Override

View File

@ -66,7 +66,7 @@ public class LexerATNFactory extends ParserATNFactory {
// use codegen to get correct language templates for lexer commands
String language = g.getOptionString("language");
CodeGenerator gen = new CodeGenerator(g.tool, null, language);
codegenTemplates = gen.templates;
codegenTemplates = gen.getTemplates();
}
@Override

View File

@ -316,12 +316,12 @@ public class ActionTranslator implements ActionSplitterListener {
public String getTokenLabel(String x) {
if ( node.resolver.resolvesToLabel(x, node) ) return x;
return factory.getGenerator().target.getImplicitTokenLabel(x);
return factory.getGenerator().getTarget().getImplicitTokenLabel(x);
}
public String getRuleLabel(String x) {
if ( node.resolver.resolvesToLabel(x, node) ) return x;
return factory.getGenerator().target.getImplicitRuleLabel(x);
return factory.getGenerator().getTarget().getImplicitRuleLabel(x);
}
}

View File

@ -56,14 +56,14 @@ public class CodeGenPipeline {
idTypes.add(ANTLRParser.TOKEN_REF);
List<GrammarAST> idNodes = g.ast.getNodesWithType(idTypes);
for (GrammarAST idNode : idNodes) {
if ( gen.target.grammarSymbolCausesIssueInGeneratedCode(idNode) ) {
if ( gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode) ) {
g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD,
g.fileName, idNode.getToken(),
idNode.getText());
}
}
if ( gen.templates==null ) return;
if ( gen.getTemplates()==null ) return;
if ( g.isLexer() ) {
ST lexer = gen.generateLexer();

View File

@ -67,8 +67,8 @@ public class CodeGenerator {
@NotNull
public final Tool tool;
public Target target;
public STGroup templates;
private Target target;
private STGroup templates;
public int lineWidth = 72;
@ -83,6 +83,14 @@ public class CodeGenerator {
loadTemplates(language);
}
public Target getTarget() {
return target;
}
public STGroup getTemplates() {
return templates;
}
void loadLanguageTarget(String language) {
String targetName = "org.antlr.v4.codegen."+language+"Target";
try {
@ -161,7 +169,7 @@ public class CodeGenerator {
}
private ST walk(OutputModelObject outputModel) {
OutputModelWalker walker = new OutputModelWalker(tool, templates);
OutputModelWalker walker = new OutputModelWalker(tool, getTemplates());
return walker.walk(outputModel);
}
@ -205,33 +213,33 @@ public class CodeGenerator {
}
public void writeRecognizer(ST outputFileST) {
target.genFile(g, outputFileST, getRecognizerFileName());
getTarget().genFile(g, outputFileST, getRecognizerFileName());
}
public void writeListener(ST outputFileST) {
target.genFile(g,outputFileST, getListenerFileName());
getTarget().genFile(g,outputFileST, getListenerFileName());
}
public void writeBaseListener(ST outputFileST) {
target.genFile(g,outputFileST, getBaseListenerFileName());
getTarget().genFile(g,outputFileST, getBaseListenerFileName());
}
public void writeVisitor(ST outputFileST) {
target.genFile(g,outputFileST, getVisitorFileName());
getTarget().genFile(g,outputFileST, getVisitorFileName());
}
public void writeBaseVisitor(ST outputFileST) {
target.genFile(g,outputFileST, getBaseVisitorFileName());
getTarget().genFile(g,outputFileST, getBaseVisitorFileName());
}
public void writeHeaderFile() {
String fileName = getHeaderFileName();
if ( fileName==null ) return;
if ( templates.isDefined("headerFile") ) {
ST extST = templates.getInstanceOf("headerFileExtension");
if ( getTemplates().isDefined("headerFile") ) {
ST extST = getTemplates().getInstanceOf("headerFileExtension");
ST headerFileST = null;
// TODO: don't hide this header file generation here!
target.genRecognizerHeaderFile(g,headerFileST,extST.render(lineWidth));
getTarget().genRecognizerHeaderFile(g,headerFileST,extST.render(lineWidth));
}
}
@ -241,7 +249,7 @@ public class CodeGenerator {
ST tokenVocabSerialization = getTokenVocabOutput();
String fileName = getVocabFileName();
if ( fileName!=null ) {
target.genFile(g, tokenVocabSerialization, fileName);
getTarget().genFile(g, tokenVocabSerialization, fileName);
}
}
@ -266,7 +274,7 @@ public class CodeGenerator {
* just use T.java as output regardless of type.
*/
public String getRecognizerFileName() {
ST extST = templates.getInstanceOf("codeFileExtension");
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String recognizerName = g.getRecognizerName();
return recognizerName+extST.render();
}
@ -276,7 +284,7 @@ public class CodeGenerator {
*/
public String getListenerFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = g.name + "Listener";
return listenerName+extST.render();
}
@ -286,7 +294,7 @@ public class CodeGenerator {
*/
public String getVisitorFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = g.name + "Visitor";
return listenerName+extST.render();
}
@ -296,7 +304,7 @@ public class CodeGenerator {
*/
public String getBaseListenerFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = g.name + "BaseListener";
return listenerName+extST.render();
}
@ -306,7 +314,7 @@ public class CodeGenerator {
*/
public String getBaseVisitorFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = g.name + "BaseVisitor";
return listenerName+extST.render();
}
@ -319,7 +327,7 @@ public class CodeGenerator {
}
public String getHeaderFileName() {
ST extST = templates.getInstanceOf("headerFileExtension");
ST extST = getTemplates().getInstanceOf("headerFileExtension");
if ( extST==null ) return null;
String recognizerName = g.getRecognizerName();
return recognizerName+extST.render();

View File

@ -213,7 +213,7 @@ public class OutputModelController {
// now inject code to start alts
CodeGenerator gen = delegate.getGenerator();
STGroup codegenTemplates = gen.templates;
STGroup codegenTemplates = gen.getTemplates();
// pick out alt(s) for primaries
CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt)function.code.get(0);
@ -300,7 +300,7 @@ public class OutputModelController {
e.printStackTrace(System.err);
}
function.ctxType = gen.target.getRuleFunctionContextStructName(function);
function.ctxType = gen.getTarget().getRuleFunctionContextStructName(function);
function.postamble = rulePostamble(function, r);
}
@ -312,7 +312,7 @@ public class OutputModelController {
CodeGenerator gen = delegate.getGenerator();
Grammar g = delegate.getGrammar();
String ctxType = gen.target.getRuleFunctionContextStructName(r);
String ctxType = gen.getTarget().getRuleFunctionContextStructName(r);
RuleActionFunction raf = lexer.actionFuncs.get(r);
if ( raf==null ) {
raf = new RuleActionFunction(delegate, r, ctxType);

View File

@ -171,7 +171,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
}
public TokenListDecl getTokenListLabelDecl(String label) {
return new TokenListDecl(this, gen.target.getListLabel(label));
return new TokenListDecl(this, gen.getTarget().getListLabel(label));
}
@Override
@ -230,7 +230,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
c.label = d;
getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), d);
if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
String listLabel = gen.target.getListLabel(label);
String listLabel = gen.getTarget().getListLabel(label);
TokenListDecl l = new TokenListDecl(this, listLabel);
getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), l);
}
@ -331,20 +331,20 @@ public class ParserFactory extends DefaultOutputModelFactory {
Decl d;
if ( ast.getType()==ANTLRParser.SET || ast.getType()==ANTLRParser.WILDCARD ) {
String implLabel =
gen.target.getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
gen.getTarget().getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
d = getTokenLabelDecl(implLabel);
((TokenDecl)d).isImplicit = true;
}
else if ( ast.getType()==ANTLRParser.RULE_REF ) { // a rule reference?
Rule r = g.getRule(ast.getText());
String implLabel = gen.target.getImplicitRuleLabel(ast.getText());
String implLabel = gen.getTarget().getImplicitRuleLabel(ast.getText());
String ctxName =
gen.target.getRuleFunctionContextStructName(r);
gen.getTarget().getRuleFunctionContextStructName(r);
d = new RuleContextDecl(this, implLabel, ctxName);
((RuleContextDecl)d).isImplicit = true;
}
else {
String implLabel = gen.target.getImplicitTokenLabel(ast.getText());
String implLabel = gen.getTarget().getImplicitTokenLabel(ast.getText());
d = getTokenLabelDecl(implLabel);
((TokenDecl)d).isImplicit = true;
}
@ -356,7 +356,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
public AddToLabelList getAddToListOpIfListLabelPresent(LabeledOp op, GrammarAST label) {
AddToLabelList labelOp = null;
if ( label!=null && label.parent.getType()==ANTLRParser.PLUS_ASSIGN ) {
String listLabel = gen.target.getListLabel(label.getText());
String listLabel = gen.getTarget().getListLabel(label.getText());
labelOp = new AddToLabelList(this, listLabel, op.getLabels().get(0));
}
return labelOp;

View File

@ -350,20 +350,20 @@ public class Target {
}
public String getListLabel(String label) {
ST st = gen.templates.getInstanceOf("ListLabelName");
ST st = gen.getTemplates().getInstanceOf("ListLabelName");
st.add("label", label);
return st.render();
}
public String getRuleFunctionContextStructName(Rule r) {
if ( r.g.isLexer() ) {
return gen.templates.getInstanceOf("LexerRuleContext").render();
return gen.getTemplates().getInstanceOf("LexerRuleContext").render();
}
return Utils.capitalize(r.name)+gen.templates.getInstanceOf("RuleContextNameSuffix").render();
return Utils.capitalize(r.name)+gen.getTemplates().getInstanceOf("RuleContextNameSuffix").render();
}
public String getAltLabelContextStructName(String label) {
return Utils.capitalize(label)+gen.templates.getInstanceOf("RuleContextNameSuffix").render();
return Utils.capitalize(label)+gen.getTemplates().getInstanceOf("RuleContextNameSuffix").render();
}
/** If we know which actual function, we can provide the actual ctx type.
@ -374,15 +374,15 @@ public class Target {
public String getRuleFunctionContextStructName(RuleFunction function) {
Rule r = function.rule;
if ( r.g.isLexer() ) {
return gen.templates.getInstanceOf("LexerRuleContext").render();
return gen.getTemplates().getInstanceOf("LexerRuleContext").render();
}
return Utils.capitalize(r.name)+gen.templates.getInstanceOf("RuleContextNameSuffix").render();
return Utils.capitalize(r.name)+gen.getTemplates().getInstanceOf("RuleContextNameSuffix").render();
}
// should be same for all refs to same token like ctx.ID within single rule function
// for literals like 'while', we gen _s<ttype>
public String getImplicitTokenLabel(String tokenName) {
ST st = gen.templates.getInstanceOf("ImplicitTokenLabel");
ST st = gen.getTemplates().getInstanceOf("ImplicitTokenLabel");
int ttype = gen.g.getTokenType(tokenName);
if ( tokenName.startsWith("'") ) {
return "s"+ttype;
@ -394,19 +394,19 @@ public class Target {
// x=(A|B)
public String getImplicitSetLabel(String id) {
ST st = gen.templates.getInstanceOf("ImplicitSetLabel");
ST st = gen.getTemplates().getInstanceOf("ImplicitSetLabel");
st.add("id", id);
return st.render();
}
public String getImplicitRuleLabel(String ruleName) {
ST st = gen.templates.getInstanceOf("ImplicitRuleLabel");
ST st = gen.getTemplates().getInstanceOf("ImplicitRuleLabel");
st.add("ruleName", ruleName);
return st.render();
}
public String getElementListName(String name) {
ST st = gen.templates.getInstanceOf("ElementListName");
ST st = gen.getTemplates().getInstanceOf("ElementListName");
st.add("elemName", getElementName(name));
return st.render();
}

View File

@ -73,7 +73,7 @@ public abstract class Choice extends RuleElement {
List<String[]> altLook = new ArrayList<String[]>();
for (int a=1; a<altLookSets.length; a++) {
IntervalSet s = altLookSets[a];
altLook.add(factory.getGenerator().target.getTokenTypesAsTargetLabels(factory.getGrammar(), s.toArray()));
altLook.add(factory.getGenerator().getTarget().getTokenTypesAsTargetLabels(factory.getGrammar(), s.toArray()));
}
return altLook;
}

View File

@ -64,7 +64,7 @@ public class InvokeRule extends RuleElement implements LabeledOp {
this.name = ast.getText();
CodeGenerator gen = factory.getGenerator();
Rule r = factory.getGrammar().getRule(name);
ctxName = gen.target.getRuleFunctionContextStructName(r);
ctxName = gen.getTarget().getRuleFunctionContextStructName(r);
// TODO: move to factory
RuleFunction rf = factory.getCurrentRuleFunction();
@ -73,7 +73,7 @@ public class InvokeRule extends RuleElement implements LabeledOp {
String label = labelAST.getText();
if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
factory.defineImplicitLabel(ast, this);
String listLabel = gen.target.getListLabel(label);
String listLabel = gen.getTarget().getListLabel(label);
RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName);
rf.addContextDecl(ast.getAltLabel(), l);
}
@ -90,7 +90,7 @@ public class InvokeRule extends RuleElement implements LabeledOp {
// If action refs rule as rulename not label, we need to define implicit label
if ( factory.getCurrentOuterMostAlt().ruleRefsInActions.containsKey(ast.getText()) ) {
String label = gen.target.getImplicitRuleLabel(ast.getText());
String label = gen.getTarget().getImplicitRuleLabel(ast.getText());
RuleContextDecl d = new RuleContextDecl(factory,label,ctxName);
labels.add(d);
rf.addContextDecl(ast.getAltLabel(), d);

View File

@ -71,11 +71,11 @@ public class LL1PlusBlock extends LL1Loop {
loopExpr = addCodeForLoopLookaheadTempVar(all);
loopLabel = gen.target.getLoopLabel(plusRoot);
loopCounterVar = gen.target.getLoopCounter(plusRoot);
loopLabel = gen.getTarget().getLoopLabel(plusRoot);
loopCounterVar = gen.getTarget().getLoopCounter(plusRoot);
IntervalSet exitLookSet = altLookSets[altLookSets.length-1];
this.exitLook = gen.target.getTokenTypesAsTargetLabels(g,
this.exitLook = gen.getTarget().getTokenTypesAsTargetLabels(g,
exitLookSet.toArray());
}
}

View File

@ -62,9 +62,9 @@ public class LL1StarBlock extends LL1Loop {
System.arraycopy(altLookSets, 0, copy, 0, altLookSets.length-1); // remove last (exit) alt
altLookSets = copy;
altLook = getAltLookaheadAsStringLists(altLookSets);
loopLabel = factory.getGenerator().target.getLoopLabel(starRootAST);
loopLabel = factory.getGenerator().getTarget().getLoopLabel(starRootAST);
this.exitLook =
factory.getGenerator().target.getTokenTypesAsTargetLabels(factory.getGrammar(), lastLook.toArray());
factory.getGenerator().getTarget().getTokenTypesAsTargetLabels(factory.getGrammar(), lastLook.toArray());
}
}

View File

@ -54,7 +54,7 @@ public class LeftRecursiveRuleFunction extends RuleFunction {
GrammarAST rrefAST = (GrammarAST)idAST.getParent().getChild(1);
if ( rrefAST.getType() == ANTLRParser.RULE_REF ) {
Rule targetRule = factory.getGrammar().getRule(rrefAST.getText());
String ctxName = gen.target.getRuleFunctionContextStructName(targetRule);
String ctxName = gen.getTarget().getRuleFunctionContextStructName(targetRule);
RuleContextDecl d = new RuleContextDecl(factory,label,ctxName);
StructDecl struct = ruleCtx;
if ( altLabelCtxs!=null ) {

View File

@ -84,13 +84,13 @@ public class Lexer extends OutputModelObject {
if ( tokenNames[i].charAt(0)=='\'' ) {
boolean addQuotes = false;
tokenNames[i] =
gen.target.getTargetStringLiteralFromANTLRStringLiteral(gen,
gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen,
tokenNames[i],
addQuotes);
tokenNames[i] = "\"'"+tokenNames[i]+"'\"";
}
else {
tokenNames[i] = gen.target.getTargetStringLiteralFromString(tokenNames[i], true);
tokenNames[i] = gen.getTarget().getTargetStringLiteralFromString(tokenNames[i], true);
}
}
ruleNames = g.rules.keySet();

View File

@ -51,7 +51,7 @@ public class MatchToken extends RuleElement implements LabeledOp {
Grammar g = factory.getGrammar();
CodeGenerator gen = factory.getGenerator();
ttype = g.getTokenType(ast.getText());
name = gen.target.getTokenTypeAsTargetLabel(g, ttype);
name = gen.getTarget().getTokenTypeAsTargetLabel(g, ttype);
}
public MatchToken(OutputModelFactory factory, GrammarAST ast) {

View File

@ -82,13 +82,13 @@ public class Parser extends OutputModelObject {
if ( tokenNames[i].charAt(0)=='\'' ) {
boolean addQuotes = false;
tokenNames[i] =
gen.target.getTargetStringLiteralFromANTLRStringLiteral(gen,
gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen,
tokenNames[i],
addQuotes);
tokenNames[i] = "\"'"+tokenNames[i]+"'\"";
}
else {
tokenNames[i] = gen.target.getTargetStringLiteralFromString(tokenNames[i], true);
tokenNames[i] = gen.getTarget().getTargetStringLiteralFromString(tokenNames[i], true);
}
}
ruleNames = g.rules.keySet();

View File

@ -249,7 +249,7 @@ public class RuleFunction extends OutputModelObject {
List<Decl> decls = new ArrayList<Decl>();
if ( t.getType()==RULE_REF ) {
Rule rref = factory.getGrammar().getRule(t.getText());
String ctxName = factory.getGenerator().target
String ctxName = factory.getGenerator().getTarget()
.getRuleFunctionContextStructName(rref);
if ( needList ) {
decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );

View File

@ -55,7 +55,7 @@ public class SemPred extends Action {
if (predicate.startsWith("{") && predicate.endsWith("}?")) {
predicate = predicate.substring(1, predicate.length() - 2);
}
predicate = gen.target.getTargetStringLiteralFromString(predicate);
predicate = gen.getTarget().getTargetStringLiteralFromString(predicate);
if ( failNode==null ) return;
@ -67,7 +67,7 @@ public class SemPred extends Action {
failActionNode);
}
else {
msg = gen.target.getTargetStringLiteralFromANTLRStringLiteral(gen,
msg = gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen,
failNode.getText(),
true);
}

View File

@ -46,7 +46,7 @@ public class SerializedATN extends OutputModelObject {
IntegerList data = ATNSerializer.getSerialized(factory.getGrammar(), atn);
serialized = new ArrayList<String>(data.size());
for (int c : data.toArray()) {
String encoded = factory.getGenerator().target.encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c);
String encoded = factory.getGenerator().getTarget().encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c);
serialized.add(encoded);
}
// System.out.println(ATNSerializer.getDecoded(factory.getGrammar(), atn));

View File

@ -44,7 +44,7 @@ public class StarBlock extends Loop {
List<CodeBlockForAlt> alts)
{
super(factory, blkOrEbnfRootAST, alts);
loopLabel = factory.getGenerator().target.getLoopLabel(blkOrEbnfRootAST);
loopLabel = factory.getGenerator().getTarget().getLoopLabel(blkOrEbnfRootAST);
StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState;
loopBackStateNumber = star.loopBackState.stateNumber;
decision = star.decision;

View File

@ -67,7 +67,7 @@ public class TestSetInline extends SrcOp {
bitsetList.add(current);
}
current.ttypes.add(factory.getGenerator().target.getTokenTypeAsTargetLabel(factory.getGrammar(), ttype));
current.ttypes.add(factory.getGenerator().getTarget().getTokenTypeAsTargetLabel(factory.getGrammar(), ttype));
}
return bitsetList.toArray(new Bitset[bitsetList.size()]);

View File

@ -47,7 +47,7 @@ public class AltLabelStructDecl extends StructDecl {
super(factory, r);
this.altNum = altNum;
this.name = // override name set in super to the label ctx
factory.getGenerator().target.getAltLabelContextStructName(label);
factory.getGenerator().getTarget().getAltLabelContextStructName(label);
derivedFromName = label;
}

View File

@ -58,7 +58,7 @@ public class StructDecl extends Decl {
@ModelElement public List<OutputModelObject> extensionMembers;
public StructDecl(OutputModelFactory factory, Rule r) {
super(factory, factory.getGenerator().target.getRuleFunctionContextStructName(r));
super(factory, factory.getGenerator().getTarget().getRuleFunctionContextStructName(r));
addDispatchMethods(r);
derivedFromName = r.name;
provideCopyFrom = r.hasAltSpecificContexts();

View File

@ -110,9 +110,9 @@ public class BuildDependencyGenerator {
files.add(getOutputFile(generator.getVocabFileName()));
// are we generating a .h file?
ST headerExtST = null;
ST extST = generator.templates.getInstanceOf("codeFileExtension");
if (generator.templates.isDefined("headerFile")) {
headerExtST = generator.templates.getInstanceOf("headerFileExtension");
ST extST = generator.getTemplates().getInstanceOf("codeFileExtension");
if (generator.getTemplates().isDefined("headerFile")) {
headerExtST = generator.getTemplates().getInstanceOf("headerFileExtension");
String suffix = Grammar.getGrammarTypeToFileNameSuffix(g.getType());
String fileName = g.name + suffix + headerExtST.render();
files.add(getOutputFile(fileName));