Skip to content

Commit 5f6278f

Browse files
committed
ban empty ident in syntax-level
1 parent c634719 commit 5f6278f

File tree

3 files changed

+92
-26
lines changed

3 files changed

+92
-26
lines changed

jscomp/syntax/src/res_scanner.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,12 @@ let scanNumber scanner =
291291
else Token.Int {i = literal; suffix}
292292

293293
let scanExoticIdentifier scanner =
294-
(* TODO: are we disregarding the current char...? Should be a quote *)
295-
next scanner;
296294
let buffer = Buffer.create 20 in
297295
let startPos = position scanner in
298296

297+
(* TODO: are we disregarding the current char...? Should be a quote *)
298+
next scanner;
299+
299300
let rec scan () =
300301
match scanner.ch with
301302
| '"' -> next scanner
@@ -315,8 +316,17 @@ let scanExoticIdentifier scanner =
315316
scan ()
316317
in
317318
scan ();
319+
320+
let ident = Buffer.contents buffer in
321+
let _ =
322+
if ident = "" then
323+
let endPos = position scanner in
324+
scanner.err ~startPos ~endPos
325+
(Diagnostics.message "A quoted identifier can't be empty string.")
326+
in
327+
318328
(* TODO: do we really need to create a new buffer instead of substring once? *)
319-
Token.Lident (Buffer.contents buffer)
329+
Token.Lident ident
320330

321331
let scanStringEscapeSequence ~startPos scanner =
322332
let scan ~n ~base ~max =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
type \""
2+
3+
type \"" = int
4+
5+
let \""
6+
17
let \"a
28
b
39
c" = 1
Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,97 @@
11

22
Syntax error!
3-
tests/parsing/errors/scanner/exoticIdent.res:1:7
3+
tests/parsing/errors/scanner/exoticIdent.res:1:7-8
44

5-
1 │ let \"a
6-
2 │ b
7-
3 │ c" = 1
5+
1 │ type \""
6+
2 │
7+
3 │ type \"" = int
88

9-
A quoted identifier can't contain line breaks.
9+
A quoted identifier can't be empty string.
1010

1111

1212
Syntax error!
13-
tests/parsing/errors/scanner/exoticIdent.res:2:1
13+
tests/parsing/errors/scanner/exoticIdent.res:3:7-8
1414

15-
1 │ let \"a
16-
2 │ b
17-
3 │ c" = 1
15+
1 │ type \""
16+
2 │
17+
3 │ type \"" = int
1818
4 │
19+
5 │ let \""
1920

20-
Did you forget a `=` here?
21+
A quoted identifier can't be empty string.
2122

2223

2324
Syntax error!
24-
tests/parsing/errors/scanner/exoticIdent.res:3:2-4:0
25+
tests/parsing/errors/scanner/exoticIdent.res:5:6-7
2526

26-
1 │ let \"a
27-
2 │ b
28-
3 │ c" = 1
27+
3 │ type \"" = int
2928
4 │
29+
5 │ let \""
30+
6 │
31+
7 │ let \"a
3032

31-
This string is missing a double quote at the end
33+
A quoted identifier can't be empty string.
3234

3335

3436
Syntax error!
35-
tests/parsing/errors/scanner/exoticIdent.res:3:2-4:0
37+
tests/parsing/errors/scanner/exoticIdent.res:5:8-7:3
3638

37-
1 │ let \"a
38-
2 │ b
39-
3 │ c" = 1
39+
3 │ type \"" = int
4040
4 │
41+
5 │ let \""
42+
6 │
43+
7 │ let \"a
44+
8 │ b
45+
9 │ c" = 1
46+
47+
Did you forget a `=` here?
48+
49+
50+
Syntax error!
51+
tests/parsing/errors/scanner/exoticIdent.res:7:6-7
52+
53+
5 │ let \""
54+
6 │
55+
7 │ let \"a
56+
8 │ b
57+
9 │ c" = 1
58+
59+
A quoted identifier can't contain line breaks.
60+
61+
62+
Syntax error!
63+
tests/parsing/errors/scanner/exoticIdent.res:8:1
64+
65+
6 │
66+
7 │ let \"a
67+
8 │ b
68+
9 │ c" = 1
69+
10 │
70+
71+
Did you forget a `=` here?
72+
73+
74+
Syntax error!
75+
tests/parsing/errors/scanner/exoticIdent.res:9:2-10:0
76+
77+
7 │ let \"a
78+
8 │ b
79+
9 │ c" = 1
80+
10 │
81+
82+
This string is missing a double quote at the end
83+
84+
85+
Syntax error!
86+
tests/parsing/errors/scanner/exoticIdent.res:9:2-10:0
87+
88+
7 │ let \"a
89+
8 │ b
90+
9 │ c" = 1
91+
10 │
4192

4293
consecutive statements on a line must be separated by ';' or a newline
4394

44-
let a = b
45-
;;c
46-
;;{js| = 1
47-
|js}
95+
type nonrec
96+
type nonrec = int
97+
let Fatal error: exception Invalid_argument("index out of bounds")

0 commit comments

Comments
 (0)