Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages for \x and \u #8192

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/core/ast.ml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class U1 {
var u = "\u";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U2.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class U2 {
var u = "\u{007f";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U3.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class U3 {
var u = "\u{}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U4.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class U4 {
var u = "\u{007g}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/U5.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class U4 {
var u = "\u{110000}";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/X1.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class X1 {
var x = "\x";
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue8191/X2.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class X2 {
var x = "\xfg";
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
X1
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
X2
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile2-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U1
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile3-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U2
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile4-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U3
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile5-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U4
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile6-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U5
1 change: 1 addition & 0 deletions tests/misc/projects/Issue8191/compile7-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
U5.hx:2: character 11 : Invalid escape sequence \u. Maximum allowed value for unicode escape sequence is \u{10FFFF}