forked from jasder/antlr
gitignore was dropping all my target generators. ugh.
This commit is contained in:
parent
8e361ef401
commit
b3133abb21
|
@ -24,13 +24,6 @@
|
|||
<component name="EntryPointsManager">
|
||||
<entry_points version="2.0" />
|
||||
</component>
|
||||
<component name="MavenImportPreferences">
|
||||
<option name="generalSettings">
|
||||
<MavenGeneralSettings>
|
||||
<option name="mavenHome" value="Bundled (Maven 3)" />
|
||||
</MavenGeneralSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
|
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2013 Terence Parr
|
||||
* Copyright (c) 2013 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.target;
|
||||
|
||||
import org.antlr.v4.codegen.CodeGenerator;
|
||||
import org.antlr.v4.codegen.Target;
|
||||
import org.antlr.v4.tool.ErrorType;
|
||||
import org.antlr.v4.tool.ast.GrammarAST;
|
||||
import org.stringtemplate.v4.NumberRenderer;
|
||||
import org.stringtemplate.v4.STErrorListener;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
import org.stringtemplate.v4.STGroupFile;
|
||||
import org.stringtemplate.v4.StringRenderer;
|
||||
import org.stringtemplate.v4.misc.STMessage;
|
||||
|
||||
public class CSharpTarget extends Target {
|
||||
|
||||
public CSharpTarget(CodeGenerator gen) {
|
||||
super(gen, "CSharp");
|
||||
targetCharValueEscape[0] = "\\0";
|
||||
targetCharValueEscape[0x0007] = "\\a";
|
||||
targetCharValueEscape[0x000B] = "\\v";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "4.5.1"; // crossing fingers that it's close enough.
|
||||
}
|
||||
|
||||
@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 && (v < '0' || v > '9') && (v < 'a' || v > 'f') && (v < 'A' || v > 'F')) {
|
||||
return String.valueOf((char)v);
|
||||
}
|
||||
|
||||
return String.format("\\x%X", v & 0xFFFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTargetStringLiteralFromANTLRStringLiteral(
|
||||
CodeGenerator generator,
|
||||
String literal, boolean addQuotes)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String is = literal;
|
||||
|
||||
if ( addQuotes ) sb.append('"');
|
||||
|
||||
for (int i = 1; i < is.length() -1; i++) {
|
||||
if (is.charAt(i) == '\\') {
|
||||
// Anything escaped is what it is! We assume that
|
||||
// people know how to escape characters correctly. However
|
||||
// we catch anything that does not need an escape in Java (which
|
||||
// is what the default implementation is dealing with and remove
|
||||
// the escape. The C target does this for instance.
|
||||
//
|
||||
switch (is.charAt(i+1)) {
|
||||
// Pass through any escapes that Java also needs
|
||||
//
|
||||
case '"':
|
||||
case 'n':
|
||||
case 'r':
|
||||
case 't':
|
||||
case 'b':
|
||||
case 'f':
|
||||
case '\\':
|
||||
// Pass the escape through
|
||||
sb.append('\\');
|
||||
break;
|
||||
|
||||
case 'u': // Assume unnnn
|
||||
// Pass the escape through as double \\
|
||||
// so that Java leaves as \u0000 string not char
|
||||
sb.append('\\');
|
||||
sb.append('\\');
|
||||
break;
|
||||
|
||||
default:
|
||||
// Remove the escape by virtue of not adding it here
|
||||
// Thus \' becomes ' and so on
|
||||
break;
|
||||
}
|
||||
|
||||
// Go past the \ character
|
||||
i++;
|
||||
} else {
|
||||
// Characters that don't need \ in ANTLR 'strings' but do in Java
|
||||
if (is.charAt(i) == '"') {
|
||||
// We need to escape " in Java
|
||||
sb.append('\\');
|
||||
}
|
||||
}
|
||||
// Add in the next character, which may have been escaped
|
||||
sb.append(is.charAt(i));
|
||||
}
|
||||
|
||||
if ( addQuotes ) sb.append('"');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* [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.target;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Eric Vergnaud
|
||||
*/
|
||||
public class JavaScriptTarget extends Target {
|
||||
|
||||
/** Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar */
|
||||
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",
|
||||
//future reserved
|
||||
"enum", "await", "implements", "package", "protected", "static",
|
||||
"interface", "private", "public",
|
||||
//future reserved in older standards
|
||||
"abstract", "boolean", "byte", "char", "double", "final", "float",
|
||||
"goto", "int", "long", "native", "short", "synchronized", "transient",
|
||||
"volatile",
|
||||
//literals
|
||||
"null", "true", "false"
|
||||
};
|
||||
|
||||
/** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
|
||||
protected final Set<String> badWords = new HashSet<String>();
|
||||
|
||||
public JavaScriptTarget(CodeGenerator gen) {
|
||||
super(gen, "JavaScript");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "4.5.1";
|
||||
}
|
||||
|
||||
public Set<String> getBadWords() {
|
||||
if (badWords.isEmpty()) {
|
||||
addBadWords();
|
||||
}
|
||||
|
||||
return badWords;
|
||||
}
|
||||
|
||||
protected void addBadWords() {
|
||||
badWords.addAll(Arrays.asList(javaScriptKeywords));
|
||||
badWords.add("rule");
|
||||
badWords.add("parserRule");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p/>
|
||||
* For Java, this is the translation {@code 'a\n"'} → {@code "a\n\""}.
|
||||
* Expect single quotes around the incoming literal. Just flip the quotes
|
||||
* and replace double quotes with {@code \"}.
|
||||
* <p/>
|
||||
* Note that we have decided to allow people to use '\"' without penalty, so
|
||||
* we must build the target string in a loop as {@link String#replace}
|
||||
* cannot handle both {@code \"} and {@code "} without a lot of messing
|
||||
* around.
|
||||
*/
|
||||
@Override
|
||||
public String getTargetStringLiteralFromANTLRStringLiteral(
|
||||
CodeGenerator generator,
|
||||
String literal, boolean addQuotes)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String is = literal;
|
||||
|
||||
if ( addQuotes ) sb.append('"');
|
||||
|
||||
for (int i = 1; i < is.length() -1; i++) {
|
||||
if (is.charAt(i) == '\\') {
|
||||
// Anything escaped is what it is! We assume that
|
||||
// people know how to escape characters correctly. However
|
||||
// we catch anything that does not need an escape in Java (which
|
||||
// is what the default implementation is dealing with and remove
|
||||
// the escape. The C target does this for instance.
|
||||
//
|
||||
switch (is.charAt(i+1)) {
|
||||
// Pass through any escapes that Java also needs
|
||||
//
|
||||
case '"':
|
||||
case 'n':
|
||||
case 'r':
|
||||
case 't':
|
||||
case 'b':
|
||||
case 'f':
|
||||
case '\\':
|
||||
// Pass the escape through
|
||||
sb.append('\\');
|
||||
break;
|
||||
|
||||
case 'u': // Assume unnnn
|
||||
// Pass the escape through as double \\
|
||||
// so that Java leaves as \u0000 string not char
|
||||
sb.append('\\');
|
||||
sb.append('\\');
|
||||
break;
|
||||
|
||||
default:
|
||||
// Remove the escape by virtue of not adding it here
|
||||
// Thus \' becomes ' and so on
|
||||
break;
|
||||
}
|
||||
|
||||
// Go past the \ character
|
||||
i++;
|
||||
} else {
|
||||
// Characters that don't need \ in ANTLR 'strings' but do in Java
|
||||
if (is.charAt(i) == '"') {
|
||||
// We need to escape " in Java
|
||||
sb.append('\\');
|
||||
}
|
||||
}
|
||||
// Add in the next character, which may have been escaped
|
||||
sb.append(is.charAt(i));
|
||||
}
|
||||
|
||||
if ( addQuotes ) sb.append('"');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@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 && (!Character.isDigit(v) || v == '8' || v == '9')) {
|
||||
return String.valueOf((char)v);
|
||||
}
|
||||
|
||||
if ( v>=0 && v<=127 ) {
|
||||
String oct = Integer.toOctalString(v);
|
||||
return "\\"+ oct;
|
||||
}
|
||||
|
||||
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)) {
|
||||
// 5C is the hex code for the \ itself
|
||||
return ((String)o).replace("\\u", "\\u005Cu");
|
||||
}
|
||||
|
||||
return super.toString(o, formatString, locale);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean wantsBaseListener() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean wantsBaseVisitor() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOverloadedMethods() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* [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.target;
|
||||
|
||||
import org.antlr.v4.Tool;
|
||||
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 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* [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.target;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Eric Vergnaud
|
||||
*/
|
||||
public class Python2Target extends Target {
|
||||
protected static final String[] python2Keywords = {
|
||||
"abs", "all", "any", "apply", "as",
|
||||
"bin", "bool", "buffer", "bytearray",
|
||||
"callable", "chr", "classmethod", "coerce", "compile", "complex",
|
||||
"delattr", "dict", "dir", "divmod",
|
||||
"enumerate", "eval", "execfile",
|
||||
"file", "filter", "float", "format", "frozenset",
|
||||
"getattr", "globals",
|
||||
"hasattr", "hash", "help", "hex",
|
||||
"id", "input", "int", "intern", "isinstance", "issubclass", "iter",
|
||||
"len", "list", "locals",
|
||||
"map", "max", "min", "next",
|
||||
"memoryview",
|
||||
"object", "oct", "open", "ord",
|
||||
"pow", "print", "property",
|
||||
"range", "raw_input", "reduce", "reload", "repr", "reversed", "round",
|
||||
"set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super",
|
||||
"tuple", "type",
|
||||
"unichr", "unicode",
|
||||
"vars",
|
||||
"with",
|
||||
"xrange",
|
||||
"zip",
|
||||
"__import__",
|
||||
"True", "False", "None"
|
||||
};
|
||||
|
||||
/** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
|
||||
protected final Set<String> badWords = new HashSet<String>();
|
||||
|
||||
public Python2Target(CodeGenerator gen) {
|
||||
super(gen, "Python2");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
|
||||
return getBadWords().contains(idNode.getText());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wantsBaseVisitor() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsOverloadedMethods() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "4.5.2";
|
||||
}
|
||||
|
||||
public Set<String> getBadWords() {
|
||||
if (badWords.isEmpty()) {
|
||||
addBadWords();
|
||||
}
|
||||
|
||||
return badWords;
|
||||
}
|
||||
|
||||
protected void addBadWords() {
|
||||
badWords.addAll(Arrays.asList(python2Keywords));
|
||||
badWords.add("rule");
|
||||
badWords.add("parserRule");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* [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.target;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Eric Vergnaud
|
||||
*/
|
||||
public class Python3Target extends Target {
|
||||
protected static final String[] python3Keywords = {
|
||||
"abs", "all", "any", "apply", "as",
|
||||
"bin", "bool", "buffer", "bytearray",
|
||||
"callable", "chr", "classmethod", "coerce", "compile", "complex",
|
||||
"delattr", "dict", "dir", "divmod",
|
||||
"enumerate", "eval", "execfile",
|
||||
"file", "filter", "float", "format", "frozenset",
|
||||
"getattr", "globals",
|
||||
"hasattr", "hash", "help", "hex",
|
||||
"id", "input", "int", "intern", "isinstance", "issubclass", "iter",
|
||||
"len", "list", "locals",
|
||||
"map", "max", "min", "next",
|
||||
"memoryview",
|
||||
"object", "oct", "open", "ord",
|
||||
"pow", "print", "property",
|
||||
"range", "raw_input", "reduce", "reload", "repr", "reversed", "round",
|
||||
"set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super",
|
||||
"tuple", "type",
|
||||
"unichr", "unicode",
|
||||
"vars",
|
||||
"with",
|
||||
"zip",
|
||||
"__import__",
|
||||
"True", "False", "None"
|
||||
};
|
||||
|
||||
public Python3Target(CodeGenerator gen) {
|
||||
super(gen, "Python3");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSerializedATNSegmentLimit() {
|
||||
// set to something stupid to avoid segmentation
|
||||
return 2 ^ 31;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
|
||||
return getBadWords().contains(idNode.getText());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wantsBaseVisitor() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsOverloadedMethods() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "4.5.2";
|
||||
}
|
||||
|
||||
/** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
|
||||
protected final Set<String> badWords = new HashSet<String>();
|
||||
|
||||
public Set<String> getBadWords() {
|
||||
if (badWords.isEmpty()) {
|
||||
addBadWords();
|
||||
}
|
||||
|
||||
return badWords;
|
||||
}
|
||||
|
||||
protected void addBadWords() {
|
||||
badWords.addAll(Arrays.asList(python3Keywords));
|
||||
badWords.add("rule");
|
||||
badWords.add("parserRule");
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue