-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathres_ast_debugger.ml
More file actions
981 lines (922 loc) · 30.8 KB
/
res_ast_debugger.ml
File metadata and controls
981 lines (922 loc) · 30.8 KB
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
module Doc = Res_doc
module CommentTable = Res_comments_table
let print_engine =
Res_driver.
{
print_implementation =
(fun ~width:_ ~filename:_ ~comments:_ structure ->
Printast.implementation Format.std_formatter structure);
print_interface =
(fun ~width:_ ~filename:_ ~comments:_ signature ->
Printast.interface Format.std_formatter signature);
}
module Sexp : sig
type t
val atom : string -> t
val list : t list -> t
val to_string : t -> string
end = struct
type t = Atom of string | List of t list
let atom s = Atom s
let list l = List l
let rec to_doc t =
match t with
| Atom s -> Doc.text s
| List [] -> Doc.text "()"
| List [sexpr] -> Doc.concat [Doc.lparen; to_doc sexpr; Doc.rparen]
| List (hd :: tail) ->
Doc.group
(Doc.concat
[
Doc.lparen;
to_doc hd;
Doc.indent
(Doc.concat
[Doc.line; Doc.join ~sep:Doc.line (List.map to_doc tail)]);
Doc.rparen;
])
let to_string sexpr =
let doc = to_doc sexpr in
Doc.to_string ~width:80 doc
end
module SexpAst = struct
open Parsetree
let map_empty ~f items =
match items with
| [] -> [Sexp.list []]
| items -> List.map f items
let string txt =
Sexp.atom ("\"" ^ Ext_ident.unwrap_uppercase_exotic txt ^ "\"")
let char c = Sexp.atom ("'" ^ Char.escaped c ^ "'")
let opt_char oc =
match oc with
| None -> Sexp.atom "None"
| Some c -> Sexp.list [Sexp.atom "Some"; char c]
let longident l =
let rec loop l =
match l with
| Longident.Lident ident -> Sexp.list [Sexp.atom "Lident"; string ident]
| Longident.Ldot (lident, txt) ->
Sexp.list [Sexp.atom "Ldot"; loop lident; string txt]
| Longident.Lapply (l1, l2) ->
Sexp.list [Sexp.atom "Lapply"; loop l1; loop l2]
in
Sexp.list [Sexp.atom "longident"; loop l]
let closed_flag flag =
match flag with
| Asttypes.Closed -> Sexp.atom "Closed"
| Open -> Sexp.atom "Open"
let direction_flag flag =
match flag with
| Asttypes.Upto -> Sexp.atom "Upto"
| Downto -> Sexp.atom "Downto"
let rec_flag flag =
match flag with
| Asttypes.Recursive -> Sexp.atom "Recursive"
| Nonrecursive -> Sexp.atom "Nonrecursive"
let override_flag flag =
match flag with
| Asttypes.Override -> Sexp.atom "Override"
| Fresh -> Sexp.atom "Fresh"
let private_flag flag =
match flag with
| Asttypes.Public -> Sexp.atom "Public"
| Private -> Sexp.atom "Private"
let mutable_flag flag =
match flag with
| Asttypes.Immutable -> Sexp.atom "Immutable"
| Mutable -> Sexp.atom "Mutable"
let variance v =
match v with
| Asttypes.Covariant -> Sexp.atom "Covariant"
| Contravariant -> Sexp.atom "Contravariant"
| Invariant -> Sexp.atom "Invariant"
let arg_label_loc lbl =
match lbl with
| Asttypes.Nolabel -> Sexp.atom "Nolabel"
| Labelled {txt} -> Sexp.list [Sexp.atom "Labelled"; string txt]
| Optional {txt} -> Sexp.list [Sexp.atom "Optional"; string txt]
let constant c =
let sexpr =
match c with
| Pconst_integer (txt, tag) ->
Sexp.list [Sexp.atom "Pconst_integer"; string txt; opt_char tag]
| Pconst_char _ -> Sexp.list [Sexp.atom "Pconst_char"]
| Pconst_string (_, Some "INTERNAL_RES_CHAR_CONTENTS") ->
Sexp.list [Sexp.atom "Pconst_char"]
| Pconst_string (txt, tag) ->
Sexp.list
[
Sexp.atom "Pconst_string";
string txt;
(match tag with
| Some txt -> Sexp.list [Sexp.atom "Some"; string txt]
| None -> Sexp.atom "None");
]
| Pconst_float (txt, tag) ->
Sexp.list [Sexp.atom "Pconst_float"; string txt; opt_char tag]
in
Sexp.list [Sexp.atom "constant"; sexpr]
let rec structure s =
Sexp.list (Sexp.atom "structure" :: List.map structure_item s)
and structure_item si =
let desc =
match si.pstr_desc with
| Pstr_eval (expr, attrs) ->
Sexp.list [Sexp.atom "Pstr_eval"; expression expr; attributes attrs]
| Pstr_value (flag, vbs) ->
Sexp.list
[
Sexp.atom "Pstr_value";
rec_flag flag;
Sexp.list (map_empty ~f:value_binding vbs);
]
| Pstr_primitive vd ->
Sexp.list [Sexp.atom "Pstr_primitive"; value_description vd]
| Pstr_type (flag, tds) ->
Sexp.list
[
Sexp.atom "Pstr_type";
rec_flag flag;
Sexp.list (map_empty ~f:type_declaration tds);
]
| Pstr_typext typext ->
Sexp.list [Sexp.atom "Pstr_type"; type_extension typext]
| Pstr_exception ec ->
Sexp.list [Sexp.atom "Pstr_exception"; extension_constructor ec]
| Pstr_module mb -> Sexp.list [Sexp.atom "Pstr_module"; module_binding mb]
| Pstr_recmodule mbs ->
Sexp.list
[
Sexp.atom "Pstr_recmodule";
Sexp.list (map_empty ~f:module_binding mbs);
]
| Pstr_modtype mod_typ_decl ->
Sexp.list
[Sexp.atom "Pstr_modtype"; module_type_declaration mod_typ_decl]
| Pstr_open open_desc ->
Sexp.list [Sexp.atom "Pstr_open"; open_description open_desc]
| Pstr_include id ->
Sexp.list [Sexp.atom "Pstr_include"; include_declaration id]
| Pstr_attribute attr ->
Sexp.list [Sexp.atom "Pstr_attribute"; attribute attr]
| Pstr_extension (ext, attrs) ->
Sexp.list [Sexp.atom "Pstr_extension"; extension ext; attributes attrs]
in
Sexp.list [Sexp.atom "structure_item"; desc]
and include_declaration id =
Sexp.list
[
Sexp.atom "include_declaration";
module_expression id.pincl_mod;
attributes id.pincl_attributes;
]
and open_description od =
Sexp.list
[
Sexp.atom "open_description";
longident od.popen_lid.Asttypes.txt;
attributes od.popen_attributes;
]
and module_type_declaration mtd =
Sexp.list
[
Sexp.atom "module_type_declaration";
string mtd.pmtd_name.Asttypes.txt;
(match mtd.pmtd_type with
| None -> Sexp.atom "None"
| Some mod_type -> Sexp.list [Sexp.atom "Some"; module_type mod_type]);
attributes mtd.pmtd_attributes;
]
and module_binding mb =
Sexp.list
[
Sexp.atom "module_binding";
string mb.pmb_name.Asttypes.txt;
module_expression mb.pmb_expr;
attributes mb.pmb_attributes;
]
and module_expression me =
let desc =
match me.pmod_desc with
| Pmod_ident mod_name ->
Sexp.list [Sexp.atom "Pmod_ident"; longident mod_name.Asttypes.txt]
| Pmod_structure s -> Sexp.list [Sexp.atom "Pmod_structure"; structure s]
| Pmod_functor (lbl, opt_mod_type, mod_expr) ->
Sexp.list
[
Sexp.atom "Pmod_functor";
string lbl.Asttypes.txt;
(match opt_mod_type with
| None -> Sexp.atom "None"
| Some mod_type ->
Sexp.list [Sexp.atom "Some"; module_type mod_type]);
module_expression mod_expr;
]
| Pmod_apply (call_mod_expr, mod_expr_arg) ->
Sexp.list
[
Sexp.atom "Pmod_apply";
module_expression call_mod_expr;
module_expression mod_expr_arg;
]
| Pmod_constraint (mod_expr, mod_type) ->
Sexp.list
[
Sexp.atom "Pmod_constraint";
module_expression mod_expr;
module_type mod_type;
]
| Pmod_unpack expr -> Sexp.list [Sexp.atom "Pmod_unpack"; expression expr]
| Pmod_extension ext ->
Sexp.list [Sexp.atom "Pmod_extension"; extension ext]
in
Sexp.list [Sexp.atom "module_expr"; desc; attributes me.pmod_attributes]
and module_type mt =
let desc =
match mt.pmty_desc with
| Pmty_ident longident_loc ->
Sexp.list [Sexp.atom "Pmty_ident"; longident longident_loc.Asttypes.txt]
| Pmty_signature s -> Sexp.list [Sexp.atom "Pmty_signature"; signature s]
| Pmty_functor (lbl, opt_mod_type, mod_type) ->
Sexp.list
[
Sexp.atom "Pmty_functor";
string lbl.Asttypes.txt;
(match opt_mod_type with
| None -> Sexp.atom "None"
| Some mod_type ->
Sexp.list [Sexp.atom "Some"; module_type mod_type]);
module_type mod_type;
]
| Pmty_alias longident_loc ->
Sexp.list [Sexp.atom "Pmty_alias"; longident longident_loc.Asttypes.txt]
| Pmty_extension ext ->
Sexp.list [Sexp.atom "Pmty_extension"; extension ext]
| Pmty_typeof mod_expr ->
Sexp.list [Sexp.atom "Pmty_typeof"; module_expression mod_expr]
| Pmty_with (mod_type, with_constraints) ->
Sexp.list
[
Sexp.atom "Pmty_with";
module_type mod_type;
Sexp.list (map_empty ~f:with_constraint with_constraints);
]
in
Sexp.list [Sexp.atom "module_type"; desc; attributes mt.pmty_attributes]
and with_constraint wc =
match wc with
| Pwith_type (longident_loc, td) ->
Sexp.list
[
Sexp.atom "Pmty_with";
longident longident_loc.Asttypes.txt;
type_declaration td;
]
| Pwith_module (l1, l2) ->
Sexp.list
[
Sexp.atom "Pwith_module";
longident l1.Asttypes.txt;
longident l2.Asttypes.txt;
]
| Pwith_typesubst (longident_loc, td) ->
Sexp.list
[
Sexp.atom "Pwith_typesubst";
longident longident_loc.Asttypes.txt;
type_declaration td;
]
| Pwith_modsubst (l1, l2) ->
Sexp.list
[
Sexp.atom "Pwith_modsubst";
longident l1.Asttypes.txt;
longident l2.Asttypes.txt;
]
and signature s =
Sexp.list (Sexp.atom "signature" :: List.map signature_item s)
and signature_item si =
let descr =
match si.psig_desc with
| Psig_value vd ->
Sexp.list [Sexp.atom "Psig_value"; value_description vd]
| Psig_type (flag, type_declarations) ->
Sexp.list
[
Sexp.atom "Psig_type";
rec_flag flag;
Sexp.list (map_empty ~f:type_declaration type_declarations);
]
| Psig_typext typ_ext ->
Sexp.list [Sexp.atom "Psig_typext"; type_extension typ_ext]
| Psig_exception ext_constr ->
Sexp.list [Sexp.atom "Psig_exception"; extension_constructor ext_constr]
| Psig_module mod_decl ->
Sexp.list [Sexp.atom "Psig_module"; module_declaration mod_decl]
| Psig_recmodule mod_decls ->
Sexp.list
[
Sexp.atom "Psig_recmodule";
Sexp.list (map_empty ~f:module_declaration mod_decls);
]
| Psig_modtype mod_typ_decl ->
Sexp.list
[Sexp.atom "Psig_modtype"; module_type_declaration mod_typ_decl]
| Psig_open open_desc ->
Sexp.list [Sexp.atom "Psig_open"; open_description open_desc]
| Psig_include incl_decl ->
Sexp.list [Sexp.atom "Psig_include"; include_description incl_decl]
| Psig_attribute attr ->
Sexp.list [Sexp.atom "Psig_attribute"; attribute attr]
| Psig_extension (ext, attrs) ->
Sexp.list [Sexp.atom "Psig_extension"; extension ext; attributes attrs]
in
Sexp.list [Sexp.atom "signature_item"; descr]
and include_description id =
Sexp.list
[
Sexp.atom "include_description";
module_type id.pincl_mod;
attributes id.pincl_attributes;
]
and module_declaration md =
Sexp.list
[
Sexp.atom "module_declaration";
string md.pmd_name.Asttypes.txt;
module_type md.pmd_type;
attributes md.pmd_attributes;
]
and value_binding vb =
Sexp.list
[
Sexp.atom "value_binding";
pattern vb.pvb_pat;
expression vb.pvb_expr;
attributes vb.pvb_attributes;
]
and value_description vd =
Sexp.list
[
Sexp.atom "value_description";
string vd.pval_name.Asttypes.txt;
core_type vd.pval_type;
Sexp.list (map_empty ~f:string vd.pval_prim);
attributes vd.pval_attributes;
]
and type_declaration td =
Sexp.list
[
Sexp.atom "type_declaration";
string td.ptype_name.Asttypes.txt;
Sexp.list
[
Sexp.atom "ptype_params";
Sexp.list
(map_empty
~f:(fun (typexpr, var) ->
Sexp.list [core_type typexpr; variance var])
td.ptype_params);
];
Sexp.list
[
Sexp.atom "ptype_cstrs";
Sexp.list
(map_empty
~f:(fun (typ1, typ2, _loc) ->
Sexp.list [core_type typ1; core_type typ2])
td.ptype_cstrs);
];
Sexp.list [Sexp.atom "ptype_kind"; type_kind td.ptype_kind];
Sexp.list
[
Sexp.atom "ptype_manifest";
(match td.ptype_manifest with
| None -> Sexp.atom "None"
| Some typ -> Sexp.list [Sexp.atom "Some"; core_type typ]);
];
Sexp.list [Sexp.atom "ptype_private"; private_flag td.ptype_private];
attributes td.ptype_attributes;
]
and extension_constructor ec =
Sexp.list
[
Sexp.atom "extension_constructor";
string ec.pext_name.Asttypes.txt;
extension_constructor_kind ec.pext_kind;
attributes ec.pext_attributes;
]
and extension_constructor_kind kind =
match kind with
| Pext_decl (args, opt_typ_expr) ->
Sexp.list
[
Sexp.atom "Pext_decl";
constructor_arguments args;
(match opt_typ_expr with
| None -> Sexp.atom "None"
| Some typ -> Sexp.list [Sexp.atom "Some"; core_type typ]);
]
| Pext_rebind longident_loc ->
Sexp.list [Sexp.atom "Pext_rebind"; longident longident_loc.Asttypes.txt]
and type_extension te =
Sexp.list
[
Sexp.atom "type_extension";
Sexp.list
[Sexp.atom "ptyext_path"; longident te.ptyext_path.Asttypes.txt];
Sexp.list
[
Sexp.atom "ptyext_parms";
Sexp.list
(map_empty
~f:(fun (typexpr, var) ->
Sexp.list [core_type typexpr; variance var])
te.ptyext_params);
];
Sexp.list
[
Sexp.atom "ptyext_constructors";
Sexp.list
(map_empty ~f:extension_constructor te.ptyext_constructors);
];
Sexp.list [Sexp.atom "ptyext_private"; private_flag te.ptyext_private];
attributes te.ptyext_attributes;
]
and type_kind kind =
match kind with
| Ptype_abstract -> Sexp.atom "Ptype_abstract"
| Ptype_variant constr_decls ->
Sexp.list
[
Sexp.atom "Ptype_variant";
Sexp.list (map_empty ~f:constructor_declaration constr_decls);
]
| Ptype_record lbl_decls ->
Sexp.list
[
Sexp.atom "Ptype_record";
Sexp.list (map_empty ~f:label_declaration lbl_decls);
]
| Ptype_open -> Sexp.atom "Ptype_open"
and constructor_declaration cd =
Sexp.list
[
Sexp.atom "constructor_declaration";
string cd.pcd_name.Asttypes.txt;
Sexp.list [Sexp.atom "pcd_args"; constructor_arguments cd.pcd_args];
Sexp.list
[
Sexp.atom "pcd_res";
(match cd.pcd_res with
| None -> Sexp.atom "None"
| Some typ -> Sexp.list [Sexp.atom "Some"; core_type typ]);
];
attributes cd.pcd_attributes;
]
and constructor_arguments args =
match args with
| Pcstr_tuple types ->
Sexp.list
[Sexp.atom "Pcstr_tuple"; Sexp.list (map_empty ~f:core_type types)]
| Pcstr_record lds ->
Sexp.list
[
Sexp.atom "Pcstr_record";
Sexp.list (map_empty ~f:label_declaration lds);
]
and label_declaration ld =
Sexp.list
[
Sexp.atom "label_declaration";
string ld.pld_name.Asttypes.txt;
mutable_flag ld.pld_mutable;
core_type ld.pld_type;
attributes ld.pld_attributes;
]
and expression expr =
let desc =
match expr.pexp_desc with
| Pexp_ident longident_loc ->
Sexp.list [Sexp.atom "Pexp_ident"; longident longident_loc.Asttypes.txt]
| Pexp_constant c -> Sexp.list [Sexp.atom "Pexp_constant"; constant c]
| Pexp_let (flag, vbs, expr) ->
Sexp.list
[
Sexp.atom "Pexp_let";
rec_flag flag;
Sexp.list (map_empty ~f:value_binding vbs);
expression expr;
]
| Pexp_fun
{arg_label = arg_lbl; default = expr_opt; lhs = pat; rhs = expr} ->
Sexp.list
[
Sexp.atom "Pexp_fun";
arg_label_loc arg_lbl;
(match expr_opt with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
pattern pat;
expression expr;
]
| Pexp_apply {funct = expr; args} ->
Sexp.list
[
Sexp.atom "Pexp_apply";
expression expr;
Sexp.list
(map_empty
~f:(fun (arg_lbl, expr) ->
Sexp.list [arg_label_loc arg_lbl; expression expr])
args);
]
| Pexp_match (expr, cases) ->
Sexp.list
[
Sexp.atom "Pexp_match";
expression expr;
Sexp.list (map_empty ~f:case cases);
]
| Pexp_try (expr, cases) ->
Sexp.list
[
Sexp.atom "Pexp_try";
expression expr;
Sexp.list (map_empty ~f:case cases);
]
| Pexp_tuple exprs ->
Sexp.list
[Sexp.atom "Pexp_tuple"; Sexp.list (map_empty ~f:expression exprs)]
| Pexp_construct (longident_loc, expr_opt) ->
Sexp.list
[
Sexp.atom "Pexp_construct";
longident longident_loc.Asttypes.txt;
(match expr_opt with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
]
| Pexp_variant (lbl, expr_opt) ->
Sexp.list
[
Sexp.atom "Pexp_variant";
string lbl;
(match expr_opt with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
]
| Pexp_record (rows, opt_expr) ->
Sexp.list
[
Sexp.atom "Pexp_record";
Sexp.list
(map_empty
~f:(fun {lid = longident_loc; x = expr} ->
Sexp.list
[longident longident_loc.Asttypes.txt; expression expr])
rows);
(match opt_expr with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
]
| Pexp_field (expr, longident_loc) ->
Sexp.list
[
Sexp.atom "Pexp_field";
expression expr;
longident longident_loc.Asttypes.txt;
]
| Pexp_setfield (expr1, longident_loc, expr2) ->
Sexp.list
[
Sexp.atom "Pexp_setfield";
expression expr1;
longident longident_loc.Asttypes.txt;
expression expr2;
]
| Pexp_array exprs ->
Sexp.list
[Sexp.atom "Pexp_array"; Sexp.list (map_empty ~f:expression exprs)]
| Pexp_ifthenelse (expr1, expr2, opt_expr) ->
Sexp.list
[
Sexp.atom "Pexp_ifthenelse";
expression expr1;
expression expr2;
(match opt_expr with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
]
| Pexp_sequence (expr1, expr2) ->
Sexp.list
[Sexp.atom "Pexp_sequence"; expression expr1; expression expr2]
| Pexp_while (expr1, expr2) ->
Sexp.list [Sexp.atom "Pexp_while"; expression expr1; expression expr2]
| Pexp_for (pat, e1, e2, flag, e3) ->
Sexp.list
[
Sexp.atom "Pexp_for";
pattern pat;
expression e1;
expression e2;
direction_flag flag;
expression e3;
]
| Pexp_constraint (expr, typexpr) ->
Sexp.list
[Sexp.atom "Pexp_constraint"; expression expr; core_type typexpr]
| Pexp_coerce (expr, (), typexpr) ->
Sexp.list [Sexp.atom "Pexp_coerce"; expression expr; core_type typexpr]
| Pexp_send _ -> Sexp.list [Sexp.atom "Pexp_send"]
| Pexp_letmodule (mod_name, mod_expr, expr) ->
Sexp.list
[
Sexp.atom "Pexp_letmodule";
string mod_name.Asttypes.txt;
module_expression mod_expr;
expression expr;
]
| Pexp_letexception (ext_constr, expr) ->
Sexp.list
[
Sexp.atom "Pexp_letexception";
extension_constructor ext_constr;
expression expr;
]
| Pexp_assert expr -> Sexp.list [Sexp.atom "Pexp_assert"; expression expr]
| Pexp_newtype (lbl, expr) ->
Sexp.list
[Sexp.atom "Pexp_newtype"; string lbl.Asttypes.txt; expression expr]
| Pexp_pack mod_expr ->
Sexp.list [Sexp.atom "Pexp_pack"; module_expression mod_expr]
| Pexp_open (flag, longident_loc, expr) ->
Sexp.list
[
Sexp.atom "Pexp_open";
override_flag flag;
longident longident_loc.Asttypes.txt;
expression expr;
]
| Pexp_extension ext ->
Sexp.list [Sexp.atom "Pexp_extension"; extension ext]
| Pexp_await e -> Sexp.list [Sexp.atom "Pexp_await"; expression e]
| Pexp_jsx_element (Jsx_fragment {jsx_fragment_children = xs}) ->
Sexp.list
[
Sexp.atom "Pexp_jsx_fragment"; Sexp.list (map_empty ~f:expression xs);
]
| Pexp_jsx_element (Jsx_unary_element {jsx_unary_element_props = props})
->
Sexp.list
[
Sexp.atom "Pexp_jsx_unary_element";
Sexp.list (map_empty ~f:jsx_prop props);
]
| Pexp_jsx_element
(Jsx_container_element
{
jsx_container_element_props = props;
jsx_container_element_children = xs;
}) ->
Sexp.list
[
Sexp.atom "Pexp_jsx_container_element";
Sexp.list (map_empty ~f:jsx_prop props);
Sexp.list (map_empty ~f:expression xs);
]
| Pexp_jsx_text
{jsx_text_content; jsx_text_leading_space; jsx_text_trailing_space} ->
Sexp.list
[
Sexp.atom "Pexp_jsx_text";
Sexp.atom jsx_text_content;
Sexp.atom (Printf.sprintf "leading=%b" jsx_text_leading_space);
Sexp.atom (Printf.sprintf "trailing=%b" jsx_text_trailing_space);
]
in
Sexp.list [Sexp.atom "expression"; desc]
and jsx_prop = function
| JSXPropPunning (_, name) -> Sexp.atom name.txt
| JSXPropValue (name, _, expr) ->
Sexp.list [Sexp.atom name.txt; expression expr]
| JSXPropSpreading (_, expr) -> expression expr
and case c =
Sexp.list
[
Sexp.atom "case";
Sexp.list [Sexp.atom "pc_lhs"; pattern c.pc_lhs];
Sexp.list
[
Sexp.atom "pc_guard";
(match c.pc_guard with
| None -> Sexp.atom "None"
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
];
Sexp.list [Sexp.atom "pc_rhs"; expression c.pc_rhs];
]
and pattern p =
let descr =
match p.ppat_desc with
| Ppat_any -> Sexp.atom "Ppat_any"
| Ppat_var var ->
Sexp.list [Sexp.atom "Ppat_var"; string var.Location.txt]
| Ppat_alias (p, alias) ->
Sexp.list [Sexp.atom "Ppat_alias"; pattern p; string alias.txt]
| Ppat_constant c -> Sexp.list [Sexp.atom "Ppat_constant"; constant c]
| Ppat_interval (lo, hi) ->
Sexp.list [Sexp.atom "Ppat_interval"; constant lo; constant hi]
| Ppat_tuple patterns ->
Sexp.list
[Sexp.atom "Ppat_tuple"; Sexp.list (map_empty ~f:pattern patterns)]
| Ppat_construct (longident_loc, opt_pattern) ->
Sexp.list
[
Sexp.atom "Ppat_construct";
longident longident_loc.Location.txt;
(match opt_pattern with
| None -> Sexp.atom "None"
| Some p -> Sexp.list [Sexp.atom "some"; pattern p]);
]
| Ppat_variant (lbl, opt_pattern) ->
Sexp.list
[
Sexp.atom "Ppat_variant";
string lbl;
(match opt_pattern with
| None -> Sexp.atom "None"
| Some p -> Sexp.list [Sexp.atom "Some"; pattern p]);
]
| Ppat_record (rows, flag) ->
Sexp.list
[
Sexp.atom "Ppat_record";
closed_flag flag;
Sexp.list
(map_empty
~f:(fun {lid = longident_loc; x = p} ->
Sexp.list [longident longident_loc.Location.txt; pattern p])
rows);
]
| Ppat_array patterns ->
Sexp.list
[Sexp.atom "Ppat_array"; Sexp.list (map_empty ~f:pattern patterns)]
| Ppat_or (p1, p2) ->
Sexp.list [Sexp.atom "Ppat_or"; pattern p1; pattern p2]
| Ppat_constraint (p, typexpr) ->
Sexp.list [Sexp.atom "Ppat_constraint"; pattern p; core_type typexpr]
| Ppat_type longident_loc ->
Sexp.list [Sexp.atom "Ppat_type"; longident longident_loc.Location.txt]
| Ppat_unpack string_loc ->
Sexp.list [Sexp.atom "Ppat_unpack"; string string_loc.Location.txt]
| Ppat_exception p -> Sexp.list [Sexp.atom "Ppat_exception"; pattern p]
| Ppat_extension ext ->
Sexp.list [Sexp.atom "Ppat_extension"; extension ext]
| Ppat_open (longident_loc, p) ->
Sexp.list
[
Sexp.atom "Ppat_open";
longident longident_loc.Location.txt;
pattern p;
]
in
Sexp.list [Sexp.atom "pattern"; descr]
and object_field field =
match field with
| Otag (lbl_loc, attrs, typexpr) ->
Sexp.list
[
Sexp.atom "Otag";
string lbl_loc.txt;
attributes attrs;
core_type typexpr;
]
| Oinherit typexpr -> Sexp.list [Sexp.atom "Oinherit"; core_type typexpr]
and row_field field =
match field with
| Rtag (label_loc, attrs, truth, types) ->
Sexp.list
[
Sexp.atom "Rtag";
string label_loc.txt;
attributes attrs;
Sexp.atom (if truth then "true" else "false");
Sexp.list (map_empty ~f:core_type types);
]
| Rinherit typexpr -> Sexp.list [Sexp.atom "Rinherit"; core_type typexpr]
and package_type (mod_name_loc, package_constraints) =
Sexp.list
[
Sexp.atom "package_type";
longident mod_name_loc.Asttypes.txt;
Sexp.list
(map_empty
~f:(fun (mod_name_loc, typexpr) ->
Sexp.list
[longident mod_name_loc.Asttypes.txt; core_type typexpr])
package_constraints);
]
and core_type typexpr =
let desc =
match typexpr.ptyp_desc with
| Ptyp_any -> Sexp.atom "Ptyp_any"
| Ptyp_var var -> Sexp.list [Sexp.atom "Ptyp_var"; string var]
| Ptyp_arrow {arg; ret} ->
Sexp.list
[
Sexp.atom "Ptyp_arrow";
arg_label_loc arg.lbl;
core_type arg.typ;
core_type ret;
]
| Ptyp_tuple types ->
Sexp.list
[Sexp.atom "Ptyp_tuple"; Sexp.list (map_empty ~f:core_type types)]
| Ptyp_constr (longident_loc, types) ->
Sexp.list
[
Sexp.atom "Ptyp_constr";
longident longident_loc.txt;
Sexp.list (map_empty ~f:core_type types);
]
| Ptyp_alias (typexpr, alias) ->
Sexp.list [Sexp.atom "Ptyp_alias"; core_type typexpr; string alias]
| Ptyp_object (fields, flag) ->
Sexp.list
[
Sexp.atom "Ptyp_object";
closed_flag flag;
Sexp.list (map_empty ~f:object_field fields);
]
| Ptyp_variant (fields, flag, opt_labels) ->
Sexp.list
[
Sexp.atom "Ptyp_variant";
Sexp.list (map_empty ~f:row_field fields);
closed_flag flag;
(match opt_labels with
| None -> Sexp.atom "None"
| Some lbls -> Sexp.list (map_empty ~f:string lbls));
]
| Ptyp_poly (lbls, typexpr) ->
Sexp.list
[
Sexp.atom "Ptyp_poly";
Sexp.list (map_empty ~f:(fun lbl -> string lbl.Asttypes.txt) lbls);
core_type typexpr;
]
| Ptyp_package package ->
Sexp.list [Sexp.atom "Ptyp_package"; package_type package]
| Ptyp_extension ext ->
Sexp.list [Sexp.atom "Ptyp_extension"; extension ext]
in
Sexp.list [Sexp.atom "core_type"; desc]
and payload p =
match p with
| PStr s -> Sexp.list (Sexp.atom "PStr" :: map_empty ~f:structure_item s)
| PSig s -> Sexp.list [Sexp.atom "PSig"; signature s]
| PTyp ct -> Sexp.list [Sexp.atom "PTyp"; core_type ct]
| PPat (pat, opt_expr) ->
Sexp.list
[
Sexp.atom "PPat";
pattern pat;
(match opt_expr with
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]
| None -> Sexp.atom "None");
]
and attribute (string_loc, p) =
Sexp.list
[Sexp.atom "attribute"; Sexp.atom string_loc.Asttypes.txt; payload p]
and extension (string_loc, p) =
Sexp.list
[Sexp.atom "extension"; Sexp.atom string_loc.Asttypes.txt; payload p]
and attributes attrs =
let sexprs = map_empty ~f:attribute attrs in
Sexp.list (Sexp.atom "attributes" :: sexprs)
let print_engine =
Res_driver.
{
print_implementation =
(fun ~width:_ ~filename:_ ~comments:_ parsetree ->
parsetree |> structure |> Sexp.to_string |> print_string);
print_interface =
(fun ~width:_ ~filename:_ ~comments:_ parsetree ->
parsetree |> signature |> Sexp.to_string |> print_string);
}
end
let sexp_print_engine = SexpAst.print_engine
let comments_print_engine =
{
Res_driver.print_implementation =
(fun ~width:_ ~filename:_ ~comments s ->
let cmt_tbl = CommentTable.make () in
CommentTable.walk_structure s cmt_tbl comments;
CommentTable.log cmt_tbl);
print_interface =
(fun ~width:_ ~filename:_ ~comments s ->
let cmt_tbl = CommentTable.make () in
CommentTable.walk_signature s cmt_tbl comments;
CommentTable.log cmt_tbl);
}