Browse Source

Better error messages for \x and \u (#8192)

* better error messages for \x and \u

* changed error msg for \u

* adjusted tests

* final fix

* final final fix
Alexander Kuzmenko 6 years ago
parent
commit
032d959574

+ 9 - 5
src/core/ast.ml

@@ -501,13 +501,15 @@ let unescape s =
 					Buffer.add_char b c;
 					inext := !inext + 2;
 				| 'x' ->
-					let hex = String.sub s (i+1) 2 in
-					let u = (try (int_of_string ("0x" ^ hex)) with _ -> fail None) in
+					let fail_no_hex () = fail (Some "Must be followed by a hexadecimal sequence.") in
+					let hex = try String.sub s (i+1) 2 with _ -> fail_no_hex () in
+					let u = (try (int_of_string ("0x" ^ hex)) with _ -> fail_no_hex ()) in
 					if u > 127 then
 						fail (Some ("Values greater than \\x7f are not allowed. Use \\u00" ^ hex ^ " instead."));
 					UTF8.add_uchar b (UChar.uchar_of_int u);
 					inext := !inext + 2;
 				| 'u' ->
+					let fail_no_hex () = fail (Some "Must be followed by a hexadecimal sequence enclosed in curly brackets.") in
 					let (u, a) =
 						try
 							(int_of_string ("0x" ^ String.sub s (i+1) 4), 4)
@@ -515,10 +517,12 @@ let unescape s =
 							assert (s.[i+1] = '{');
 							let l = String.index_from s (i+3) '}' - (i+2) in
 							let u = int_of_string ("0x" ^ String.sub s (i+2) l) in
-							assert (u <= 0x10FFFF);
+							if u > 0x10FFFF then
+								fail (Some "Maximum allowed value for unicode escape sequence is \\u{10FFFF}");
 							(u, l+2)
-						with _ ->
-							fail None
+						with
+							| Invalid_escape_sequence (c,i,msg) as e -> raise e
+							| _ -> fail_no_hex ()
 					in
 					UTF8.add_uchar b (UChar.uchar_of_int u);
 					inext := !inext + a;

+ 3 - 0
tests/misc/projects/Issue8191/U1.hx

@@ -0,0 +1,3 @@
+class U1 {
+	var u = "\u";
+}

+ 3 - 0
tests/misc/projects/Issue8191/U2.hx

@@ -0,0 +1,3 @@
+class U2 {
+	var u = "\u{007f";
+}

+ 3 - 0
tests/misc/projects/Issue8191/U3.hx

@@ -0,0 +1,3 @@
+class U3 {
+	var u = "\u{}";
+}

+ 3 - 0
tests/misc/projects/Issue8191/U4.hx

@@ -0,0 +1,3 @@
+class U4 {
+	var u = "\u{007g}";
+}

+ 3 - 0
tests/misc/projects/Issue8191/U5.hx

@@ -0,0 +1,3 @@
+class U4 {
+	var u = "\u{110000}";
+}

+ 3 - 0
tests/misc/projects/Issue8191/X1.hx

@@ -0,0 +1,3 @@
+class X1 {
+	var x = "\x";
+}

+ 3 - 0
tests/misc/projects/Issue8191/X2.hx

@@ -0,0 +1,3 @@
+class X2 {
+	var x = "\xfg";
+}

+ 1 - 0
tests/misc/projects/Issue8191/compile-fail.hxml

@@ -0,0 +1 @@
+X1

+ 1 - 0
tests/misc/projects/Issue8191/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+X1.hx:2: character 11 : Invalid escape sequence \x. Must be followed by a hexadecimal sequence.

+ 1 - 0
tests/misc/projects/Issue8191/compile2-fail.hxml

@@ -0,0 +1 @@
+X2

+ 1 - 0
tests/misc/projects/Issue8191/compile2-fail.hxml.stderr

@@ -0,0 +1 @@
+X2.hx:2: character 11 : Invalid escape sequence \x. Must be followed by a hexadecimal sequence.

+ 1 - 0
tests/misc/projects/Issue8191/compile3-fail.hxml

@@ -0,0 +1 @@
+U1

+ 1 - 0
tests/misc/projects/Issue8191/compile3-fail.hxml.stderr

@@ -0,0 +1 @@
+U1.hx:2: character 11 : Invalid escape sequence \u. Must be followed by a hexadecimal sequence enclosed in curly brackets.

+ 1 - 0
tests/misc/projects/Issue8191/compile4-fail.hxml

@@ -0,0 +1 @@
+U2

+ 1 - 0
tests/misc/projects/Issue8191/compile4-fail.hxml.stderr

@@ -0,0 +1 @@
+U2.hx:2: character 11 : Invalid escape sequence \u. Must be followed by a hexadecimal sequence enclosed in curly brackets.

+ 1 - 0
tests/misc/projects/Issue8191/compile5-fail.hxml

@@ -0,0 +1 @@
+U3

+ 1 - 0
tests/misc/projects/Issue8191/compile5-fail.hxml.stderr

@@ -0,0 +1 @@
+U3.hx:2: character 11 : Invalid escape sequence \u. Must be followed by a hexadecimal sequence enclosed in curly brackets.

+ 1 - 0
tests/misc/projects/Issue8191/compile6-fail.hxml

@@ -0,0 +1 @@
+U4

+ 1 - 0
tests/misc/projects/Issue8191/compile6-fail.hxml.stderr

@@ -0,0 +1 @@
+U4.hx:2: character 11 : Invalid escape sequence \u. Must be followed by a hexadecimal sequence enclosed in curly brackets.

+ 1 - 0
tests/misc/projects/Issue8191/compile7-fail.hxml

@@ -0,0 +1 @@
+U5

+ 1 - 0
tests/misc/projects/Issue8191/compile7-fail.hxml.stderr

@@ -0,0 +1 @@
+U5.hx:2: character 11 : Invalid escape sequence \u. Maximum allowed value for unicode escape sequence is \u{10FFFF}