diff --git a/tool/src/org/antlr/v4/codegen/Target.java b/tool/src/org/antlr/v4/codegen/Target.java index d7e90d9ac6..5fb03409c8 100644 --- a/tool/src/org/antlr/v4/codegen/Target.java +++ b/tool/src/org/antlr/v4/codegen/Target.java @@ -34,7 +34,6 @@ public abstract class Target { protected final CodeGenerator gen; private STGroup templates; - private boolean isPreviousOctal = false; protected static final Map defaultCharValueEscape; static { @@ -350,63 +349,25 @@ public String encodeIntAsCharEscape(int v) { char c = (char)v; String escaped = getTargetCharValueEscape().get(c); if (escaped != null) { - isPreviousOctal = false; return escaped; } - switch (Character.getType(c)) - { + switch (Character.getType(c)) { case Character.CONTROL: case Character.LINE_SEPARATOR: case Character.PARAGRAPH_SEPARATOR: return escapeChar(v); default: - if (v == 0xfffe) { - return escapeChar(v); + if ( v<=127 ) { + return String.valueOf(c); // ascii chars can be as-is, no encoding } - - if (isPreviousOctal) { - String language = getLanguage(); - char upperBound = language.equals("PHP") ? '9' : '7'; - if (c >= '0' && c <= upperBound) { - return escapeChar(v); - } - } - - isPreviousOctal = false; - return String.valueOf(c); + // else we use hex encoding to ensure pure ascii chars generated + return escapeChar(v); } } - private String escapeChar(int v) { - String language = getLanguage(); - - boolean isPhp = language.equals("PHP"); - boolean supportsOctalEncoding = language.equals("Java") - || language.equals("Python2") - || language.equals("Python3") - || isPhp; - if (supportsOctalEncoding && v <= (isPhp ? 127 : 255)) { - isPreviousOctal = true; - return String.format("\\%o", v); - } else { - isPreviousOctal = false; - } - - switch (language) { - default: - case "Java": - case "JavaScript": - case "Python2": - case "Python3": - return String.format("\\u%04x", v); - case "CSharp": - return String.format("\\x%X", v); - case "Dart": - case "PHP": - case "Swift": - return String.format("\\u{%X}", v); - } + protected String escapeChar(int v) { + return String.format("\\u%04x", v); } public String getLoopLabel(GrammarAST ast) { diff --git a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java index 924432db17..5a72562a2a 100644 --- a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java @@ -175,4 +175,9 @@ private void reportError(STMessage msg) { public boolean isATNSerializedAsInts() { return false; } + + @Override + protected String escapeChar(int v) { + return String.format("\\x%X", v); + } } diff --git a/tool/src/org/antlr/v4/codegen/target/DartTarget.java b/tool/src/org/antlr/v4/codegen/target/DartTarget.java index 4a417b9078..066d37100e 100644 --- a/tool/src/org/antlr/v4/codegen/target/DartTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/DartTarget.java @@ -75,4 +75,9 @@ protected STGroup loadTemplates() { public boolean isATNSerializedAsInts() { return false; } + + @Override + protected String escapeChar(int v) { + return String.format("\\u{%X}", v); + } } diff --git a/tool/src/org/antlr/v4/codegen/target/PHPTarget.java b/tool/src/org/antlr/v4/codegen/target/PHPTarget.java index 1b455c3dc1..84164e12b0 100644 --- a/tool/src/org/antlr/v4/codegen/target/PHPTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/PHPTarget.java @@ -86,11 +86,21 @@ public boolean supportsOverloadedMethods() { return false; } - @Override - public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes, - boolean escapeSpecial) { - String targetStringLiteral = super.getTargetStringLiteralFromANTLRStringLiteral(generator, literal, addQuotes, escapeSpecial); - targetStringLiteral = targetStringLiteral.replace("$", "\\$"); - return targetStringLiteral; - } + @Override + public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes, + boolean escapeSpecial) { + String targetStringLiteral = super.getTargetStringLiteralFromANTLRStringLiteral(generator, literal, addQuotes, escapeSpecial); + targetStringLiteral = targetStringLiteral.replace("$", "\\$"); + return targetStringLiteral; + } + + @Override + public boolean isATNSerializedAsInts() { + return false; + } + + @Override + protected String escapeChar(int v) { + return String.format("\\u{%X}", v); + } } diff --git a/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java b/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java index 4431efe111..54ae117162 100644 --- a/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java @@ -106,4 +106,9 @@ public String toString(Object o, String formatString, Locale locale) { public boolean isATNSerializedAsInts() { return false; } + + @Override + protected String escapeChar(int v) { + return String.format("\\u{%X}", v); + } }