Skip to content

Commit 6f77efa

Browse files
authored
Fix exponential syntax (#7174)
* Check for digits after exponent char * Add tests * Update CHANGELOG * Better variable name * Update CHANGELOG
1 parent 5ec125a commit 6f77efa

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
> - :nail_care: [Polish]
1212
1313
# 12.0.0-alpha.6 (Unreleased)
14+
- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174
1415

1516
# 12.0.0-alpha.5
1617

compiler/syntax/src/res_scanner.ml

+24-13
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,30 @@ let scan_identifier scanner =
209209

210210
let scan_digits scanner ~base =
211211
if base <= 10 then
212-
let rec loop scanner =
212+
let rec loop scanner found_digits =
213213
match scanner.ch with
214-
| '0' .. '9' | '_' ->
214+
| '0' .. '9' ->
215215
next scanner;
216-
loop scanner
217-
| _ -> ()
216+
loop scanner true
217+
| '_' ->
218+
next scanner;
219+
loop scanner false
220+
| _ -> found_digits
218221
in
219-
loop scanner
222+
loop scanner false
220223
else
221-
let rec loop scanner =
224+
let rec loop scanner found_digits =
222225
match scanner.ch with
223226
(* hex *)
224-
| '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' ->
227+
| '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' ->
228+
next scanner;
229+
loop scanner true
230+
| '_' ->
225231
next scanner;
226-
loop scanner
227-
| _ -> ()
232+
loop scanner false
233+
| _ -> found_digits
228234
in
229-
loop scanner
235+
loop scanner false
230236

231237
(* float: (0…9) { 0…9∣ _ } [. { 0…9∣ _ }] [(e∣ E) [+∣ -] (0…9) { 0…9∣ _ }] *)
232238
let scan_number scanner =
@@ -251,25 +257,30 @@ let scan_number scanner =
251257
8)
252258
| _ -> 10
253259
in
254-
scan_digits scanner ~base;
260+
ignore (scan_digits scanner ~base);
255261

256262
(* *)
257263
let is_float =
258264
if '.' == scanner.ch then (
259265
next scanner;
260-
scan_digits scanner ~base;
266+
ignore (scan_digits scanner ~base);
261267
true)
262268
else false
263269
in
264270

265271
(* exponent part *)
266272
let is_float =
273+
let start_pos = position scanner in
267274
match scanner.ch with
268275
| 'e' | 'E' | 'p' | 'P' ->
269276
(match peek scanner with
270277
| '+' | '-' -> next2 scanner
271278
| _ -> next scanner);
272-
scan_digits scanner ~base;
279+
let end_pos = position scanner in
280+
let found_digits = scan_digits scanner ~base in
281+
if not found_digits then
282+
scanner.err ~start_pos ~end_pos
283+
(Diagnostics.message "Expected digits after exponential notation.");
273284
true
274285
| _ -> is_float
275286
in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
Syntax error!
3+
syntax_tests/data/parsing/errors/scanner/exponent_notation.res:7:10
4+
5+
5 │ let x = 1_e_1
6+
6 │
7+
7 │ let x = 1e
8+
8 │
9+
9 │ let x = 1_e_
10+
11+
Expected digits after exponential notation.
12+
13+
14+
Syntax error!
15+
syntax_tests/data/parsing/errors/scanner/exponent_notation.res:9:11
16+
17+
7 │ let x = 1e
18+
8 │
19+
9 │ let x = 1_e_
20+
10 │
21+
11 │ let x = 1.
22+
23+
Expected digits after exponential notation.
24+
25+
let x = 1e1
26+
let x = 1e_1
27+
let x = 1_e_1
28+
let x = 1e
29+
let x = 1_e_
30+
let x = 1.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let x = 1e1
2+
3+
let x = 1e_1
4+
5+
let x = 1_e_1
6+
7+
let x = 1e
8+
9+
let x = 1_e_
10+
11+
let x = 1.

0 commit comments

Comments
 (0)