public static String decodeStringLiteral(String str) {
StringBuffer result = new StringBuffer();
char[] s = str.toCharArray();
int end = s.length - 1;
if (s[0] != '"' && s[0] != '\'') {
throw new SyntaxException("Invalid delimiter at position 0: "+s[0]);
}
char delimiter = s[0];
for (int i=1; i<end; i++) {
char c = s[i];
switch (c) {
case '\\':
c = s[++i];
switch (c) {
case 'n':
result.append('\n');
break;
case 't':
result.append('\t');
break;
case '\\':
result.append('\\');
break;
default:
result.append(c);
}
break;
default:
result.append(c);
}
}
if (s[end] != delimiter) {
throw new SyntaxException("End delimiter doesn't match "+delimiter+" at position "+end);
}
return result.toString();
}