/*
 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.codegen;

import org.antlr.v4.codegen.target.*;

/**
 * Utility class to escape Unicode code points using various
 * languages' syntax.
 */
public class UnicodeEscapes {
	public static String escapeCodePoint(int codePoint, String language) {
		StringBuilder result = new StringBuilder();
		appendEscapedCodePoint(result, codePoint, language);
		return result.toString();
	}

	public static void appendEscapedCodePoint(StringBuilder sb, int codePoint, String language) {
		switch (language) {
			case JavaTarget.key:
			case JavaScriptTarget.key:
			case DartTarget.key:
				if (Character.isSupplementaryCodePoint(codePoint)) {
					// char is not an 'integral' type, so we have to explicitly convert
					// to int before passing to the %X formatter or else it throws.
					sb.append(String.format("\\u%04X", (int)Character.highSurrogate(codePoint)));
					sb.append(String.format("\\u%04X", (int)Character.lowSurrogate(codePoint)));
				}
				else {
					sb.append(String.format("\\u%04X", codePoint));
				}
				break;
			case CSharpTarget.key:
			case Python2Target.key:
			case Python3Target.key:
			case CppTarget.key:
			case GoTarget.key:
			case PHPTarget.key:
				String format = Character.isSupplementaryCodePoint(codePoint) ? "\\U%08X" : "\\u%04X";
				sb.append(String.format(format, codePoint));
				break;
			case SwiftTarget.key:
				sb.append(String.format("\\u{%04X}", codePoint));
				break;
		}
	}
}

@@ -707,16 +707,405 @@
 uage) %7B%0A
+%09%09%09case CSharpTarget.key:%0A%09%09%09case Python2Target.key:%0A%09%09%09case Python3Target.key:%0A%09%09%09case CppTarget.key:%0A%09%09%09case GoTarget.key:%0A%09%09%09case PHPTarget.key:%0A%09%09%09%09String format = Character.isSupplementaryCodePoint(codePoint) ? %22%5C%5CU%2508X%22 : %22%5C%5Cu%2504X%22;%0A%09%09%09%09sb.append(String.format(format, codePoint));%0A%09%09%09%09break;%0A%09%09%09case SwiftTarget.key:%0A%09%09%09%09sb.append(String.format(%22%5C%5Cu%7B%2504X%7D%22, codePoint));%0A%09%09%09%09break;%0A
 %09%09%09case 
@@ -1112,32 +1112,32 @@
 JavaTarget.key:%0A
-
 %09%09%09case JavaScri
@@ -1170,24 +1170,36 @@
 Target.key:%0A
+%09%09%09default:%0A
 %09%09%09%09if (Char
@@ -1616,24 +1616,24 @@
 odePoint));%0A
+
 %09%09%09%09%7D%0A%09%09%09%09br
@@ -1641,397 +1641,8 @@
 ak;%0A
-%09%09%09case CSharpTarget.key:%0A%09%09%09case Python2Target.key:%0A%09%09%09case Python3Target.key:%0A%09%09%09case CppTarget.key:%0A%09%09%09case GoTarget.key:%0A%09%09%09case PHPTarget.key:%0A%09%09%09%09String format = Character.isSupplementaryCodePoint(codePoint) ? %22%5C%5CU%2508X%22 : %22%5C%5Cu%2504X%22;%0A%09%09%09%09sb.append(String.format(format, codePoint));%0A%09%09%09%09break;%0A%09%09%09case SwiftTarget.key:%0A%09%09%09%09sb.append(String.format(%22%5C%5Cu%7B%2504X%7D%22, codePoint));%0A%09%09%09%09break;%0A
 %09%09%7D%0A

Add default branch to appendEscapedCodePoint for unofficial targets (Kotlin)