forked from ocaml-ppx/ocamlformat
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAst.ml
2401 lines (2246 loc) · 87.3 KB
/
Ast.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
(**************************************************************************)
(* *)
(* OCamlFormat *)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(** Abstract syntax tree term *)
open Migrate_ast
open Extended_ast
let init, register_reset, leading_nested_match_parens, parens_ite =
let l = ref [] in
let leading_nested_match_parens = ref false in
let parens_ite = ref false in
let register f = l := f :: !l in
let init (conf : Conf.t) =
leading_nested_match_parens := conf.leading_nested_match_parens ;
parens_ite := conf.parens_ite ;
List.iter !l ~f:(fun f -> f ())
in
(init, register, leading_nested_match_parens, parens_ite)
(** [fit_margin c x] returns [true] if and only if [x] does not exceed 1/3 of
the margin. *)
let fit_margin (c : Conf.t) x = x * 3 < c.margin
(** 'Classes' of expressions which are parenthesized differently. *)
type cls = Let_match | Match | Non_apply | Sequence | Then | ThenElse
(** Predicates recognizing special symbol identifiers. *)
module Char_id = struct
let is_kwdop = function
| '$' | '&' | '*' | '+' | '-' | '/' | '<' | '=' | '>' | '@' | '^' | '|'
|'!' | '%' | ':' | '?' ->
true
| _ -> false
let is_infixop = function
| '$' | '%' | '*' | '+' | '-' | '/' | '<' | '=' | '>' | '|' | '&' | '@'
|'^' | '#' ->
true
| _ -> false
end
module Token = struct
let is_infix = function
| Parser.AMPERAMPER | AMPERSAND | ANDOP _ | BAR | BARBAR | COLON
|COLONCOLON | COLONEQUAL | DOTDOT | DOTOP _ | EQUAL | GREATER
|HASHOP _ | INFIXOP0 _ | INFIXOP1 _ | INFIXOP2 _ | INFIXOP3 _
|INFIXOP4 _ | LESS | LESSMINUS | LETOP _ | MINUS | MINUSDOT
|MINUSGREATER | PERCENT | PLUS | PLUSDOT | PLUSEQ | SEMI | STAR ->
true
| _ -> false
end
module Indexing_op = struct
type brackets = Round | Square | Curly
type custom_operator =
{path: string list; opchars: string; brackets: brackets}
type indexing_op =
| Defined of expression * custom_operator
| Extended of expression list * custom_operator
(** Take a [Pexp_array] of at least 2 elements *)
| Special of expression list * brackets
(** Desugared to the application of the corresponding [get] function
by the parser. (eg. [Array.get], [String.get]) *)
type t =
{ lhs: expression
; op: indexing_op
; rhs: expression option
; loc: Location.t }
type raw =
{ opchars: string
; brackets: brackets
; extended: bool (** eg. [.*{;..}] *)
; has_rhs: bool (** eg. [.*{}<-] *) }
let parse ident =
match String.chop_prefix ~prefix:"." ident with
| None -> None
| Some ident ->
let ident, has_rhs =
match String.chop_suffix ident ~suffix:"<-" with
| Some ident -> (ident, true)
| None -> (ident, false)
in
let find_suffix (suffix, brackets, extended) =
match String.chop_suffix ident ~suffix with
| None -> None
| Some opchars -> Some {opchars; brackets; extended; has_rhs}
in
List.find_map ~f:find_suffix
[ ("{}", Curly, false)
; ("[]", Square, false)
; ("()", Round, false)
; ("{;..}", Curly, true)
; ("[;..]", Square, true)
; ("(;..)", Round, true) ]
let special ~id_tl ~args_tl brackets args =
let op = Special (args, brackets) in
match (id_tl, args_tl) with
| "get", [] -> Some (op, None)
| "set", [rhs] -> Some (op, Some rhs)
| _ -> None
let custom ~extended ~rhs op arg1 =
match (extended, arg1) with
| true, {pexp_desc= Pexp_array (_ :: _ :: _ as args); _} ->
Some (Extended (args, op), rhs)
| true, _ -> None
| false, arg1 -> Some (Defined (arg1, op), rhs)
let get_sugar_ident ident args =
match (Longident.flatten ident, args) with
| ["String"; id_tl], arg1 :: args_tl ->
special ~id_tl ~args_tl Square [arg1]
| ["Array"; id_tl], arg1 :: args_tl ->
special ~id_tl ~args_tl Round [arg1]
| ["Bigarray"; "Array1"; id_tl], arg1 :: args_tl ->
special ~id_tl ~args_tl Curly [arg1]
| ["Bigarray"; "Array2"; id_tl], arg1 :: arg2 :: args_tl ->
special ~id_tl ~args_tl Curly [arg1; arg2]
| ["Bigarray"; "Array3"; id_tl], arg1 :: arg2 :: arg3 :: args_tl ->
special ~id_tl ~args_tl Curly [arg1; arg2; arg3]
| ( ["Bigarray"; "Genarray"; id_tl]
, {pexp_desc= Pexp_array args; _} :: args_tl )
when List.length args > 3 ->
special ~id_tl ~args_tl Curly args
| ident, args -> (
match List.rev ident with
| [] -> None
| ident :: path_rev -> (
let path = List.rev path_rev in
match parse ident with
| None -> None
| Some {opchars; brackets; extended; has_rhs} -> (
let op = {path; opchars; brackets} in
match (has_rhs, args) with
| true, [arg1; rhs] -> custom ~extended ~rhs:(Some rhs) op arg1
| false, [arg1] -> custom ~extended ~rhs:None op arg1
| _, _ -> None ) ) )
let rec all_args_unlabeled acc = function
| [] -> Some (List.rev acc)
| (Asttypes.Nolabel, e) :: tl -> all_args_unlabeled (e :: acc) tl
| _ :: _ -> None
let get_sugar ident args =
match all_args_unlabeled [] args with
| None | Some [] -> None
| Some (lhs :: args) -> (
match ident with
| {pexp_desc= Pexp_ident {txt= ident; loc}; pexp_attributes= []; _}
(* We only use the sugared form if it was already used in the
source. *)
when loc.loc_ghost -> (
match get_sugar_ident ident args with
| None -> None
| Some (op, rhs) -> Some {lhs; op; rhs; loc} )
| _ -> None )
end
module String_id = struct
let is_prefix i =
match i with
| "!=" -> false
| _ -> ( match i.[0] with '!' | '?' | '~' -> true | _ -> false )
let is_monadic_binding s =
String.length s > 3
&& (String.is_prefix s ~prefix:"let" || String.is_prefix s ~prefix:"and")
&& Option.is_none
(String.lfindi s ~pos:3 ~f:(fun _ c -> not (Char_id.is_kwdop c)))
let is_infix i =
if Char_id.is_infixop i.[0] then true
else
match i with
| "!=" | "land" | "lor" | "lxor" | "mod" | "::" | ":=" | "asr"
|"lsl" | "lsr" | "or" | "||" ->
true
| _ -> is_monadic_binding i
let is_hash_getter i =
let is_infix_char c = Char.equal c '.' || Char_id.is_infixop c in
match (i.[0], i.[String.length i - 1]) with
| '#', ('#' | '.') when String.for_all i ~f:is_infix_char -> true
| _ -> false
let is_index_op ident = Option.is_some (Indexing_op.parse ident)
let is_symbol i = is_prefix i || is_infix i || is_index_op i
end
module Longident = struct
include Longident
let test ~f = function Longident.Lident i -> f i | _ -> false
let is_prefix = test ~f:String_id.is_prefix
let is_monadic_binding = test ~f:String_id.is_monadic_binding
let is_infix = test ~f:String_id.is_infix
let is_hash_getter = test ~f:String_id.is_hash_getter
let is_index_op i = Longident.last i |> String_id.is_index_op
let is_symbol i = is_prefix i || is_infix i || is_index_op i
(** [fit_margin c x] returns [true] if and only if [x] does not exceed 2/3
of the margin. *)
let fit_margin (c : Conf.t) x = x * 3 < c.margin * 2
let is_simple c x =
let rec length (x : Longident.t) =
match x with
| Lident x -> String.length x
| Ldot (x, y) -> length x + 1 + String.length y
| Lapply (x, y) -> length x + length y + 3
in
fit_margin c (length x)
end
module Attr = struct
let is_doc = function
| {attr_name= {Location.txt= "ocaml.doc" | "ocaml.text"; _}; _} -> true
| _ -> false
end
module Exp = struct
let test_id ~f = function
| {pexp_desc= Pexp_ident {txt= i; _}; _} -> f i
| _ -> false
let is_prefix = test_id ~f:Longident.is_prefix
let is_infix = test_id ~f:Longident.is_infix
let is_index_op = test_id ~f:Longident.is_index_op
let is_monadic_binding = test_id ~f:Longident.is_monadic_binding
let is_symbol = test_id ~f:Longident.is_symbol
let is_sequence exp =
match exp.pexp_desc with
| Pexp_sequence _ -> true
| Pexp_extension
( ext
, PStr
[ { pstr_desc=
Pstr_eval (({pexp_desc= Pexp_sequence _; _} as e), [])
; _ } ] )
when Source.extension_using_sugar ~name:ext ~payload:e.pexp_loc ->
true
| _ -> false
let has_trailing_attributes {pexp_desc; pexp_attributes; _} =
match pexp_desc with
| Pexp_fun _ | Pexp_function _ | Pexp_ifthenelse _ | Pexp_match _
|Pexp_newtype _ | Pexp_try _ ->
false
| _ -> List.exists pexp_attributes ~f:(Fn.non Attr.is_doc)
let rec is_trivial c exp =
match exp.pexp_desc with
| Pexp_constant (Pconst_string (_, _, None)) -> true
| Pexp_constant _ | Pexp_field _ | Pexp_ident _ | Pexp_send _ -> true
| Pexp_construct (_, exp) -> Option.for_all exp ~f:(is_trivial c)
| Pexp_apply (e0, [(_, e1)]) when is_prefix e0 -> is_trivial c e1
| Pexp_apply
({pexp_desc= Pexp_ident {txt= Lident "not"; _}; _}, [(_, e1)]) ->
is_trivial c e1
| _ -> false
let rec exposed_left e =
match e.pexp_desc with
| Pexp_apply (op, _) -> is_prefix op || exposed_left op
| Pexp_field (e, _) -> exposed_left e
| _ -> false
(** [mem_cls cls exp] holds if [exp] is in the named class of expressions
[cls]. *)
let mem_cls cls ast =
match (ast, cls) with
| {pexp_desc= Pexp_ifthenelse (_, _, None); _}, (Non_apply | ThenElse)
|{pexp_desc= Pexp_ifthenelse _; _}, Non_apply
|( {pexp_desc= Pexp_sequence _; _}
, (Non_apply | Sequence | Then | ThenElse) )
|( {pexp_desc= Pexp_function _ | Pexp_match _ | Pexp_try _; _}
, (Match | Let_match | Non_apply) )
|( { pexp_desc=
( Pexp_fun _ | Pexp_let _ | Pexp_letop _ | Pexp_letexception _
| Pexp_letmodule _ | Pexp_newtype _ | Pexp_open _ )
; _ }
, (Let_match | Non_apply) ) ->
true
| _ -> false
end
module Pat = struct
let is_simple {ppat_desc; _} =
match ppat_desc with
| Ppat_any | Ppat_constant _ | Ppat_var _
|Ppat_variant (_, (None | Some {ppat_desc= Ppat_any; _}))
|Ppat_construct (_, (None | Some ([], {ppat_desc= Ppat_any; _}))) ->
true
| _ -> false
let has_trailing_attributes {ppat_desc; ppat_attributes; _} =
match ppat_desc with
| Ppat_construct (_, None)
|Ppat_constant _ | Ppat_any | Ppat_var _
|Ppat_variant (_, None)
|Ppat_record _ | Ppat_array _ | Ppat_list _ | Ppat_type _
|Ppat_unpack _ | Ppat_extension _ | Ppat_open _ | Ppat_interval _ ->
false
| _ -> List.exists ppat_attributes ~f:(Fn.non Attr.is_doc)
end
let doc_atrs ?(acc = []) atrs =
let docs, rev_atrs =
List.fold atrs ~init:(acc, []) ~f:(fun (docs, rev_atrs) atr ->
let open Asttypes in
match atr with
| { attr_name=
{ txt= ("ocaml.doc" | "ocaml.text") as txt
; loc= {loc_ghost= true; _} }
; attr_payload=
PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc=
Pexp_constant (Pconst_string (doc, _, None))
; pexp_loc= loc
; pexp_attributes= []
; _ }
, [] )
; _ } ]
; _ } -> (
match (txt, docs) with
| "ocaml.doc", (_, false) :: _ ->
(* cannot put two doc comment next to each other *)
(docs, atr :: rev_atrs)
| _ ->
( ({txt= doc; loc}, String.equal "ocaml.text" txt) :: docs
, rev_atrs ) )
| _ -> (docs, atr :: rev_atrs) )
in
let docs = match docs with [] -> None | l -> Some (List.rev l) in
(docs, List.rev rev_atrs)
let rec mty_is_simple x =
match x.pmty_desc with
| Pmty_ident _ | Pmty_alias _ | Pmty_signature [] -> true
| Pmty_signature (_ :: _)
|Pmty_with (_, _ :: _ :: _)
|Pmty_extension _
|Pmty_functor (_, _) ->
false
| Pmty_typeof e -> mod_is_simple e
| Pmty_with (t, ([] | [_])) -> mty_is_simple t
and mod_is_simple x =
match x.pmod_desc with
| Pmod_ident _ | Pmod_unpack _ | Pmod_structure [] | Pmod_hole -> true
| Pmod_structure (_ :: _) | Pmod_extension _ | Pmod_functor (_, _) -> false
| Pmod_constraint (e, t) -> mod_is_simple e && mty_is_simple t
| Pmod_apply (a, b) -> mod_is_simple a && mod_is_simple b
module Mty = struct
let is_simple = mty_is_simple
let has_trailing_attributes {pmty_attributes; _} =
List.exists pmty_attributes ~f:(Fn.non Attr.is_doc)
end
module Mod = struct
let is_simple = mod_is_simple
let has_trailing_attributes {pmod_attributes; _} =
List.exists pmod_attributes ~f:(Fn.non Attr.is_doc)
end
module Cty = struct
let rec is_simple x =
match x.pcty_desc with
| Pcty_constr _ | Pcty_signature {pcsig_fields= []; _} -> true
| Pcty_signature {pcsig_fields= _ :: _; _}
|Pcty_open _ | Pcty_extension _ ->
false
| Pcty_arrow (_, _, t) -> is_simple t
end
module Cl = struct
let rec is_simple x =
match x.pcl_desc with
| Pcl_constr _ | Pcl_structure {pcstr_fields= []; _} -> true
| Pcl_structure {pcstr_fields= _ :: _; _}
|Pcl_let _ | Pcl_open _ | Pcl_extension _ ->
false
| Pcl_apply (e, _) | Pcl_fun (_, _, _, e) -> is_simple e
| Pcl_constraint (e, t) -> is_simple e && Cty.is_simple t
(** [mem_cls cls cl] holds if [cl] is in the named class of expressions
[cls]. *)
let mem_cls cls ast =
match (ast, cls) with
| {pcl_desc= Pcl_fun _; _}, Non_apply -> true
| _ -> false
end
module Tyd = struct
let is_simple x =
match x.ptype_kind with
| Ptype_abstract | Ptype_open -> true
| Ptype_variant _ | Ptype_record _ -> false
end
module Structure_item = struct
let has_doc itm =
match itm.pstr_desc with
| Pstr_attribute atr -> Option.is_some (fst (doc_atrs [atr]))
| Pstr_eval (_, atrs)
|Pstr_value (_, {pvb_attributes= atrs; _} :: _)
|Pstr_primitive {pval_attributes= atrs; _}
|Pstr_type (_, {ptype_attributes= atrs; _} :: _)
|Pstr_typext {ptyext_attributes= atrs; _}
|Pstr_recmodule ({pmb_expr= {pmod_attributes= atrs; _}; _} :: _)
|Pstr_modtype {pmtd_attributes= atrs; _}
|Pstr_open {popen_attributes= atrs; _}
|Pstr_extension (_, atrs)
|Pstr_class_type ({pci_attributes= atrs; _} :: _)
|Pstr_class ({pci_attributes= atrs; _} :: _) ->
Option.is_some (fst (doc_atrs atrs))
| Pstr_include
{pincl_mod= {pmod_attributes= atrs1; _}; pincl_attributes= atrs2; _}
|Pstr_exception
{ ptyexn_attributes= atrs1
; ptyexn_constructor= {pext_attributes= atrs2; _}
; _ }
|Pstr_module
{pmb_attributes= atrs1; pmb_expr= {pmod_attributes= atrs2; _}; _} ->
Option.is_some (fst (doc_atrs (List.append atrs1 atrs2)))
| Pstr_value (_, [])
|Pstr_type (_, [])
|Pstr_recmodule []
|Pstr_class_type []
|Pstr_class [] ->
false
let is_simple (itm, c) =
match c.Conf.module_item_spacing with
| `Compact | `Preserve ->
Location.is_single_line itm.pstr_loc c.Conf.margin
| `Sparse -> (
match itm.pstr_desc with
| Pstr_include {pincl_mod= me; _} | Pstr_module {pmb_expr= me; _} ->
let rec is_simple_mod me =
match me.pmod_desc with
| Pmod_apply (me1, me2) -> is_simple_mod me1 && is_simple_mod me2
| Pmod_functor (_, me) -> is_simple_mod me
| Pmod_ident i -> Longident.is_simple c i.txt
| _ -> false
in
is_simple_mod me
| Pstr_open {popen_expr= {pmod_desc= Pmod_ident i; _}; _} ->
Longident.is_simple c i.txt
| _ -> false )
let allow_adjacent (itmI, cI) (itmJ, cJ) =
match Conf.(cI.module_item_spacing, cJ.module_item_spacing) with
| `Compact, `Compact -> (
match (itmI.pstr_desc, itmJ.pstr_desc) with
| Pstr_eval _, Pstr_eval _
|Pstr_value _, Pstr_value _
|Pstr_primitive _, Pstr_primitive _
|(Pstr_type _ | Pstr_typext _), (Pstr_type _ | Pstr_typext _)
|Pstr_exception _, Pstr_exception _
|( (Pstr_module _ | Pstr_recmodule _ | Pstr_open _ | Pstr_include _)
, (Pstr_module _ | Pstr_recmodule _ | Pstr_open _ | Pstr_include _) )
|Pstr_modtype _, Pstr_modtype _
|Pstr_class _, Pstr_class _
|Pstr_class_type _, Pstr_class_type _
|Pstr_attribute _, Pstr_attribute _
|Pstr_extension _, Pstr_extension _ ->
true
| _ -> false )
| _ -> true
let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
has_cmts_after cmts i1.pstr_loc
|| has_cmts_before cmts i2.pstr_loc
|| has_doc i1 || has_doc i2
||
match Conf.(c1.module_item_spacing, c2.module_item_spacing) with
| `Preserve, `Preserve ->
Source.empty_line_between s i1.pstr_loc.loc_end i2.pstr_loc.loc_start
| _ ->
(not (is_simple (i1, c1)))
|| (not (is_simple (i2, c2)))
|| not (allow_adjacent (i1, c1) (i2, c2))
end
module Signature_item = struct
let has_doc itm =
match itm.psig_desc with
| Psig_attribute atr -> Option.is_some (fst (doc_atrs [atr]))
| Psig_value {pval_attributes= atrs; _}
|Psig_type (_, {ptype_attributes= atrs; _} :: _)
|Psig_typesubst ({ptype_attributes= atrs; _} :: _)
|Psig_typext {ptyext_attributes= atrs; _}
|Psig_modtype {pmtd_attributes= atrs; _}
|Psig_modtypesubst {pmtd_attributes= atrs; _}
|Psig_modsubst {pms_attributes= atrs; _}
|Psig_open {popen_attributes= atrs; _}
|Psig_extension (_, atrs)
|Psig_class_type ({pci_attributes= atrs; _} :: _)
|Psig_class ({pci_attributes= atrs; _} :: _) ->
Option.is_some (fst (doc_atrs atrs))
| Psig_recmodule
({pmd_type= {pmty_attributes= atrs1; _}; pmd_attributes= atrs2; _}
:: _ )
|Psig_include
{pincl_mod= {pmty_attributes= atrs1; _}; pincl_attributes= atrs2; _}
|Psig_exception
{ ptyexn_attributes= atrs1
; ptyexn_constructor= {pext_attributes= atrs2; _}
; _ }
|Psig_module
{pmd_attributes= atrs1; pmd_type= {pmty_attributes= atrs2; _}; _} ->
Option.is_some (fst (doc_atrs (List.append atrs1 atrs2)))
| Psig_type (_, [])
|Psig_typesubst []
|Psig_recmodule []
|Psig_class_type []
|Psig_class [] ->
false
let is_simple (itm, c) =
match c.Conf.module_item_spacing with
| `Compact | `Preserve ->
Location.is_single_line itm.psig_loc c.Conf.margin
| `Sparse -> (
match itm.psig_desc with
| Psig_open {popen_expr= i; _}
|Psig_module {pmd_type= {pmty_desc= Pmty_alias i; _}; _}
|Psig_modsubst {pms_manifest= i; _} ->
Longident.is_simple c i.txt
| _ -> false )
let allow_adjacent (itmI, cI) (itmJ, cJ) =
match Conf.(cI.module_item_spacing, cJ.module_item_spacing) with
| `Compact, `Compact -> (
match (itmI.psig_desc, itmJ.psig_desc) with
| Psig_value _, Psig_value _
|( (Psig_type _ | Psig_typesubst _ | Psig_typext _)
, (Psig_type _ | Psig_typesubst _ | Psig_typext _) )
|Psig_exception _, Psig_exception _
|( ( Psig_module _ | Psig_modsubst _ | Psig_recmodule _ | Psig_open _
| Psig_include _ )
, ( Psig_module _ | Psig_modsubst _ | Psig_recmodule _ | Psig_open _
| Psig_include _ ) )
|Psig_modtype _, Psig_modtype _
|Psig_class _, Psig_class _
|Psig_class_type _, Psig_class_type _
|Psig_attribute _, Psig_attribute _
|Psig_extension _, Psig_extension _ ->
true
| _ -> false )
| _ -> true
let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
has_cmts_after cmts i1.psig_loc
|| has_cmts_before cmts i2.psig_loc
|| has_doc i1 || has_doc i2
||
match Conf.(c1.module_item_spacing, c2.module_item_spacing) with
| `Preserve, `Preserve ->
Source.empty_line_between s i1.psig_loc.loc_end i2.psig_loc.loc_start
| _ ->
(not (is_simple (i1, c1)))
|| (not (is_simple (i2, c2)))
|| not (allow_adjacent (i1, c1) (i2, c2))
end
module Vb = struct
let has_doc itm = Option.is_some (fst (doc_atrs itm.pvb_attributes))
let is_simple (i, c) =
Poly.(c.Conf.module_item_spacing = `Compact)
&& Location.is_single_line i.pvb_loc c.Conf.margin
let break_between ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
has_cmts_after cmts i1.pvb_loc
|| has_cmts_before cmts i2.pvb_loc
|| has_doc i1 || has_doc i2
|| (not (is_simple (i1, c1)))
|| not (is_simple (i2, c2))
end
module Class_field = struct
let has_doc itm =
Option.is_some (fst (doc_atrs itm.pcf_attributes))
||
match itm.pcf_desc with
| Pcf_attribute atr -> Option.is_some (fst (doc_atrs [atr]))
| _ -> false
let is_simple (itm, c) =
match c.Conf.module_item_spacing with
| `Compact | `Preserve ->
Location.is_single_line itm.pcf_loc c.Conf.margin
| `Sparse -> false
let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
has_cmts_after cmts i1.pcf_loc
|| has_cmts_before cmts i2.pcf_loc
|| has_doc i1 || has_doc i2
||
match Conf.(c1.module_item_spacing, c2.module_item_spacing) with
| `Preserve, `Preserve ->
Source.empty_line_between s i1.pcf_loc.loc_end i2.pcf_loc.loc_start
| _ -> (not (is_simple (i1, c1))) || not (is_simple (i2, c2))
end
module Class_type_field = struct
let has_doc itm =
Option.is_some (fst (doc_atrs itm.pctf_attributes))
||
match itm.pctf_desc with
| Pctf_attribute atr -> Option.is_some (fst (doc_atrs [atr]))
| _ -> false
let is_simple (itm, c) =
match c.Conf.module_item_spacing with
| `Compact | `Preserve ->
Location.is_single_line itm.pctf_loc c.Conf.margin
| `Sparse -> false
let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
has_cmts_after cmts i1.pctf_loc
|| has_cmts_before cmts i2.pctf_loc
|| has_doc i1 || has_doc i2
||
match Conf.(c1.module_item_spacing, c2.module_item_spacing) with
| `Preserve, `Preserve ->
Source.empty_line_between s i1.pctf_loc.loc_end i2.pctf_loc.loc_start
| _ -> (not (is_simple (i1, c1))) || not (is_simple (i2, c2))
end
type toplevel_item =
[`Item of structure_item | `Directive of toplevel_directive]
(** Ast terms of various forms. *)
module T = struct
type t =
| Pld of payload
| Typ of core_type
| Cty of class_type
| Pat of pattern
| Exp of expression
| Vb of value_binding
| Cl of class_expr
| Mty of module_type
| Mod of module_expr
| Sig of signature_item
| Str of structure_item
| Clf of class_field
| Ctf of class_type_field
| Tli of toplevel_item
| Top
let dump fs = function
| Pld l -> Format.fprintf fs "Pld:@\n%a" Pprintast.payload l
| Typ t -> Format.fprintf fs "Typ:@\n%a" Pprintast.core_type t
| Pat p -> Format.fprintf fs "Pat:@\n%a" Pprintast.pattern p
| Exp e -> Format.fprintf fs "Exp:@\n%a" Pprintast.expression e
| Vb b -> Format.fprintf fs "Vb:@\n%a" Pprintast.binding b
| Cl cl -> Format.fprintf fs "Cl:@\n%a" Pprintast.class_expr cl
| Mty mt -> Format.fprintf fs "Mty:@\n%a" Pprintast.module_type mt
| Cty cty -> Format.fprintf fs "Cty:@\n%a" Pprintast.class_type cty
| Mod m -> Format.fprintf fs "Mod:@\n%a" Pprintast.module_expr m
| Sig s -> Format.fprintf fs "Sig:@\n%a" Pprintast.signature_item s
| Str s | Tli (`Item s) ->
Format.fprintf fs "Str:@\n%a" Pprintast.structure_item s
| Clf clf -> Format.fprintf fs "Clf:@\n%a@\n" Pprintast.class_field clf
| Ctf ctf ->
Format.fprintf fs "Ctf:@\n%a@\n" Pprintast.class_type_field ctf
| Tli (`Directive d) ->
Format.fprintf fs "Dir:@\n%a" Pprintast.toplevel_phrase (Ptop_dir d)
| Top -> Format.pp_print_string fs "Top"
end
include T
let is_top = function Top -> true | _ -> false
let attributes = function
| Pld _ -> []
| Typ x -> x.ptyp_attributes
| Cty x -> x.pcty_attributes
| Pat x -> x.ppat_attributes
| Exp x -> x.pexp_attributes
| Vb x -> x.pvb_attributes
| Cl x -> x.pcl_attributes
| Mty x -> x.pmty_attributes
| Mod x -> x.pmod_attributes
| Sig _ -> []
| Str _ -> []
| Clf x -> x.pcf_attributes
| Ctf x -> x.pctf_attributes
| Top -> []
| Tli _ -> []
let location = function
| Pld _ -> Location.none
| Typ x -> x.ptyp_loc
| Cty x -> x.pcty_loc
| Pat x -> x.ppat_loc
| Exp x -> x.pexp_loc
| Vb x -> x.pvb_loc
| Cl x -> x.pcl_loc
| Mty x -> x.pmty_loc
| Mod x -> x.pmod_loc
| Sig x -> x.psig_loc
| Str x -> x.pstr_loc
| Clf x -> x.pcf_loc
| Ctf x -> x.pctf_loc
| Tli (`Item x) -> x.pstr_loc
| Tli (`Directive x) -> x.pdir_loc
| Top -> Location.none
let break_between_modules ~cmts ~has_cmts_before ~has_cmts_after (i1, c1)
(i2, c2) =
let has_doc itm = Option.is_some (fst (doc_atrs (attributes itm))) in
let is_simple (itm, c) =
Location.is_single_line (location itm) c.Conf.margin
in
has_cmts_after cmts (location i1)
|| has_cmts_before cmts (location i2)
|| has_doc i1 || has_doc i2
|| (not (is_simple (i1, c1)))
|| not (is_simple (i2, c2))
let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
=
match (i1, i2) with
| Str i1, Str i2 ->
Structure_item.break_between s ~cmts ~has_cmts_before ~has_cmts_after
(i1, c1) (i2, c2)
| Sig i1, Sig i2 ->
Signature_item.break_between s ~cmts ~has_cmts_before ~has_cmts_after
(i1, c1) (i2, c2)
| Vb i1, Vb i2 ->
Vb.break_between ~cmts ~has_cmts_before ~has_cmts_after (i1, c1)
(i2, c2)
| Mty _, Mty _ ->
break_between_modules ~cmts ~has_cmts_before ~has_cmts_after (i1, c1)
(i2, c2)
| Mod _, Mod _ ->
break_between_modules ~cmts ~has_cmts_before ~has_cmts_after (i1, c1)
(i2, c2)
| Tli (`Item i1), Tli (`Item i2) ->
Structure_item.break_between s ~cmts ~has_cmts_before ~has_cmts_after
(i1, c1) (i2, c2)
| Tli (`Directive _), Tli (`Directive _) | Tli _, Tli _ ->
true (* always break between an item and a directive *)
| Clf i1, Clf i2 ->
Class_field.break_between s ~cmts ~has_cmts_before ~has_cmts_after
(i1, c1) (i2, c2)
| Ctf i1, Ctf i2 ->
Class_type_field.break_between s ~cmts ~has_cmts_before ~has_cmts_after
(i1, c1) (i2, c2)
| _ -> assert false
(** Term-in-context, [{ctx; ast}] records that [ast] is (considered to be) an
immediate sub-term of [ctx] as assumed by the operations in
[Requires_sub_terms]. *)
module rec In_ctx : sig
type 'a xt = private {ctx: T.t; ast: 'a}
val sub_ast : ctx:T.t -> T.t -> T.t xt
val sub_typ : ctx:T.t -> core_type -> core_type xt
val sub_cty : ctx:T.t -> class_type -> class_type xt
val sub_pat : ctx:T.t -> pattern -> pattern xt
val sub_exp : ctx:T.t -> expression -> expression xt
val sub_cl : ctx:T.t -> class_expr -> class_expr xt
val sub_mty : ctx:T.t -> module_type -> module_type xt
val sub_mod : ctx:T.t -> module_expr -> module_expr xt
val sub_sig : ctx:T.t -> signature_item -> signature_item xt
val sub_str : ctx:T.t -> structure_item -> structure_item xt
end = struct
open Requires_sub_terms
type 'a xt = {ctx: T.t; ast: 'a}
let sub_ast ~ctx ast = {ctx; ast}
let sub_typ ~ctx typ = check parenze_typ {ctx; ast= typ}
let sub_cty ~ctx cty = {ctx; ast= cty}
let sub_pat ~ctx pat = check parenze_pat {ctx; ast= pat}
let sub_exp ~ctx exp = check parenze_exp {ctx; ast= exp}
let sub_cl ~ctx cl = {ctx; ast= cl}
let sub_mty ~ctx mty = {ctx; ast= mty}
let sub_mod ~ctx mod_ = {ctx; ast= mod_}
let sub_sig ~ctx sig_ = {ctx; ast= sig_}
let sub_str ~ctx str = {ctx; ast= str}
end
(** Operations determining precedence and necessary parenthesization of terms
based on their super-terms. *)
and Requires_sub_terms : sig
val is_simple :
Conf.t -> (expression In_ctx.xt -> int) -> expression In_ctx.xt -> bool
val exposed_right_exp : cls -> expression -> bool
val prec_ast : T.t -> Prec.t option
val parenze_typ : core_type In_ctx.xt -> bool
val parenze_mty : module_type In_ctx.xt -> bool
val parenze_mod : module_expr In_ctx.xt -> bool
val parenze_cty : class_type In_ctx.xt -> bool
val parenze_cl : class_expr In_ctx.xt -> bool
val parenze_pat : pattern In_ctx.xt -> bool
val parenze_exp : expression In_ctx.xt -> bool
val parenze_nested_exp : expression In_ctx.xt -> bool
val is_displaced_infix_op : expression In_ctx.xt -> bool
end = struct
open In_ctx
(* This module uses physical equality extensively to detect sub-terms. *)
let ( == ) = Base.phys_equal
let dump ctx ast fs =
Format.fprintf fs "ast: %a@\nctx: %a@\n" T.dump ast T.dump ctx
let assert_no_raise ~f ~dump x =
assert (
try
ignore (f x) ;
true
with exc ->
let bt = Caml.Printexc.get_backtrace () in
dump x Format.err_formatter ;
Format.eprintf "%s%!" bt ;
raise exc )
(** Predicates to check the claimed sub-term relation. *)
let check_typ {ctx; ast= typ} =
let f tI = typ == tI in
let fst_f (tI, _) = typ == tI in
let snd_f (_, tI) = typ == tI in
let check_cstr = function
| Pcstr_tuple t1N -> List.exists t1N ~f
| Pcstr_record ld1N ->
List.exists ld1N ~f:(fun {pld_type; _} -> typ == pld_type)
in
let check_ext {pext_kind; _} =
match pext_kind with
| Pext_decl (_, cstr, t0) -> check_cstr cstr || Option.exists t0 ~f
| _ -> false
in
let check_typext {ptyext_params; ptyext_constructors; _} =
List.exists ptyext_params ~f:fst_f
|| List.exists ptyext_constructors ~f:check_ext
in
let check_typexn {ptyexn_constructor; _} =
check_ext ptyexn_constructor
in
let check_type {ptype_params; ptype_cstrs; ptype_kind; ptype_manifest; _}
=
List.exists ptype_params ~f:fst_f
|| List.exists ptype_cstrs ~f:(fun (t1, t2, _) ->
typ == t1 || typ == t2 )
|| ( match ptype_kind with
| Ptype_variant cd1N ->
List.exists cd1N ~f:(fun {pcd_args; pcd_res; _} ->
check_cstr pcd_args || Option.exists pcd_res ~f )
| Ptype_record ld1N ->
List.exists ld1N ~f:(fun {pld_type; _} -> typ == pld_type)
| _ -> false )
|| Option.exists ptype_manifest ~f
in
let check_pcstr_fields pcstr_fields =
List.exists pcstr_fields ~f:(fun f ->
match f.pcf_desc with
| Pcf_inherit (_, _, _) -> false
| Pcf_val (_, _, Cfk_virtual t) -> typ == t
| Pcf_val
(_, _, Cfk_concrete (_, {pexp_desc= Pexp_constraint (_, t); _}))
->
typ == t
| Pcf_val (_, _, Cfk_concrete _) -> false
| Pcf_method (_, _, Cfk_virtual t) -> typ == t
| Pcf_method
(_, _, Cfk_concrete (_, {pexp_desc= Pexp_constraint (_, t); _}))
->
typ == t
| Pcf_method
(_, _, Cfk_concrete (_, {pexp_desc= Pexp_poly (e, topt); _}))
->
let rec loop = function
| {pexp_desc= Pexp_newtype (_, e); _} -> loop e
| {pexp_desc= Pexp_constraint (_, t); _} -> t == typ
| {pexp_desc= Pexp_fun (_, _, _, e); _} -> loop e
| _ -> false
in
(match topt with None -> false | Some t -> typ == t)
|| loop e
| Pcf_method (_, _, Cfk_concrete _) -> false
| Pcf_constraint (t1, t2) -> t1 == typ || t2 == typ
| Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _ -> false )
in
let check_class_type l =
List.exists l ~f:(fun {pci_expr= {pcty_desc; _}; pci_params; _} ->
List.exists pci_params ~f:(fun (t, _) -> t == typ)
||
match pcty_desc with
| Pcty_constr (_, l) -> List.exists l ~f:(fun x -> x == typ)
| Pcty_arrow (_, t, _) -> t == typ
| _ -> false )
in
match ctx with
| Pld (PTyp t1) -> assert (typ == t1)
| Pld _ -> assert false
| Typ ctx -> (
match ctx.ptyp_desc with
| Ptyp_extension _ -> ()
| Ptyp_any | Ptyp_var _ -> assert false
| Ptyp_alias (t1, _) | Ptyp_poly (_, t1) -> assert (typ == t1)
| Ptyp_arrow (_, t1, t2) -> assert (typ == t1 || typ == t2)
| Ptyp_tuple t1N | Ptyp_constr (_, t1N) -> assert (List.exists t1N ~f)
| Ptyp_variant (r1N, _, _) ->
assert (
List.exists r1N ~f:(function
| {prf_desc= Rtag (_, _, t1N); _} -> List.exists t1N ~f
| {prf_desc= Rinherit t1; _} -> typ == t1 ) )
| Ptyp_package (_, it1N) -> assert (List.exists it1N ~f:snd_f)
| Ptyp_object (fields, _) ->
assert (
List.exists fields ~f:(function
| {pof_desc= Otag (_, t1); _} -> typ == t1
| {pof_desc= Oinherit t1; _} -> typ == t1 ) )
| Ptyp_class (_, l) -> assert (List.exists l ~f) )
| Cty {pcty_desc; _} ->
assert (
match pcty_desc with
| Pcty_constr (_, l) -> List.exists l ~f
| Pcty_arrow (_, t, _) -> t == typ
| Pcty_open _ -> false
| Pcty_extension _ -> false
| Pcty_signature {pcsig_self; pcsig_fields; _} ->