public String getTargetStringLiteralFromANTLRStringLiteral(
		CodeGenerator generator,
		String literal,
		boolean addQuotes,
		boolean escapeSpecial)
	{
		StringBuilder sb = new StringBuilder();

		if ( addQuotes ) sb.append('"');

		for (int i = 1; i < literal.length() -1; ) {
			int codePoint = literal.codePointAt(i);
			int toAdvance = Character.charCount(codePoint);
			if  (codePoint == '\\') {
				// 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.
				//
				int escapedCodePoint = literal.codePointAt(i+toAdvance);
				toAdvance++;
				switch (escapedCodePoint) {
					// Pass through any escapes that Java also needs
					//
					case    'n':
					case    'r':
					case    't':
					case    'b':
					case    'f':
					case    '\\':
						// Pass the escape through
						if (escapeSpecial && escapedCodePoint != '\\') {
							sb.append('\\');
						}
						sb.append('\\');
						sb.appendCodePoint(escapedCodePoint);
						break;

					case    'u':    // Either unnnn or u{nnnnnn}
						if (literal.charAt(i+toAdvance) == '{') {
							while (literal.charAt(i+toAdvance) != '}') {
								toAdvance++;
							}
							toAdvance++;
						}
						else {
							toAdvance += 4;
						}
						if ( i+toAdvance <= literal.length() ) { // we might have an invalid \\uAB or something
							String fullEscape = literal.substring(i, i+toAdvance);
							appendUnicodeEscapedCodePoint(
								CharSupport.getCharValueFromCharInGrammarLiteral(fullEscape),
								sb,
								escapeSpecial);
						}
						break;
					default:
						if (shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(escapedCodePoint)) {
							appendUnicodeEscapedCodePoint(escapedCodePoint, sb, escapeSpecial);
						}
						else {
							sb.appendCodePoint(escapedCodePoint);
						}
						break;
				}
			}
			else {
				if (codePoint == 0x22) {
					// ANTLR doesn't escape " in literal strings,
					// but every other language needs to do so.
					sb.append("\\\"");
				}
				else if (shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(codePoint)) {
					appendUnicodeEscapedCodePoint(codePoint, sb, escapeSpecial);
				}
				else {
					sb.appendCodePoint(codePoint);
				}
			}
			i += toAdvance;
		}

		if ( addQuotes ) sb.append('"');

		return sb.toString();
	}
<p>Convert from an ANTLR string literal found in a grammar file to an
equivalent string literal in the target language.
</p>
<p>
For Java, this is the translation {@code 'a\n"'} &rarr; {@code "a\n\""}.
Expect single quotes around the incoming literal. Just flip the quotes
and replace double quotes with {@code \"}.
</p>
<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.
</p>
tool/src/org/antlr/v4/codegen/Target.java