Skip to content

Commit

Permalink
Cache templates for codegen to static field, load only one time
Browse files Browse the repository at this point in the history
Remove useless loadTemplates overloads

Signed-off-by: Ivan Kochurkin <kvanttt@gmail.com>
  • Loading branch information
KvanTTT authored and parrt committed Jun 26, 2022
1 parent 70d0a7a commit 6a7e651
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 249 deletions.
30 changes: 17 additions & 13 deletions tool/src/org/antlr/v4/codegen/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.GrammarAST;
import org.stringtemplate.v4.NumberRenderer;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.StringRenderer;
import org.stringtemplate.v4.*;
import org.stringtemplate.v4.misc.STMessage;

import java.util.HashMap;
Expand All @@ -32,8 +27,9 @@

/** */
public abstract class Target {
private final static Map<String, STGroup> languageTemplates = new HashMap<>();

protected final CodeGenerator gen;
private STGroup templates;

protected static final Map<Character, String> defaultCharValueEscape;
static {
Expand Down Expand Up @@ -92,14 +88,22 @@ public String getVersion() {
}

public STGroup getTemplates() {
String language = getLanguage();
STGroup templates = languageTemplates.get(language);

if (templates == null) {
String version = getVersion();
if ( version==null ||
!RuntimeMetaData.getMajorMinorVersion(version).equals(RuntimeMetaData.getMajorMinorVersion(Tool.VERSION)))
{
gen.tool.errMgr.toolError(ErrorType.INCOMPATIBLE_TOOL_AND_TEMPLATES, version, Tool.VERSION, getLanguage());
synchronized (languageTemplates) {
templates = languageTemplates.get(language);
if (templates == null) {
String version = getVersion();
if (version == null ||
!RuntimeMetaData.getMajorMinorVersion(version).equals(RuntimeMetaData.getMajorMinorVersion(Tool.VERSION))) {
gen.tool.errMgr.toolError(ErrorType.INCOMPATIBLE_TOOL_AND_TEMPLATES, version, Tool.VERSION, language);
}
templates = loadTemplates();
languageTemplates.put(language, templates);
}
}
templates = loadTemplates();
}

return templates;
Expand Down
35 changes: 0 additions & 35 deletions tool/src/org/antlr/v4/codegen/target/CSharpTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,41 +136,6 @@ protected String escapeWord(String word) {
return "@" + word;
}

@Override
protected STGroup loadTemplates() {
// override the superclass behavior to put all C# templates in the same folder
STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT+"/CSharp/"+ getLanguage()+STGroup.GROUP_FILE_EXTENSION);
result.registerRenderer(Integer.class, new NumberRenderer());
result.registerRenderer(String.class, new StringRenderer());
result.setListener(new STErrorListener() {
@Override
public void compileTimeError(STMessage msg) {
reportError(msg);
}

@Override
public void runTimeError(STMessage msg) {
reportError(msg);
}

@Override
public void IOError(STMessage msg) {
reportError(msg);
}

@Override
public void internalError(STMessage msg) {
reportError(msg);
}

private void reportError(STMessage msg) {
getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
}
});

return result;
}

@Override
public boolean isATNSerializedAsInts() {
return true;
Expand Down
41 changes: 0 additions & 41 deletions tool/src/org/antlr/v4/codegen/target/CppTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.antlr.v4.tool.ErrorType;
import org.stringtemplate.v4.NumberRenderer;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;
import org.stringtemplate.v4.misc.STMessage;

import java.util.*;

public class CppTarget extends Target {
Expand Down Expand Up @@ -124,38 +117,4 @@ public String getBaseVisitorFileName(boolean header) {
String listenerName = gen.g.name + "BaseVisitor";
return listenerName+extST.render();
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(Integer.class, new NumberRenderer());
result.registerRenderer(String.class, new StringRenderer());
result.setListener(new STErrorListener() {
@Override
public void compileTimeError(STMessage msg) {
reportError(msg);
}

@Override
public void runTimeError(STMessage msg) {
reportError(msg);
}

@Override
public void IOError(STMessage msg) {
reportError(msg);
}

@Override
public void internalError(STMessage msg) {
reportError(msg);
}

private void reportError(STMessage msg) {
getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
}
});

return result;
}
}
15 changes: 1 addition & 14 deletions tool/src/org/antlr/v4/codegen/target/DartTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.*;

public class DartTarget extends Target {
protected static final Map<Character, String> targetCharValueEscape;
static {
HashMap<Character, String> map = new HashMap<>();
for (Map.Entry<Character, String> entry : defaultCharValueEscape.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
HashMap<Character, String> map = new HashMap<>(defaultCharValueEscape);
addEscapedChar(map, '$');
targetCharValueEscape = map;
}
Expand Down Expand Up @@ -63,14 +58,6 @@ public Set<String> getReservedWords() {
return reservedWords;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new StringRenderer(), true);

return result;
}

@Override
public boolean isATNSerializedAsInts() {
return true;
Expand Down
20 changes: 0 additions & 20 deletions tool/src/org/antlr/v4/codegen/target/GoTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ public int getInlineTestSetWordSize() {
return 32;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new JavaStringRenderer(), true);
return result;
}

public String getRecognizerFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
Expand Down Expand Up @@ -164,17 +157,4 @@ public String getBaseVisitorFileName(boolean header) {
assert g.name != null;
return g.name.toLowerCase()+"_base_visitor.go";
}

protected static class JavaStringRenderer extends StringRenderer {
@Override
public String toString(Object o, String formatString, Locale locale) {

if ("java-escape".equals(formatString)) {
// 5C is the hex code for the \ itself
return ((String)o).replace("\\u", "\\u005Cu");
}

return super.toString(o, formatString, locale);
}
}
}
25 changes: 0 additions & 25 deletions tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.antlr.v4.tool.ast.GrammarAST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class JavaScriptTarget extends Target {
Expand Down Expand Up @@ -56,27 +52,6 @@ public int getInlineTestSetWordSize() {
return 32;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new JavaStringRenderer(), true);
return result;
}

protected static class JavaStringRenderer extends StringRenderer {

@Override
public String toString(Object o, String formatString, Locale locale) {
if ("java-escape".equals(formatString)) {
// 5C is the hex code for the \ itself
return ((String)o).replace("\\u", "\\u005Cu");
}

return super.toString(o, formatString, locale);
}

}

@Override
public boolean wantsBaseListener() {
return false;
Expand Down
25 changes: 0 additions & 25 deletions tool/src/org/antlr/v4/codegen/target/JavaTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,6 @@ public int getSerializedATNSegmentLimit() {
return 65535 / 3;
}

@Override
protected STGroup loadTemplates() {
STGroup result = targetTemplates.get();
if (result == null) {
result = super.loadTemplates();
result.registerRenderer(String.class, new JavaStringRenderer(), true);
targetTemplates.set(result);
}

return result;
}

protected static class JavaStringRenderer extends StringRenderer {

@Override
public String toString(Object o, String formatString, Locale locale) {
if ("java-escape".equals(formatString)) {
// 5C is the hex code for the \ itself
return ((String)o).replace("\\u", "\\u005Cu");
}

return super.toString(o, formatString, locale);
}
}

@Override
public boolean isATNSerializedAsInts() {
return false;
Expand Down
10 changes: 0 additions & 10 deletions tool/src/org/antlr/v4/codegen/target/PHPTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.*;

Expand Down Expand Up @@ -73,14 +71,6 @@ protected Set<String> getReservedWords() {
return reservedWords;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new StringRenderer(), true);

return result;
}

@Override
public boolean supportsOverloadedMethods() {
return false;
Expand Down
17 changes: 0 additions & 17 deletions tool/src/org/antlr/v4/codegen/target/Python2Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.*;

Expand Down Expand Up @@ -76,21 +74,6 @@ protected Set<String> getReservedWords() {
return reservedWords;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new PythonStringRenderer(), true);
return result;
}

protected static class PythonStringRenderer extends StringRenderer {

@Override
public String toString(Object o, String formatString, Locale locale) {
return super.toString(o, formatString, locale);
}
}

@Override
public boolean wantsBaseListener() {
return false;
Expand Down
17 changes: 0 additions & 17 deletions tool/src/org/antlr/v4/codegen/target/Python3Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.*;

Expand Down Expand Up @@ -75,21 +73,6 @@ protected Set<String> getReservedWords() {
return reservedWords;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
result.registerRenderer(String.class, new PythonStringRenderer(), true);
return result;
}

protected static class PythonStringRenderer extends StringRenderer {

@Override
public String toString(Object o, String formatString, Locale locale) {
return super.toString(o, formatString, locale);
}
}

@Override
public boolean wantsBaseListener() {
return false;
Expand Down
Loading

0 comments on commit 6a7e651

Please sign in to comment.