-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathres_comments_table.ml
1980 lines (1900 loc) · 75.1 KB
/
res_comments_table.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 Comment = Res_comment
module Doc = Res_doc
module ParsetreeViewer = Res_parsetree_viewer
type t = {
leading: (Location.t, Comment.t list) Hashtbl.t;
inside: (Location.t, Comment.t list) Hashtbl.t;
trailing: (Location.t, Comment.t list) Hashtbl.t;
}
let make () =
{
leading = Hashtbl.create 100;
inside = Hashtbl.create 100;
trailing = Hashtbl.create 100;
}
let copy tbl =
{
leading = Hashtbl.copy tbl.leading;
inside = Hashtbl.copy tbl.inside;
trailing = Hashtbl.copy tbl.trailing;
}
let empty = make ()
let print_entries tbl =
let open Location in
Hashtbl.fold
(fun (k : Location.t) (v : Comment.t list) acc ->
let loc =
Doc.concat
[
Doc.lbracket;
Doc.text (string_of_int k.loc_start.pos_lnum);
Doc.text ":";
Doc.text
(string_of_int (k.loc_start.pos_cnum - k.loc_start.pos_bol));
Doc.text "-";
Doc.text (string_of_int k.loc_end.pos_lnum);
Doc.text ":";
Doc.text (string_of_int (k.loc_end.pos_cnum - k.loc_end.pos_bol));
Doc.rbracket;
]
in
let doc =
Doc.breakable_group ~force_break:true
(Doc.concat
[
loc;
Doc.indent
(Doc.concat
[
Doc.line;
Doc.join
~sep:(Doc.concat [Doc.comma; Doc.line])
(List.map (fun c -> Doc.text (Comment.txt c)) v);
]);
Doc.line;
])
in
doc :: acc)
tbl []
let log t =
let leading_stuff = print_entries t.leading in
let trailing_stuff = print_entries t.trailing in
let stuff_inside = print_entries t.inside in
Doc.breakable_group ~force_break:true
(Doc.concat
[
Doc.text "leading comments:";
Doc.indent (Doc.concat [Doc.line; Doc.concat leading_stuff]);
Doc.line;
Doc.text "comments inside:";
Doc.indent (Doc.concat [Doc.line; Doc.concat stuff_inside]);
Doc.line;
Doc.text "trailing comments:";
Doc.indent (Doc.concat [Doc.line; Doc.concat trailing_stuff]);
Doc.line;
])
|> Doc.to_string ~width:80 |> print_endline
let attach tbl loc comments =
match comments with
| [] -> ()
| comments -> Hashtbl.replace tbl loc comments
let partition_by_loc comments loc =
let rec loop (leading, inside, trailing) comments =
let open Location in
match comments with
| comment :: rest ->
let cmt_loc = Comment.loc comment in
if cmt_loc.loc_end.pos_cnum <= loc.loc_start.pos_cnum then
loop (comment :: leading, inside, trailing) rest
else if cmt_loc.loc_start.pos_cnum >= loc.loc_end.pos_cnum then
loop (leading, inside, comment :: trailing) rest
else loop (leading, comment :: inside, trailing) rest
| [] -> (List.rev leading, List.rev inside, List.rev trailing)
in
loop ([], [], []) comments
let partition_leading_trailing comments loc =
let rec loop (leading, trailing) comments =
let open Location in
match comments with
| comment :: rest ->
let cmt_loc = Comment.loc comment in
if cmt_loc.loc_end.pos_cnum <= loc.loc_start.pos_cnum then
loop (comment :: leading, trailing) rest
else loop (leading, comment :: trailing) rest
| [] -> (List.rev leading, List.rev trailing)
in
loop ([], []) comments
let partition_by_on_same_line loc comments =
let rec loop (on_same_line, on_other_line) comments =
let open Location in
match comments with
| [] -> (List.rev on_same_line, List.rev on_other_line)
| comment :: rest ->
let cmt_loc = Comment.loc comment in
if cmt_loc.loc_start.pos_lnum == loc.loc_end.pos_lnum then
loop (comment :: on_same_line, on_other_line) rest
else loop (on_same_line, comment :: on_other_line) rest
in
loop ([], []) comments
let partition_adjacent_trailing loc1 comments =
let open Location in
let open Lexing in
let rec loop ~prev_end_pos after_loc1 comments =
match comments with
| [] -> (List.rev after_loc1, [])
| comment :: rest as comments ->
let cmt_prev_end_pos = Comment.prev_tok_end_pos comment in
if prev_end_pos.Lexing.pos_cnum == cmt_prev_end_pos.pos_cnum then
let comment_end = (Comment.loc comment).loc_end in
loop ~prev_end_pos:comment_end (comment :: after_loc1) rest
else (List.rev after_loc1, comments)
in
loop ~prev_end_pos:loc1.loc_end [] comments
let rec collect_list_patterns acc pattern =
let open Parsetree in
match pattern.ppat_desc with
| Ppat_construct
({txt = Longident.Lident "::"}, Some {ppat_desc = Ppat_tuple [pat; rest]})
->
collect_list_patterns (pat :: acc) rest
| Ppat_construct ({txt = Longident.Lident "[]"}, None) -> List.rev acc
| _ -> List.rev (pattern :: acc)
let rec collect_list_exprs acc expr =
let open Parsetree in
match expr.pexp_desc with
| Pexp_construct
({txt = Longident.Lident "::"}, Some {pexp_desc = Pexp_tuple [expr; rest]})
->
collect_list_exprs (expr :: acc) rest
| Pexp_construct ({txt = Longident.Lident "[]"}, _) -> List.rev acc
| _ -> List.rev (expr :: acc)
(* TODO: use ParsetreeViewer *)
let arrow_type ct =
let open Parsetree in
let rec process attrs_before acc typ =
match typ with
| {
ptyp_desc = Ptyp_arrow ((Nolabel as lbl), typ1, typ2);
ptyp_attributes = [];
} ->
let arg = ([], lbl, typ1) in
process attrs_before (arg :: acc) typ2
| {
ptyp_desc = Ptyp_arrow ((Nolabel as lbl), typ1, typ2);
ptyp_attributes = [({txt = "bs"}, _)] as attrs;
} ->
let arg = (attrs, lbl, typ1) in
process attrs_before (arg :: acc) typ2
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = _attrs}
as return_type ->
let args = List.rev acc in
(attrs_before, args, return_type)
| {
ptyp_desc = Ptyp_arrow (((Labelled _ | Optional _) as lbl), typ1, typ2);
ptyp_attributes = attrs;
} ->
let arg = (attrs, lbl, typ1) in
process attrs_before (arg :: acc) typ2
| typ -> (attrs_before, List.rev acc, typ)
in
match ct with
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs} as
typ ->
process attrs [] {typ with ptyp_attributes = []}
| typ -> process [] [] typ
(* TODO: avoiding the dependency on ParsetreeViewer here, is this a good idea? *)
let mod_expr_apply mod_expr =
let rec loop acc mod_expr =
match mod_expr with
| {Parsetree.pmod_desc = Pmod_apply (next, arg)} -> loop (arg :: acc) next
| _ -> mod_expr :: acc
in
loop [] mod_expr
(* TODO: avoiding the dependency on ParsetreeViewer here, is this a good idea? *)
let mod_expr_functor mod_expr =
let rec loop acc mod_expr =
match mod_expr with
| {
Parsetree.pmod_desc = Pmod_functor (lbl, mod_type, return_mod_expr);
pmod_attributes = attrs;
} ->
let param = (attrs, lbl, mod_type) in
loop (param :: acc) return_mod_expr
| return_mod_expr -> (List.rev acc, return_mod_expr)
in
loop [] mod_expr
let functor_type modtype =
let rec process acc modtype =
match modtype with
| {
Parsetree.pmty_desc = Pmty_functor (lbl, arg_type, return_type);
pmty_attributes = attrs;
} ->
let arg = (attrs, lbl, arg_type) in
process (arg :: acc) return_type
| mod_type -> (List.rev acc, mod_type)
in
process [] modtype
let fun_expr expr =
let open Parsetree in
(* Turns (type t, type u, type z) into "type t u z" *)
let rec collect_new_types acc return_expr =
match return_expr with
| {pexp_desc = Pexp_newtype (string_loc, return_expr); pexp_attributes = []}
->
collect_new_types (string_loc :: acc) return_expr
| return_expr ->
let loc =
match (acc, List.rev acc) with
| _startLoc :: _, end_loc :: _ ->
{end_loc.loc with loc_end = end_loc.loc.loc_end}
| _ -> Location.none
in
let txt =
List.fold_right
(fun curr acc -> acc ^ " " ^ curr.Location.txt)
acc "type"
in
(Location.mkloc txt loc, return_expr)
in
(* For simplicity reason Pexp_newtype gets converted to a Nolabel parameter,
* otherwise this function would need to return a variant:
* | NormalParamater(...)
* | NewType(...)
* This complicates printing with an extra variant/boxing/allocation for a code-path
* that is not often used. Lets just keep it simple for now *)
let rec collect attrs_before acc expr =
match expr with
| {
pexp_desc = Pexp_fun (lbl, default_expr, pattern, return_expr);
pexp_attributes = [];
} ->
let parameter = ([], lbl, default_expr, pattern) in
collect attrs_before (parameter :: acc) return_expr
| {pexp_desc = Pexp_newtype (string_loc, rest); pexp_attributes = attrs} ->
let var, return_expr = collect_new_types [string_loc] rest in
let parameter =
( attrs,
Asttypes.Nolabel,
None,
Ast_helper.Pat.var ~loc:string_loc.loc var )
in
collect attrs_before (parameter :: acc) return_expr
| {
pexp_desc = Pexp_fun (lbl, default_expr, pattern, return_expr);
pexp_attributes = [({txt = "bs"}, _)] as attrs;
} ->
let parameter = (attrs, lbl, default_expr, pattern) in
collect attrs_before (parameter :: acc) return_expr
| {
pexp_desc =
Pexp_fun
(((Labelled _ | Optional _) as lbl), default_expr, pattern, return_expr);
pexp_attributes = attrs;
} ->
let parameter = (attrs, lbl, default_expr, pattern) in
collect attrs_before (parameter :: acc) return_expr
| expr -> (attrs_before, List.rev acc, expr)
in
match expr with
| {
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
pexp_attributes = attrs;
} as expr ->
collect attrs [] {expr with pexp_attributes = []}
| expr -> collect [] [] expr
let rec is_block_expr expr =
let open Parsetree in
match expr.pexp_desc with
| Pexp_letmodule _ | Pexp_letexception _ | Pexp_let _ | Pexp_open _
| Pexp_sequence _ ->
true
| Pexp_apply (call_expr, _) when is_block_expr call_expr -> true
| Pexp_constraint (expr, _) when is_block_expr expr -> true
| Pexp_field (expr, _) when is_block_expr expr -> true
| Pexp_setfield (expr, _, _) when is_block_expr expr -> true
| _ -> false
let is_if_then_else_expr expr =
let open Parsetree in
match expr.pexp_desc with
| Pexp_ifthenelse _ -> true
| _ -> false
type node =
| Case of Parsetree.case
| CoreType of Parsetree.core_type
| ExprArgument of Parsetree.expression
| Expression of Parsetree.expression
| ExprRecordRow of Longident.t Asttypes.loc * Parsetree.expression
| ExtensionConstructor of Parsetree.extension_constructor
| LabelDeclaration of Parsetree.label_declaration
| ModuleBinding of Parsetree.module_binding
| ModuleDeclaration of Parsetree.module_declaration
| ModuleExpr of Parsetree.module_expr
| ObjectField of Parsetree.object_field
| PackageConstraint of Longident.t Asttypes.loc * Parsetree.core_type
| Pattern of Parsetree.pattern
| PatternRecordRow of Longident.t Asttypes.loc * Parsetree.pattern
| RowField of Parsetree.row_field
| SignatureItem of Parsetree.signature_item
| StructureItem of Parsetree.structure_item
| TypeDeclaration of Parsetree.type_declaration
| ValueBinding of Parsetree.value_binding
let get_loc node =
let open Parsetree in
match node with
| Case case ->
{
case.pc_lhs.ppat_loc with
loc_end =
(match ParsetreeViewer.process_braces_attr case.pc_rhs with
| None, _ -> case.pc_rhs.pexp_loc.loc_end
| Some ({loc}, _), _ -> loc.Location.loc_end);
}
| CoreType ct -> ct.ptyp_loc
| ExprArgument expr -> (
match expr.Parsetree.pexp_attributes with
| ({Location.txt = "res.namedArgLoc"; loc}, _) :: _attrs ->
{loc with loc_end = expr.pexp_loc.loc_end}
| _ -> expr.pexp_loc)
| Expression e -> (
match e.pexp_attributes with
| ({txt = "res.braces" | "ns.braces"; loc}, _) :: _ -> loc
| _ -> e.pexp_loc)
| ExprRecordRow (li, e) -> {li.loc with loc_end = e.pexp_loc.loc_end}
| ExtensionConstructor ec -> ec.pext_loc
| LabelDeclaration ld -> ld.pld_loc
| ModuleBinding mb -> mb.pmb_loc
| ModuleDeclaration md -> md.pmd_loc
| ModuleExpr me -> me.pmod_loc
| ObjectField field -> (
match field with
| Parsetree.Otag (lbl, _, typ) ->
{lbl.loc with loc_end = typ.ptyp_loc.loc_end}
| _ -> Location.none)
| PackageConstraint (li, te) -> {li.loc with loc_end = te.ptyp_loc.loc_end}
| Pattern p -> p.ppat_loc
| PatternRecordRow (li, p) -> {li.loc with loc_end = p.ppat_loc.loc_end}
| RowField rf -> (
match rf with
| Parsetree.Rtag ({loc}, _, _, _) -> loc
| Rinherit {ptyp_loc} -> ptyp_loc)
| SignatureItem si -> si.psig_loc
| StructureItem si -> si.pstr_loc
| TypeDeclaration td -> td.ptype_loc
| ValueBinding vb -> vb.pvb_loc
let rec walk_structure s t comments =
match s with
| _ when comments = [] -> ()
| [] -> attach t.inside Location.none comments
| s -> walk_list (s |> List.map (fun si -> StructureItem si)) t comments
and walk_structure_item si t comments =
match si.Parsetree.pstr_desc with
| _ when comments = [] -> ()
| Pstr_primitive value_description ->
walk_value_description value_description t comments
| Pstr_open open_description ->
walk_open_description open_description t comments
| Pstr_value (_, value_bindings) ->
walk_value_bindings value_bindings t comments
| Pstr_type (_, type_declarations) ->
walk_type_declarations type_declarations t comments
| Pstr_eval (expr, _) -> walk_expression expr t comments
| Pstr_module module_binding -> walk_module_binding module_binding t comments
| Pstr_recmodule module_bindings ->
walk_list
(module_bindings |> List.map (fun mb -> ModuleBinding mb))
t comments
| Pstr_modtype mod_typ_decl ->
walk_module_type_declaration mod_typ_decl t comments
| Pstr_attribute attribute -> walk_attribute attribute t comments
| Pstr_extension (extension, _) -> walk_extension extension t comments
| Pstr_include include_declaration ->
walk_include_declaration include_declaration t comments
| Pstr_exception extension_constructor ->
walk_extension_constructor extension_constructor t comments
| Pstr_typext type_extension -> walk_type_extension type_extension t comments
| Pstr_class_type _ | Pstr_class _ -> ()
and walk_value_description vd t comments =
let leading, trailing =
partition_leading_trailing comments vd.pval_name.loc
in
attach t.leading vd.pval_name.loc leading;
let after_name, rest =
partition_adjacent_trailing vd.pval_name.loc trailing
in
attach t.trailing vd.pval_name.loc after_name;
let before, inside, after = partition_by_loc rest vd.pval_type.ptyp_loc in
attach t.leading vd.pval_type.ptyp_loc before;
walk_core_type vd.pval_type t inside;
attach t.trailing vd.pval_type.ptyp_loc after
and walk_type_extension te t comments =
let leading, trailing =
partition_leading_trailing comments te.ptyext_path.loc
in
attach t.leading te.ptyext_path.loc leading;
let after_path, rest =
partition_adjacent_trailing te.ptyext_path.loc trailing
in
attach t.trailing te.ptyext_path.loc after_path;
(* type params *)
let rest =
match te.ptyext_params with
| [] -> rest
| type_params ->
visit_list_but_continue_with_remaining_comments
~get_loc:(fun (typexpr, _variance) -> typexpr.Parsetree.ptyp_loc)
~walk_node:walk_type_param ~newline_delimited:false type_params t rest
in
walk_list
(te.ptyext_constructors |> List.map (fun ec -> ExtensionConstructor ec))
t rest
and walk_include_declaration incl_decl t comments =
let before, inside, after =
partition_by_loc comments incl_decl.pincl_mod.pmod_loc
in
attach t.leading incl_decl.pincl_mod.pmod_loc before;
walk_module_expr incl_decl.pincl_mod t inside;
attach t.trailing incl_decl.pincl_mod.pmod_loc after
and walk_module_type_declaration mtd t comments =
let leading, trailing =
partition_leading_trailing comments mtd.pmtd_name.loc
in
attach t.leading mtd.pmtd_name.loc leading;
match mtd.pmtd_type with
| None -> attach t.trailing mtd.pmtd_name.loc trailing
| Some mod_type ->
let after_name, rest =
partition_adjacent_trailing mtd.pmtd_name.loc trailing
in
attach t.trailing mtd.pmtd_name.loc after_name;
let before, inside, after = partition_by_loc rest mod_type.pmty_loc in
attach t.leading mod_type.pmty_loc before;
walk_mod_type mod_type t inside;
attach t.trailing mod_type.pmty_loc after
and walk_module_binding mb t comments =
let leading, trailing = partition_leading_trailing comments mb.pmb_name.loc in
attach t.leading mb.pmb_name.loc leading;
let after_name, rest = partition_adjacent_trailing mb.pmb_name.loc trailing in
attach t.trailing mb.pmb_name.loc after_name;
let leading, inside, trailing = partition_by_loc rest mb.pmb_expr.pmod_loc in
(match mb.pmb_expr.pmod_desc with
| Pmod_constraint _ ->
walk_module_expr mb.pmb_expr t (List.concat [leading; inside])
| _ ->
attach t.leading mb.pmb_expr.pmod_loc leading;
walk_module_expr mb.pmb_expr t inside);
attach t.trailing mb.pmb_expr.pmod_loc trailing
and walk_signature signature t comments =
match signature with
| _ when comments = [] -> ()
| [] -> attach t.inside Location.none comments
| _s ->
walk_list (signature |> List.map (fun si -> SignatureItem si)) t comments
and walk_signature_item (si : Parsetree.signature_item) t comments =
match si.psig_desc with
| _ when comments = [] -> ()
| Psig_value value_description ->
walk_value_description value_description t comments
| Psig_type (_, type_declarations) ->
walk_type_declarations type_declarations t comments
| Psig_typext type_extension -> walk_type_extension type_extension t comments
| Psig_exception extension_constructor ->
walk_extension_constructor extension_constructor t comments
| Psig_module module_declaration ->
walk_module_declaration module_declaration t comments
| Psig_recmodule module_declarations ->
walk_list
(module_declarations |> List.map (fun md -> ModuleDeclaration md))
t comments
| Psig_modtype module_type_declaration ->
walk_module_type_declaration module_type_declaration t comments
| Psig_open open_description ->
walk_open_description open_description t comments
| Psig_include include_description ->
walk_include_description include_description t comments
| Psig_attribute attribute -> walk_attribute attribute t comments
| Psig_extension (extension, _) -> walk_extension extension t comments
| Psig_class _ | Psig_class_type _ -> ()
and walk_include_description id t comments =
let before, inside, after = partition_by_loc comments id.pincl_mod.pmty_loc in
attach t.leading id.pincl_mod.pmty_loc before;
walk_mod_type id.pincl_mod t inside;
attach t.trailing id.pincl_mod.pmty_loc after
and walk_module_declaration md t comments =
let leading, trailing = partition_leading_trailing comments md.pmd_name.loc in
attach t.leading md.pmd_name.loc leading;
let after_name, rest = partition_adjacent_trailing md.pmd_name.loc trailing in
attach t.trailing md.pmd_name.loc after_name;
let leading, inside, trailing = partition_by_loc rest md.pmd_type.pmty_loc in
attach t.leading md.pmd_type.pmty_loc leading;
walk_mod_type md.pmd_type t inside;
attach t.trailing md.pmd_type.pmty_loc trailing
and walk_node node tbl comments =
match node with
| Case c -> walk_case c tbl comments
| CoreType ct -> walk_core_type ct tbl comments
| ExprArgument ea -> walk_expr_argument ea tbl comments
| Expression e -> walk_expression e tbl comments
| ExprRecordRow (ri, e) -> walk_expr_record_row (ri, e) tbl comments
| ExtensionConstructor ec -> walk_extension_constructor ec tbl comments
| LabelDeclaration ld -> walk_label_declaration ld tbl comments
| ModuleBinding mb -> walk_module_binding mb tbl comments
| ModuleDeclaration md -> walk_module_declaration md tbl comments
| ModuleExpr me -> walk_module_expr me tbl comments
| ObjectField f -> walk_object_field f tbl comments
| PackageConstraint (li, te) -> walk_package_constraint (li, te) tbl comments
| Pattern p -> walk_pattern p tbl comments
| PatternRecordRow (li, p) -> walk_pattern_record_row (li, p) tbl comments
| RowField rf -> walk_row_field rf tbl comments
| SignatureItem si -> walk_signature_item si tbl comments
| StructureItem si -> walk_structure_item si tbl comments
| TypeDeclaration td -> walk_type_declaration td tbl comments
| ValueBinding vb -> walk_value_binding vb tbl comments
and walk_list : ?prev_loc:Location.t -> node list -> t -> Comment.t list -> unit
=
fun ?prev_loc l t comments ->
match l with
| _ when comments = [] -> ()
| [] -> (
match prev_loc with
| Some loc -> attach t.trailing loc comments
| None -> ())
| node :: rest ->
let curr_loc = get_loc node in
let leading, inside, trailing = partition_by_loc comments curr_loc in
(match prev_loc with
| None ->
(* first node, all leading comments attach here *)
attach t.leading curr_loc leading
| Some prev_loc ->
(* Same line *)
if prev_loc.loc_end.pos_lnum == curr_loc.loc_start.pos_lnum then (
let after_prev, before_curr =
partition_adjacent_trailing prev_loc leading
in
attach t.trailing prev_loc after_prev;
attach t.leading curr_loc before_curr)
else
let on_same_line_as_prev, after_prev =
partition_by_on_same_line prev_loc leading
in
attach t.trailing prev_loc on_same_line_as_prev;
let leading, _inside, _trailing =
partition_by_loc after_prev curr_loc
in
attach t.leading curr_loc leading);
walk_node node t inside;
walk_list ~prev_loc:curr_loc rest t trailing
(* The parsetree doesn't always contain location info about the opening or
* closing token of a "list-of-things". This routine visits the whole list,
* but returns any remaining comments that likely fall after the whole list. *)
and visit_list_but_continue_with_remaining_comments :
'node.
?prev_loc:Location.t ->
newline_delimited:bool ->
get_loc:('node -> Location.t) ->
walk_node:('node -> t -> Comment.t list -> unit) ->
'node list ->
t ->
Comment.t list ->
Comment.t list =
fun ?prev_loc ~newline_delimited ~get_loc ~walk_node l t comments ->
let open Location in
match l with
| _ when comments = [] -> []
| [] -> (
match prev_loc with
| Some loc ->
let after_prev, rest =
if newline_delimited then partition_by_on_same_line loc comments
else partition_adjacent_trailing loc comments
in
attach t.trailing loc after_prev;
rest
| None -> comments)
| node :: rest ->
let curr_loc = get_loc node in
let leading, inside, trailing = partition_by_loc comments curr_loc in
let () =
match prev_loc with
| None ->
(* first node, all leading comments attach here *)
attach t.leading curr_loc leading;
()
| Some prev_loc ->
(* Same line *)
if prev_loc.loc_end.pos_lnum == curr_loc.loc_start.pos_lnum then
let after_prev, before_curr =
partition_adjacent_trailing prev_loc leading
in
let () = attach t.trailing prev_loc after_prev in
let () = attach t.leading curr_loc before_curr in
()
else
let on_same_line_as_prev, after_prev =
partition_by_on_same_line prev_loc leading
in
let () = attach t.trailing prev_loc on_same_line_as_prev in
let leading, _inside, _trailing =
partition_by_loc after_prev curr_loc
in
let () = attach t.leading curr_loc leading in
()
in
walk_node node t inside;
visit_list_but_continue_with_remaining_comments ~prev_loc:curr_loc ~get_loc
~walk_node ~newline_delimited rest t trailing
and walk_value_bindings vbs t comments =
walk_list (vbs |> List.map (fun vb -> ValueBinding vb)) t comments
and walk_open_description open_description t comments =
let loc = open_description.popen_lid.loc in
let leading, trailing = partition_leading_trailing comments loc in
attach t.leading loc leading;
attach t.trailing loc trailing
and walk_type_declarations type_declarations t comments =
walk_list
(type_declarations |> List.map (fun td -> TypeDeclaration td))
t comments
and walk_type_param (typexpr, _variance) t comments =
walk_core_type typexpr t comments
and walk_type_declaration (td : Parsetree.type_declaration) t comments =
let before_name, rest =
partition_leading_trailing comments td.ptype_name.loc
in
attach t.leading td.ptype_name.loc before_name;
let after_name, rest = partition_adjacent_trailing td.ptype_name.loc rest in
attach t.trailing td.ptype_name.loc after_name;
(* type params *)
let rest =
match td.ptype_params with
| [] -> rest
| type_params ->
visit_list_but_continue_with_remaining_comments
~get_loc:(fun (typexpr, _variance) -> typexpr.Parsetree.ptyp_loc)
~walk_node:walk_type_param ~newline_delimited:false type_params t rest
in
(* manifest: = typexpr *)
let rest =
match td.ptype_manifest with
| Some typexpr ->
let before_typ, inside_typ, after_typ =
partition_by_loc rest typexpr.ptyp_loc
in
attach t.leading typexpr.ptyp_loc before_typ;
walk_core_type typexpr t inside_typ;
let after_typ, rest =
partition_adjacent_trailing typexpr.Parsetree.ptyp_loc after_typ
in
attach t.trailing typexpr.ptyp_loc after_typ;
rest
| None -> rest
in
let rest =
match td.ptype_kind with
| Ptype_abstract | Ptype_open -> rest
| Ptype_record label_declarations ->
let () =
if label_declarations = [] then attach t.inside td.ptype_loc rest
else
walk_list
(label_declarations |> List.map (fun ld -> LabelDeclaration ld))
t rest
in
[]
| Ptype_variant constructor_declarations ->
walk_constructor_declarations constructor_declarations t rest
in
attach t.trailing td.ptype_loc rest
and walk_label_declarations lds t comments =
visit_list_but_continue_with_remaining_comments
~get_loc:(fun ld -> ld.Parsetree.pld_loc)
~walk_node:walk_label_declaration ~newline_delimited:false lds t comments
and walk_label_declaration ld t comments =
let before_name, rest = partition_leading_trailing comments ld.pld_name.loc in
attach t.leading ld.pld_name.loc before_name;
let after_name, rest = partition_adjacent_trailing ld.pld_name.loc rest in
attach t.trailing ld.pld_name.loc after_name;
let before_typ, inside_typ, after_typ =
partition_by_loc rest ld.pld_type.ptyp_loc
in
attach t.leading ld.pld_type.ptyp_loc before_typ;
walk_core_type ld.pld_type t inside_typ;
attach t.trailing ld.pld_type.ptyp_loc after_typ
and walk_constructor_declarations cds t comments =
visit_list_but_continue_with_remaining_comments
~get_loc:(fun cd -> cd.Parsetree.pcd_loc)
~walk_node:walk_constructor_declaration ~newline_delimited:false cds t
comments
and walk_constructor_declaration cd t comments =
let before_name, rest = partition_leading_trailing comments cd.pcd_name.loc in
attach t.leading cd.pcd_name.loc before_name;
let after_name, rest = partition_adjacent_trailing cd.pcd_name.loc rest in
attach t.trailing cd.pcd_name.loc after_name;
let rest = walk_constructor_arguments cd.pcd_args t rest in
let rest =
match cd.pcd_res with
| Some typexpr ->
let before_typ, inside_typ, after_typ =
partition_by_loc rest typexpr.ptyp_loc
in
attach t.leading typexpr.ptyp_loc before_typ;
walk_core_type typexpr t inside_typ;
let after_typ, rest =
partition_adjacent_trailing typexpr.Parsetree.ptyp_loc after_typ
in
attach t.trailing typexpr.ptyp_loc after_typ;
rest
| None -> rest
in
attach t.trailing cd.pcd_loc rest
and walk_constructor_arguments args t comments =
match args with
| Pcstr_tuple typexprs ->
visit_list_but_continue_with_remaining_comments
~get_loc:(fun n -> n.Parsetree.ptyp_loc)
~walk_node:walk_core_type ~newline_delimited:false typexprs t comments
| Pcstr_record label_declarations ->
walk_label_declarations label_declarations t comments
and walk_value_binding vb t comments =
let open Location in
let vb =
let open Parsetree in
match (vb.pvb_pat, vb.pvb_expr) with
| ( {ppat_desc = Ppat_constraint (pat, {ptyp_desc = Ptyp_poly ([], t)})},
{pexp_desc = Pexp_constraint (expr, _typ)} ) ->
{
vb with
pvb_pat =
Ast_helper.Pat.constraint_
~loc:{pat.ppat_loc with loc_end = t.Parsetree.ptyp_loc.loc_end}
pat t;
pvb_expr = expr;
}
| ( {ppat_desc = Ppat_constraint (pat, {ptyp_desc = Ptyp_poly (_ :: _, t)})},
{pexp_desc = Pexp_fun _} ) ->
{
vb with
pvb_pat =
{
vb.pvb_pat with
ppat_loc = {pat.ppat_loc with loc_end = t.ptyp_loc.loc_end};
};
}
| ( ({
ppat_desc =
Ppat_constraint (pat, ({ptyp_desc = Ptyp_poly (_ :: _, t)} as typ));
} as constrained_pattern),
{pexp_desc = Pexp_newtype (_, {pexp_desc = Pexp_constraint (expr, _)})}
) ->
(*
* The location of the Ptyp_poly on the pattern is the whole thing.
* let x:
* type t. (int, int) => int =
* (a, b) => {
* // comment
* a + b
* }
*)
{
vb with
pvb_pat =
{
constrained_pattern with
ppat_desc = Ppat_constraint (pat, typ);
ppat_loc =
{constrained_pattern.ppat_loc with loc_end = t.ptyp_loc.loc_end};
};
pvb_expr = expr;
}
| _ -> vb
in
let pattern_loc = vb.Parsetree.pvb_pat.ppat_loc in
let expr_loc = vb.Parsetree.pvb_expr.pexp_loc in
let expr = vb.pvb_expr in
let leading, inside, trailing = partition_by_loc comments pattern_loc in
(* everything before start of pattern can only be leading on the pattern:
* let |* before *| a = 1 *)
attach t.leading pattern_loc leading;
walk_pattern vb.Parsetree.pvb_pat t inside;
let after_pat, surrounding_expr =
partition_adjacent_trailing pattern_loc trailing
in
attach t.trailing pattern_loc after_pat;
let before_expr, inside_expr, after_expr =
partition_by_loc surrounding_expr expr_loc
in
if is_block_expr expr then
walk_expression expr t (List.concat [before_expr; inside_expr; after_expr])
else (
attach t.leading expr_loc before_expr;
walk_expression expr t inside_expr;
attach t.trailing expr_loc after_expr)
and walk_expression expr t comments =
let open Location in
match expr.Parsetree.pexp_desc with
| _ when comments = [] -> ()
| Pexp_constant _ ->
let leading, trailing = partition_leading_trailing comments expr.pexp_loc in
attach t.leading expr.pexp_loc leading;
attach t.trailing expr.pexp_loc trailing
| Pexp_ident longident ->
let leading, trailing = partition_leading_trailing comments longident.loc in
attach t.leading longident.loc leading;
attach t.trailing longident.loc trailing
| Pexp_let
( _recFlag,
value_bindings,
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, None)} ) ->
walk_value_bindings value_bindings t comments
| Pexp_let (_recFlag, value_bindings, expr2) ->
let comments =
visit_list_but_continue_with_remaining_comments
~get_loc:(fun n ->
if n.Parsetree.pvb_pat.ppat_loc.loc_ghost then n.pvb_expr.pexp_loc
else n.Parsetree.pvb_loc)
~walk_node:walk_value_binding ~newline_delimited:true value_bindings t
comments
in
if is_block_expr expr2 then walk_expression expr2 t comments
else
let leading, inside, trailing =
partition_by_loc comments expr2.pexp_loc
in
attach t.leading expr2.pexp_loc leading;
walk_expression expr2 t inside;
attach t.trailing expr2.pexp_loc trailing
| Pexp_sequence (expr1, expr2) ->
let leading, inside, trailing = partition_by_loc comments expr1.pexp_loc in
let comments =
if is_block_expr expr1 then (
let after_expr, comments =
partition_by_on_same_line expr1.pexp_loc trailing
in
walk_expression expr1 t (List.concat [leading; inside; after_expr]);
comments)
else (
attach t.leading expr1.pexp_loc leading;
walk_expression expr1 t inside;
let after_expr, comments =
partition_by_on_same_line expr1.pexp_loc trailing
in
attach t.trailing expr1.pexp_loc after_expr;
comments)
in
if is_block_expr expr2 then walk_expression expr2 t comments
else
let leading, inside, trailing =
partition_by_loc comments expr2.pexp_loc
in
attach t.leading expr2.pexp_loc leading;
walk_expression expr2 t inside;
attach t.trailing expr2.pexp_loc trailing
| Pexp_open (_override, longident, expr2) ->
let leading, comments = partition_leading_trailing comments expr.pexp_loc in
attach t.leading
{expr.pexp_loc with loc_end = longident.loc.loc_end}
leading;
let leading, trailing = partition_leading_trailing comments longident.loc in
attach t.leading longident.loc leading;
let after_longident, rest =
partition_by_on_same_line longident.loc trailing
in
attach t.trailing longident.loc after_longident;
if is_block_expr expr2 then walk_expression expr2 t rest
else
let leading, inside, trailing = partition_by_loc rest expr2.pexp_loc in
attach t.leading expr2.pexp_loc leading;
walk_expression expr2 t inside;
attach t.trailing expr2.pexp_loc trailing
| Pexp_extension
( {txt = "obj"},
PStr [{pstr_desc = Pstr_eval ({pexp_desc = Pexp_record (rows, _)}, [])}]
) ->
walk_list
(rows |> List.map (fun (li, e) -> ExprRecordRow (li, e)))
t comments
| Pexp_extension extension -> walk_extension extension t comments
| Pexp_letexception (extension_constructor, expr2) ->
let leading, comments = partition_leading_trailing comments expr.pexp_loc in
attach t.leading
{expr.pexp_loc with loc_end = extension_constructor.pext_loc.loc_end}
leading;
let leading, inside, trailing =
partition_by_loc comments extension_constructor.pext_loc
in
attach t.leading extension_constructor.pext_loc leading;
walk_extension_constructor extension_constructor t inside;
let after_ext_constr, rest =
partition_by_on_same_line extension_constructor.pext_loc trailing
in
attach t.trailing extension_constructor.pext_loc after_ext_constr;
if is_block_expr expr2 then walk_expression expr2 t rest
else
let leading, inside, trailing = partition_by_loc rest expr2.pexp_loc in
attach t.leading expr2.pexp_loc leading;
walk_expression expr2 t inside;
attach t.trailing expr2.pexp_loc trailing
| Pexp_letmodule (string_loc, mod_expr, expr2) ->
let leading, comments = partition_leading_trailing comments expr.pexp_loc in
attach t.leading
{expr.pexp_loc with loc_end = mod_expr.pmod_loc.loc_end}
leading;
let leading, trailing =
partition_leading_trailing comments string_loc.loc
in
attach t.leading string_loc.loc leading;
let after_string, rest =
partition_adjacent_trailing string_loc.loc trailing
in
attach t.trailing string_loc.loc after_string;
let before_mod_expr, inside_mod_expr, after_mod_expr =
partition_by_loc rest mod_expr.pmod_loc
in
attach t.leading mod_expr.pmod_loc before_mod_expr;
walk_module_expr mod_expr t inside_mod_expr;
let after_mod_expr, rest =
partition_by_on_same_line mod_expr.pmod_loc after_mod_expr
in
attach t.trailing mod_expr.pmod_loc after_mod_expr;
if is_block_expr expr2 then walk_expression expr2 t rest
else
let leading, inside, trailing = partition_by_loc rest expr2.pexp_loc in
attach t.leading expr2.pexp_loc leading;
walk_expression expr2 t inside;
attach t.trailing expr2.pexp_loc trailing