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

Fix exponential notation syntax #7105

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
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 flag =
match scanner.ch with
| '0' .. '9' | '_' ->
| '0' .. '9' ->
next scanner;
loop scanner
| _ -> ()
loop scanner true
| '_' ->
next scanner;
loop scanner false
| _ -> flag
in
loop scanner
loop scanner false
else
let rec loop scanner =
let rec loop scanner flag =
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
| _ -> flag
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.