diff --git a/CHANGELOG.md b/CHANGELOG.md index bb20b8affc..d6088856cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ > - :nail_care: [Polish] # 12.0.0-alpha.6 (Unreleased) +- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174 # 12.0.0-alpha.5 diff --git a/compiler/syntax/src/res_scanner.ml b/compiler/syntax/src/res_scanner.ml index 2ee1828a35..613d273d6c 100644 --- a/compiler/syntax/src/res_scanner.ml +++ b/compiler/syntax/src/res_scanner.ml @@ -209,24 +209,30 @@ let scan_identifier scanner = let scan_digits scanner ~base = if base <= 10 then - let rec loop scanner = + let rec loop scanner found_digits = match scanner.ch with - | '0' .. '9' | '_' -> + | '0' .. '9' -> next scanner; - loop scanner - | _ -> () + loop scanner true + | '_' -> + next scanner; + loop scanner false + | _ -> found_digits in - loop scanner + loop scanner false else - let rec loop scanner = + let rec loop scanner found_digits = match scanner.ch with (* hex *) - | '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' -> + | '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' -> + next scanner; + loop scanner true + | '_' -> next scanner; - loop scanner - | _ -> () + loop scanner false + | _ -> found_digits in - loop scanner + loop scanner false (* float: (0…9) { 0…9∣ _ } [. { 0…9∣ _ }] [(e∣ E) [+∣ -] (0…9) { 0…9∣ _ }] *) let scan_number scanner = @@ -251,25 +257,30 @@ let scan_number scanner = 8) | _ -> 10 in - scan_digits scanner ~base; + ignore (scan_digits scanner ~base); (* *) let is_float = if '.' == scanner.ch then ( next scanner; - scan_digits scanner ~base; + ignore (scan_digits scanner ~base); true) else false in (* exponent part *) let is_float = + let start_pos = position scanner in match scanner.ch with | 'e' | 'E' | 'p' | 'P' -> (match peek scanner with | '+' | '-' -> next2 scanner | _ -> next scanner); - scan_digits scanner ~base; + let end_pos = position scanner in + let found_digits = scan_digits scanner ~base in + if not found_digits then + scanner.err ~start_pos ~end_pos + (Diagnostics.message "Expected digits after exponential notation."); true | _ -> is_float in diff --git a/tests/syntax_tests/data/parsing/errors/scanner/expected/exponent_notation.res.txt b/tests/syntax_tests/data/parsing/errors/scanner/expected/exponent_notation.res.txt new file mode 100644 index 0000000000..542d37ad26 --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/scanner/expected/exponent_notation.res.txt @@ -0,0 +1,30 @@ + + Syntax error! + syntax_tests/data/parsing/errors/scanner/exponent_notation.res:7:10 + + 5 │ let x = 1_e_1 + 6 │ + 7 │ let x = 1e + 8 │ + 9 │ let x = 1_e_ + + Expected digits after exponential notation. + + + Syntax error! + syntax_tests/data/parsing/errors/scanner/exponent_notation.res:9:11 + + 7 │ let x = 1e + 8 │ + 9 │ let x = 1_e_ + 10 │ + 11 │ let x = 1. + + Expected digits after exponential notation. + +let x = 1e1 +let x = 1e_1 +let x = 1_e_1 +let x = 1e +let x = 1_e_ +let x = 1. \ No newline at end of file diff --git a/tests/syntax_tests/data/parsing/errors/scanner/exponent_notation.res b/tests/syntax_tests/data/parsing/errors/scanner/exponent_notation.res new file mode 100644 index 0000000000..623c94ccfd --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/scanner/exponent_notation.res @@ -0,0 +1,11 @@ +let x = 1e1 + +let x = 1e_1 + +let x = 1_e_1 + +let x = 1e + +let x = 1_e_ + +let x = 1.