Move Java-specific character escaping code to JavaTarget, make methods in Target abstract
This commit is contained in:
parent
b4e66f18cd
commit
68050dd126
|
@ -74,6 +74,92 @@ public class JavaTarget extends Target {
|
|||
badWords.add("parserRule");
|
||||
}
|
||||
|
||||
@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
|
||||
protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
|
||||
return getBadWords().contains(idNode.getText());
|
||||
|
|
|
@ -199,90 +199,12 @@ public abstract class Target {
|
|||
* we must build the target string in a loop as {@link Utils#replace} cannot
|
||||
* handle both {@code \"} and {@code "} without a lot of messing around.
|
||||
*/
|
||||
public String getTargetStringLiteralFromANTLRStringLiteral(
|
||||
public abstract 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();
|
||||
}
|
||||
String literal, boolean addQuotes);
|
||||
|
||||
/** Assume 16-bit char */
|
||||
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;
|
||||
}
|
||||
public abstract String encodeIntAsCharEscape(int v);
|
||||
|
||||
public String getLoopLabel(GrammarAST ast) {
|
||||
return "loop"+ ast.token.getTokenIndex();
|
||||
|
|
Loading…
Reference in New Issue