Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ascii only ATN serialization #3566

Merged
merged 6 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 7 additions & 46 deletions tool/src/org/antlr/v4/codegen/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
public abstract class Target {
protected final CodeGenerator gen;
private STGroup templates;
private boolean isPreviousOctal = false;

protected static final Map<Character, String> defaultCharValueEscape;
static {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions tool/src/org/antlr/v4/codegen/target/CSharpTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions tool/src/org/antlr/v4/codegen/target/DartTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ protected STGroup loadTemplates() {
public boolean isATNSerializedAsInts() {
return false;
}

@Override
protected String escapeChar(int v) {
return String.format("\\u{%X}", v);
}
}
24 changes: 17 additions & 7 deletions tool/src/org/antlr/v4/codegen/target/PHPTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions tool/src/org/antlr/v4/codegen/target/SwiftTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}