Skip to content

Commit

Permalink
Cache ANTLR error message templates to static field
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kochurkin <[email protected]>
  • Loading branch information
KvanTTT committed Jun 19, 2022
1 parent 3ee248b commit c9f5a77
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions tool/src/org/antlr/v4/tool/ErrorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.net.URL;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Set;

public class ErrorManager {
private final static HashMap<String, STGroupFile> loadedFormats = new HashMap<>();

public static final String FORMATS_DIR = "org/antlr/v4/tool/templates/messages/formats/";

public Tool tool;
Expand Down Expand Up @@ -216,25 +219,37 @@ public void emit(ErrorType etype, ANTLRMessage msg) {
* Otherwise we just use the default "antlr".
*/
public void setFormat(String formatName) {
this.formatName = formatName;
String fileName = FORMATS_DIR +formatName+STGroup.GROUP_FILE_EXTENSION;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource(fileName);
if ( url==null ) {
cl = ErrorManager.class.getClassLoader();
url = cl.getResource(fileName);
}
if ( url==null && formatName.equals("antlr") ) {
rawError("ANTLR installation corrupted; cannot find ANTLR messages format file "+fileName);
panic();
}
else if ( url==null ) {
rawError("no such message format file "+fileName+" retrying with default ANTLR format");
setFormat("antlr"); // recurse on this rule, trying the default message format
return;
}
format = new STGroupFile(url, "UTF-8", '<', '>');
format.load();
STGroupFile loadedFormat = loadedFormats.get(formatName);
if (loadedFormat == null) {
synchronized (loadedFormats) {
loadedFormat = loadedFormats.get(formatName);
if (loadedFormat == null) {
String fileName = FORMATS_DIR +formatName+STGroup.GROUP_FILE_EXTENSION;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource(fileName);
if ( url==null ) {
cl = ErrorManager.class.getClassLoader();
url = cl.getResource(fileName);
}
if ( url==null && formatName.equals("antlr") ) {
rawError("ANTLR installation corrupted; cannot find ANTLR messages format file "+fileName);
panic();
}
else if ( url==null ) {
rawError("no such message format file "+fileName+" retrying with default ANTLR format");
setFormat("antlr"); // recurse on this rule, trying the default message format
return;
}
loadedFormat = new STGroupFile(url, "UTF-8", '<', '>');
loadedFormat.load();

loadedFormats.put(formatName, loadedFormat);
}
}
}

this.formatName = formatName;
this.format = loadedFormat;

if ( !initSTListener.errors.isEmpty() ) {
rawError("ANTLR installation corrupted; can't load messages format file:\n"+
Expand Down

0 comments on commit c9f5a77

Please sign in to comment.