Skip to content

Commit

Permalink
Better error messages for \x and \u (#8192)
Browse files Browse the repository at this point in the history
* better error messages for \x and \u

* changed error msg for \u

* adjusted tests

* final fix

* final final fix
  • Loading branch information
RealyUniqueName committed Apr 22, 2019
1 parent de0be7d commit 032d959
Show file tree
Hide file tree
Showing 22 changed files with 44 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/core/ast.ml
Expand Up @@ -501,24 +501,28 @@ 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)
with _ -> try
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;
Expand Down
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U1.hx
@@ -0,0 +1,3 @@
class U1 {
var u = "\u";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U2.hx
@@ -0,0 +1,3 @@
class U2 {
var u = "\u{007f";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U3.hx
@@ -0,0 +1,3 @@
class U3 {
var u = "\u{}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U4.hx
@@ -0,0 +1,3 @@
class U4 {
var u = "\u{007g}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U5.hx
@@ -0,0 +1,3 @@
class U4 {
var u = "\u{110000}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/X1.hx
@@ -0,0 +1,3 @@
class X1 {
var x = "\x";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/X2.hx
@@ -0,0 +1,3 @@
class X2 {
var x = "\xfg";
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile-fail.hxml
@@ -0,0 +1 @@
X1
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile2-fail.hxml
@@ -0,0 +1 @@
X2
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile3-fail.hxml
@@ -0,0 +1 @@
U1
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile4-fail.hxml
@@ -0,0 +1 @@
U2
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile5-fail.hxml
@@ -0,0 +1 @@
U3
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile6-fail.hxml
@@ -0,0 +1 @@
U4
1 change: 1 addition & 0 deletions 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 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile7-fail.hxml
@@ -0,0 +1 @@
U5
1 change: 1 addition & 0 deletions 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}

0 comments on commit 032d959

Please sign in to comment.