-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_zipper.ml
2252 lines (2185 loc) · 87.6 KB
/
ast_zipper.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
[@@@warning "-26"]
open Core
open Generic_types
let split_last ls =
let rec loop ls acc =
match ls with
| [] ->
acc |> Option.map ~f:(fun (ls,it) -> List.rev ls, it)
| h :: t ->
let acc = match acc with
| None -> Some ([], h)
| Some (belast,last) ->
Some (last :: belast, h) in
loop t acc
in
loop ls None
type type_seq =
| TyArrow (* T1 -> T2 *)
| TyTuple (* T1 * ... * Tn *)
| TyConstr (* tconst | T tconstr | (T1, ..., Tn) tconstr *)
| TyObj (* < l1:T1; ...; ln:Tn > *)
| TyClass (* #tconstr *)
| TyAlias (* T as 'a *)
| TyVariant (* [`A | `B] *)
| TyPoly (* 'a. T *)
| TyPackage (* (module S) *)
| TyExtension (* [%id] *)
| TyObjectField (* inherit T1 | A : T1 -> T2 *)
| TyPackageField (* val S : T1 -> T2 *)
| TyRowField (* inherit T1 | A : T1 -> T2 *)
[@@deriving show]
type unwrapped_type =
| ConstructorDefinition (* _ A of b _ *)
| LabelSpecification (* _ x : t _ *)
| TypeExtensionCase (* t += _ | C of a _ *)
| TypeExtension (* t += e *)
| Apply (* _ f a b c _ *)
| Array (* _ [x;y;z] _ *)
| Assert (* _ assert E _ *)
| AssignField (* x <- 2 *)
| BindingOp (* _ (let+) _ *)
| ClassArrow (* _ T -> CT _ *)
| ClassConstraint (* _ constraint T1 = T2 _ *)
| ClassConstructor (* _ c [ 'a, .... 'a] _ *)
| ClassField (* _ [@@ A] _ *)
| ClassInherit (* _ inherit CE _ *)
| ClassInitializer (* _ initializer E _ *)
| ClassObject (* _ object ... end _ *)
| ClassObjectType (* _ object ... end _ *) (* only has types *)
| ClassOpen (* _ let open E in CS _ *)
| ClassSignature (* _ object ... end _ *)
| ClassStructure (* _ object ... end _ *)
| ClassTypeDefinition (* _ class s = object ... end _ *)
| ClassTypeDeclaration (* _ class s = object ... end _ *) (* only has types*)
| ClassDefinition (* _ class s = object ... end _ *)
| ClassTypeField (* _ [@@ A ] _ *)
| Coerce (* _ E1 : T1 :> T2 _ *)
| Constraint (* _ E1 : T1 _ *)
| Constructor (* C E *)
| Eval (* module M = begin _ assert true _ end *)
| Extension
| For (* _ for E1 to E2 do E3 done _ *)
| Function (* _ function A -> B | ... | X -> Z _ *)
| GetField (* _ x.(y) _ *)
| IfThenElse (* _ if E1 then E2 else E3 _ *)
| LabelDeclaration
| Lambda
| Lazy
| LetBinding (* _ let P1 = E1 in E2 _ *)
| LetException
| LetModuleBinding (* _ M = X _ *)
| LetModule (* _ let module M = x in E _*)
| LetOp
| Match (* _ match X with _ -> | ... | _ -> E3 _*)
| MethodDeclaration (* _ method x = E _ *)
| MethodType (* _ method x : T _ *)
| ModuleApply (* module X = _ X (Y) _ *)
| ModuleBinding (* _ Name = ME _*)
| ModuleConstraint (* module X = _ X : Y _ *)
| ModuleName (* module X = _ Example _*)
| ModuleOpen (* _ open M _ *)
| ModuleSubst (* _ module X := M _ *)
| ModuleTypeOf (* _ module type of ME _ *)
| ModuleTypeWithConstraint (* MT with ... *)
| ModuleFunctor (* _ functor (X: MT) -> ME _ *)
| ModuleTypeFunctor (* _ functor(X : MT1) -> MT2 _ *)
| ModuleDeclaration (* _ module M = MT _ *)
| ModuleDefinition (* _ module M = MEXPR _ *)
| ModuleTypeDefinition (* _ module type M = X _ *)
| ModuleSignature (* _ sig ... end _ *)
| ModuleStructure (* _ struct ... end _ *)
| ModuleUnpack (* let module X = _ (val X) _ *)
| New (* _ new M.c _ *)
| LambdaType (* fun (type t) E*)
| Override (* _ <E1, .. E2> _ *)
| OverrideField (* _ X1=X2; ...; Xn = En > _ *)
| PackModule (* _ (module ME) _*)
| Poly (* fun x -> _ (e : t ) _*)
| RecordField (* _ x=y _ *)
| Record (* _ { x=y; ... ; } _ *)
| Send (* _ E1 # m _ *)
| Seq (* _ E1; E2; E3; _ *) (* Note: auto unwrapped into sequence*)
| SetField (* _ x.(y) <- j _ *)
| Try (* _ try X with _ -> | ... | _ -> E3 _ *)
| Tuple (* _ (E1,...,EN) _ *)
| TypeDefinition (* _ type t1 = ... and ... and tn = ... *)
| TypeDeclaration (* _ type t1 = t2 _ *)
| TypeConstraint (* _ constraint t1 = t2 _ *)
| ValueBinding (* _ let P1 = E1 _ *)
| ExceptionDefinition (* _ exception E _ *)
| ValueDeclaration (* _ val x : t _ *)
| Variant (* _ `A _ *)
| While (* _ while E1 do E2 done _ *)
| WithConstraint (* _ with type t1 = t2 _ *)
| Pattern
| Type of type_seq
[@@deriving show]
type t =
| Signature_item of Parsetree.signature_item[@opaque]
| Structure_item of Parsetree.structure_item[@opaque]
| Value_binding of Parsetree.value_binding[@opaque]
| Type_declaration of Parsetree.type_declaration[@opaque]
| Attribute of Parsetree.attribute[@opaque]
| CoreType of Parsetree.core_type[@opaque]
| Pattern of Parsetree.pattern[@opaque]
| Expression of Parsetree.expression[@opaque]
| Text of Text_region.t
| Wildcard of Text_region.t
| Sequence of ((Text_region.t * unwrapped_type) option * t list * t * t list)[@opaque]
| EmptySequence of Text_region.t * unwrapped_type[@opaque]
(* Note: Generally all "sequences" that were produced from normal
OCaml code will not be empty - this additional variant is
included specifically to handle strange asts produced by zipper
based modifications *)
let rec to_string =
let truncate ?(n = 15) items =
let items = if String.length items > n then (String.slice items 0 n) ^ "..." else items in
items
in
function
| Signature_item _si ->
(Printf.sprintf "Signature_item")
| Structure_item _si -> "Structure_item" (* " - {" ^ (Pprintast.string_of_structure [si]) ^ "}" *)
| Value_binding _ -> "Value_binding"
| Type_declaration _ -> "Type_declaration"
| Wildcard _ -> "Wildcard"
| Attribute _ -> "Attribute of"
| CoreType _ -> "CoreType of"
| Pattern _ -> "Pattern of"
| Text _ -> "Text"
| Expression _ -> "Expression of"
| Sequence (bound, left, current, right) ->
let bound = match bound with
| None -> ""
| Some (_,s) -> show_unwrapped_type s
in
let items = List.map ~f:to_string (left @ current :: right)
|> String.concat ~sep:", " in
let items = if String.length items > 20 then (String.slice items 0 20) ^ "..." else items in
"Sequence of " ^ bound ^ " (" ^ items ^ ")"
| EmptySequence (_, ty) -> "EmptySequence of " ^ (show_unwrapped_type ty)
(** Huet's zipper for asts *)
type zipper =
| Top
| Node of {
bounds: (Text_region.t * unwrapped_type) option;
below: t list;
parent: zipper;
above: t list;
}
type location =
| MkLocation of t * zipper
let rec t_to_bounds = function
| Text s -> s
| Wildcard s -> s
| Signature_item si ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.signature_item iter si;
get ()
| Structure_item si ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.structure_item iter si;
get ()
| CoreType ct ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.typ iter ct;
get ()
| Attribute attr ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.attribute iter attr;
get ()
| Type_declaration tdcl ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.type_declaration iter tdcl;
get ()
| Value_binding vb ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.value_binding iter vb;
get ()
| Pattern pat ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.pat iter pat;
get ()
| Expression expr ->
let (iter,get) = Text_region.ast_bounds_iterator () in
iter.expr iter expr;
get ()
(* if its a sequence, take the union *)
| Sequence (None, left,elem,right) ->
List.map ~f:t_to_bounds (left @ right)
|> List.fold ~f:(fun a b -> Text_region.union a b ) ~init:(t_to_bounds elem)
| Sequence (Some region, left,elem,right) ->
List.map ~f:t_to_bounds (left @ elem :: right)
|> List.fold ~f:Text_region.union ~init:(fst region)
| EmptySequence (b,_) -> b
let t_shift_by_offset ~diff t =
let rec map t =
let mapper = Text_region.ast_bounds_mapper ~diff in
match t with
| Signature_item si -> Signature_item (mapper.signature_item mapper si)
| Structure_item si -> Structure_item (mapper.structure_item mapper si)
| Attribute si -> Attribute (mapper.attribute mapper si)
| CoreType si -> CoreType (mapper.typ mapper si)
| Value_binding vb -> Value_binding (mapper.value_binding mapper vb)
| Type_declaration tdcl -> Type_declaration (mapper.type_declaration mapper tdcl)
| Pattern pat -> Pattern (mapper.pat mapper pat)
| Expression expr -> Expression (mapper.expr mapper expr)
| Text s -> Text (Text_region.shift_region s diff)
| Wildcard s -> Wildcard (Text_region.shift_region s diff)
| Sequence (bounds, left,elem,right) ->
let left = List.map ~f:map left in
let right = List.map ~f:map right in
let elem = map elem in
let bounds = match bounds with
| None -> None
| Some (region,ty) ->
let region = Text_region.shift_region region diff in
Some (region,ty)
in
Sequence (bounds, left, elem, right)
| EmptySequence (region,ty) ->
let region = Text_region.shift_region region diff in
EmptySequence (region,ty) in
map t
let t_list_to_bounds ls =
match ls with
| h :: t ->
List.map ~f:t_to_bounds t
|> List.fold ~f:Text_region.union ~init:(t_to_bounds h)
|> fun x -> Some x
| _ -> None
(** converts a zipper to the bounds of the current item *)
let to_bounds (MkLocation (current,_)) =
Text_region.to_bounds (t_to_bounds current)
(** updates the bounds of the zipper by a fixed offset *)
let update_bounds ~diff state =
let mapper = Text_region.ast_bounds_mapper ~diff in
(* update the bounds of a zipper by a fixed offset *)
let rec update state =
match state with
| Signature_item si -> Signature_item (mapper.signature_item mapper si)
| Structure_item si -> Structure_item (mapper.structure_item mapper si)
| Value_binding vb -> Value_binding (mapper.value_binding mapper vb)
| CoreType si -> CoreType (mapper.typ mapper si)
| Attribute si -> Attribute (mapper.attribute mapper si)
| Type_declaration tdcl -> Type_declaration (mapper.type_declaration mapper tdcl)
| Pattern pat -> Pattern (mapper.pat mapper pat)
| Expression expr -> Expression (mapper.expr mapper expr)
| Text s -> Text (Text_region.shift_region s diff)
| Wildcard s -> Wildcard (Text_region.shift_region s diff)
| Sequence (None, l,c,r) ->
let update_ls = List.map ~f:update in
Sequence (None, update_ls l, update c, update_ls r)
| Sequence (Some (region, ty), l,c,r) ->
let update_ls = List.map ~f:update in
let region = Text_region.shift_region region diff in
Sequence (Some (region,ty), update_ls l, update c, update_ls r)
| EmptySequence (region,ty) ->
let region = Text_region.shift_region region diff in
EmptySequence (region,ty)
in
update state
let t_sort (ls: t list) =
ls
|> List.map ~f:(fun l -> Text_region.column_start (t_to_bounds l), l)
|> List.sort ~compare:(fun (l, _) (l', _) -> Int.compare l l')
|> List.map ~f:(fun (_,l) -> l)
(** convert an arbitrary location delimited element into a text region *)
let unwrap_loc ({ loc; _ }: 'a Asttypes.loc) =
Text (Text_region.of_location loc)
let sort_t_list =
let to_bounds vl = Text_region.column_start (t_to_bounds vl) in
List.sort ~compare:(fun a b -> Int.compare (to_bounds a) (to_bounds b))
let rec unwrap_extensions ((loc, payload): Parsetree.extension) =
let loc = unwrap_loc loc in
match payload with
| Parsetree.PStr si ->
let si = List.map ~f:(fun v -> Structure_item v) si in
let range =
let sequence = Sequence (None, [], loc, si) in
t_to_bounds sequence in
let bounds = Some (range, Extension) in
Sequence (bounds, [], loc, si)
(* correct *)
| Parsetree.PSig si ->
let si = List.map ~f:(fun v -> Signature_item v) si in
let range =
let sequence = Sequence (None, [], loc, si) in
t_to_bounds sequence in
let bounds = Some (range, Extension) in
Sequence (bounds, [], loc, si)
(* correct *)
| Parsetree.PTyp si ->
let si = CoreType si in
let range =
let sequence = Sequence (None, [], loc, [si]) in
t_to_bounds sequence in
let bounds = Some (range, Extension) in
Sequence (bounds, [], loc, [si])
(* correct *)
| Parsetree.PPat (pat, expr) ->
let si =
let expr = Option.map ~f:(fun e -> Expression e ) expr |> Option.to_list in
(Pattern pat) :: (expr) in
let range =
let sequence = Sequence (None, [], loc, si) in
t_to_bounds sequence in
let bounds = Some (range, Extension) in
Sequence (bounds, [], loc, si)
(* correct *)
and unwrap_binding_op ({ pbop_op; pbop_pat; pbop_exp; _ }: Parsetree.binding_op) =
let loc = unwrap_loc pbop_op in
let pat = Pattern pbop_pat in
let exp = [Expression pbop_exp] in
let range =
let sequence = Sequence (None, [], loc, pat :: exp) in
t_to_bounds sequence in
let bounds = Some (range, BindingOp) in
Sequence (bounds, [], loc, pat :: exp)
(* correct *)
and unwrap_type_declaration ({
ptype_name; (* name of record *)
ptype_params; (* params of record *)
ptype_cstrs; (* constraints on record *)
ptype_manifest; (* manifest *)
ptype_kind; (* fields *)
_
} : Parsetree.type_declaration) =
let loc = unwrap_loc ptype_name in
let params = List.map ptype_params ~f:(fun (cty, _) -> CoreType cty) in
let strs = List.map ptype_cstrs ~f:(fun (c1, c2, _loc) ->
(* let range = Text_region.of_location loc in *)
let c1 = CoreType c1 in
let c2 = CoreType c2 in
let range =
let sequence = Sequence (None, [], c1, [c2]) in
t_to_bounds sequence
in
let bounds = Some (range, TypeConstraint) in
Sequence (bounds, [], c1, [c2])
) in
let o_man = Option.map ~f:(fun v -> CoreType v) ptype_manifest |> Option.to_list in
let kind = unwrap_type_kind ptype_kind in
let items = params @ strs @ o_man @ kind in
let bounds =
let range =
let sequence = Sequence (None, [], loc, items) in
t_to_bounds sequence
in
Some (range, TypeDeclaration) in
Sequence (bounds, [], loc, items)
and unwrap_type_kind t : t list =
match t with
| Parsetree.Ptype_abstract -> []
| Parsetree.Ptype_variant cdecl -> List.map ~f:unwrap_constructor_declaration cdecl
| Parsetree.Ptype_record ldecls -> List.map ~f:unwrap_label_declaration ldecls
| Parsetree.Ptype_open -> []
(* correct *)
and unwrap_module_type
({ pmty_desc;
_ (* pmty_loc; pmty_attributes *) } as mt: Parsetree.module_type) : _ option =
let range =
let iter,get = Text_region.ast_bounds_iterator () in
iter.module_type iter mt ;
get ()
in
begin match pmty_desc with
| Parsetree.Pmty_alias loc
| Parsetree.Pmty_ident loc ->
let loc = unwrap_loc loc in
let bounds = Some (range, ModuleName) in
Some (Sequence (bounds, [], loc, []))
| Parsetree.Pmty_signature (h :: t) ->
let current = Signature_item h in
let above = List.map ~f:(fun x -> Signature_item x) t in
let bounds = Some (range, ModuleSignature) in
Some (Sequence (bounds, [], current, above))
| Parsetree.Pmty_functor (omt, mt) ->
let loc = Text (Text_region.of_location mt.pmty_loc) (* unwrap_loc loc *) in
let loc, o_mt = match omt with
| Parsetree.Unit -> loc, []
| Parsetree.Named (loc, mt) -> unwrap_loc loc, unwrap_module_type mt |> Option.to_list in
let mt = unwrap_module_type mt |> Option.to_list in
let items = o_mt @ mt in
let bounds = Some (range, ModuleTypeFunctor) in
Some (Sequence (bounds, [], loc, items))
| Parsetree.Pmty_with (mt, constraints) ->
let mt = unwrap_module_type mt |> Option.to_list in
let constraints = List.map ~f:unwrap_with_constraint constraints in
let items = constraints @ mt in
begin
match items with
| [] -> None
| h :: t ->
let bounds = Some (range, ModuleTypeWithConstraint) in
Some (Sequence (bounds, [], h, t))
end
| Parsetree.Pmty_typeof mexpr ->
let mexpr = unwrap_module_expr mexpr in
mexpr
|> Option.map ~f:(fun mexpr ->
let bounds = Some (range, ModuleTypeOf) in
Sequence (bounds, [], mexpr, []))
| Parsetree.Pmty_extension ext ->
let ext = unwrap_extensions ext in
let bounds = Some (range, Extension) in
Some (Sequence (bounds, [], ext, []))
| _ -> None
end
(* correct *)
and unwrap_with_constraint (c: Parsetree.with_constraint) =
match c with
| Parsetree.Pwith_typesubst (loc, type_decl)
| Parsetree.Pwith_type (loc, type_decl) ->
let loc = unwrap_loc loc in
let decl = [unwrap_type_declaration type_decl] in
let range =
let sequence = Sequence (None, [], loc, decl) in
t_to_bounds sequence
in
let bounds = Some (range, WithConstraint) in
Sequence (bounds, [], loc, decl)
| Parsetree.Pwith_modsubst (loc1, loc2)
| Parsetree.Pwith_module (loc1, loc2) ->
let loc1 = unwrap_loc loc1 in
let loc2 = unwrap_loc loc2 in
let range =
let sequence = Sequence (None, [], loc1, [loc2]) in
t_to_bounds sequence
in
let bounds = Some (range, WithConstraint) in
Sequence (bounds, [], loc1, [loc2])
| Parsetree.Pwith_modtypesubst (loc, type_decl)
| Parsetree.Pwith_modtype (loc, type_decl) ->
let loc = unwrap_loc loc in
let decl = Option.to_list (unwrap_module_type type_decl) in
let range =
let sequence = Sequence (None, [], loc, decl) in
t_to_bounds sequence
in
let bounds = Some (range, WithConstraint) in
Sequence (bounds, [], loc, decl)
and unwrap_module_expr
({ pmod_desc;
(* pmod_loc=location; *)
_ (* pmod_loc; pmod_attributes *) } as expr: Parsetree.module_expr) =
let range =
let iter,get = Text_region.ast_bounds_iterator () in
iter.module_expr iter expr;
get ()
in
match pmod_desc with
| Parsetree.Pmod_ident loc ->
let bounds = Some (range,ModuleName) in
let loc = unwrap_loc loc in
Some (Sequence (bounds, [], loc, []))
| Parsetree.Pmod_structure (m :: mt) ->
let bounds = Some (range,ModuleStructure) in
Some (Sequence (bounds, [], Structure_item m, List.map ~f:(fun x -> Structure_item x) mt))
| Parsetree.Pmod_functor (o_mt, me) ->
let bounds = Some (range,ModuleFunctor) in
let loc, o_mt = match o_mt with
| Parsetree.Unit -> Text (Text_region.of_location expr.pmod_loc), []
| Parsetree.Named (loc, o_mt) ->
unwrap_loc loc, unwrap_module_type o_mt |> Option.to_list
in
let o_me = match unwrap_module_expr me with
| None -> []
| Some (Sequence (Some (_,ModuleFunctor), [], h, t)) -> h :: t
| Some (t) -> [t]
in
let items = o_mt @ o_me in
Some (Sequence (bounds, [], loc, items))
| Parsetree.Pmod_apply (mexp1, mexp2) ->
let bounds = Some (range,ModuleApply) in
let expr = [mexp1;mexp2]
|> List.map ~f:unwrap_module_expr
|> List.map ~f:Option.to_list
|> ListLabels.flatten
in
begin match expr with
| h :: t -> Some (Sequence (bounds, [], h, t))
| _ -> None
end
| Parsetree.Pmod_constraint (mexp1, mtyp1) ->
let bounds = Some (range,ModuleConstraint) in
let mexp = unwrap_module_expr mexp1 |> Option.to_list in
let mtyp = unwrap_module_type mtyp1 |> Option.to_list in
let items = mtyp @ mexp in
begin match items with
| h :: t -> Some (Sequence (bounds, [], h, t))
| _ -> None
end
| Parsetree.Pmod_unpack expr ->
let bounds = Some (range,ModuleUnpack) in
let expr = Expression expr in
Some (Sequence (bounds, [], expr, []))
| Parsetree.Pmod_extension ext ->
let bounds = Some (range,Extension) in
let ext = unwrap_extensions ext in
Some (Sequence (bounds, [], ext, []))
| _ -> None
and unwrap_case ({ pc_lhs; pc_guard; pc_rhs }: Parsetree.case) =
let items =
let patt = [Pattern pc_lhs] in
let guard = Option.map ~f:(fun v -> Expression v) pc_guard |> Option.to_list in
let expr = [Expression pc_rhs] in
patt @ guard @ expr in
begin
match items with
| h :: t ->
let range =
let sequence = Sequence (None, [], h, t) in
t_to_bounds sequence
in
let bounds = Some (range, Function) in
Some (Sequence (bounds, [], h, t))
| [] -> None
end
and unwrap_core_type ({ptyp_desc; _} as ty : Parsetree.core_type) : t =
let range = t_to_bounds (CoreType ty) in
match ptyp_desc with
| Parsetree.Ptyp_any -> CoreType ty
| Parsetree.Ptyp_var _ -> CoreType ty
| Parsetree.Ptyp_arrow (_, t1, t2) ->
(* TODO: can't handle label as it doesn't have an explicit
location - can generate custom locations from string length? *)
begin match unwrap_core_type t2 with
| Sequence (Some (_, Type TyArrow), below, current, after) ->
Sequence (Some (range, Type TyArrow), [], CoreType t1, below @ current :: after)
| _ ->
Sequence (Some (range, Type TyArrow), [], CoreType t1, [CoreType t2])
end
| Parsetree.Ptyp_tuple [] -> CoreType ty
| Parsetree.Ptyp_tuple (h :: t) ->
Sequence (Some (range, Type TyTuple), [], CoreType h, List.map ~f:(fun v -> CoreType v) t)
| Parsetree.Ptyp_constr (name, tys) ->
let name = unwrap_loc name in
begin match tys with
| [] -> Sequence (Some (range, Type TyConstr), [], name, [])
| h :: t -> Sequence (
Some (range, Type TyConstr), [],
CoreType h,
List.map ~f:(fun v -> CoreType v) t @ [name])
end
| Parsetree.Ptyp_object ([], _) -> CoreType ty
| Parsetree.Ptyp_object (h :: t, _) ->
Sequence (Some (range, Type TyObj), [], unwrap_object_field h, List.map ~f:unwrap_object_field t)
| Parsetree.Ptyp_class (name, tys) ->
let name = unwrap_loc name in
Sequence (Some (range, Type TyClass), [], name, List.map ~f:(fun v -> CoreType v) tys)
| Parsetree.Ptyp_alias (ty, _) ->
Sequence (Some (range, Type TyAlias), [], CoreType ty, [])
| Parsetree.Ptyp_variant ([], _, _) -> CoreType ty
| Parsetree.Ptyp_variant (h :: t, _, _) ->
Sequence (Some (range, Type TyVariant), [], unwrap_record_field h, List.map ~f:unwrap_record_field t)
| Parsetree.Ptyp_poly ([], cty) ->
Sequence (Some (range, Type TyPoly), [], CoreType cty, [])
| Parsetree.Ptyp_poly (h :: t, cty) ->
Sequence (Some (range, Type TyPoly), [], unwrap_loc h, List.map ~f:unwrap_loc t @ [CoreType cty])
| Parsetree.Ptyp_package (name, pckg_fields) ->
let name = unwrap_loc name in
Sequence (Some (range, Type TyPackage), [], name, List.map ~f:unwrap_package_field pckg_fields)
| Parsetree.Ptyp_extension ext ->
Sequence (Some (range, Type TyExtension), [], unwrap_extensions ext, [])
and unwrap_package_field (name, cty) : t =
let name = unwrap_loc name in
let range = t_to_bounds @@ Sequence (None, [], name, [CoreType cty]) in
Sequence (Some (range, Type TyPackageField), [], name, [CoreType cty])
and unwrap_record_field { prf_desc; _ } : t =
match prf_desc with
| Parsetree.Rtag (name, _, tys) ->
let name = unwrap_loc name in
let tys = List.map ~f:(fun v -> CoreType v) tys in
let range = t_to_bounds @@ Sequence (None, [], name, tys) in
Sequence (Some (range, Type TyRowField), [], name, tys)
| Parsetree.Rinherit cty ->
let range = t_to_bounds @@ Sequence (None, [], CoreType cty, []) in
Sequence (Some (range, Type TyRowField), [], CoreType cty, [])
and unwrap_object_field ({ pof_desc; _ }) : t =
match pof_desc with
| Parsetree.Otag (name, ty) ->
let name = unwrap_loc name in
let range = t_to_bounds @@ Sequence (None, [], name, [CoreType ty]) in
Sequence (Some (range, Type TyObjectField), [], name, [CoreType ty])
| Parsetree.Oinherit ty ->
let range = t_to_bounds @@ Sequence (None, [], CoreType ty, []) in
Sequence (Some (range, Type TyObjectField), [], CoreType ty, [])
and unwrap_pattern ({ppat_desc; _} as pat : Parsetree.pattern) : t =
let range = t_to_bounds (Pattern pat) in
match ppat_desc with
| Parsetree.Ppat_constant _ -> Pattern pat (* 1, *)
| Parsetree.Ppat_var _ -> Pattern pat (* x *)
| Parsetree.Ppat_any -> Wildcard range (* _ *)
| Parsetree.Ppat_interval (_, _) ->
(* TODO: add support for generating custom locations from string lengths? *)
Pattern pat (* 'a' .. 'z' *)
| Parsetree.Ppat_alias (pat, name) ->
let name = unwrap_loc name in
Sequence (Some (range, Pattern), [], Pattern pat, [name])
(* P as 'a *)
| Parsetree.Ppat_tuple [] -> Pattern pat
| Parsetree.Ppat_tuple (h :: t) ->
Sequence (Some (range, Pattern), [], Pattern h, List.map ~f:(fun v -> Pattern v) t)
(* (p1, ..., pn) (n >= 2) *)
| Parsetree.Ppat_construct (cons, pat) ->
let cons = unwrap_loc cons in
begin match pat with
| None -> cons
| Some (_, pat) -> Sequence (Some (range, Pattern), [], cons, [Pattern pat])
end
(* C, C P, C (P1, ..., Pn) *)
| Parsetree.Ppat_variant (_, pat) ->
(* TODO: add support for generating custom locations from string lengths *)
begin match pat with
| None -> Sequence (Some (range, Pattern), [], Text range, [])
| Some pat -> Pattern pat
end (* `A, `A P *)
| Parsetree.Ppat_record ([], _) -> Pattern pat
| Parsetree.Ppat_record (h :: t, _) ->
Sequence (Some (range, Pattern), [], unwrap_record_binding h, List.map ~f:unwrap_record_binding t)
(* { l1=P1; ...; ln=Pn } *)
| Parsetree.Ppat_array [] -> Pattern pat
| Parsetree.Ppat_array (h :: t) ->
Sequence (Some (range, Pattern), [], Pattern h, List.map ~f:(fun v -> Pattern v) t)
(* [| P1; ...; Pn |] *)
| Parsetree.Ppat_or (p1, p2) ->
Sequence (Some (range, Pattern), [], Pattern p1, [Pattern p2])
(* P1 | P2 *)
| Parsetree.Ppat_constraint (pat, ty) ->
Sequence (Some (range, Pattern), [], Pattern pat, [CoreType ty])
(* (P : T) *)
| Parsetree.Ppat_type _ -> Pattern pat (* #canst *)
| Parsetree.Ppat_lazy pat ->
Sequence (Some (range, Pattern), [], Pattern pat, [])
(* lazy P *)
| Parsetree.Ppat_unpack name ->
let name = unwrap_loc name in
Sequence (Some (range, Pattern), [], name, [])
(* (module P) *)
| Parsetree.Ppat_exception pat ->
Sequence (Some (range, Pattern), [], Pattern pat, [])
(* exception P *)
| Parsetree.Ppat_extension ext ->
Sequence (Some (range, Pattern), [], unwrap_extensions ext, [])
(* [%id] *)
| Parsetree.Ppat_open (name, pat) ->
let name = unwrap_loc name in
Sequence (Some (range, Pattern), [name], Pattern pat, [])
(* M.(P) *)
and unwrap_record_binding ((name: Longident.t Location.loc), (pat: Parsetree.pattern)) =
let name = unwrap_loc name in
let pat = Pattern pat in
let range = t_to_bounds (Sequence (None, [], name, [pat])) in
Sequence (Some (range, Pattern), [], name, [pat])
and unwrap_expr ({ pexp_desc; _ } as expr: Parsetree.expression) =
let range =
t_to_bounds (Expression expr) in
match pexp_desc with
(* | Parsetree.Pexp_ident _ -> (??) *)
(* | Parsetree.Pexp_constant _ -> (??) *)
| Parsetree.Pexp_let (_, vbs, expr) ->
let vbs = List.map ~f:(fun vb -> Value_binding vb) vbs in
(* consecutive let bindings and sequences are flattened out *)
let expr,t = match unwrap_expr expr with
| Sequence (Some (_,LetBinding), [], h, t) -> h, t
| Sequence (Some (_,LetModule), [], h, t) -> h, t
| Sequence (Some (_,LetException), [], h, t) -> h, t
| Sequence (Some (_,BindingOp), [], h, t) -> h, t
| Sequence (Some (_,Seq), [], h, t) -> h, t
| Sequence (Some (_,LetOp), [], h, t) -> h, t
| Sequence (Some (_,Extension), [], h, t) -> h, t
| _ -> Expression expr,[] in
let bounds = Some (range, LetBinding) in
begin
match vbs with
| v :: vbs -> Sequence (bounds, [], v, vbs @ expr :: t)
| [] -> Sequence (bounds, [], expr, t)
end
| Parsetree.Pexp_letmodule (loc, mexpr, expr) ->
let loc = unwrap_loc loc in
let mexpr = unwrap_module_expr mexpr in
let expr = match unwrap_expr expr with
| Sequence (Some (_,LetBinding), [], h, t) -> h :: t
| Sequence (Some (_,LetModule), [], h, t) -> h :: t
| Sequence (Some (_,LetException), [], h, t) -> h :: t
| Sequence (Some (_,BindingOp), [], h, t) -> h :: t
| Sequence (Some (_,Seq), [], h, t) -> h :: t
| Sequence (Some (_,LetOp), [], h, t) -> h :: t
| Sequence (Some (_,Extension), [], h, t) -> h :: t
| _ -> [Expression expr] in
let bounds = Some (range, LetModule) in
let modbind =
match mexpr with
| None -> loc
| Some mexpr ->
let range =
let sequence = Sequence (None, [], loc, [mexpr]) in
t_to_bounds sequence
in
let bounds = Some (range, LetModuleBinding) in
Sequence (bounds, [], loc, [mexpr]) in
Sequence (bounds, [], modbind, expr)
| Parsetree.Pexp_letexception (cons, expr) ->
let expn = unwrap_extension_constructor cons in
let expr = match unwrap_expr expr with
| Sequence (Some (_,LetBinding), [], h, t) -> h :: t
| Sequence (Some (_,LetModule), [], h, t) -> h :: t
| Sequence (Some (_,LetException), [], h, t) -> h :: t
| Sequence (Some (_,BindingOp), [], h, t) -> h :: t
| Sequence (Some (_,Seq), [], h, t) -> h :: t
| Sequence (Some (_,LetOp), [], h, t) -> h :: t
| Sequence (Some (_,Extension), [], h, t) -> h :: t
| _ -> [Expression expr] in
let bounds = Some (range, LetException) in
Sequence (bounds, [], expn, expr)
| Parsetree.Pexp_letop { let_; ands; body } ->
let current = unwrap_binding_op let_ in
let right1 = List.map ~f:unwrap_binding_op ands in
let right2 =
let sub_expression = unwrap_expr body in
match sub_expression with
| Sequence (Some (_,LetBinding), [], h, t) -> h :: t
| Sequence (Some (_,LetModule), [], h, t) -> h :: t
| Sequence (Some (_,LetException), [], h, t) -> h :: t
| Sequence (Some (_,LetOp), [], h, t) -> h :: t
| Sequence (Some (_,BindingOp), [], h, t) -> h :: t
| Sequence (Some (_,Seq), [], h, t) -> h :: t
| Sequence (Some (_,Extension), [], h, t) -> h :: t
| _ -> [Expression body] in
let bounds = Some (range, LetOp) in
Sequence (bounds, [], current, right1 @ right2)
| Parsetree.Pexp_function (cases) ->
let cases = List.filter_map ~f:unwrap_case cases in
let bounds = Some (range, Function) in
begin
match cases with
| [] -> Expression expr
| h :: t -> Sequence (bounds, [], h, t)
end
| Parsetree.Pexp_newtype (loc, expr) ->
let loc = unwrap_loc loc in
let expr = match unwrap_expr expr with
| Sequence (Some (_,Lambda), [], h, t) -> h :: t
| Sequence (Some (_,LambdaType), [], h, t) -> h :: t
| Sequence (Some (_,Constraint), [], h, t) -> h :: t
(* intermediate *)
| _ -> [Expression expr]
in
let bounds = Some (range, LambdaType) in
Sequence (bounds, [], loc, expr)
| Parsetree.Pexp_fun (_, o_deflt, pat, expr) ->
let items =
let dflt = Option.map ~f:(fun e -> Expression e) o_deflt
|> Option.to_list in
let pat = [Pattern pat] in
let expr = match unwrap_expr expr with
| Sequence (Some (_,Lambda), [], h, t) -> h :: t
| Sequence (Some (_,LambdaType), [], h, t) -> h :: t
| Sequence (Some (_,Constraint), [], h, t) -> h :: t
(* intermediate *)
| _ -> [Expression expr]
in
pat @ dflt @ expr in
let bounds = Some (range, Lambda) in
begin
match items with
| [] -> assert false
| h :: t -> Sequence (bounds, [], h , t)
end
| Parsetree.Pexp_apply (expr, elems) ->
let expr = Expression expr in
let elems = List.map ~f:(fun (_,e) -> Expression e) elems in
let bounds = Some (range, Apply) in
let items = t_sort (expr :: elems) in
begin
match items with
| [] -> assert false
| h :: t -> Sequence (bounds, [], h, t)
end
| Parsetree.Pexp_match (expr, cases) ->
let expr = Expression expr in
let cases = List.filter_map ~f:unwrap_case cases in
Sequence (Some (range, Match), [], expr, cases)
| Parsetree.Pexp_try (expr, cases) ->
let expr = Expression expr in
let cases = List.filter_map ~f:unwrap_case cases in
Sequence (Some (range, Try), [], expr, cases)
| Parsetree.Pexp_tuple (e :: exprs) ->
let expr = Expression e in
let exprs = List.map ~f:(fun e -> Expression e) exprs in
let bounds = Some (range, Tuple) in
Sequence (bounds, [], expr, exprs)
| Parsetree.Pexp_construct (_, Some expr) ->
let expr = Expression expr in
let bounds = Some (range, Constructor) in
Sequence (bounds, [], expr, [])
| Parsetree.Pexp_variant (_, Some expr) ->
let expr = Expression expr in
let bounds = Some (range, Variant) in
Sequence (bounds, [], expr, [])
| Parsetree.Pexp_record (exp_list, exp) ->
let exps = List.map ~f:(fun (vl, exp) ->
let vl = unwrap_loc vl in
let exp = Expression exp in
let range =
let sequence = Sequence (None, [], vl, [exp]) in
t_to_bounds sequence in
let bounds = Some (range, RecordField) in
Sequence (bounds, [], vl, [exp])
) exp_list in
let exp = Option.map ~f:(fun v -> Expression v) exp |> Option.to_list in
let items = exps @ exp in
let bounds = Some (range, Record) in
begin
match items with
| h :: t -> Sequence (bounds, [], h, t)
| [] -> Expression expr
end
| Parsetree.Pexp_field (expr, loc) ->
let expr = Expression expr in
let loc = unwrap_loc loc in
let bounds = Some (range, GetField) in
Sequence (bounds, [], expr, [loc])
| Parsetree.Pexp_setfield (e1, loc, e2) ->
let e1 = Expression e1 in
let loc = unwrap_loc loc in
let e2 = Expression e2 in
let bounds = Some (range, SetField) in
Sequence (bounds, [], e1, [loc; e2])
| Parsetree.Pexp_array (h :: t) ->
let expr = Expression h in
let t = List.map ~f:(fun v -> Expression v) t in
let bounds = Some (range, Array) in
Sequence (bounds, [], expr, t)
| Parsetree.Pexp_ifthenelse (e1, e2, oe3) ->
let e1 = Expression e1 in
let e2 = Expression e2 in
let e3 = Option.map oe3 ~f:(fun e -> Expression e) |> Option.to_list in
let bounds = Some (range, IfThenElse) in
Sequence (bounds, [], e1, e2::e3)
| Parsetree.Pexp_sequence (e1, e2) ->
let e1 = Expression e1 in
let e2 =
let sub_expression = unwrap_expr e2 in
match sub_expression with
| Sequence (Some (_,LetBinding), [], h, t) -> h :: t
| Sequence (Some (_,LetModule), [], h, t) -> h :: t
| Sequence (Some (_,BindingOp), [], h, t) -> h :: t
| Sequence (Some (_,Seq), [], h, t) -> h :: t
| Sequence (Some (_,LetOp), [], h, t) -> h :: t
| Sequence (Some (_,Extension), [], h, t) -> h :: t
| _ -> [Expression e2] in
let bounds = Some (range, Seq) in
Sequence (bounds, [], e1, e2)
| Parsetree.Pexp_while (e1, e2) ->
let e1 = Expression e1 in
let e2 = Expression e2 in
let bounds = Some (range, While) in
Sequence (bounds, [], e1, [e2])
| Parsetree.Pexp_for (pat, e1, e2, _, e3) ->
let pat = Pattern pat in
let e1 = Expression e1 in
let e2 = Expression e2 in
let e3 = Expression e3 in
let bounds = Some (range, For) in
Sequence (bounds, [], pat, [e1;e2;e3])
| Parsetree.Pexp_constraint (exp, coretype) ->
let expr = Expression exp in
let coretype = CoreType coretype in
let bounds = Some (range, Constraint) in
Sequence (bounds, [], coretype, [expr])
| Parsetree.Pexp_coerce (e1, c1, c2) ->
let e1 = Expression e1 in
let c1 = Option.map ~f:(fun v -> CoreType v) c1 |> Option.to_list in
let c2 = [CoreType c2] in
let bounds = Some (range, Coerce) in
Sequence (bounds, [], e1, c1 @ c2)
| Parsetree.Pexp_send (e1, loc) ->
let e1 = Expression e1 in
let loc = unwrap_loc loc in
let bounds = Some (range, Send) in
Sequence (bounds, [], e1, [loc])
| Parsetree.Pexp_new location ->
let location = unwrap_loc location in
let bounds = Some (range, New) in
Sequence (bounds, [], location, [])
| Parsetree.Pexp_setinstvar (loc, e1) ->
let loc = unwrap_loc loc in
let e1 = Expression e1 in
let bounds = Some (range, AssignField) in
Sequence (bounds, [], loc, [e1])
| Parsetree.Pexp_override ls ->
let items = List.map ~f:(fun (loc,expr) ->
let loc = unwrap_loc loc in
let expr = Expression expr in
let range =
let sequence = Sequence (None, [], loc, [expr]) in
t_to_bounds sequence in
let bounds = Some (range, OverrideField) in
Sequence (bounds, [], loc, [expr])
) ls in
let bounds = Some (range, Override) in
begin
match items with
| h :: t -> Sequence (bounds, [], h, t)
| [] -> Expression expr
end
| Parsetree.Pexp_assert expression ->
Sequence (Some (range, Assert), [], Expression expression, [])
| Parsetree.Pexp_lazy expr ->
Sequence (Some (range, Lazy), [], Expression expr, [])
| Parsetree.Pexp_poly (expr, cty) ->
let expr = Expression expr in
let cty = Option.map ~f:(fun ct -> CoreType ct) cty
|> Option.to_list in
let bounds = Some (range, Poly) in
Sequence (bounds, [], expr, cty)
| Parsetree.Pexp_pack mexpr ->
let mexpr = unwrap_module_expr mexpr in
begin
match mexpr with
| None -> Expression expr
| Some mexpr ->
Sequence (Some (range, PackModule), [], mexpr, [])
end
| Parsetree.Pexp_open ({ popen_expr; _ }, expr) ->
let mod_expr = unwrap_module_expr popen_expr in
let expr = Expression expr in
begin
match mod_expr with
| Some mod_expr -> Sequence (Some (range, ModuleOpen), [], mod_expr, [expr])
| None -> Sequence (Some (range, ModuleOpen), [], expr, [])
end
| Parsetree.Pexp_extension ext ->
unwrap_extensions ext
| Parsetree.Pexp_object cs ->
let bounds = Some (range, ClassObject) in
let cs = unwrap_class_structure cs in
Sequence (bounds, [], cs, [])
| _ -> Expression expr
(* | Parsetree.Pexp_unreachable -> (??) *)
and unwrap_class_structure ({ pcstr_self; pcstr_fields }:Parsetree.class_structure) =
let pat = Pattern pcstr_self in
let fields = List.map ~f:unwrap_class_fields pcstr_fields in
let range =
let sequence = Sequence (None, [], pat, fields) in
t_to_bounds sequence
in
let bounds = Some (range, ClassStructure) in
Sequence (bounds, [], pat, fields)
and unwrap_class_fields ({ pcf_desc; pcf_loc; _ (* pcf_loc; pcf_attributes *) }:Parsetree.class_field) =
let range = Text_region.of_location pcf_loc in
match pcf_desc with
| Parsetree.Pcf_inherit (_, c_e, name) ->
let c_e = unwrap_class_expr c_e in
let name = Option.map ~f:unwrap_loc name |> Option.to_list in
Sequence (Some (range, ClassInherit), [], c_e, name)
| Parsetree.Pcf_val (name, _, cfk) ->
let name = unwrap_loc name in