Skip to content

Commit

Permalink
Fix exponential syntax (#7174)
Browse files Browse the repository at this point in the history
* Check for digits after exponent char

* Add tests

* Update CHANGELOG

* Better variable name

* Update CHANGELOG
  • Loading branch information
shulhi authored Nov 25, 2024
1 parent 5ec125a commit 6f77efa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 24 additions & 13 deletions compiler/syntax/src/res_scanner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 6f77efa

Please sign in to comment.