forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_parser.ml
364 lines (355 loc) · 14.3 KB
/
pattern_parser.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
module Ast = Flow_ast
open Token
open Parser_common
open Parser_env
open Flow_ast
let missing_annot env = Ast.Type.Missing (Peek.loc_skip_lookahead env)
module Pattern (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) = struct
(* Reinterpret various expressions as patterns.
* This is not the correct thing to do and is only used for assignment
* expressions. This should be removed and replaced ASAP.
*)
let rec object_from_expr =
let rec properties env acc =
Ast.Expression.Object.(
function
| [] -> List.rev acc
| Property (loc, prop) :: remaining ->
let acc =
match prop with
| Property.Init { key; value; shorthand } ->
Ast.Expression.(
let key =
match key with
| Property.Literal lit -> Pattern.Object.Property.Literal lit
| Property.Identifier id -> Pattern.Object.Property.Identifier id
| Property.PrivateName _ -> failwith "Internal Error: Found object private prop"
| Property.Computed expr -> Pattern.Object.Property.Computed expr
in
let (pattern, default) =
match value with
| (_loc, Assignment { Assignment.operator = None; left; right }) ->
(left, Some right)
| _ -> (from_expr env value, None)
in
Pattern.Object.Property
(loc, { Pattern.Object.Property.key; pattern; default; shorthand })
:: acc)
| Property.Method { key = _; value = (loc, _) } ->
error_at env (loc, Parse_error.MethodInDestructuring);
acc
| Property.Get { key = _; value = (loc, _) }
| Property.Set { key = _; value = (loc, _) } ->
(* these should never happen *)
error_at env (loc, Parse_error.Unexpected "identifier");
acc
in
properties env acc remaining
| [SpreadProperty (loc, { SpreadProperty.argument })] ->
let acc =
Pattern.Object.(RestProperty (loc, { RestProperty.argument = from_expr env argument }))
:: acc
in
properties env acc []
| SpreadProperty (loc, _) :: remaining ->
error_at env (loc, Parse_error.PropertyAfterRestProperty);
properties env acc remaining)
in
fun env (loc, { Ast.Expression.Object.properties = props; comments = _ (* TODO *) }) ->
( loc,
Pattern.(Object { Object.properties = properties env [] props; annot = missing_annot env })
)
and array_from_expr =
(* Convert an Expression to a Pattern if it is a valid
DestructuringAssignmentTarget, which must be an Object, Array or
IsValidSimpleAssignmentTarget.
#sec-destructuring-assignment-static-semantics-early-errors *)
let assignment_target env ((loc, _) as expr) =
if Parse.is_assignable_lhs expr then
Some (from_expr env expr)
else (
error_at env (loc, Parse_error.InvalidLHSInAssignment);
None
)
in
let rec elements env acc =
Ast.Expression.(
function
| [] -> List.rev acc
| [Some (Spread (loc, { SpreadElement.argument }))] ->
(* AssignmentRestElement is a DestructuringAssignmentTarget, see
#prod-AssignmentRestElement *)
let acc =
match assignment_target env argument with
| Some argument ->
Some Pattern.Array.(RestElement (loc, { RestElement.argument })) :: acc
| None -> acc
in
elements env acc []
| Some (Spread (loc, _)) :: remaining ->
error_at env (loc, Parse_error.ElementAfterRestElement);
elements env acc remaining
| Some (Expression (loc, Assignment { Assignment.operator = None; left; right }))
:: remaining ->
(* AssignmentElement is a `DestructuringAssignmentTarget Initializer`, see
#prod-AssignmentElement *)
let acc =
Some
(Pattern.Array.Element
(loc, { Pattern.Array.Element.argument = left; default = Some right }))
:: acc
in
elements env acc remaining
| Some (Expression expr) :: remaining ->
(* AssignmentElement is a DestructuringAssignmentTarget, see
#prod-AssignmentElement *)
let acc =
match assignment_target env expr with
| Some ((loc, _) as expr) ->
let element =
Pattern.Array.Element
(loc, { Pattern.Array.Element.argument = expr; default = None })
in
Some element :: acc
| None -> acc
in
elements env acc remaining
| None :: remaining -> elements env (None :: acc) remaining)
in
fun env (loc, { Ast.Expression.Array.elements = elems; comments }) ->
( loc,
Pattern.Array
{ Pattern.Array.elements = elements env [] elems; annot = missing_annot env; comments } )
and from_expr env (loc, expr) =
Ast.Expression.(
match expr with
| Object obj -> object_from_expr env (loc, obj)
| Array arr -> array_from_expr env (loc, arr)
| Identifier ((id_loc, { Identifier.name = string_val; comments = _ }) as name) ->
(* per #sec-destructuring-assignment-static-semantics-early-errors,
it is a syntax error if IsValidSimpleAssignmentTarget of this
IdentifierReference is false. That happens when `string_val` is
"eval" or "arguments" in strict mode. *)
if in_strict_mode env && is_restricted string_val then
error_at env (id_loc, Parse_error.StrictLHSAssignment)
(* per #prod-IdentifierReference, yield is only a valid
IdentifierReference when [~Yield], and await is only valid
when [~Await]. but per #sec-identifiers-static-semantics-early-errors,
they are already invalid in strict mode, which we should have
already errored about when parsing the expression that we're now
converting into a pattern. *)
else if not (in_strict_mode env) then
if allow_yield env && string_val = "yield" then
error_at env (id_loc, Parse_error.YieldAsIdentifierReference)
else if allow_await env && string_val = "await" then
error_at env (id_loc, Parse_error.AwaitAsIdentifierReference);
( loc,
Pattern.Identifier
{ Pattern.Identifier.name; annot = missing_annot env; optional = false } )
| expr -> (loc, Pattern.Expression (loc, expr)))
(* Parse object destructuring pattern *)
let rec object_ restricted_error =
let rest_property env =
let (loc, argument) =
with_loc
(fun env ->
Expect.token env T_ELLIPSIS;
pattern env restricted_error)
env
in
Pattern.Object.(RestProperty (loc, { RestProperty.argument }))
in
let property_default env =
match Peek.token env with
| T_ASSIGN ->
Expect.token env T_ASSIGN;
Some (Parse.assignment env)
| _ -> None
in
let rec property env =
if Peek.token env = T_ELLIPSIS then
Some (rest_property env)
else
let start_loc = Peek.loc env in
let raw_key = Parse.object_key env in
match Peek.token env with
| T_COLON ->
Expect.token env T_COLON;
let (loc, (pattern, default)) =
with_loc
~start_loc
(fun env ->
let pattern = pattern env restricted_error in
let default = property_default env in
(pattern, default))
env
in
let key =
Ast.Expression.Object.Property.(
match raw_key with
| (_, Literal lit) -> Pattern.Object.Property.Literal lit
| (_, Identifier id) -> Pattern.Object.Property.Identifier id
| (_, PrivateName _) -> failwith "Internal Error: Found object private prop"
| (_, Computed expr) -> Pattern.Object.Property.Computed expr)
in
Some
Pattern.Object.(Property (loc, Property.{ key; pattern; default; shorthand = false }))
| _ ->
(match raw_key with
| ( _,
Ast.Expression.Object.Property.Identifier
((id_loc, { Identifier.name = string_val; comments = _ }) as name) ) ->
(* #sec-identifiers-static-semantics-early-errors *)
if is_reserved string_val && string_val <> "yield" && string_val <> "await" then
(* it is a syntax error if `name` is a reserved word other than await or yield *)
error_at env (id_loc, Parse_error.UnexpectedReserved)
else if is_strict_reserved string_val then
(* it is a syntax error if `name` is a strict reserved word, in strict mode *)
strict_error_at env (id_loc, Parse_error.StrictReservedWord);
let (loc, (pattern, default)) =
with_loc
~start_loc
(fun env ->
let pattern =
( id_loc,
Pattern.Identifier
{ Pattern.Identifier.name; annot = missing_annot env; optional = false } )
in
let default = property_default env in
(pattern, default))
env
in
Some
Pattern.Object.(
Property
( loc,
{ Property.key = Property.Identifier name; pattern; default; shorthand = true }
))
| _ ->
error_unexpected ~expected:"an identifier" env;
(* invalid shorthand destructuring *)
None)
(* seen_rest is true when we've seen a rest element. rest_trailing_comma is the location of
* the rest element's trailing command
* Trailing comma: `let { ...rest, } = obj`
* Still invalid, but not a trailing comma: `let { ...rest, x } = obj` *)
and properties env ~seen_rest ~rest_trailing_comma acc =
match Peek.token env with
| T_EOF
| T_RCURLY ->
begin
match rest_trailing_comma with
| Some loc -> error_at env (loc, Parse_error.TrailingCommaAfterRestElement)
| None -> ()
end;
List.rev acc
| _ ->
(match property env with
| Some ((Pattern.Object.Property (loc, _) | Pattern.Object.RestProperty (loc, _)) as prop)
->
let rest_trailing_comma =
if seen_rest then (
error_at env (loc, Parse_error.PropertyAfterRestProperty);
None
) else
rest_trailing_comma
in
let (seen_rest, rest_trailing_comma) =
match prop with
| Pattern.Object.RestProperty _ ->
( true,
if Peek.token env = T_COMMA then
Some (Peek.loc env)
else
None )
| _ -> (seen_rest, rest_trailing_comma)
in
if Peek.token env <> T_RCURLY then Expect.token env T_COMMA;
properties env ~seen_rest ~rest_trailing_comma (prop :: acc)
| None -> properties env ~seen_rest ~rest_trailing_comma acc)
in
with_loc (fun env ->
Expect.token env T_LCURLY;
let properties = properties env ~seen_rest:false ~rest_trailing_comma:None [] in
Expect.token env T_RCURLY;
let annot =
if Peek.token env = T_COLON then
Ast.Type.Available (Type.annotation env)
else
missing_annot env
in
Pattern.Object { Pattern.Object.properties; annot })
(* Parse array destructuring pattern *)
and array_ restricted_error =
let rec elements env acc =
match Peek.token env with
| T_EOF
| T_RBRACKET ->
List.rev acc
| T_COMMA ->
Expect.token env T_COMMA;
elements env (None :: acc)
| T_ELLIPSIS ->
let (loc, argument) =
with_loc
(fun env ->
Expect.token env T_ELLIPSIS;
pattern env restricted_error)
env
in
let element = Pattern.Array.(RestElement (loc, { RestElement.argument })) in
(* rest elements are always last, the closing ] should be next. but if not,
error and keep going so we recover gracefully by parsing the rest of the
elements. *)
if Peek.token env <> T_RBRACKET then (
error_at env (loc, Parse_error.ElementAfterRestElement);
if Peek.token env = T_COMMA then Eat.token env
);
elements env (Some element :: acc)
| _ ->
let (loc, (pattern, default)) =
with_loc
(fun env ->
let pattern = pattern env restricted_error in
let default =
match Peek.token env with
| T_ASSIGN ->
Expect.token env T_ASSIGN;
Some (Parse.assignment env)
| _ -> None
in
(pattern, default))
env
in
let element = Pattern.Array.(Element (loc, { Element.argument = pattern; default })) in
if Peek.token env <> T_RBRACKET then Expect.token env T_COMMA;
elements env (Some element :: acc)
in
with_loc (fun env ->
let leading = Peek.comments env in
Expect.token env T_LBRACKET;
let elements = elements env [] in
Expect.token env T_RBRACKET;
let annot =
if Peek.token env = T_COLON then
Ast.Type.Available (Type.annotation env)
else
missing_annot env
in
let trailing = Peek.comments env in
let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in
Pattern.Array { Pattern.Array.elements; annot; comments })
and pattern env restricted_error =
match Peek.token env with
| T_LCURLY -> object_ restricted_error env
| T_LBRACKET -> array_ restricted_error env
| _ ->
let (loc, id) = Parse.identifier_with_type env restricted_error in
(loc, Pattern.Identifier id)
end