This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNapkinscript.ml
19541 lines (18624 loc) · 619 KB
/
Napkinscript.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module MiniBuffer : sig
type t
val add_char : t -> char -> unit
val add_string : t -> string -> unit
val contents : t -> string
val create : int -> t
val flush_newline : t -> unit
val length : t -> int
val unsafe_get : t -> int -> char
end = struct
type t = {
mutable buffer : bytes;
mutable position : int;
mutable length : int;
}
let create n =
let n = if n < 1 then 1 else n in
let s = (Bytes.create [@doesNotRaise]) n in
{buffer = s; position = 0; length = n}
let contents b = Bytes.sub_string b.buffer 0 b.position
let unsafe_get b ofs =
Bytes.unsafe_get b.buffer ofs
let length b = b.position
(* Can't be called directly, don't add to the interface *)
let resize_internal b more =
let len = b.length in
let new_len = ref len in
while b.position + more > !new_len do new_len := 2 * !new_len done;
if !new_len > Sys.max_string_length then begin
if b.position + more <= Sys.max_string_length
then new_len := Sys.max_string_length
end;
let new_buffer = (Bytes.create [@doesNotRaise]) !new_len in
(* PR#6148: let's keep using [blit] rather than [unsafe_blit] in
this tricky function that is slow anyway. *)
Bytes.blit b.buffer 0 new_buffer 0 b.position [@doesNotRaise];
b.buffer <- new_buffer;
b.length <- !new_len
let add_char b c =
let pos = b.position in
if pos >= b.length then resize_internal b 1;
Bytes.unsafe_set b.buffer pos c;
b.position <- pos + 1
let add_string b s =
let len = String.length s in
let new_position = b.position + len in
if new_position > b.length then resize_internal b len;
Bytes.blit_string s 0 b.buffer b.position len [@doesNotRaise];
b.position <- new_position
(* adds newline and trims all preceding whitespace *)
let flush_newline b =
let position = ref (b.position) in
while (Bytes.unsafe_get b.buffer (!position - 1)) = ' ' && !position >= 0 do
position := !position - 1;
done;
b.position <- !position;
add_char b '\n'
end
module Doc = struct
type mode = Break | Flat
type lineStyle =
| Classic (* fits? -> replace with space *)
| Soft (* fits? -> replaced with nothing *)
| Hard (* always included, forces breaks in parents *)
type t =
| Nil
| Text of string
| Concat of t list
| Indent of t
| IfBreaks of {yes: t; no: t}
| LineSuffix of t
| LineBreak of lineStyle
| Group of {shouldBreak: bool; doc: t}
| CustomLayout of t list
| BreakParent
(* | Cursor *)
let nil = Nil
let line = LineBreak Classic
let hardLine = LineBreak Hard
let softLine = LineBreak Soft
let text s = Text s
let concat l = Concat l
let indent d = Indent d
let ifBreaks t f = IfBreaks {yes = t; no = f}
let lineSuffix d = LineSuffix d
let group d = Group {shouldBreak = false; doc = d}
let breakableGroup ~forceBreak d = Group {shouldBreak = forceBreak; doc = d}
let customLayout gs = CustomLayout gs
let breakParent = BreakParent
(* let cursor = Cursor *)
let space = Text " "
let comma = Text ","
let dot = Text "."
let dotdot = Text ".."
let dotdotdot = Text "..."
let lessThan = Text "<"
let greaterThan = Text ">"
let lbrace = Text "{"
let rbrace = Text "}"
let lparen = Text "("
let rparen = Text ")"
let lbracket = Text "["
let rbracket = Text "]"
let question = Text "?"
let tilde = Text "~"
let equal = Text "="
let trailingComma = IfBreaks {yes = comma; no = nil}
let doubleQuote = Text "\""
let propagateForcedBreaks doc =
let rec walk doc = match doc with
| Text _ | Nil | LineSuffix _ ->
(false, doc)
| BreakParent ->
(true, Nil)
| LineBreak Hard ->
(true, doc)
| LineBreak (Classic | Soft) ->
(false, doc)
| Indent children ->
let (childForcesBreak, newChildren) = walk children in
(childForcesBreak, Indent newChildren)
| IfBreaks {yes = trueDoc; no = falseDoc} ->
let (falseForceBreak, falseDoc) = walk falseDoc in
if falseForceBreak then
let (_, trueDoc) = walk trueDoc in
(true, trueDoc)
else
let forceBreak, trueDoc = walk trueDoc in
(forceBreak, IfBreaks {yes = trueDoc; no = falseDoc})
| Group {shouldBreak = forceBreak; doc = children} ->
let (childForcesBreak, newChildren) = walk children in
let shouldBreak = forceBreak || childForcesBreak in
(shouldBreak, Group {shouldBreak; doc = newChildren})
| Concat children ->
let (forceBreak, newChildren) = List.fold_left (fun (forceBreak, newChildren) child ->
let (childForcesBreak, newChild) = walk child in
(forceBreak || childForcesBreak, newChild::newChildren)
) (false, []) children
in
(forceBreak, Concat (List.rev newChildren))
| CustomLayout children ->
(* When using CustomLayout, we don't want to propagate forced breaks
* from the children up. By definition it picks the first layout that fits
* otherwise it takes the last of the list.
* However we do want to propagate forced breaks in the sublayouts. They
* might need to be broken. We just don't propagate them any higher here *)
let children = match walk (Concat children) with
| (_, Concat children) -> children
| _ -> assert false
in
(false, CustomLayout children)
in
let (_, processedDoc) = walk doc in
processedDoc
let join ~sep docs =
let rec loop acc sep docs =
match docs with
| [] -> List.rev acc
| [x] -> List.rev (x::acc)
| x::xs -> loop (sep::x::acc) sep xs
in
Concat(loop [] sep docs)
let rec fits w doc = match doc with
| _ when w < 0 -> false
| [] -> true
| (_ind, _mode, Text txt)::rest -> fits (w - String.length txt) rest
| (ind, mode, Indent doc)::rest -> fits w ((ind + 2, mode, doc)::rest)
| (_ind, Flat, LineBreak break)::rest ->
if break = Hard then true
else
let w = if break = Classic then w - 1 else w in
fits w rest
| (_ind, _mode, Nil)::rest -> fits w rest
| (_ind, Break, LineBreak _break)::_rest -> true
| (ind, mode, Group {shouldBreak = forceBreak; doc})::rest ->
let mode = if forceBreak then Break else mode in
fits w ((ind, mode, doc)::rest)
| (ind, mode, IfBreaks {yes = breakDoc; no = flatDoc})::rest ->
if mode = Break then
fits w ((ind, mode, breakDoc)::rest)
else
fits w ((ind, mode, flatDoc)::rest)
| (ind, mode, Concat docs)::rest ->
let ops = List.map (fun doc -> (ind, mode, doc)) docs in
fits w (List.append ops rest)
(* | (_ind, _mode, Cursor)::rest -> fits w rest *)
| (_ind, _mode, LineSuffix _)::rest -> fits w rest
| (_ind, _mode, BreakParent)::rest -> fits w rest
| (ind, mode, CustomLayout (hd::_))::rest ->
(* TODO: if we have nested custom layouts, what we should do here? *)
fits w ((ind, mode, hd)::rest)
| (_ind, _mode, CustomLayout _)::rest ->
fits w rest
let toString ~width doc =
let doc = propagateForcedBreaks doc in
let buffer = MiniBuffer.create 1000 in
let rec process ~pos lineSuffices stack =
match stack with
| ((ind, mode, doc) as cmd)::rest ->
begin match doc with
| Nil | BreakParent ->
process ~pos lineSuffices rest
| Text txt ->
MiniBuffer.add_string buffer txt;
process ~pos:(String.length txt + pos) lineSuffices rest
| LineSuffix doc ->
process ~pos ((ind, mode, doc)::lineSuffices) rest
| Concat docs ->
let ops = List.map (fun doc -> (ind, mode, doc)) docs in
process ~pos lineSuffices (List.append ops rest)
| Indent doc ->
process ~pos lineSuffices ((ind + 2, mode, doc)::rest)
| IfBreaks {yes = breakDoc; no = flatDoc} ->
if mode = Break then
process ~pos lineSuffices ((ind, mode, breakDoc)::rest)
else
process ~pos lineSuffices ((ind, mode, flatDoc)::rest)
| LineBreak lineStyle ->
if mode = Break then (
begin match lineSuffices with
| [] ->
MiniBuffer.flush_newline buffer;
MiniBuffer.add_string buffer (String.make ind ' ' [@doesNotRaise]);
process ~pos:ind [] rest
| _docs ->
process ~pos:ind [] (List.concat [List.rev lineSuffices; cmd::rest])
end
) else (* mode = Flat *) (
let pos = match lineStyle with
| Classic -> MiniBuffer.add_string buffer " "; pos + 1
| Hard -> MiniBuffer.flush_newline buffer; 0
| Soft -> pos
in
process ~pos lineSuffices rest
)
| Group {shouldBreak; doc} ->
if shouldBreak || not (fits (width - pos) ((ind, Flat, doc)::rest)) then
process ~pos lineSuffices ((ind, Break, doc)::rest)
else
process ~pos lineSuffices ((ind, Flat, doc)::rest)
| CustomLayout docs ->
let rec findGroupThatFits groups = match groups with
| [] -> Nil
| [lastGroup] -> lastGroup
| doc::docs ->
if (fits (width - pos) ((ind, Flat, doc)::rest)) then
doc
else
findGroupThatFits docs
in
let doc = findGroupThatFits docs in
process ~pos lineSuffices ((ind, Flat, doc)::rest)
end
| [] ->
begin match lineSuffices with
| [] -> ()
| suffices ->
process ~pos:0 [] (List.rev suffices)
end
in
process ~pos:0 [] [0, Flat, doc];
let len = MiniBuffer.length buffer in
if len > 0 && MiniBuffer.unsafe_get buffer (len - 1) != '\n' then
MiniBuffer.add_char buffer '\n';
MiniBuffer.contents buffer
let debug t =
let rec toDoc = function
| Nil -> text "nil"
| BreakParent -> text "breakparent"
| Text txt -> text ("text(" ^ txt ^ ")")
| LineSuffix doc -> group(
concat [
text "linesuffix(";
indent (
concat [line; toDoc doc]
);
line;
text ")"
]
)
| Concat docs -> group(
concat [
text "concat(";
indent (
concat [
line;
join ~sep:(concat [text ","; line])
(List.map toDoc docs) ;
]
);
line;
text ")"
]
)
| CustomLayout docs -> group(
concat [
text "customLayout(";
indent (
concat [
line;
join ~sep:(concat [text ","; line])
(List.map toDoc docs) ;
]
);
line;
text ")"
]
)
| Indent doc ->
concat [
text "indent(";
softLine;
toDoc doc;
softLine;
text ")";
]
| IfBreaks {yes = trueDoc; no = falseDoc} ->
group(
concat [
text "ifBreaks(";
indent (
concat [
line;
toDoc trueDoc;
concat [text ","; line];
toDoc falseDoc;
]
);
line;
text ")"
]
)
| LineBreak break ->
let breakTxt = match break with
| Classic -> "Classic"
| Soft -> "Soft"
| Hard -> "Hard"
in
text ("LineBreak(" ^ breakTxt ^ ")")
| Group {shouldBreak; doc} ->
group(
concat [
text "Group(";
indent (
concat [
line;
text ("shouldBreak: " ^ (string_of_bool shouldBreak));
concat [text ","; line];
toDoc doc;
]
);
line;
text ")"
]
)
in
let doc = toDoc t in
toString ~width:10 doc |> print_endline
[@@live]
end
module Sexp: sig
type t
val atom: string -> t
val list: t list -> t
val toString: 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 toDoc t =
match t with
| Atom s -> Doc.text s
| List [] -> Doc.text "()"
| List [sexpr] -> Doc.concat [Doc.lparen; toDoc sexpr; Doc.rparen;]
| List (hd::tail) ->
Doc.group (
Doc.concat [
Doc.lparen;
toDoc hd;
Doc.indent (
Doc.concat [
Doc.line;
Doc.join ~sep:Doc.line (List.map toDoc tail);
]
);
Doc.rparen;
]
)
let toString sexpr =
let doc = toDoc sexpr in
Doc.toString ~width:80 doc
end
module SexpAst: sig
val implementation: Parsetree.structure -> Sexp.t
val interface: Parsetree.signature -> Sexp.t
end = struct
open Parsetree
let mapEmpty ~f items =
match items with
| [] -> [Sexp.list []]
| items -> List.map f items
let string txt =
Sexp.atom ("\"" ^ txt ^ "\"")
let char c =
Sexp.atom ("'" ^ (Char.escaped c) ^ "'")
let optChar 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 closedFlag flag = match flag with
| Asttypes.Closed -> Sexp.atom "Closed"
| Open -> Sexp.atom "Open"
let directionFlag flag = match flag with
| Asttypes.Upto -> Sexp.atom "Upto"
| Downto -> Sexp.atom "Downto"
let recFlag flag = match flag with
| Asttypes.Recursive -> Sexp.atom "Recursive"
| Nonrecursive -> Sexp.atom "Nonrecursive"
let overrideFlag flag = match flag with
| Asttypes.Override -> Sexp.atom "Override"
| Fresh -> Sexp.atom "Fresh"
let privateFlag flag = match flag with
| Asttypes.Public -> Sexp.atom "Public"
| Private -> Sexp.atom "Private"
let mutableFlag 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 argLabel 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;
optChar tag;
]
| Pconst_char c ->
Sexp.list [
Sexp.atom "Pconst_char";
Sexp.atom (Char.escaped c);
]
| 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;
optChar tag;
]
in
Sexp.list [
Sexp.atom "constant";
sexpr
]
let rec structure s =
Sexp.list (
(Sexp.atom "structure")::(List.map structureItem s)
)
and structureItem 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";
recFlag flag;
Sexp.list (mapEmpty ~f:valueBinding vbs)
]
| Pstr_primitive (vd) ->
Sexp.list [
Sexp.atom "Pstr_primitive";
valueDescription vd;
]
| Pstr_type (flag, tds) ->
Sexp.list [
Sexp.atom "Pstr_type";
recFlag flag;
Sexp.list (mapEmpty ~f:typeDeclaration tds)
]
| Pstr_typext typext ->
Sexp.list [
Sexp.atom "Pstr_type";
typeExtension typext;
]
| Pstr_exception ec ->
Sexp.list [
Sexp.atom "Pstr_exception";
extensionConstructor ec;
]
| Pstr_module mb ->
Sexp.list [
Sexp.atom "Pstr_module";
moduleBinding mb;
]
| Pstr_recmodule mbs ->
Sexp.list [
Sexp.atom "Pstr_recmodule";
Sexp.list (mapEmpty ~f:moduleBinding mbs);
]
| Pstr_modtype modTypDecl ->
Sexp.list [
Sexp.atom "Pstr_modtype";
moduleTypeDeclaration modTypDecl;
]
| Pstr_open openDesc ->
Sexp.list [
Sexp.atom "Pstr_open";
openDescription openDesc;
]
| Pstr_class _ -> Sexp.atom "Pstr_class"
| Pstr_class_type _ -> Sexp.atom "Pstr_class_type"
| Pstr_include id ->
Sexp.list [
Sexp.atom "Pstr_include";
includeDeclaration 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 includeDeclaration id =
Sexp.list [
Sexp.atom "include_declaration";
moduleExpression id.pincl_mod;
attributes id.pincl_attributes;
]
and openDescription od =
Sexp.list [
Sexp.atom "open_description";
longident od.popen_lid.Asttypes.txt;
attributes od.popen_attributes;
]
and moduleTypeDeclaration mtd =
Sexp.list [
Sexp.atom "module_type_declaration";
string mtd.pmtd_name.Asttypes.txt;
(match mtd.pmtd_type with
| None -> Sexp.atom "None"
| Some modType -> Sexp.list [
Sexp.atom "Some";
moduleType modType;
]);
attributes mtd.pmtd_attributes;
]
and moduleBinding mb =
Sexp.list [
Sexp.atom "module_binding";
string mb.pmb_name.Asttypes.txt;
moduleExpression mb.pmb_expr;
attributes mb.pmb_attributes;
]
and moduleExpression me =
let desc = match me.pmod_desc with
| Pmod_ident modName ->
Sexp.list [
Sexp.atom "Pmod_ident";
longident modName.Asttypes.txt;
]
| Pmod_structure s ->
Sexp.list [
Sexp.atom "Pmod_structure";
structure s;
]
| Pmod_functor (lbl, optModType, modExpr) ->
Sexp.list [
Sexp.atom "Pmod_functor";
string lbl.Asttypes.txt;
(match optModType with
| None -> Sexp.atom "None"
| Some modType -> Sexp.list [
Sexp.atom "Some";
moduleType modType;
]);
moduleExpression modExpr;
]
| Pmod_apply (callModExpr, modExprArg) ->
Sexp.list [
Sexp.atom "Pmod_apply";
moduleExpression callModExpr;
moduleExpression modExprArg;
]
| Pmod_constraint (modExpr, modType) ->
Sexp.list [
Sexp.atom "Pmod_constraint";
moduleExpression modExpr;
moduleType modType;
]
| 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 moduleType mt =
let desc = match mt.pmty_desc with
| Pmty_ident longidentLoc ->
Sexp.list [
Sexp.atom "Pmty_ident";
longident longidentLoc.Asttypes.txt;
]
| Pmty_signature s ->
Sexp.list [
Sexp.atom "Pmty_signature";
signature s;
]
| Pmty_functor (lbl, optModType, modType) ->
Sexp.list [
Sexp.atom "Pmty_functor";
string lbl.Asttypes.txt;
(match optModType with
| None -> Sexp.atom "None"
| Some modType -> Sexp.list [
Sexp.atom "Some";
moduleType modType;
]);
moduleType modType;
]
| Pmty_alias longidentLoc ->
Sexp.list [
Sexp.atom "Pmty_alias";
longident longidentLoc.Asttypes.txt;
]
| Pmty_extension ext ->
Sexp.list [
Sexp.atom "Pmty_extension";
extension ext;
]
| Pmty_typeof modExpr ->
Sexp.list [
Sexp.atom "Pmty_typeof";
moduleExpression modExpr;
]
| Pmty_with (modType, withConstraints) ->
Sexp.list [
Sexp.atom "Pmty_with";
moduleType modType;
Sexp.list (mapEmpty ~f:withConstraint withConstraints);
]
in
Sexp.list [
Sexp.atom "module_type";
desc;
attributes mt.pmty_attributes;
]
and withConstraint wc = match wc with
| Pwith_type (longidentLoc, td) ->
Sexp.list [
Sexp.atom "Pmty_with";
longident longidentLoc.Asttypes.txt;
typeDeclaration td;
]
| Pwith_module (l1, l2) ->
Sexp.list [
Sexp.atom "Pwith_module";
longident l1.Asttypes.txt;
longident l2.Asttypes.txt;
]
| Pwith_typesubst (longidentLoc, td) ->
Sexp.list [
Sexp.atom "Pwith_typesubst";
longident longidentLoc.Asttypes.txt;
typeDeclaration 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 signatureItem s)
)
and signatureItem si =
let descr = match si.psig_desc with
| Psig_value vd ->
Sexp.list [
Sexp.atom "Psig_value";
valueDescription vd;
]
| Psig_type (flag, typeDeclarations) ->
Sexp.list [
Sexp.atom "Psig_type";
recFlag flag;
Sexp.list (mapEmpty ~f:typeDeclaration typeDeclarations);
]
| Psig_typext typExt ->
Sexp.list [
Sexp.atom "Psig_typext";
typeExtension typExt;
]
| Psig_exception extConstr ->
Sexp.list [
Sexp.atom "Psig_exception";
extensionConstructor extConstr;
]
| Psig_module modDecl ->
Sexp.list [
Sexp.atom "Psig_module";
moduleDeclaration modDecl;
]
| Psig_recmodule modDecls ->
Sexp.list [
Sexp.atom "Psig_recmodule";
Sexp.list (mapEmpty ~f:moduleDeclaration modDecls);
]
| Psig_modtype modTypDecl ->
Sexp.list [
Sexp.atom "Psig_modtype";
moduleTypeDeclaration modTypDecl;
]
| Psig_open openDesc ->
Sexp.list [
Sexp.atom "Psig_open";
openDescription openDesc;
]
| Psig_include inclDecl ->
Sexp.list [
Sexp.atom "Psig_include";
includeDescription inclDecl
]
| Psig_class _ -> Sexp.list [Sexp.atom "Psig_class";]
| Psig_class_type _ -> Sexp.list [ Sexp.atom "Psig_class_type"; ]
| 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 includeDescription id =
Sexp.list [
Sexp.atom "include_description";
moduleType id.pincl_mod;
attributes id.pincl_attributes;
]
and moduleDeclaration md =
Sexp.list [
Sexp.atom "module_declaration";
string md.pmd_name.Asttypes.txt;
moduleType md.pmd_type;
attributes md.pmd_attributes;
]
and valueBinding vb =
Sexp.list [
Sexp.atom "value_binding";
pattern vb.pvb_pat;
expression vb.pvb_expr;
attributes vb.pvb_attributes;
]
and valueDescription vd =
Sexp.list [
Sexp.atom "value_description";
string vd.pval_name.Asttypes.txt;
coreType vd.pval_type;
Sexp.list (mapEmpty ~f:string vd.pval_prim);
attributes vd.pval_attributes;
]
and typeDeclaration td =
Sexp.list [
Sexp.atom "type_declaration";
string td.ptype_name.Asttypes.txt;
Sexp.list [
Sexp.atom "ptype_params";
Sexp.list (mapEmpty ~f:(fun (typexpr, var) ->
Sexp.list [
coreType typexpr;
variance var;
]) td.ptype_params)
];
Sexp.list [
Sexp.atom "ptype_cstrs";
Sexp.list (mapEmpty ~f:(fun (typ1, typ2, _loc) ->
Sexp.list [
coreType typ1;
coreType typ2;
]) td.ptype_cstrs)
];
Sexp.list [
Sexp.atom "ptype_kind";
typeKind 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";
coreType typ;
]
];
Sexp.list [
Sexp.atom "ptype_private";
privateFlag td.ptype_private;
];
attributes td.ptype_attributes;
]
and extensionConstructor ec =
Sexp.list [
Sexp.atom "extension_constructor";
string ec.pext_name.Asttypes.txt;
extensionConstructorKind ec.pext_kind;
attributes ec.pext_attributes;
]
and extensionConstructorKind kind = match kind with
| Pext_decl (args, optTypExpr) ->
Sexp.list [
Sexp.atom "Pext_decl";
constructorArguments args;
match optTypExpr with
| None -> Sexp.atom "None"
| Some typ -> Sexp.list [
Sexp.atom "Some";
coreType typ;
]
]
| Pext_rebind longidentLoc ->
Sexp.list [
Sexp.atom "Pext_rebind";
longident longidentLoc.Asttypes.txt;
]
and typeExtension 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 (mapEmpty ~f:(fun (typexpr, var) ->
Sexp.list [
coreType typexpr;
variance var;
]) te.ptyext_params)
];
Sexp.list [
Sexp.atom "ptyext_constructors";
Sexp.list (mapEmpty ~f:extensionConstructor te.ptyext_constructors);
];
Sexp.list [
Sexp.atom "ptyext_private";
privateFlag te.ptyext_private;
];
attributes te.ptyext_attributes;
]
and typeKind kind = match kind with
| Ptype_abstract -> Sexp.atom "Ptype_abstract"
| Ptype_variant constrDecls ->
Sexp.list [
Sexp.atom "Ptype_variant";
Sexp.list (mapEmpty ~f:constructorDeclaration constrDecls);
]