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 pathres_outcome_printer.ml
1211 lines (1152 loc) · 35.4 KB
/
res_outcome_printer.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
(* For the curious: the outcome printer is a printer to print data
* from the outcometree.mli file in the ocaml compiler.
* The outcome tree is used by:
* - ocaml's toplevel/repl, print results/errors
* - super errors, print nice errors
* - editor tooling, e.g. show type on hover
*
* In general it represent messages to show results or errors to the user. *)
module Doc = Res_doc
module Token = Res_token
let rec unsafe_for_all_range s ~start ~finish p =
start > finish ||
p (String.unsafe_get s start) &&
unsafe_for_all_range s ~start:(start + 1) ~finish p
let for_all_from s start p =
let len = String.length s in
unsafe_for_all_range s ~start ~finish:(len - 1) p
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
let isValidNumericPolyvarNumber (x : string) =
let len = String.length x in
len > 0 && (
let a = Char.code (String.unsafe_get x 0) in
a <= 57 &&
(if len > 1 then
a > 48 &&
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
else
a >= 48 )
)
(* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
let isArityIdent ident =
if String.length ident >= 6 then
(String.sub [@doesNotRaise]) ident 0 5 = "arity"
else
false
type identifierStyle =
| ExoticIdent
| NormalIdent
let classifyIdentContent ~allowUident txt =
let len = String.length txt in
let rec go i =
if i == len then NormalIdent
else
let c = String.unsafe_get txt i in
if i == 0 && not (
(allowUident && (c >= 'A' && c <= 'Z')) ||
(c >= 'a' && c <= 'z') || c = '_') then
ExoticIdent
else if not (
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c = '\''
|| c = '_'
|| (c >= '0' && c <= '9'))
then
ExoticIdent
else
go (i + 1)
in
if Token.isKeywordTxt txt then
ExoticIdent
else
go 0
let printIdentLike ~allowUident txt =
match classifyIdentContent ~allowUident txt with
| ExoticIdent -> Doc.concat [
Doc.text "\\\"";
Doc.text txt;
Doc.text"\""
]
| NormalIdent -> Doc.text txt
let printPolyVarIdent txt =
(* numeric poly-vars don't need quotes: #644 *)
if isValidNumericPolyvarNumber txt then
Doc.text txt
else
match classifyIdentContent ~allowUident:true txt with
| ExoticIdent -> Doc.concat [
Doc.text "\"";
Doc.text txt;
Doc.text"\""
]
| NormalIdent -> Doc.text txt
(* ReScript doesn't have parenthesized identifiers.
* We don't support custom operators. *)
let parenthesized_ident _name = true
(* TODO: better allocation strategy for the buffer *)
let escapeStringContents s =
let len = String.length s in
let b = Buffer.create len in
for i = 0 to len - 1 do
let c = (String.get [@doesNotRaise]) s i in
if c = '\008' then (
Buffer.add_char b '\\';
Buffer.add_char b 'b';
) else if c = '\009' then (
Buffer.add_char b '\\';
Buffer.add_char b 't';
) else if c = '\010' then (
Buffer.add_char b '\\';
Buffer.add_char b 'n';
) else if c = '\013' then (
Buffer.add_char b '\\';
Buffer.add_char b 'r';
) else if c = '\034' then (
Buffer.add_char b '\\';
Buffer.add_char b '"';
) else if c = '\092' then (
Buffer.add_char b '\\';
Buffer.add_char b '\\';
) else (
Buffer.add_char b c;
);
done;
Buffer.contents b
(* let rec print_ident fmt ident = match ident with
| Outcometree.Oide_ident s -> Format.pp_print_string fmt s
| Oide_dot (id, s) ->
print_ident fmt id;
Format.pp_print_char fmt '.';
Format.pp_print_string fmt s
| Oide_apply (id1, id2) ->
print_ident fmt id1;
Format.pp_print_char fmt '(';
print_ident fmt id2;
Format.pp_print_char fmt ')' *)
let rec printOutIdentDoc ?(allowUident=true) (ident : Outcometree.out_ident) =
match ident with
| Oide_ident s -> printIdentLike ~allowUident s
| Oide_dot (ident, s) -> Doc.concat [
printOutIdentDoc ident;
Doc.dot;
Doc.text s;
]
| Oide_apply (call, arg) ->Doc.concat [
printOutIdentDoc call;
Doc.lparen;
printOutIdentDoc arg;
Doc.rparen;
]
let printOutAttributeDoc (outAttribute: Outcometree.out_attribute) =
Doc.concat [
Doc.text "@";
Doc.text outAttribute.oattr_name;
]
let printOutAttributesDoc (attrs: Outcometree.out_attribute list) =
match attrs with
| [] -> Doc.nil
| attrs ->
Doc.concat [
Doc.group (
Doc.join ~sep:Doc.line (List.map printOutAttributeDoc attrs)
);
Doc.line;
]
let rec collectArrowArgs (outType: Outcometree.out_type) args =
match outType with
| Otyp_arrow (label, argType, returnType) ->
let arg = (label, argType) in
collectArrowArgs returnType (arg::args)
| _ as returnType ->
(List.rev args, returnType)
let rec collectFunctorArgs (outModuleType: Outcometree.out_module_type) args =
match outModuleType with
| Omty_functor (lbl, optModType, returnModType) ->
let arg = (lbl, optModType) in
collectFunctorArgs returnModType (arg::args)
| _ ->
(List.rev args, outModuleType)
let rec printOutTypeDoc (outType: Outcometree.out_type) =
match outType with
| Otyp_abstract | Otyp_open -> Doc.nil
| Otyp_variant (nonGen, outVariant, closed, labels) ->
(* bool * out_variant * bool * (string list) option *)
let opening = match (closed, labels) with
| (true, None) -> (* [#A | #B] *) Doc.softLine
| (false, None) ->
(* [> #A | #B] *)
Doc.concat [Doc.greaterThan; Doc.line]
| (true, Some []) ->
(* [< #A | #B] *)
Doc.concat [Doc.lessThan; Doc.line]
| (true, Some _) ->
(* [< #A | #B > #X #Y ] *)
Doc.concat [Doc.lessThan; Doc.line]
| (false, Some _) ->
(* impossible!? ocaml seems to print ?, see oprint.ml in 4.06 *)
Doc.concat [Doc.text "?"; Doc.line]
in
Doc.group (
Doc.concat [
if nonGen then Doc.text "_" else Doc.nil;
Doc.lbracket;
Doc.indent (
Doc.concat [
opening;
printOutVariant outVariant
]
);
begin match labels with
| None | Some [] -> Doc.nil
| Some tags ->
Doc.group (
Doc.concat [
Doc.space;
Doc.join ~sep:Doc.space (
List.map (fun lbl -> printIdentLike ~allowUident:true lbl) tags
)
]
)
end;
Doc.softLine;
Doc.rbracket;
]
)
| Otyp_alias (typ, aliasTxt) ->
Doc.concat [
printOutTypeDoc typ;
Doc.text " as '";
Doc.text aliasTxt
]
| Otyp_constr (
Oide_dot (Oide_dot (Oide_ident "Js", "Fn") , "arity0"), (* Js.Fn.arity0 *)
[Otyp_constr (Oide_ident ident, [])] (* int or unit or string *)
) ->
(* Js.Fn.arity0<int> -> (.) => int*)
Doc.concat [
Doc.text "(.) => ";
Doc.text ident;
]
| Otyp_constr (
Oide_dot (Oide_dot (Oide_ident "Js", "Fn") , ident), (* Js.Fn.arity2 *)
[(Otyp_arrow _) as arrowType] (* (int, int) => int *)
) when isArityIdent ident ->
(* Js.Fn.arity2<(int, int) => int> -> (. int, int) => int*)
printOutArrowType ~uncurried:true arrowType
| Otyp_constr (outIdent, []) ->
printOutIdentDoc ~allowUident:false outIdent
| Otyp_manifest (typ1, typ2) ->
Doc.concat [
printOutTypeDoc typ1;
Doc.text " = ";
printOutTypeDoc typ2;
]
| Otyp_record record ->
printRecordDeclarationDoc ~inline:true record
| Otyp_stuff txt -> Doc.text txt
| Otyp_var (ng, s) -> Doc.concat [
Doc.text ("'" ^ (if ng then "_" else ""));
Doc.text s
]
| Otyp_object (fields, rest) -> printObjectFields fields rest
| Otyp_class _ -> Doc.nil
| Otyp_attribute (typ, attribute) ->
Doc.group (
Doc.concat [
printOutAttributeDoc attribute;
Doc.line;
printOutTypeDoc typ;
]
)
(* example: Red | Blue | Green | CustomColour(float, float, float) *)
| Otyp_sum constructors ->
printOutConstructorsDoc constructors
(* example: {"name": string, "age": int} *)
| Otyp_constr (
(Oide_dot ((Oide_ident "Js"), "t")),
[Otyp_object (fields, rest)]
) -> printObjectFields fields rest
(* example: node<root, 'value> *)
| Otyp_constr (outIdent, args) ->
let argsDoc = match args with
| [] -> Doc.nil
| args ->
Doc.concat [
Doc.lessThan;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printOutTypeDoc args
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.greaterThan;
]
in
Doc.group (
Doc.concat [
printOutIdentDoc outIdent;
argsDoc;
]
)
| Otyp_tuple tupleArgs ->
Doc.group (
Doc.concat [
Doc.lparen;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printOutTypeDoc tupleArgs
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rparen;
]
)
| Otyp_poly (vars, outType) ->
Doc.group (
Doc.concat [
Doc.join ~sep:Doc.space (
List.map (fun var -> Doc.text ("'" ^ var)) vars
);
Doc.dot;
Doc.space;
printOutTypeDoc outType;
]
)
| Otyp_arrow _ as typ ->
printOutArrowType ~uncurried:false typ
| Otyp_module (modName, stringList, outTypes) ->
let packageTypeDoc = match (stringList, outTypes) with
| [], [] -> Doc.nil
| labels, types ->
let i = ref 0 in
let package = Doc.join ~sep:Doc.line ((List.map2 [@doesNotRaise]) (fun lbl typ ->
Doc.concat [
Doc.text (if i.contents > 0 then "and " else "with ");
Doc.text lbl;
Doc.text " = ";
printOutTypeDoc typ;
]
) labels types)
in
Doc.indent (
Doc.concat [
Doc.line;
package
]
)
in
Doc.concat [
Doc.text "module";
Doc.lparen;
Doc.text modName;
packageTypeDoc;
Doc.rparen;
]
and printOutArrowType ~uncurried typ =
let (typArgs, typ) = collectArrowArgs typ [] in
let args = Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map (fun (lbl, typ) ->
let lblLen = String.length lbl in
if lblLen = 0 then
printOutTypeDoc typ
else
let (lbl, optionalIndicator) =
(* the ocaml compiler hardcodes the optional label inside the string of the label in printtyp.ml *)
match String.unsafe_get lbl 0 with
| '?' -> ((String.sub [@doesNotRaise]) lbl 1 (lblLen - 1) , Doc.text "=?")
| _ -> (lbl, Doc.nil)
in
Doc.group (
Doc.concat [
Doc.text ("~" ^ lbl ^ ": ");
printOutTypeDoc typ;
optionalIndicator
]
)
) typArgs
) in
let argsDoc =
let needsParens = match typArgs with
| _ when uncurried -> true
| [_, (Otyp_tuple _ | Otyp_arrow _)] -> true
(* single argument should not be wrapped *)
| ["", _] -> false
| _ -> true
in
if needsParens then
Doc.group (
Doc.concat [
if uncurried then Doc.text "(. " else Doc.lparen;
Doc.indent (
Doc.concat [
Doc.softLine;
args;
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rparen;
]
)
else args
in
Doc.concat [
argsDoc;
Doc.text " => ";
printOutTypeDoc typ;
]
and printOutVariant variant = match variant with
| Ovar_fields fields -> (* (string * bool * out_type list) list *)
Doc.join ~sep:Doc.line (
(*
* [< | #T([< u2]) & ([< u2]) & ([< u1])] --> no ampersand
* [< | #S & ([< u2]) & ([< u2]) & ([< u1])] --> ampersand
*)
List.mapi (fun i (name, ampersand, types) ->
let needsParens = match types with
| [(Outcometree.Otyp_tuple _)] -> false
| _ -> true
in
Doc.concat [
if i > 0 then
Doc.text "| "
else
Doc.ifBreaks (Doc.text "| ") Doc.nil;
Doc.group (
Doc.concat [
Doc.text "#";
printPolyVarIdent name;
match types with
| [] -> Doc.nil
| types ->
Doc.concat [
if ampersand then Doc.text " & " else Doc.nil;
Doc.indent (
Doc.concat [
Doc.join ~sep:(Doc.concat [Doc.text " &"; Doc.line])
(List.map (fun typ ->
let outTypeDoc = printOutTypeDoc typ in
if needsParens then
Doc.concat [Doc.lparen; outTypeDoc; Doc.rparen]
else
outTypeDoc
) types)
];
);
]
]
)
]
) fields
)
| Ovar_typ typ -> printOutTypeDoc typ
and printObjectFields fields rest =
let dots = match rest with
| Some non_gen -> Doc.text ((if non_gen then "_" else "") ^ "..")
| None -> if fields = [] then Doc.dot else Doc.nil
in
Doc.group (
Doc.concat [
Doc.lbrace;
dots;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map (fun (lbl, outType) -> Doc.group (
Doc.concat [
Doc.text ("\"" ^ lbl ^ "\": ");
printOutTypeDoc outType;
]
)) fields
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rbrace;
]
)
and printOutConstructorsDoc constructors =
Doc.group (
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:Doc.line (
List.mapi (fun i constructor ->
Doc.concat [
if i > 0 then Doc.text "| " else Doc.ifBreaks (Doc.text "| ") Doc.nil;
printOutConstructorDoc constructor;
]
) constructors
)
]
)
)
and printOutConstructorDoc (name, args, gadt) =
let gadtDoc = match gadt with
| Some outType ->
Doc.concat [
Doc.text ": ";
printOutTypeDoc outType
]
| None -> Doc.nil
in
let argsDoc = match args with
| [] -> Doc.nil
| [Otyp_record record] ->
(* inline records
* | Root({
* mutable value: 'value,
* mutable updatedTime: float,
* })
*)
Doc.concat [
Doc.lparen;
Doc.indent (
printRecordDeclarationDoc ~inline:true record;
);
Doc.rparen;
]
| _types ->
Doc.indent (
Doc.concat [
Doc.lparen;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printOutTypeDoc args
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rparen;
]
)
in
Doc.group (
Doc.concat [
Doc.text name;
argsDoc;
gadtDoc
]
)
and printRecordDeclRowDoc (name, mut, arg) =
Doc.group (
Doc.concat [
if mut then Doc.text "mutable " else Doc.nil;
printIdentLike ~allowUident:false name;
Doc.text ": ";
printOutTypeDoc arg;
]
)
and printRecordDeclarationDoc ~inline rows =
let content = Doc.concat [
Doc.lbrace;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printRecordDeclRowDoc rows
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rbrace;
] in
if not inline then
Doc.group content
else content
let printOutType fmt outType =
Format.pp_print_string fmt
(Doc.toString ~width:80 (printOutTypeDoc outType))
let printTypeParameterDoc (typ, (co, cn)) =
Doc.concat [
if not cn then Doc.text "+" else if not co then Doc.text "-" else Doc.nil;
if typ = "_" then Doc.text "_" else Doc.text ("'" ^ typ)
]
let rec printOutSigItemDoc (outSigItem : Outcometree.out_sig_item) =
match outSigItem with
| Osig_class _ | Osig_class_type _ -> Doc.nil
| Osig_ellipsis -> Doc.dotdotdot
| Osig_value valueDecl ->
Doc.group (
Doc.concat [
printOutAttributesDoc valueDecl.oval_attributes;
Doc.text (
match valueDecl.oval_prims with | [] -> "let " | _ -> "external "
);
Doc.text valueDecl.oval_name;
Doc.text ":";
Doc.space;
printOutTypeDoc valueDecl.oval_type;
match valueDecl.oval_prims with
| [] -> Doc.nil
| primitives -> Doc.indent (
Doc.concat [
Doc.text " =";
Doc.line;
Doc.group (
Doc.join ~sep:Doc.line (List.map (fun prim ->
let prim = if prim <> "" && (prim.[0] [@doesNotRaise]) = '\132' then "#rescript-external" else prim in
(* not display those garbage '\132' is a magic number for marshal *)
Doc.text ("\"" ^ prim ^ "\"")) primitives)
)
]
)
]
)
| Osig_typext (outExtensionConstructor, _outExtStatus) ->
printOutExtensionConstructorDoc outExtensionConstructor
| Osig_modtype (modName, Omty_signature []) ->
Doc.concat [
Doc.text "module type ";
Doc.text modName;
]
| Osig_modtype (modName, outModuleType) ->
Doc.group (
Doc.concat [
Doc.text "module type ";
Doc.text modName;
Doc.text " = ";
printOutModuleTypeDoc outModuleType;
]
)
| Osig_module (modName, Omty_alias ident, _) ->
Doc.group (
Doc.concat [
Doc.text "module ";
Doc.text modName;
Doc.text " =";
Doc.line;
printOutIdentDoc ident;
]
)
| Osig_module (modName, outModType, outRecStatus) ->
Doc.group (
Doc.concat [
Doc.text (
match outRecStatus with
| Orec_not -> "module "
| Orec_first -> "module rec "
| Orec_next -> "and "
);
Doc.text modName;
Doc.text ": ";
printOutModuleTypeDoc outModType;
]
)
| Osig_type (outTypeDecl, outRecStatus) ->
(* TODO: manifest ? *)
let attrs = match outTypeDecl.otype_immediate, outTypeDecl.otype_unboxed with
| false, false -> Doc.nil
| true, false ->
Doc.concat [Doc.text "@immediate"; Doc.line]
| false, true ->
Doc.concat [Doc.text "@unboxed"; Doc.line]
| true, true ->
Doc.concat [Doc.text "@immediate @unboxed"; Doc.line]
in
let kw = Doc.text (
match outRecStatus with
| Orec_not -> "type "
| Orec_first -> "type rec "
| Orec_next -> "and "
) in
let typeParams = match outTypeDecl.otype_params with
| [] -> Doc.nil
| _params -> Doc.group (
Doc.concat [
Doc.lessThan;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printTypeParameterDoc outTypeDecl.otype_params
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.greaterThan;
]
)
in
let privateDoc = match outTypeDecl.otype_private with
| Asttypes.Private -> Doc.text "private "
| Public -> Doc.nil
in
let kind = match outTypeDecl.otype_type with
| Otyp_open -> Doc.concat [
Doc.text " = ";
privateDoc;
Doc.text "..";
]
| Otyp_abstract -> Doc.nil
| Otyp_record record -> Doc.concat [
Doc.text " = ";
privateDoc;
printRecordDeclarationDoc ~inline:false record;
]
| typ -> Doc.concat [
Doc.text " = ";
printOutTypeDoc typ
]
in
let constraints = match outTypeDecl.otype_cstrs with
| [] -> Doc.nil
| _ -> Doc.group (
Doc.indent (
Doc.concat [
Doc.hardLine;
Doc.join ~sep:Doc.line (List.map (fun (typ1, typ2) ->
Doc.group (
Doc.concat [
Doc.text "constraint ";
printOutTypeDoc typ1;
Doc.text " =";
Doc.space;
printOutTypeDoc typ2;
]
)
) outTypeDecl.otype_cstrs)
]
)
) in
Doc.group (
Doc.concat [
attrs;
Doc.group (
Doc.concat [
attrs;
kw;
printIdentLike ~allowUident:false outTypeDecl.otype_name;
typeParams;
kind
]
);
constraints
]
)
and printOutModuleTypeDoc (outModType : Outcometree.out_module_type) =
match outModType with
| Omty_abstract -> Doc.nil
| Omty_ident ident -> printOutIdentDoc ident
(* example: module Increment = (M: X_int) => X_int *)
| Omty_functor _ ->
let (args, returnModType) = collectFunctorArgs outModType [] in
let argsDoc = match args with
| [_, None] -> Doc.text "()"
| args ->
Doc.group (
Doc.concat [
Doc.lparen;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map (fun (lbl, optModType) -> Doc.group (
Doc.concat [
Doc.text lbl;
match optModType with
| None -> Doc.nil
| Some modType -> Doc.concat [
Doc.text ": ";
printOutModuleTypeDoc modType;
]
]
)) args
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rparen;
]
)
in
Doc.group (
Doc.concat [
argsDoc;
Doc.text " => ";
printOutModuleTypeDoc returnModType
]
)
| Omty_signature [] -> Doc.nil
| Omty_signature signature ->
Doc.breakableGroup ~forceBreak:true (
Doc.concat [
Doc.lbrace;
Doc.indent (
Doc.concat [
Doc.line;
printOutSignatureDoc signature;
]
);
Doc.softLine;
Doc.rbrace;
]
)
| Omty_alias _ident -> Doc.nil
and printOutSignatureDoc (signature : Outcometree.out_sig_item list) =
let rec loop signature acc =
match signature with
| [] -> List.rev acc
| Outcometree.Osig_typext(ext, Oext_first) :: items ->
(* Gather together the extension constructors *)
let rec gather_extensions acc items =
match items with
Outcometree.Osig_typext(ext, Oext_next) :: items ->
gather_extensions
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
items
| _ -> (List.rev acc, items)
in
let exts, items =
gather_extensions
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
items
in
let te =
{ Outcometree.otyext_name = ext.oext_type_name;
otyext_params = ext.oext_type_params;
otyext_constructors = exts;
otyext_private = ext.oext_private }
in
let doc = printOutTypeExtensionDoc te in
loop items (doc::acc)
| item::items ->
let doc = printOutSigItemDoc item in
loop items (doc::acc)
in
match loop signature [] with
| [doc] -> doc
| docs ->
Doc.breakableGroup ~forceBreak:true (
Doc.join ~sep:Doc.line docs
)
and printOutExtensionConstructorDoc (outExt : Outcometree.out_extension_constructor) =
let typeParams = match outExt.oext_type_params with
| [] -> Doc.nil
| params ->
Doc.group(
Doc.concat [
Doc.lessThan;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (List.map
(fun ty -> Doc.text (if ty = "_" then ty else "'" ^ ty))
params
)
]
);
Doc.softLine;
Doc.greaterThan;
]
)
in
Doc.group (
Doc.concat [
Doc.text "type ";
printIdentLike ~allowUident:false outExt.oext_type_name;
typeParams;
Doc.text " += ";
Doc.line;
if outExt.oext_private = Asttypes.Private then
Doc.text "private "
else
Doc.nil;
printOutConstructorDoc
(outExt.oext_name, outExt.oext_args, outExt.oext_ret_type)
]
)
and printOutTypeExtensionDoc (typeExtension : Outcometree.out_type_extension) =
let typeParams = match typeExtension.otyext_params with
| [] -> Doc.nil
| params ->
Doc.group(
Doc.concat [
Doc.lessThan;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (List.map
(fun ty -> Doc.text (if ty = "_" then ty else "'" ^ ty))
params
)
]
);
Doc.softLine;
Doc.greaterThan;
]
)
in
Doc.group (
Doc.concat [
Doc.text "type ";
printIdentLike ~allowUident:false typeExtension.otyext_name;
typeParams;
Doc.text " += ";
if typeExtension.otyext_private = Asttypes.Private then
Doc.text "private "
else
Doc.nil;
printOutConstructorsDoc typeExtension.otyext_constructors;
]
)
let printOutSigItem fmt outSigItem =
Format.pp_print_string fmt
(Doc.toString ~width:80 (printOutSigItemDoc outSigItem))
let printOutSignature fmt signature =
Format.pp_print_string fmt
(Doc.toString ~width:80 (printOutSignatureDoc signature))
let validFloatLexeme s =
let l = String.length s in
let rec loop i =
if i >= l then s ^ "." else
match (s.[i] [@doesNotRaise]) with
| '0' .. '9' | '-' -> loop (i+1)
| _ -> s
in loop 0
let floatRepres f =
match classify_float f with
| FP_nan -> "nan"
| FP_infinite ->
if f < 0.0 then "neg_infinity" else "infinity"
| _ ->
let float_val =
let s1 = Printf.sprintf "%.12g" f in
if f = (float_of_string [@doesNotRaise]) s1 then s1 else
let s2 = Printf.sprintf "%.15g" f in
if f = (float_of_string [@doesNotRaise]) s2 then s2 else
Printf.sprintf "%.18g" f
in validFloatLexeme float_val
let rec printOutValueDoc (outValue : Outcometree.out_value) =
match outValue with
| Oval_array outValues ->
Doc.group (
Doc.concat [
Doc.lbracket;
Doc.indent (
Doc.concat [
Doc.softLine;
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
List.map printOutValueDoc outValues
)
]
);
Doc.trailingComma;
Doc.softLine;
Doc.rbracket;
]