Skip to content

Commit e011b06

Browse files
shulhicknitt
authored andcommitted
Fix exponential syntax (#7174)
1 parent 0e37667 commit e011b06

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- Handle absolute file paths in gentype. https://github.com/rescript-lang/rescript-compiler/pull/7111
2424
- Fix "rescript format" with many files. https://github.com/rescript-lang/rescript-compiler/pull/7081
25+
- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174
2526

2627
#### :house: Internal
2728

jscomp/syntax/src/res_scanner.ml

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,30 @@ let scanIdentifier scanner =
203203

204204
let scanDigits scanner ~base =
205205
if base <= 10 then
206-
let rec loop scanner =
206+
let rec loop scanner foundDigits =
207207
match scanner.ch with
208-
| '0' .. '9' | '_' ->
208+
| '0' .. '9' ->
209209
next scanner;
210-
loop scanner
211-
| _ -> ()
210+
loop scanner true
211+
| '_' ->
212+
next scanner;
213+
loop scanner false
214+
| _ -> foundDigits
212215
in
213-
loop scanner
216+
loop scanner false
214217
else
215-
let rec loop scanner =
218+
let rec loop scanner foundDigits =
216219
match scanner.ch with
217220
(* hex *)
218-
| '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' ->
221+
| '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' ->
222+
next scanner;
223+
loop scanner true
224+
| '_' ->
219225
next scanner;
220-
loop scanner
221-
| _ -> ()
226+
loop scanner false
227+
| _ -> foundDigits
222228
in
223-
loop scanner
229+
loop scanner false
224230

225231
(* float: (0…9) { 0…9∣ _ } [. { 0…9∣ _ }] [(e∣ E) [+∣ -] (0…9) { 0…9∣ _ }] *)
226232
let scanNumber scanner =
@@ -245,25 +251,30 @@ let scanNumber scanner =
245251
8)
246252
| _ -> 10
247253
in
248-
scanDigits scanner ~base;
254+
ignore (scanDigits scanner ~base);
249255

250256
(* *)
251257
let isFloat =
252258
if '.' == scanner.ch then (
253259
next scanner;
254-
scanDigits scanner ~base;
260+
ignore (scanDigits scanner ~base);
255261
true)
256262
else false
257263
in
258264

259265
(* exponent part *)
260266
let isFloat =
267+
let startPos = position scanner in
261268
match scanner.ch with
262269
| 'e' | 'E' | 'p' | 'P' ->
263270
(match peek scanner with
264271
| '+' | '-' -> next2 scanner
265272
| _ -> next scanner);
266-
scanDigits scanner ~base;
273+
let endPos = position scanner in
274+
let foundDigits = scanDigits scanner ~base in
275+
if not foundDigits then
276+
scanner.err ~startPos ~endPos
277+
(Diagnostics.message "Expected digits after exponential notation.");
267278
true
268279
| _ -> isFloat
269280
in
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
Syntax error!
3+
tests/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+
tests/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.
Lines changed: 11 additions & 0 deletions
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)