forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression_parser.ml
1598 lines (1544 loc) · 55.7 KB
/
expression_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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* 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_env
open Flow_ast
open Parser_common
open Comment_attachment
module type EXPRESSION = sig
val assignment : env -> (Loc.t, Loc.t) Expression.t
val assignment_cover : env -> pattern_cover
val conditional : env -> (Loc.t, Loc.t) Expression.t
val property_name_include_private :
env -> Loc.t * (Loc.t, Loc.t) Identifier.t * bool * Loc.t Comment.t list
val is_assignable_lhs : (Loc.t, Loc.t) Expression.t -> bool
val left_hand_side : env -> (Loc.t, Loc.t) Expression.t
val number : env -> number_type -> string -> float
val sequence :
env -> start_loc:Loc.t -> (Loc.t, Loc.t) Expression.t list -> (Loc.t, Loc.t) Expression.t
end
module Expression
(Parse : PARSER)
(Type : Type_parser.TYPE)
(Declaration : Declaration_parser.DECLARATION)
(Pattern_cover : Pattern_cover.COVER) : EXPRESSION = struct
type op_precedence =
| Left_assoc of int
| Right_assoc of int
type group_cover =
| Group_expr of (Loc.t, Loc.t) Expression.t
| Group_typecast of (Loc.t, Loc.t) Expression.TypeCast.t
let is_tighter a b =
let a_prec =
match a with
| Left_assoc x -> x
| Right_assoc x -> x - 1
in
let b_prec =
match b with
| Left_assoc x -> x
| Right_assoc x -> x
in
a_prec >= b_prec
let is_assignable_lhs =
let open Expression in
function
| ( _,
MetaProperty
{
MetaProperty.meta = (_, { Identifier.name = "new"; comments = _ });
property = (_, { Identifier.name = "target"; comments = _ });
comments = _;
} ) ->
false
| ( _,
MetaProperty
{
MetaProperty.meta = (_, { Identifier.name = "import"; comments = _ });
property = (_, { Identifier.name = "meta"; comments = _ });
comments = _;
} ) ->
false
| (_, Array _)
| (_, Identifier _)
| (_, Member _)
| (_, MetaProperty _)
| (_, Object _) ->
true
| (_, ArrowFunction _)
| (_, Assignment _)
| (_, Binary _)
| (_, Call _)
| (_, Class _)
| (_, Comprehension _)
| (_, Conditional _)
| (_, Function _)
| (_, Generator _)
| (_, Import _)
| (_, JSXElement _)
| (_, JSXFragment _)
| (_, Literal _)
| (_, Logical _)
| (_, New _)
| (_, OptionalCall _)
| (_, OptionalMember _)
| (_, Sequence _)
| (_, Super _)
| (_, TaggedTemplate _)
| (_, TemplateLiteral _)
| (_, This _)
| (_, TypeCast _)
| (_, Unary _)
| (_, Update _)
| (_, Yield _) ->
false
let as_expression = Pattern_cover.as_expression
let as_pattern = Pattern_cover.as_pattern
let rec assignment_cover =
let assignment_but_not_arrow_function_cover env =
let start_loc = Peek.loc env in
let expr_or_pattern = conditional_cover env in
match assignment_op env with
| Some operator ->
let expr =
with_loc
~start_loc
(fun env ->
let left = as_pattern env expr_or_pattern in
let right = assignment env in
let open Expression in
Assignment { Assignment.operator; left; right; comments = None })
env
in
Cover_expr expr
| _ -> expr_or_pattern
in
let error_callback _ = function
| Parse_error.StrictReservedWord -> ()
| _ -> raise Try.Rollback
in
let try_assignment_but_not_arrow_function env =
let env = env |> with_error_callback error_callback in
let ret = assignment_but_not_arrow_function_cover env in
match Peek.token env with
| T_ARROW -> raise Try.Rollback
| T_COLON
when match last_token env with
| Some T_RPAREN -> true
| _ -> false ->
raise Try.Rollback
| _ when Peek.is_identifier env ->
(match ret with
| Cover_expr (_, Expression.Identifier (_, { Identifier.name = "async"; comments = _ }))
when not (Peek.is_line_terminator env) ->
raise Try.Rollback
| _ -> ret)
| _ -> ret
in
fun env ->
match (Peek.token env, Peek.is_identifier env) with
| (T_YIELD, _) when allow_yield env -> Cover_expr (yield env)
| ((T_LPAREN as t), _)
| ((T_LESS_THAN as t), _)
| ((T_THIS as t), _)
| (t, true) ->
let (initial, secondary) =
if t = T_ASYNC && should_parse_types env && Peek.ith_token ~i:1 env = T_LESS_THAN then
(try_arrow_function, try_assignment_but_not_arrow_function)
else
(try_assignment_but_not_arrow_function, try_arrow_function)
in
(match Try.to_parse env initial with
| Try.ParsedSuccessfully expr -> expr
| Try.FailedToParse ->
(match Try.to_parse env secondary with
| Try.ParsedSuccessfully expr -> expr
| Try.FailedToParse -> assignment_but_not_arrow_function_cover env))
| _ -> assignment_but_not_arrow_function_cover env
and assignment env = as_expression env (assignment_cover env)
and yield env =
with_loc
(fun env ->
if in_formal_parameters env then error env Parse_error.YieldInFormalParameters;
let leading = Peek.comments env in
Expect.token env T_YIELD;
let (argument, delegate) =
if Peek.is_implicit_semicolon env then
(None, false)
else
let delegate = Eat.maybe env T_MULT in
let has_argument =
match Peek.token env with
| T_SEMICOLON
| T_RBRACKET
| T_RCURLY
| T_RPAREN
| T_COLON
| T_COMMA ->
false
| _ -> true
in
let argument =
if delegate || has_argument then
Some (assignment env)
else
None
in
(argument, delegate)
in
let trailing =
match argument with
| None -> Eat.trailing_comments env
| Some _ -> []
in
let open Expression in
Yield
(let open Yield in
{ argument; delegate; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () }))
env
and is_lhs =
let open Expression in
function
| ( _,
MetaProperty
{
MetaProperty.meta = (_, { Identifier.name = "new"; comments = _ });
property = (_, { Identifier.name = "target"; comments = _ });
comments = _;
} ) ->
false
| ( _,
MetaProperty
{
MetaProperty.meta = (_, { Identifier.name = "import"; comments = _ });
property = (_, { Identifier.name = "meta"; comments = _ });
comments = _;
} ) ->
false
| (_, Identifier _)
| (_, Member _)
| (_, MetaProperty _) ->
true
| (_, Array _)
| (_, ArrowFunction _)
| (_, Assignment _)
| (_, Binary _)
| (_, Call _)
| (_, Class _)
| (_, Comprehension _)
| (_, Conditional _)
| (_, Function _)
| (_, Generator _)
| (_, Import _)
| (_, JSXElement _)
| (_, JSXFragment _)
| (_, Literal _)
| (_, Logical _)
| (_, New _)
| (_, Object _)
| (_, OptionalCall _)
| (_, OptionalMember _)
| (_, Sequence _)
| (_, Super _)
| (_, TaggedTemplate _)
| (_, TemplateLiteral _)
| (_, This _)
| (_, TypeCast _)
| (_, Unary _)
| (_, Update _)
| (_, Yield _) ->
false
and assignment_op env =
let op =
let open Expression.Assignment in
match Peek.token env with
| T_RSHIFT3_ASSIGN -> Some (Some RShift3Assign)
| T_RSHIFT_ASSIGN -> Some (Some RShiftAssign)
| T_LSHIFT_ASSIGN -> Some (Some LShiftAssign)
| T_BIT_XOR_ASSIGN -> Some (Some BitXorAssign)
| T_BIT_OR_ASSIGN -> Some (Some BitOrAssign)
| T_BIT_AND_ASSIGN -> Some (Some BitAndAssign)
| T_MOD_ASSIGN -> Some (Some ModAssign)
| T_DIV_ASSIGN -> Some (Some DivAssign)
| T_MULT_ASSIGN -> Some (Some MultAssign)
| T_EXP_ASSIGN -> Some (Some ExpAssign)
| T_MINUS_ASSIGN -> Some (Some MinusAssign)
| T_PLUS_ASSIGN -> Some (Some PlusAssign)
| T_ASSIGN -> Some None
| _ -> None
in
if op <> None then Eat.token env;
op
and conditional_cover env =
let start_loc = Peek.loc env in
let expr = logical_cover env in
if Peek.token env = T_PLING then (
Eat.token env;
let env' = env |> with_no_in false in
let consequent = assignment env' in
Expect.token env T_COLON;
let (end_loc, alternate) = with_loc assignment env in
let loc = Loc.btwn start_loc end_loc in
Cover_expr
( loc,
let open Expression in
Conditional
{ Conditional.test = as_expression env expr; consequent; alternate; comments = None } )
) else
expr
and conditional env = as_expression env (conditional_cover env)
and logical_cover =
let open Expression in
let make_logical env left right operator loc =
let left = as_expression env left in
let right = as_expression env right in
Cover_expr (loc, Logical { Logical.operator; left; right; comments = None })
in
let rec logical_and env left lloc =
match Peek.token env with
| T_AND ->
Eat.token env;
let (rloc, right) = with_loc binary_cover env in
let loc = Loc.btwn lloc rloc in
let left = make_logical env left right Logical.And loc in
let (loc, left) = coalesce ~allowed:false env left loc in
logical_and env left loc
| _ -> (lloc, left)
and logical_or env left lloc =
match Peek.token env with
| T_OR ->
Eat.token env;
let (rloc, right) = with_loc binary_cover env in
let (rloc, right) = logical_and env right rloc in
let loc = Loc.btwn lloc rloc in
let left = make_logical env left right Logical.Or loc in
let (loc, left) = coalesce ~allowed:false env left loc in
logical_or env left loc
| _ -> (lloc, left)
and coalesce ~allowed env left lloc =
match Peek.token env with
| T_PLING_PLING ->
let options = parse_options env in
if not options.esproposal_nullish_coalescing then
error env Parse_error.NullishCoalescingDisabled;
if not allowed then error env (Parse_error.NullishCoalescingUnexpectedLogical "??");
Expect.token env T_PLING_PLING;
let (rloc, right) = with_loc binary_cover env in
let (rloc, right) =
match Peek.token env with
| (T_AND | T_OR) as t ->
error env (Parse_error.NullishCoalescingUnexpectedLogical (Token.value_of_token t));
let (rloc, right) = logical_and env right rloc in
logical_or env right rloc
| _ -> (rloc, right)
in
let loc = Loc.btwn lloc rloc in
coalesce ~allowed:true env (make_logical env left right Logical.NullishCoalesce loc) loc
| _ -> (lloc, left)
in
fun env ->
let (loc, left) = with_loc binary_cover env in
let (_, left) =
match Peek.token env with
| T_PLING_PLING -> coalesce ~allowed:true env left loc
| _ ->
let (loc, left) = logical_and env left loc in
logical_or env left loc
in
left
and binary_cover =
let binary_op env =
let ret =
let open Expression.Binary in
match Peek.token env with
| T_BIT_OR -> Some (BitOr, Left_assoc 2)
| T_BIT_XOR -> Some (Xor, Left_assoc 3)
| T_BIT_AND -> Some (BitAnd, Left_assoc 4)
| T_EQUAL -> Some (Equal, Left_assoc 5)
| T_STRICT_EQUAL -> Some (StrictEqual, Left_assoc 5)
| T_NOT_EQUAL -> Some (NotEqual, Left_assoc 5)
| T_STRICT_NOT_EQUAL -> Some (StrictNotEqual, Left_assoc 5)
| T_LESS_THAN -> Some (LessThan, Left_assoc 6)
| T_LESS_THAN_EQUAL -> Some (LessThanEqual, Left_assoc 6)
| T_GREATER_THAN -> Some (GreaterThan, Left_assoc 6)
| T_GREATER_THAN_EQUAL -> Some (GreaterThanEqual, Left_assoc 6)
| T_IN ->
if no_in env then
None
else
Some (In, Left_assoc 6)
| T_INSTANCEOF -> Some (Instanceof, Left_assoc 6)
| T_LSHIFT -> Some (LShift, Left_assoc 7)
| T_RSHIFT -> Some (RShift, Left_assoc 7)
| T_RSHIFT3 -> Some (RShift3, Left_assoc 7)
| T_PLUS -> Some (Plus, Left_assoc 8)
| T_MINUS -> Some (Minus, Left_assoc 8)
| T_MULT -> Some (Mult, Left_assoc 9)
| T_DIV -> Some (Div, Left_assoc 9)
| T_MOD -> Some (Mod, Left_assoc 9)
| T_EXP -> Some (Exp, Right_assoc 10)
| _ -> None
in
if ret <> None then Eat.token env;
ret
in
let make_binary left right operator loc =
( loc,
let open Expression in
Binary
(let open Binary in
{ operator; left; right; comments = None }) )
in
let rec add_to_stack right (rop, rpri) rloc = function
| (left, (lop, lpri), lloc) :: rest when is_tighter lpri rpri ->
let loc = Loc.btwn lloc rloc in
let right = make_binary left right lop loc in
add_to_stack right (rop, rpri) loc rest
| stack -> (right, (rop, rpri), rloc) :: stack
in
let rec collapse_stack right rloc = function
| [] -> right
| (left, (lop, _), lloc) :: rest ->
let loc = Loc.btwn lloc rloc in
collapse_stack (make_binary left right lop loc) loc rest
in
let rec helper env stack =
let (right_loc, (is_unary, right)) =
with_loc
(fun env ->
let is_unary = peek_unary_op env <> None in
let right = unary_cover (env |> with_no_in false) in
(is_unary, right))
env
in
(if Peek.token env = T_LESS_THAN then
match right with
| Cover_expr (_, Expression.JSXElement _) -> error env Parse_error.AdjacentJSXElements
| _ -> ());
match (stack, binary_op env) with
| ([], None) -> right
| (_, None) ->
let right = as_expression env right in
Cover_expr (collapse_stack right right_loc stack)
| (_, Some (rop, rpri)) ->
if is_unary && rop = Expression.Binary.Exp then
error_at env (right_loc, Parse_error.InvalidLHSInExponentiation);
let right = as_expression env right in
helper env (add_to_stack right (rop, rpri) right_loc stack)
in
(fun env -> helper env [])
and peek_unary_op env =
let open Expression.Unary in
match Peek.token env with
| T_NOT -> Some Not
| T_BIT_NOT -> Some BitNot
| T_PLUS -> Some Plus
| T_MINUS -> Some Minus
| T_TYPEOF -> Some Typeof
| T_VOID -> Some Void
| T_DELETE -> Some Delete
| T_AWAIT when allow_await env -> Some Await
| _ -> None
and unary_cover env =
let begin_loc = Peek.loc env in
let leading = Peek.comments env in
let op = peek_unary_op env in
match op with
| None ->
let op =
let open Expression.Update in
match Peek.token env with
| T_INCR -> Some Increment
| T_DECR -> Some Decrement
| _ -> None
in
(match op with
| None -> postfix_cover env
| Some operator ->
Eat.token env;
let (end_loc, argument) = with_loc unary env in
if not (is_lhs argument) then error_at env (fst argument, Parse_error.InvalidLHSInAssignment);
(match argument with
| (_, Expression.Identifier (_, { Identifier.name; comments = _ })) when is_restricted name
->
strict_error env Parse_error.StrictLHSPrefix
| _ -> ());
let loc = Loc.btwn begin_loc end_loc in
Cover_expr
( loc,
let open Expression in
Update
{
Update.operator;
prefix = true;
argument;
comments = Flow_ast_utils.mk_comments_opt ~leading ();
} ))
| Some operator ->
Eat.token env;
let (end_loc, argument) = with_loc unary env in
let loc = Loc.btwn begin_loc end_loc in
let open Expression in
(match (operator, argument) with
| (Unary.Delete, (_, Identifier _)) -> strict_error_at env (loc, Parse_error.StrictDelete)
| (Unary.Delete, (_, Member member)) ->
(match member.Ast.Expression.Member.property with
| Ast.Expression.Member.PropertyPrivateName _ ->
error_at env (loc, Parse_error.PrivateDelete)
| _ -> ())
| _ -> ());
Cover_expr
( loc,
let open Expression in
Unary { Unary.operator; argument; comments = Flow_ast_utils.mk_comments_opt ~leading () }
)
and unary env = as_expression env (unary_cover env)
and postfix_cover env =
let argument = left_hand_side_cover env in
if Peek.is_line_terminator env then
argument
else
let op =
let open Expression.Update in
match Peek.token env with
| T_INCR -> Some Increment
| T_DECR -> Some Decrement
| _ -> None
in
match op with
| None -> argument
| Some operator ->
let argument = as_expression env argument in
if not (is_lhs argument) then error_at env (fst argument, Parse_error.InvalidLHSInAssignment);
(match argument with
| (_, Expression.Identifier (_, { Identifier.name; comments = _ })) when is_restricted name
->
strict_error env Parse_error.StrictLHSPostfix
| _ -> ());
let end_loc = Peek.loc env in
Eat.token env;
let trailing = Eat.trailing_comments env in
let loc = Loc.btwn (fst argument) end_loc in
Cover_expr
( loc,
let open Expression in
Update
{
Update.operator;
prefix = false;
argument;
comments = Flow_ast_utils.mk_comments_opt ~trailing ();
} )
and left_hand_side_cover env =
let start_loc = Peek.loc env in
let allow_new = not (no_new env) in
let env = with_no_new false env in
let expr =
match Peek.token env with
| T_NEW when allow_new -> Cover_expr (new_expression env)
| T_IMPORT -> Cover_expr (import env)
| T_SUPER -> Cover_expr (super env)
| _ when Peek.is_function env -> Cover_expr (_function env)
| _ -> primary_cover env
in
call_cover env start_loc expr
and left_hand_side env = as_expression env (left_hand_side_cover env)
and super env =
let (allowed, call_allowed) =
match allow_super env with
| No_super -> (false, false)
| Super_prop -> (true, false)
| Super_prop_or_call -> (true, true)
in
let loc = Peek.loc env in
let leading = Peek.comments env in
Expect.token env T_SUPER;
let trailing = Eat.trailing_comments env in
let super =
( loc,
Expression.Super
{ Expression.Super.comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () } )
in
match Peek.token env with
| T_PERIOD
| T_LBRACKET ->
let super =
if not allowed then (
error_at env (loc, Parse_error.UnexpectedSuper);
(loc, Expression.Identifier (Flow_ast_utils.ident_of_source (loc, "super")))
) else
super
in
call ~allow_optional_chain:false env loc super
| T_LPAREN ->
let super =
if not call_allowed then (
error_at env (loc, Parse_error.UnexpectedSuperCall);
(loc, Expression.Identifier (Flow_ast_utils.ident_of_source (loc, "super")))
) else
super
in
call ~allow_optional_chain:false env loc super
| _ ->
if not allowed then
error_at env (loc, Parse_error.UnexpectedSuper)
else
error_unexpected ~expected:"either a call or access of `super`" env;
super
and import env =
with_loc
(fun env ->
let leading = Peek.comments env in
let start_loc = Peek.loc env in
Expect.token env T_IMPORT;
if Eat.maybe env T_PERIOD then (
let import_ident = Flow_ast_utils.ident_of_source (start_loc, "import") in
let meta_loc = Peek.loc env in
Expect.identifier env "meta";
let meta_ident = Flow_ast_utils.ident_of_source (meta_loc, "meta") in
let trailing = Eat.trailing_comments env in
Expression.MetaProperty
{
Expression.MetaProperty.meta = import_ident;
property = meta_ident;
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
}
) else
let leading_arg = Peek.comments env in
Expect.token env T_LPAREN;
let argument = add_comments (assignment (with_no_in false env)) ~leading:leading_arg in
Expect.token env T_RPAREN;
let trailing = Eat.trailing_comments env in
Expression.Import
{
Expression.Import.argument;
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
})
env
and call_cover ?(allow_optional_chain = true) ?(in_optional_chain = false) env start_loc left =
let left = member_cover ~allow_optional_chain ~in_optional_chain env start_loc left in
let optional =
match last_token env with
| Some T_PLING_PERIOD -> true
| _ -> false
in
let left_to_callee env =
let { remove_trailing; _ } = trailing_and_remover env in
remove_trailing (as_expression env left) (fun remover left -> remover#expression left)
in
let arguments ?targs env callee =
let (args_loc, arguments) = arguments env in
let loc = Loc.btwn start_loc args_loc in
let call =
{ Expression.Call.callee; targs; arguments = (args_loc, arguments); comments = None }
in
let call =
if optional || in_optional_chain then
let open Expression in
OptionalCall { OptionalCall.call; optional }
else
Expression.Call call
in
let in_optional_chain = in_optional_chain || optional in
call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, call))
in
if no_call env then
left
else
match Peek.token env with
| T_LPAREN -> arguments env (left_to_callee env)
| T_LESS_THAN when should_parse_types env ->
let error_callback _ _ = raise Try.Rollback in
let env = env |> with_error_callback error_callback in
Try.or_else env ~fallback:left (fun env ->
let callee = left_to_callee env in
let targs = call_type_args env in
arguments ?targs env callee)
| _ -> left
and call ?(allow_optional_chain = true) env start_loc left =
as_expression env (call_cover ~allow_optional_chain env start_loc (Cover_expr left))
and new_expression env =
with_loc
(fun env ->
let start_loc = Peek.loc env in
let leading = Peek.comments env in
Expect.token env T_NEW;
if in_function env && Peek.token env = T_PERIOD then (
let trailing = Eat.trailing_comments env in
Eat.token env;
let meta =
Flow_ast_utils.ident_of_source
(start_loc, "new")
?comments:(Flow_ast_utils.mk_comments_opt ~leading ~trailing ())
in
match Peek.token env with
| T_IDENTIFIER { raw = "target"; _ } ->
let property = Parse.identifier env in
let open Expression in
MetaProperty
(let open MetaProperty in
{ meta; property; comments = None })
| _ ->
error_unexpected ~expected:"the identifier `target`" env;
Eat.token env;
Expression.Identifier meta
) else
let callee_loc = Peek.loc env in
let expr =
match Peek.token env with
| T_NEW -> new_expression env
| T_SUPER -> super (env |> with_no_call true)
| _ when Peek.is_function env -> _function env
| _ -> primary env
in
let callee =
member ~allow_optional_chain:false (env |> with_no_call true) callee_loc expr
in
let callee =
let callee =
match Peek.token env with
| T_TEMPLATE_PART part -> tagged_template env callee_loc callee part
| _ -> callee
in
if Peek.token env = T_LPAREN || (should_parse_types env && Peek.token env = T_LESS_THAN)
then
let { remove_trailing; _ } = trailing_and_remover env in
remove_trailing callee (fun remover callee -> remover#expression callee)
else
callee
in
let targs =
if should_parse_types env then
let error_callback _ _ = raise Try.Rollback in
let env = env |> with_error_callback error_callback in
Try.or_else env ~fallback:None call_type_args
else
None
in
let arguments =
match Peek.token env with
| T_LPAREN -> Some (arguments env)
| _ -> None
in
let comments = Flow_ast_utils.mk_comments_opt ~leading () in
let open Expression in
New
(let open New in
{ callee; targs; arguments; comments }))
env
and call_type_args =
let args =
let rec args_helper env acc =
match Peek.token env with
| T_EOF
| T_GREATER_THAN ->
List.rev acc
| _ ->
let t =
match Peek.token env with
| T_IDENTIFIER { value = "_"; _ } ->
let loc = Peek.loc env in
let leading = Peek.comments env in
Expect.identifier env "_";
let trailing = Eat.trailing_comments env in
Expression.CallTypeArg.Implicit
( loc,
{
Expression.CallTypeArg.Implicit.comments =
Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
} )
| _ -> Expression.CallTypeArg.Explicit (Type._type env)
in
let acc = t :: acc in
if Peek.token env <> T_GREATER_THAN then Expect.token env T_COMMA;
args_helper env acc
in
fun env ->
let leading = Peek.comments env in
Expect.token env T_LESS_THAN;
let arguments = args_helper env [] in
let internal = Peek.comments env in
Expect.token env T_GREATER_THAN;
let trailing =
if Peek.token env = T_LPAREN then
let { trailing; _ } = trailing_and_remover env in
trailing
else
Eat.trailing_comments env
in
{
Expression.CallTypeArgs.arguments;
comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal ();
}
in
fun env ->
if Peek.token env = T_LESS_THAN then
Some (with_loc args env)
else
None
and arguments =
let spread_element env =
let leading = Peek.comments env in
Expect.token env T_ELLIPSIS;
let argument = assignment env in
let open Expression.SpreadElement in
{ argument; comments = Flow_ast_utils.mk_comments_opt ~leading () }
in
let argument env =
match Peek.token env with
| T_ELLIPSIS -> Expression.Spread (with_loc spread_element env)
| _ -> Expression.Expression (assignment env)
in
let rec arguments' env acc =
match Peek.token env with
| T_EOF
| T_RPAREN ->
List.rev acc
| _ ->
let acc = argument env :: acc in
if Peek.token env <> T_RPAREN then Expect.token env T_COMMA;
arguments' env acc
in
fun env ->
with_loc
(fun env ->
let leading = Peek.comments env in
Expect.token env T_LPAREN;
let args = arguments' env [] in
let internal = Peek.comments env in
Expect.token env T_RPAREN;
let trailing = Eat.trailing_comments env in
{
Expression.ArgList.arguments = args;
comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal ();
})
env
and member_cover =
let dynamic
?(allow_optional_chain = true)
?(in_optional_chain = false)
?(optional = false)
env
start_loc
left =
let expr = Parse.expression (env |> with_no_call false) in
let last_loc = Peek.loc env in
Expect.token env T_RBRACKET;
let trailing = Eat.trailing_comments env in
let loc = Loc.btwn start_loc last_loc in
let member =
let open Expression.Member in
{
_object = as_expression env left;
property = PropertyExpression expr;
comments = Flow_ast_utils.mk_comments_opt ~trailing ();
}
in
let member =
if in_optional_chain then
let open Expression in
OptionalMember { OptionalMember.member; optional }
else
Expression.Member member
in
call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, member))
in
let static
?(allow_optional_chain = true)
?(in_optional_chain = false)
?(optional = false)
env
start_loc
left =
let (id_loc, id, is_private, leading) = property_name_include_private env in
if is_private then add_used_private env (Flow_ast_utils.name_of_ident id) id_loc;
let loc = Loc.btwn start_loc id_loc in
let open Expression.Member in
let property =
if is_private then
PropertyPrivateName
(id_loc, { PrivateName.id; comments = Flow_ast_utils.mk_comments_opt ~leading () })
else
PropertyIdentifier id
in
(match left with
| Cover_expr (_, Ast.Expression.Super _) when is_private ->
error_at env (loc, Parse_error.SuperPrivate)
| _ -> ());
let member =
let open Expression.Member in
{ _object = as_expression env left; property; comments = None }
in
let member =
if in_optional_chain then
let open Expression in
OptionalMember { OptionalMember.member; optional }
else
Expression.Member member
in
call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, member))
in
fun ?(allow_optional_chain = true) ?(in_optional_chain = false) env start_loc left ->
let options = parse_options env in
match Peek.token env with
| T_PLING_PERIOD ->
if not options.esproposal_optional_chaining then
error env Parse_error.OptionalChainingDisabled;
if not allow_optional_chain then error env Parse_error.OptionalChainNew;
Expect.token env T_PLING_PERIOD;
(match Peek.token env with
| T_TEMPLATE_PART _ ->
error env Parse_error.OptionalChainTemplate;
left
| T_LPAREN -> left
| T_LESS_THAN when should_parse_types env -> left
| T_LBRACKET ->
Eat.token env;
dynamic ~allow_optional_chain ~in_optional_chain:true ~optional:true env start_loc left
| _ ->
static ~allow_optional_chain ~in_optional_chain:true ~optional:true env start_loc left)
| T_LBRACKET ->
Eat.token env;
dynamic ~allow_optional_chain ~in_optional_chain env start_loc left
| T_PERIOD ->
Eat.token env;
static ~allow_optional_chain ~in_optional_chain env start_loc left
| T_TEMPLATE_PART part ->
if in_optional_chain then error env Parse_error.OptionalChainTemplate;
let expr = tagged_template env start_loc (as_expression env left) part in
call_cover ~allow_optional_chain:false env start_loc (Cover_expr expr)
| _ -> left
and member ?(allow_optional_chain = true) env start_loc left =
as_expression env (member_cover ~allow_optional_chain env start_loc (Cover_expr left))
and _function env =
with_loc
(fun env ->
let (async, leading_async) = Declaration.async env in
let (sig_loc, (id, params, generator, predicate, return, tparams, leading)) =
with_loc
(fun env ->
let leading_function = Peek.comments env in
Expect.token env T_FUNCTION;
let (generator, leading_generator) = Declaration.generator env in
let leading = List.concat [leading_async; leading_function; leading_generator] in
let await = async in
let yield = generator in
let (id, tparams) =
if Peek.token env = T_LPAREN then
(None, None)
else
let id =
match Peek.token env with
| T_LESS_THAN -> None
| _ ->
let env = env |> with_allow_await await |> with_allow_yield yield in
let id =
id_remove_trailing
env
(Parse.identifier ~restricted_error:Parse_error.StrictFunctionName env)
in
Some id
in
let tparams = type_params_remove_trailing env (Type.type_params env) in
(id, tparams)
in
let env = env |> with_allow_super No_super in
let params =
let params = Declaration.function_params ~await ~yield env in
if Peek.token env = T_COLON then
params
else
function_params_remove_trailing env params
in
let (return, predicate) = Type.annotation_and_predicate_opt env in
let (return, predicate) =
match predicate with