-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathres_core.ml
6619 lines (6358 loc) · 215 KB
/
res_core.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
module Doc = Res_doc
module Grammar = Res_grammar
module Token = Res_token
module Diagnostics = Res_diagnostics
module CommentTable = Res_comments_table
module ResPrinter = Res_printer
module Scanner = Res_scanner
module Parser = Res_parser
module LoopProgress = struct
let list_rest list =
match list with
| [] -> assert false
| _ :: rest -> rest
end
let mk_loc start_loc end_loc =
Location.{loc_start = start_loc; loc_end = end_loc; loc_ghost = false}
module Recover = struct
let default_expr () =
let id = Location.mknoloc "rescript.exprhole" in
Ast_helper.Exp.mk (Pexp_extension (id, PStr []))
let default_type () =
let id = Location.mknoloc "rescript.typehole" in
Ast_helper.Typ.extension (id, PStr [])
let default_pattern () =
let id = Location.mknoloc "rescript.patternhole" in
Ast_helper.Pat.extension (id, PStr [])
let default_module_expr () = Ast_helper.Mod.structure []
let default_module_type () = Ast_helper.Mty.signature []
let default_signature_item =
let id = Location.mknoloc "rescript.sigitemhole" in
Ast_helper.Sig.extension (id, PStr [])
let recover_equal_greater p =
Parser.expect EqualGreater p;
match p.Parser.token with
| MinusGreater -> Parser.next p
| _ -> ()
let should_abort_list_parse p =
let rec check breadcrumbs =
match breadcrumbs with
| [] -> false
| (grammar, _) :: rest ->
if Grammar.is_part_of_list grammar p.Parser.token then true
else check rest
in
check p.breadcrumbs
end
module ErrorMessages = struct
let list_pattern_spread =
"List pattern matches only supports one `...` spread, at the end.\n\
Explanation: a list spread at the tail is efficient, but a spread in the \
middle would create new lists; out of performance concern, our pattern \
matching currently guarantees to never create new intermediate data."
let record_pattern_spread =
"Record's `...` spread is not supported in pattern matches.\n\
Explanation: you can't collect a subset of a record's field into its own \
record, since a record needs an explicit declaration and that subset \
wouldn't have one.\n\
Solution: you need to pull out each field you want explicitly."
(* let recordPatternUnderscore = "Record patterns only support one `_`, at the end." *)
[@@live]
let array_pattern_spread =
"Array's `...` spread is not supported in pattern matches.\n\
Explanation: such spread would create a subarray; out of performance \
concern, our pattern matching currently guarantees to never create new \
intermediate data.\n\
Solution: if it's to validate the first few elements, use a `when` clause \
+ Array size check + `get` checks on the current pattern. If it's to \
obtain a subarray, use `Array.sub` or `Belt.Array.slice`."
let record_expr_spread =
"Records can only have one `...` spread, at the beginning.\n\
Explanation: since records have a known, fixed shape, a spread like `{a, \
...b}` wouldn't make sense, as `b` would override every field of `a` \
anyway."
let variant_ident =
"A polymorphic variant (e.g. #id) must start with an alphabetical letter \
or be a number (e.g. #742)"
let experimental_if_let expr =
let switch_expr = {expr with Parsetree.pexp_attributes = []} in
Doc.concat
[
Doc.text "If-let is currently highly experimental.";
Doc.line;
Doc.text "Use a regular `switch` with pattern matching instead:";
Doc.concat
[
Doc.hard_line;
Doc.hard_line;
ResPrinter.print_expression switch_expr CommentTable.empty;
];
]
|> Doc.to_string ~width:80
let type_param =
"A type param consists of a singlequote followed by a name like `'a` or \
`'A`"
let type_var =
"A type variable consists of a singlequote followed by a name like `'a` or \
`'A`"
let attribute_without_node (attr : Parsetree.attribute) =
let {Asttypes.txt = attr_name}, _ = attr in
"Did you forget to attach `" ^ attr_name
^ "` to an item?\n Standalone attributes start with `@@` like: `@@"
^ attr_name ^ "`"
let type_declaration_name_longident longident =
"A type declaration's name cannot contain a module access. Did you mean `"
^ Longident.last longident ^ "`?"
let tuple_single_element = "A tuple needs at least two elements"
let missing_tilde_labeled_parameter name =
if name = "" then "A labeled parameter starts with a `~`."
else "A labeled parameter starts with a `~`. Did you mean: `~" ^ name ^ "`?"
let string_interpolation_in_pattern =
"String interpolation is not supported in pattern matching."
let spread_in_record_declaration =
"A record type declaration doesn't support the ... spread. Only an object \
(with quoted field names) does."
let object_quoted_field_name name =
"An object type declaration needs quoted field names. Did you mean \""
^ name ^ "\"?"
let forbidden_inline_record_declaration =
"An inline record type declaration is only allowed in a variant \
constructor's declaration"
let poly_var_int_with_suffix number =
"A numeric polymorphic variant cannot be followed by a letter. Did you \
mean `#" ^ number ^ "`?"
end
module InExternal = struct
let status = ref false
end
let jsx_attr = (Location.mknoloc "JSX", Parsetree.PStr [])
let ternary_attr = (Location.mknoloc "res.ternary", Parsetree.PStr [])
let if_let_attr = (Location.mknoloc "res.iflet", Parsetree.PStr [])
let optional_attr = (Location.mknoloc "res.optional", Parsetree.PStr [])
let make_await_attr loc = (Location.mkloc "res.await" loc, Parsetree.PStr [])
let make_async_attr loc = (Location.mkloc "res.async" loc, Parsetree.PStr [])
let make_expression_optional ~optional (e : Parsetree.expression) =
if optional then {e with pexp_attributes = optional_attr :: e.pexp_attributes}
else e
let make_pattern_optional ~optional (p : Parsetree.pattern) =
if optional then {p with ppat_attributes = optional_attr :: p.ppat_attributes}
else p
let suppress_fragile_match_warning_attr =
( Location.mknoloc "warning",
Parsetree.PStr
[
Ast_helper.Str.eval
(Ast_helper.Exp.constant (Pconst_string ("-4", None)));
] )
let make_braces_attr loc = (Location.mkloc "res.braces" loc, Parsetree.PStr [])
let template_literal_attr = (Location.mknoloc "res.template", Parsetree.PStr [])
let tagged_template_literal_attr =
(Location.mknoloc "res.taggedTemplate", Parsetree.PStr [])
let spread_attr = (Location.mknoloc "res.spread", Parsetree.PStr [])
type argument = {label: Asttypes.arg_label; expr: Parsetree.expression}
type type_parameter = {
attrs: Ast_helper.attrs;
label: Asttypes.arg_label;
typ: Parsetree.core_type;
start_pos: Lexing.position;
}
type typ_def_or_ext =
| TypeDef of {
rec_flag: Asttypes.rec_flag;
types: Parsetree.type_declaration list;
}
| TypeExt of Parsetree.type_extension
type labelled_parameter =
| TermParameter of {
attrs: Parsetree.attributes;
label: Asttypes.arg_label;
expr: Parsetree.expression option;
pat: Parsetree.pattern;
pos: Lexing.position;
}
| TypeParameter of {
attrs: Parsetree.attributes;
locs: string Location.loc list;
pos: Lexing.position;
}
type record_pattern_item =
| PatUnderscore
| PatField of (Ast_helper.lid * Parsetree.pattern)
type context = OrdinaryExpr | TernaryTrueBranchExpr | WhenExpr
let get_closing_token = function
| Token.Lparen -> Token.Rparen
| Lbrace -> Rbrace
| Lbracket -> Rbracket
| List -> Rbrace
| LessThan -> GreaterThan
| _ -> assert false
let rec go_to_closing closing_token state =
match (state.Parser.token, closing_token) with
| Rparen, Token.Rparen
| Rbrace, Rbrace
| Rbracket, Rbracket
| GreaterThan, GreaterThan ->
Parser.next state;
()
| ((Token.Lbracket | Lparen | Lbrace | List | LessThan) as t), _ ->
Parser.next state;
go_to_closing (get_closing_token t) state;
go_to_closing closing_token state
| (Rparen | Token.Rbrace | Rbracket | Eof), _ ->
() (* TODO: how do report errors here? *)
| _ ->
Parser.next state;
go_to_closing closing_token state
(* Madness *)
let is_es6_arrow_expression ~in_ternary p =
Parser.lookahead p (fun state ->
let async =
match state.Parser.token with
| Lident "async" ->
Parser.next state;
true
| _ -> false
in
match state.Parser.token with
| Lident _ | Underscore -> (
Parser.next state;
match state.Parser.token with
(* Don't think that this valid
* Imagine: let x = (a: int)
* This is a parenthesized expression with a type constraint, wait for
* the arrow *)
(* | Colon when not inTernary -> true *)
| EqualGreater -> true
| _ -> false)
| Lparen -> (
let prev_end_pos = state.prev_end_pos in
Parser.next state;
match state.token with
(* arrived at `()` here *)
| Rparen -> (
Parser.next state;
match state.Parser.token with
(* arrived at `() :` here *)
| Colon when not in_ternary -> (
Parser.next state;
match state.Parser.token with
(* arrived at `() :typ` here *)
| Lident _ -> (
Parser.next state;
(match state.Parser.token with
(* arrived at `() :typ<` here *)
| LessThan ->
Parser.next state;
go_to_closing GreaterThan state
| _ -> ());
match state.Parser.token with
(* arrived at `() :typ =>` or `() :typ<'a,'b> =>` here *)
| EqualGreater -> true
| _ -> false)
| _ -> true)
| EqualGreater -> true
| _ -> false)
| Dot (* uncurried *) -> true
| Tilde when not async -> true
| Backtick ->
false
(* (` always indicates the start of an expr, can't be es6 parameter *)
| _ -> (
go_to_closing Rparen state;
match state.Parser.token with
| EqualGreater -> true
(* | Lbrace TODO: detect missing =>, is this possible? *)
| Colon when not in_ternary -> true
| Rparen ->
(* imagine having something as :
* switch colour {
* | Red
* when l == l'
* || (&Clflags.classic && (l == Nolabel && !is_optional(l'))) => (t1, t2)
* We'll arrive at the outer rparen just before the =>.
* This is not an es6 arrow.
* *)
false
| _ -> (
Parser.next_unsafe state;
(* error recovery, peek at the next token,
* (elements, providerId] => {
* in the example above, we have an unbalanced ] here
*)
match state.Parser.token with
| EqualGreater
when state.start_pos.pos_lnum == prev_end_pos.pos_lnum ->
true
| _ -> false)))
| _ -> false)
let is_es6_arrow_functor p =
Parser.lookahead p (fun state ->
match state.Parser.token with
(* | Uident _ | Underscore -> *)
(* Parser.next state; *)
(* begin match state.Parser.token with *)
(* | EqualGreater -> true *)
(* | _ -> false *)
(* end *)
| Lparen -> (
Parser.next state;
match state.token with
| Rparen -> (
Parser.next state;
match state.token with
| Colon | EqualGreater -> true
| _ -> false)
| _ -> (
go_to_closing Rparen state;
match state.Parser.token with
| EqualGreater | Lbrace -> true
| Colon -> true
| _ -> false))
| _ -> false)
let is_es6_arrow_type p =
Parser.lookahead p (fun state ->
match state.Parser.token with
| Lparen -> (
Parser.next state;
match state.Parser.token with
| Rparen -> (
Parser.next state;
match state.Parser.token with
| EqualGreater -> true
| _ -> false)
| Tilde | Dot -> true
| _ -> (
go_to_closing Rparen state;
match state.Parser.token with
| EqualGreater -> true
| _ -> false))
| Tilde -> true
| _ -> false)
let build_longident words =
match List.rev words with
| [] -> assert false
| hd :: tl -> List.fold_left (fun p s -> Longident.Ldot (p, s)) (Lident hd) tl
let make_infix_operator (p : Parser.t) token start_pos end_pos =
let stringified_token =
if token = Token.MinusGreater then "|.u"
else if token = Token.PlusPlus then "^"
else if token = Token.BangEqual then "<>"
else if token = Token.BangEqualEqual then "!="
else if token = Token.Equal then (
(* TODO: could have a totally different meaning like x->fooSet(y)*)
Parser.err ~start_pos ~end_pos p
(Diagnostics.message "Did you mean `==` here?");
"=")
else if token = Token.EqualEqual then "="
else if token = Token.EqualEqualEqual then "=="
else Token.to_string token
in
let loc = mk_loc start_pos end_pos in
let operator = Location.mkloc (Longident.Lident stringified_token) loc in
Ast_helper.Exp.ident ~loc operator
let negate_string s =
if String.length s > 0 && (s.[0] [@doesNotRaise]) = '-' then
(String.sub [@doesNotRaise]) s 1 (String.length s - 1)
else "-" ^ s
let make_unary_expr start_pos token_end token operand =
match (token, operand.Parsetree.pexp_desc) with
| (Token.Plus | PlusDot), Pexp_constant (Pconst_integer _ | Pconst_float _) ->
operand
| Minus, Pexp_constant (Pconst_integer (n, m)) ->
{
operand with
pexp_desc = Pexp_constant (Pconst_integer (negate_string n, m));
}
| (Minus | MinusDot), Pexp_constant (Pconst_float (n, m)) ->
{operand with pexp_desc = Pexp_constant (Pconst_float (negate_string n, m))}
| (Token.Plus | PlusDot | Minus | MinusDot), _ ->
let token_loc = mk_loc start_pos token_end in
let operator = "~" ^ Token.to_string token in
Ast_helper.Exp.apply
~loc:(mk_loc start_pos operand.Parsetree.pexp_loc.loc_end)
(Ast_helper.Exp.ident ~loc:token_loc
(Location.mkloc (Longident.Lident operator) token_loc))
[(Nolabel, operand)]
| Token.Bang, _ ->
let token_loc = mk_loc start_pos token_end in
Ast_helper.Exp.apply
~loc:(mk_loc start_pos operand.Parsetree.pexp_loc.loc_end)
(Ast_helper.Exp.ident ~loc:token_loc
(Location.mkloc (Longident.Lident "not") token_loc))
[(Nolabel, operand)]
| _ -> operand
let make_list_expression loc seq ext_opt =
let rec handle_seq = function
| [] -> (
match ext_opt with
| Some ext -> ext
| None ->
let loc = {loc with Location.loc_ghost = true} in
let nil = Location.mkloc (Longident.Lident "[]") loc in
Ast_helper.Exp.construct ~loc nil None)
| e1 :: el ->
let exp_el = handle_seq el in
let loc =
mk_loc e1.Parsetree.pexp_loc.Location.loc_start exp_el.pexp_loc.loc_end
in
let arg = Ast_helper.Exp.tuple ~loc [e1; exp_el] in
Ast_helper.Exp.construct ~loc
(Location.mkloc (Longident.Lident "::") loc)
(Some arg)
in
let expr = handle_seq seq in
{expr with pexp_loc = loc}
let make_list_pattern loc seq ext_opt =
let rec handle_seq = function
| [] ->
let base_case =
match ext_opt with
| Some ext -> ext
| None ->
let loc = {loc with Location.loc_ghost = true} in
let nil = {Location.txt = Longident.Lident "[]"; loc} in
Ast_helper.Pat.construct ~loc nil None
in
base_case
| p1 :: pl ->
let pat_pl = handle_seq pl in
let loc =
mk_loc p1.Parsetree.ppat_loc.loc_start pat_pl.ppat_loc.loc_end
in
let arg = Ast_helper.Pat.mk ~loc (Ppat_tuple [p1; pat_pl]) in
Ast_helper.Pat.mk ~loc
(Ppat_construct (Location.mkloc (Longident.Lident "::") loc, Some arg))
in
handle_seq seq
(* TODO: diagnostic reporting *)
let lident_of_path longident =
match Longident.flatten longident |> List.rev with
| [] -> ""
| ident :: _ -> ident
let make_newtypes ~attrs ~loc newtypes exp =
let expr =
List.fold_right
(fun newtype exp -> Ast_helper.Exp.mk ~loc (Pexp_newtype (newtype, exp)))
newtypes exp
in
{expr with pexp_attributes = attrs}
(* locally abstract types syntax sugar
* Transforms
* let f: type t u v. = (foo : list</t, u, v/>) => ...
* into
* let f = (type t u v. foo : list</t, u, v/>) => ...
*)
let wrap_type_annotation ~loc newtypes core_type body =
let exp =
make_newtypes ~attrs:[] ~loc newtypes
(Ast_helper.Exp.constraint_ ~loc body core_type)
in
let typ =
Ast_helper.Typ.poly ~loc newtypes
(Ast_helper.Typ.varify_constructors newtypes core_type)
in
(exp, typ)
(**
* process the occurrence of _ in the arguments of a function application
* replace _ with a new variable, currently __x, in the arguments
* return a wrapping function that wraps ((__x) => ...) around an expression
* e.g. foo(_, 3) becomes (__x) => foo(__x, 3)
*)
let process_underscore_application args =
let exp_question = ref None in
let hidden_var = "__x" in
let check_arg ((lab, exp) as arg) =
match exp.Parsetree.pexp_desc with
| Pexp_ident ({txt = Lident "_"} as id) ->
let new_id = Location.mkloc (Longident.Lident hidden_var) id.loc in
let new_exp = Ast_helper.Exp.mk (Pexp_ident new_id) ~loc:exp.pexp_loc in
exp_question := Some new_exp;
(lab, new_exp)
| _ -> arg
in
let args = List.map check_arg args in
let wrap (exp_apply : Parsetree.expression) =
match !exp_question with
| Some {pexp_loc = loc} ->
let pattern =
Ast_helper.Pat.mk
(Ppat_var (Location.mkloc hidden_var loc))
~loc:Location.none
in
let fun_expr = Ast_helper.Exp.fun_ ~loc Nolabel None pattern exp_apply in
Ast_uncurried.uncurried_fun ~loc ~arity:1 fun_expr
| None -> exp_apply
in
(args, wrap)
(* Transform A.a into a. For use with punned record fields as in {A.a, b}. *)
let remove_module_name_from_punned_field_value exp =
match exp.Parsetree.pexp_desc with
| Pexp_ident path_ident ->
{
exp with
pexp_desc =
Pexp_ident
{path_ident with txt = Lident (Longident.last path_ident.txt)};
}
| _ -> exp
let rec parse_lident p =
let recover_lident p =
if
Token.is_keyword p.Parser.token
&& p.Parser.prev_end_pos.pos_lnum == p.start_pos.pos_lnum
then (
Parser.err p (Diagnostics.lident p.Parser.token);
Parser.next p;
None)
else
let rec loop p =
if (not (Recover.should_abort_list_parse p)) && p.token <> Eof then (
Parser.next p;
loop p)
in
Parser.err p (Diagnostics.lident p.Parser.token);
Parser.next p;
loop p;
match p.Parser.token with
| Lident _ -> Some ()
| _ -> None
in
let start_pos = p.Parser.start_pos in
match p.Parser.token with
| Lident ident ->
Parser.next p;
let loc = mk_loc start_pos p.prev_end_pos in
(ident, loc)
| Eof ->
Parser.err ~start_pos p
(Diagnostics.unexpected p.Parser.token p.breadcrumbs);
("_", mk_loc start_pos p.prev_end_pos)
| _ -> (
match recover_lident p with
| Some () -> parse_lident p
| None -> ("_", mk_loc start_pos p.prev_end_pos))
let parse_ident ~msg ~start_pos p =
match p.Parser.token with
| Lident ident | Uident ident ->
Parser.next p;
let loc = mk_loc start_pos p.prev_end_pos in
(ident, loc)
| token
when Token.is_keyword token
&& p.prev_end_pos.pos_lnum == p.start_pos.pos_lnum ->
let token_txt = Token.to_string token in
let msg =
"`" ^ token_txt
^ "` is a reserved keyword. Keywords need to be escaped: \\\"" ^ token_txt
^ "\""
in
Parser.err ~start_pos p (Diagnostics.message msg);
Parser.next p;
(token_txt, mk_loc start_pos p.prev_end_pos)
| _token ->
Parser.err ~start_pos p (Diagnostics.message msg);
Parser.next p;
("", mk_loc start_pos p.prev_end_pos)
let parse_hash_ident ~start_pos p =
Parser.expect Hash p;
match p.token with
| String text ->
Parser.next p;
(text, mk_loc start_pos p.prev_end_pos)
| Int {i; suffix} ->
let () =
match suffix with
| Some _ ->
Parser.err p
(Diagnostics.message (ErrorMessages.poly_var_int_with_suffix i))
| None -> ()
in
Parser.next p;
(i, mk_loc start_pos p.prev_end_pos)
| Eof ->
Parser.err ~start_pos p (Diagnostics.unexpected p.token p.breadcrumbs);
("", mk_loc start_pos p.prev_end_pos)
| _ -> parse_ident ~start_pos ~msg:ErrorMessages.variant_ident p
(* Ldot (Ldot (Lident "Foo", "Bar"), "baz") *)
let parse_value_path p =
let start_pos = p.Parser.start_pos in
let rec aux p path =
let start_pos = p.Parser.start_pos in
let token = p.token in
Parser.next p;
if p.Parser.token = Dot then (
Parser.expect Dot p;
match p.Parser.token with
| Lident ident -> Longident.Ldot (path, ident)
| Uident uident -> aux p (Ldot (path, uident))
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Longident.Ldot (path, "_"))
else (
Parser.err p ~start_pos ~end_pos:p.prev_end_pos (Diagnostics.lident token);
path)
in
let ident =
match p.Parser.token with
| Lident ident ->
Parser.next p;
Longident.Lident ident
| Uident ident ->
let res = aux p (Lident ident) in
Parser.next_unsafe p;
res
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Parser.next_unsafe p;
Longident.Lident "_"
in
Location.mkloc ident (mk_loc start_pos p.prev_end_pos)
let parse_value_path_after_dot p =
let start_pos = p.Parser.start_pos in
match p.Parser.token with
| Lident _ | Uident _ -> parse_value_path p
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Location.mkloc (Longident.Lident "_") (mk_loc start_pos p.prev_end_pos)
let parse_value_path_tail p start_pos ident =
let rec loop p path =
match p.Parser.token with
| Lident ident ->
Parser.next p;
Location.mkloc
(Longident.Ldot (path, ident))
(mk_loc start_pos p.prev_end_pos)
| Uident ident ->
Parser.next p;
Parser.expect Dot p;
loop p (Longident.Ldot (path, ident))
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Location.mkloc
(Longident.Ldot (path, "_"))
(mk_loc start_pos p.prev_end_pos)
in
loop p ident
let parse_module_long_ident_tail ~lowercase p start_pos ident =
let rec loop p acc =
match p.Parser.token with
| Lident ident when lowercase ->
Parser.next p;
let lident = Longident.Ldot (acc, ident) in
Location.mkloc lident (mk_loc start_pos p.prev_end_pos)
| Uident ident -> (
Parser.next p;
let end_pos = p.prev_end_pos in
let lident = Longident.Ldot (acc, ident) in
match p.Parser.token with
| Dot ->
Parser.next p;
loop p lident
| _ -> Location.mkloc lident (mk_loc start_pos end_pos))
| t ->
Parser.err p (Diagnostics.uident t);
Location.mkloc
(Longident.Ldot (acc, "_"))
(mk_loc start_pos p.prev_end_pos)
in
loop p ident
(* Parses module identifiers:
Foo
Foo.Bar *)
let parse_module_long_ident ~lowercase p =
(* Parser.leaveBreadcrumb p Reporting.ModuleLongIdent; *)
let start_pos = p.Parser.start_pos in
let module_ident =
match p.Parser.token with
| Lident ident when lowercase ->
let loc = mk_loc start_pos p.end_pos in
let lident = Longident.Lident ident in
Parser.next p;
Location.mkloc lident loc
| Uident ident -> (
let lident = Longident.Lident ident in
let end_pos = p.end_pos in
Parser.next p;
match p.Parser.token with
| Dot ->
Parser.next p;
parse_module_long_ident_tail ~lowercase p start_pos lident
| _ -> Location.mkloc lident (mk_loc start_pos end_pos))
| t ->
Parser.err p (Diagnostics.uident t);
Location.mkloc (Longident.Lident "_") (mk_loc start_pos p.prev_end_pos)
in
(* Parser.eatBreadcrumb p; *)
module_ident
let verify_jsx_opening_closing_name p name_expr =
let closing =
match p.Parser.token with
| Lident lident ->
Parser.next p;
Longident.Lident lident
| Uident _ -> (parse_module_long_ident ~lowercase:true p).txt
| _ -> Longident.Lident ""
in
match name_expr.Parsetree.pexp_desc with
| Pexp_ident opening_ident ->
let opening =
let without_create_element =
Longident.flatten opening_ident.txt
|> List.filter (fun s -> s <> "createElement")
in
match Longident.unflatten without_create_element with
| Some li -> li
| None -> Longident.Lident ""
in
opening = closing
| _ -> assert false
let string_of_pexp_ident name_expr =
match name_expr.Parsetree.pexp_desc with
| Pexp_ident opening_ident ->
Longident.flatten opening_ident.txt
|> List.filter (fun s -> s <> "createElement")
|> String.concat "."
| _ -> ""
(* open-def ::=
* | open module-path
* | open! module-path *)
let parse_open_description ~attrs p =
Parser.leave_breadcrumb p Grammar.OpenDescription;
let start_pos = p.Parser.start_pos in
Parser.expect Open p;
let override =
if Parser.optional p Token.Bang then Asttypes.Override else Asttypes.Fresh
in
let modident = parse_module_long_ident ~lowercase:false p in
let loc = mk_loc start_pos p.prev_end_pos in
Parser.eat_breadcrumb p;
Ast_helper.Opn.mk ~loc ~attrs ~override modident
(* constant ::= integer-literal *)
(* ∣ float-literal *)
(* ∣ string-literal *)
let parse_constant p =
let is_negative =
match p.Parser.token with
| Token.Minus ->
Parser.next p;
true
| Plus ->
Parser.next p;
false
| _ -> false
in
let constant =
match p.Parser.token with
| Int {i; suffix} ->
(* Only decimal literal is allowed for bigint *)
if suffix = Some 'n' && not (Bigint_utils.is_valid i) then
Parser.err p
(Diagnostics.message
"Invalid bigint literal. Only decimal literal is allowed for \
bigint.");
let int_txt = if is_negative then "-" ^ i else i in
Parsetree.Pconst_integer (int_txt, suffix)
| Float {f; suffix} ->
let float_txt = if is_negative then "-" ^ f else f in
Parsetree.Pconst_float (float_txt, suffix)
| String s ->
Pconst_string (s, if p.mode = ParseForTypeChecker then Some "js" else None)
| Codepoint {c; original} ->
if p.mode = ParseForTypeChecker then Pconst_char c
else
(* Pconst_char char does not have enough information for formatting.
* When parsing for the printer, we encode the char contents as a string
* with a special prefix. *)
Pconst_string (original, Some "INTERNAL_RES_CHAR_CONTENTS")
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Pconst_string ("", None)
in
Parser.next_unsafe p;
constant
let parse_template_constant ~prefix (p : Parser.t) =
(* Arrived at the ` char *)
let start_pos = p.start_pos in
Parser.next_template_literal_token p;
match p.token with
| TemplateTail (txt, _) ->
Parser.next p;
Parsetree.Pconst_string (txt, prefix)
| _ ->
let rec skip_tokens () =
if p.token <> Eof then (
Parser.next p;
match p.token with
| Backtick ->
Parser.next p;
()
| _ -> skip_tokens ())
in
skip_tokens ();
Parser.err ~start_pos ~end_pos:p.prev_end_pos p
(Diagnostics.message ErrorMessages.string_interpolation_in_pattern);
Pconst_string ("", None)
let parse_comma_delimited_region p ~grammar ~closing ~f =
Parser.leave_breadcrumb p grammar;
let rec loop nodes =
match f p with
| Some node -> (
match p.Parser.token with
| Comma ->
Parser.next p;
loop (node :: nodes)
| token when token = closing || token = Eof -> List.rev (node :: nodes)
| _ when Grammar.is_list_element grammar p.token ->
(* missing comma between nodes in the region and the current token
* looks like the start of something valid in the current region.
* Example:
* type student<'extraInfo> = {
* name: string,
* age: int
* otherInfo: 'extraInfo
* }
* There is a missing comma between `int` and `otherInfo`.
* `otherInfo` looks like a valid start of the record declaration.
* We report the error here and then continue parsing the region.
*)
Parser.expect Comma p;
loop (node :: nodes)
| _ ->
if
not
(p.token = Eof || p.token = closing
|| Recover.should_abort_list_parse p)
then Parser.expect Comma p;
if p.token = Semicolon then Parser.next p;
loop (node :: nodes))
| None ->
if p.token = Eof || p.token = closing || Recover.should_abort_list_parse p
then List.rev nodes
else (
Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs);
Parser.next p;
loop nodes)
in
let nodes = loop [] in
Parser.eat_breadcrumb p;
nodes
let parse_comma_delimited_reversed_list p ~grammar ~closing ~f =
Parser.leave_breadcrumb p grammar;
let rec loop nodes =
match f p with
| Some node -> (
match p.Parser.token with
| Comma ->
Parser.next p;
loop (node :: nodes)
| token when token = closing || token = Eof -> node :: nodes
| _ when Grammar.is_list_element grammar p.token ->
(* missing comma between nodes in the region and the current token
* looks like the start of something valid in the current region.
* Example:
* type student<'extraInfo> = {
* name: string,
* age: int
* otherInfo: 'extraInfo
* }
* There is a missing comma between `int` and `otherInfo`.
* `otherInfo` looks like a valid start of the record declaration.
* We report the error here and then continue parsing the region.
*)
Parser.expect Comma p;
loop (node :: nodes)
| _ ->
if
not
(p.token = Eof || p.token = closing
|| Recover.should_abort_list_parse p)
then Parser.expect Comma p;
if p.token = Semicolon then Parser.next p;
loop (node :: nodes))
| None ->
if p.token = Eof || p.token = closing || Recover.should_abort_list_parse p
then nodes
else (
Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs);
Parser.next p;
loop nodes)
in
let nodes = loop [] in
Parser.eat_breadcrumb p;
nodes
let parse_delimited_region p ~grammar ~closing ~f =
Parser.leave_breadcrumb p grammar;
let rec loop nodes =
match f p with
| Some node -> loop (node :: nodes)
| None ->
if
p.Parser.token = Token.Eof || p.token = closing
|| Recover.should_abort_list_parse p
then List.rev nodes
else (
Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs);
Parser.next p;
loop nodes)
in
let nodes = loop [] in
Parser.eat_breadcrumb p;
nodes
let parse_region p ~grammar ~f =
Parser.leave_breadcrumb p grammar;
let rec loop nodes =
match f p with
| Some node -> loop (node :: nodes)
| None ->
if p.Parser.token = Token.Eof || Recover.should_abort_list_parse p then
List.rev nodes
else (
Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs);
Parser.next p;
loop nodes)
in
let nodes = loop [] in
Parser.eat_breadcrumb p;
nodes
(* let-binding ::= pattern = expr *)
(* ∣ value-name { parameter } [: typexpr] [:> typexpr] = expr *)
(* ∣ value-name : poly-typexpr = expr *)
(* pattern ::= value-name *)
(* ∣ _ *)
(* ∣ constant *)
(* ∣ pattern as value-name *)
(* ∣ ( pattern ) *)
(* ∣ ( pattern : typexpr ) *)
(* ∣ pattern | pattern *)