package org.antlr.v4.codegen.target;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.antlr.v4.codegen.UnicodeEscapes;
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 {
protected static final String[] javaScriptKeywords = {
"break", "case", "class", "catch", "const", "continue", "debugger",
"default", "delete", "do", "else", "export", "extends", "finally", "for",
"function", "if", "import", "in", "instanceof", "let", "new", "return",
"super", "switch", "this", "throw", "try", "typeof", "var", "void",
"while", "with", "yield",
"enum", "await", "implements", "package", "protected", "static",
"interface", "private", "public",
"abstract", "boolean", "byte", "char", "double", "final", "float",
"goto", "int", "long", "native", "short", "synchronized", "transient",
"volatile",
"null", "true", "false"
};
protected final Set<String> badWords = new HashSet<String>();
public JavaScriptTarget(CodeGenerator gen) {
super(gen, "JavaScript");
}
@Override
public String getVersion() {
return "4.7.2";
}
public Set<String> getBadWords() {
if (badWords.isEmpty()) {
addBadWords();
}
return badWords;
}
protected void addBadWords() {
badWords.addAll(Arrays.asList(javaScriptKeywords));
badWords.add("rule");
badWords.add("parserRule");
}
@Override
public String encodeIntAsCharEscape(int v) {
if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) {
throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v));
}
if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) {
return targetCharValueEscape[v];
}
if (v >= 0x20 && v < 127) {
return String.valueOf((char)v);
}
String hex = Integer.toHexString(v|0x10000).substring(1,5);
return "\\u"+hex;
}
@Override
public int getSerializedATNSegmentLimit() {
return 2 ^ 31;
}
@Override
public int getInlineTestSetWordSize() {
return 32;
}
@Override
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
return getBadWords().contains(idNode.getText());
}
@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)) {
return ((String)o).replace("\\u", "\\u005Cu");
}
return super.toString(o, formatString, locale);
}
}
@Override
public boolean wantsBaseListener() {
return false;
}
@Override
public boolean wantsBaseVisitor() {
return false;
}
@Override
public boolean supportsOverloadedMethods() {
return false;
}
@Override
protected void appendUnicodeEscapedCodePoint(int codePoint, StringBuilder sb) {
UnicodeEscapes.appendJavaStyleEscapedCodePoint(codePoint, sb);
}
}