forked from jasder/antlr
mv JavaTarget to new target package
This commit is contained in:
parent
f97183955f
commit
6d892c71b5
|
@ -84,7 +84,7 @@ public class CodeGenerator {
|
|||
}
|
||||
|
||||
public static boolean targetExists(String language) {
|
||||
String targetName = "org.antlr.v4.codegen."+language+"Target";
|
||||
String targetName = "org.antlr.v4.codegen.target."+language+"Target";
|
||||
try {
|
||||
Class<? extends Target> c = Class.forName(targetName).asSubclass(Target.class);
|
||||
Constructor<? extends Target> ctor = c.getConstructor(CodeGenerator.class);
|
||||
|
@ -112,7 +112,7 @@ public class CodeGenerator {
|
|||
}
|
||||
|
||||
protected void loadLanguageTarget(String language) {
|
||||
String targetName = "org.antlr.v4.codegen."+language+"Target";
|
||||
String targetName = "org.antlr.v4.codegen.target."+language+"Target";
|
||||
try {
|
||||
Class<? extends Target> c = Class.forName(targetName).asSubclass(Target.class);
|
||||
Constructor<? extends Target> ctor = c.getConstructor(CodeGenerator.class);
|
||||
|
|
|
@ -1,124 +0,0 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2012 Terence Parr
|
||||
* Copyright (c) 2012 Sam Harwell
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.codegen;
|
||||
|
||||
import org.antlr.v4.Tool;
|
||||
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 JavaTarget extends Target {
|
||||
|
||||
/**
|
||||
* The Java target can cache the code generation templates.
|
||||
*/
|
||||
private static final ThreadLocal<STGroup> targetTemplates = new ThreadLocal<STGroup>();
|
||||
|
||||
protected static final String[] javaKeywords = {
|
||||
"abstract", "assert", "boolean", "break", "byte", "case", "catch",
|
||||
"char", "class", "const", "continue", "default", "do", "double", "else",
|
||||
"enum", "extends", "false", "final", "finally", "float", "for", "goto",
|
||||
"if", "implements", "import", "instanceof", "int", "interface",
|
||||
"long", "native", "new", "null", "package", "private", "protected",
|
||||
"public", "return", "short", "static", "strictfp", "super", "switch",
|
||||
"synchronized", "this", "throw", "throws", "transient", "true", "try",
|
||||
"void", "volatile", "while"
|
||||
};
|
||||
|
||||
/** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
|
||||
protected final Set<String> badWords = new HashSet<String>();
|
||||
|
||||
public JavaTarget(CodeGenerator gen) {
|
||||
super(gen, "Java");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return Tool.VERSION; // Java and tool versions move in lock step
|
||||
}
|
||||
|
||||
public Set<String> getBadWords() {
|
||||
if (badWords.isEmpty()) {
|
||||
addBadWords();
|
||||
}
|
||||
|
||||
return badWords;
|
||||
}
|
||||
|
||||
protected void addBadWords() {
|
||||
badWords.addAll(Arrays.asList(javaKeywords));
|
||||
badWords.add("rule");
|
||||
badWords.add("parserRule");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSerializedATNSegmentLimit() {
|
||||
// 65535 is the class file format byte limit for a UTF-8 encoded string literal
|
||||
// 3 is the maximum number of bytes it takes to encode a value in the range 0-0xFFFF
|
||||
return 65535 / 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
|
||||
return getBadWords().contains(idNode.getText());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue