Skip to content

Commit 20fb4e4

Browse files
gkzmeta-codesync[bot]
authored andcommitted
[flow][parser] Better parser recovery from 'const enum' error
Summary: Better parser recovery from 'const enum' error: parse the remainder as a enum rather than a mess of unexpected tokens. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D87126408 fbshipit-source-id: 75e1c07aa7f4165f58682ee6967440bece7b046d
1 parent 6bfee0a commit 20fb4e4

File tree

4 files changed

+41
-113
lines changed

4 files changed

+41
-113
lines changed

src/parser/declaration_parser.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,6 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Parser_common.TYPE) :
443443
let declarations token env =
444444
let leading = Peek.comments env in
445445
Expect.token env token;
446-
if (parse_options env).enums && token = T_CONST && Peek.token env = T_ENUM then
447-
error env Parse_error.EnumInvalidConstPrefix;
448446
let (declarations, errs) = variable_declaration_list env in
449447
(declarations, leading, errs)
450448

src/parser/parser_flow.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ module rec Parse : PARSER = struct
321321
(* Remember kids, these look like statements but they're not
322322
* statements... (see section 13) *)
323323
| T_LET -> let_ env
324+
| T_CONST when (parse_options env).enums && Peek.ith_token ~i:1 env = T_ENUM ->
325+
error env Parse_error.EnumInvalidConstPrefix;
326+
Eat.token env;
327+
Declaration.enum_declaration env
324328
| T_CONST -> const env
325329
| _ when Peek.is_function env || Peek.is_hook env -> Declaration._function env
326330
| _ when Peek.is_class env -> class_declaration env decorators

src/parser/test/flow/enums/const-enum.tree.json

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,34 @@
11
{
22
"errors":[
33
{
4-
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
4+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
55
"message":"`const` enums are not supported. Flow Enums are designed to allow for inlining, however the inlining itself needs to be part of the build system (whatever you use) rather than Flow itself."
6-
},
7-
{
8-
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
9-
"message":"Unexpected reserved word"
10-
},
11-
{
12-
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":12}},
13-
"message":"Unexpected identifier, expected the token `;`"
14-
},
15-
{
16-
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
17-
"message":"Const must be initialized"
18-
},
19-
{
20-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":14}},
21-
"message":"Unexpected token `{`, expected the end of an expression statement (`;`)"
226
}
237
],
248
"type":"Program",
25-
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
26-
"range":[0,15],
9+
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":15}},
10+
"range":[6,15],
2711
"body":[
2812
{
29-
"type":"VariableDeclaration",
30-
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
31-
"range":[0,10],
32-
"declarations":[
33-
{
34-
"type":"VariableDeclarator",
35-
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
36-
"range":[6,10],
37-
"id":{
38-
"type":"Identifier",
39-
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
40-
"range":[6,10],
41-
"name":"enum",
42-
"typeAnnotation":null,
43-
"optional":false
44-
},
45-
"init":null
46-
}
47-
],
48-
"kind":"const"
49-
},
50-
{
51-
"type":"ExpressionStatement",
52-
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":12}},
53-
"range":[11,12],
54-
"expression":{
13+
"type":"EnumDeclaration",
14+
"loc":{"source":null,"start":{"line":1,"column":6},"end":{"line":1,"column":15}},
15+
"range":[6,15],
16+
"id":{
5517
"type":"Identifier",
5618
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":12}},
5719
"range":[11,12],
5820
"name":"E",
5921
"typeAnnotation":null,
6022
"optional":false
6123
},
62-
"directive":null
63-
},
64-
{
65-
"type":"BlockStatement",
66-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":15}},
67-
"range":[13,15],
68-
"body":[]
24+
"body":{
25+
"type":"EnumStringBody",
26+
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":15}},
27+
"range":[13,15],
28+
"members":[],
29+
"explicitType":false,
30+
"hasUnknownMembers":false
31+
}
6932
}
7033
],
7134
"comments":[]

src/parser/test/flow/enums/export-const-enum.tree.json

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
{
22
"errors":[
33
{
4-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
4+
"loc":{"source":null,"start":{"line":1,"column":7},"end":{"line":1,"column":12}},
55
"message":"`const` enums are not supported. Flow Enums are designed to allow for inlining, however the inlining itself needs to be part of the build system (whatever you use) rather than Flow itself."
6-
},
7-
{
8-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
9-
"message":"Unexpected reserved word"
10-
},
11-
{
12-
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":19}},
13-
"message":"Unexpected identifier, expected the token `;`"
14-
},
15-
{
16-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
17-
"message":"Const must be initialized"
18-
},
19-
{
20-
"loc":{"source":null,"start":{"line":1,"column":20},"end":{"line":1,"column":21}},
21-
"message":"Unexpected token `{`, expected the end of an expression statement (`;`)"
226
}
237
],
248
"type":"Program",
@@ -27,53 +11,32 @@
2711
"body":[
2812
{
2913
"type":"ExportNamedDeclaration",
30-
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
31-
"range":[0,17],
14+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
15+
"range":[0,22],
3216
"declaration":{
33-
"type":"VariableDeclaration",
34-
"loc":{"source":null,"start":{"line":1,"column":7},"end":{"line":1,"column":17}},
35-
"range":[7,17],
36-
"declarations":[
37-
{
38-
"type":"VariableDeclarator",
39-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
40-
"range":[13,17],
41-
"id":{
42-
"type":"Identifier",
43-
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
44-
"range":[13,17],
45-
"name":"enum",
46-
"typeAnnotation":null,
47-
"optional":false
48-
},
49-
"init":null
50-
}
51-
],
52-
"kind":"const"
17+
"type":"EnumDeclaration",
18+
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":22}},
19+
"range":[13,22],
20+
"id":{
21+
"type":"Identifier",
22+
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":19}},
23+
"range":[18,19],
24+
"name":"E",
25+
"typeAnnotation":null,
26+
"optional":false
27+
},
28+
"body":{
29+
"type":"EnumStringBody",
30+
"loc":{"source":null,"start":{"line":1,"column":20},"end":{"line":1,"column":22}},
31+
"range":[20,22],
32+
"members":[],
33+
"explicitType":false,
34+
"hasUnknownMembers":false
35+
}
5336
},
5437
"specifiers":[],
5538
"source":null,
5639
"exportKind":"value"
57-
},
58-
{
59-
"type":"ExpressionStatement",
60-
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":19}},
61-
"range":[18,19],
62-
"expression":{
63-
"type":"Identifier",
64-
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":19}},
65-
"range":[18,19],
66-
"name":"E",
67-
"typeAnnotation":null,
68-
"optional":false
69-
},
70-
"directive":null
71-
},
72-
{
73-
"type":"BlockStatement",
74-
"loc":{"source":null,"start":{"line":1,"column":20},"end":{"line":1,"column":22}},
75-
"range":[20,22],
76-
"body":[]
7740
}
7841
],
7942
"comments":[]

0 commit comments

Comments
 (0)