forked from ManasJayanth/flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_parser.ml
326 lines (315 loc) · 12.8 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
(**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
open Token
open Parser_common
open Parser_env
open Ast
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 key, pattern, shorthand = match prop with
| Property.Init { key; value; shorthand } ->
key, from_expr env value, shorthand
| Property.Method { key; value = (loc, f) } ->
error_at env (loc, Parse_error.MethodInDestructuring);
key, (loc, Pattern.Expression (loc, Ast.Expression.Function f)), false
| Property.Get { key; value = (loc, f) }
| Property.Set { key; value = (loc, f) } ->
(* these should never happen *)
error_at env (loc, Parse_error.UnexpectedIdentifier);
key, (loc, Pattern.Expression (loc, Ast.Expression.Function f)), false
in
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 acc = Pattern.(Object.Property (loc, { Object.Property.
key;
pattern;
shorthand;
})) :: 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 }) ->
loc, Pattern.(Object { Object.
properties = properties env [] props;
annot = None;
})
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 begin
error_at env (loc, Parse_error.InvalidLHSInAssignment);
None
end
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 (_, Assignment { Assignment.
operator = Assignment.Assign; _
} as expr))::remaining ->
(* AssignmentElement is a `DestructuringAssignmentTarget Initializer`, see
#prod-AssignmentElement *)
let acc = Some (Pattern.Array.Element (from_expr env expr)) :: 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 expr -> (Some (Pattern.Array.Element expr)) :: acc
| None -> acc
in
elements env acc remaining
| None::remaining ->
elements env (None::acc) remaining
)
in
fun env (loc, { Ast.Expression.Array.elements = elems }) ->
loc, Pattern.Array { Pattern.Array.
elements = elements env [] elems;
annot = None;
}
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, string_val) 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 begin
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)
end;
loc, Pattern.Identifier { Pattern.Identifier.
name;
annot = None;
optional = false;
}
| Assignment { Assignment.operator = Assignment.Assign; left; right } ->
loc, Pattern.Assignment { Pattern.Assignment.left; right }
| expr -> loc, Pattern.Expression (loc, expr))
(* Parse object destructuring pattern *)
let rec object_ restricted_error =
let rec property env =
if Peek.token env = T_ELLIPSIS then begin
let loc, argument = with_loc (fun env ->
Expect.token env T_ELLIPSIS;
pattern env restricted_error
) env in
Some Pattern.Object.(RestProperty (loc, { RestProperty.
argument
}))
end else begin
let start_loc = Peek.loc env in
let key = Ast.Expression.Object.Property.(
match Parse.object_key env 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
let prop = match Peek.token env with
| T_COLON ->
Expect.token env T_COLON;
Some (pattern env restricted_error, false)
| _ ->
(match key with
| Pattern.Object.Property.Identifier ((id_loc, string_val) as name) ->
(* #sec-identifiers-static-semantics-early-errors *)
begin
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)
end;
let pattern = (id_loc, Pattern.Identifier { Pattern.Identifier.
name;
annot = None;
optional = false;
}) in
Some (pattern, true)
| _ ->
error_unexpected env; (* invalid shorthand destructuring *)
None)
in
match prop with
| Some (pattern, shorthand) ->
let pattern = match Peek.token env with
| T_ASSIGN ->
Expect.token env T_ASSIGN;
let default = Parse.assignment env in
let loc = Loc.btwn (fst pattern) (fst default) in
loc, Pattern.(Assignment Assignment.({
left = pattern;
right = default;
}));
| _ -> pattern
in
let loc = Loc.btwn start_loc (fst pattern) in
Some Pattern.Object.(Property (loc, Property.({
key;
pattern;
shorthand;
})))
| None -> None
end
(* 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 begin
error_at env (loc, Parse_error.PropertyAfterRestProperty);
None
end
else rest_trailing_comma
in
let seen_rest, rest_trailing_comma = begin 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 end 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 Some (Type.annotation env)
else None
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 begin
error_at env (loc, Parse_error.ElementAfterRestElement);
if Peek.token env = T_COMMA then Eat.token env
end;
elements env ((Some element)::acc)
| _ ->
let pattern = pattern env restricted_error in
let pattern = match Peek.token env with
| T_ASSIGN ->
Expect.token env T_ASSIGN;
let default = Parse.assignment env in
let loc = Loc.btwn (fst pattern) (fst default) in
loc, Pattern.(Assignment Assignment.({
left = pattern;
right = default;
}))
| _ -> pattern
in
let element = Pattern.Array.(Element pattern) in
if Peek.token env <> T_RBRACKET then Expect.token env T_COMMA;
elements env ((Some element)::acc)
in
with_loc (fun env ->
Expect.token env T_LBRACKET;
let elements = elements env [] in
Expect.token env T_RBRACKET;
let annot =
if Peek.token env = T_COLON then Some (Type.annotation env)
else None
in
Pattern.Array { Pattern.Array.elements; annot; }
)
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