Source file pprintast.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
open Asttypes
open Format
open Location
open Longident
open Parsetree
let prefix_symbols = [ '!'; '?'; '~' ]
let infix_symbols = [ '='; '<'; '>'; '@'; '^'; '|'; '&'; '+'; '-'; '*'; '/';
'$'; '%'; '#' ]
let special_infix_strings =
["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"; ":="; "!="; "::" ]
let letop s =
String.length s > 3
&& s.[0] = 'l'
&& s.[1] = 'e'
&& s.[2] = 't'
&& List.mem s.[3] infix_symbols
let andop s =
String.length s > 3
&& s.[0] = 'a'
&& s.[1] = 'n'
&& s.[2] = 'd'
&& List.mem s.[3] infix_symbols
let fixity_of_string = function
| "" -> `Normal
| s when List.mem s special_infix_strings -> `Infix s
| s when List.mem s.[0] infix_symbols -> `Infix s
| s when List.mem s.[0] prefix_symbols -> `Prefix s
| s when s.[0] = '.' -> `Mixfix s
| s when letop s -> `Letop s
| s when andop s -> `Andop s
| _ -> `Normal
let view_fixity_of_exp = function
| {pexp_desc = Pexp_ident {txt=Lident l;_}; pexp_attributes = []} ->
fixity_of_string l
| _ -> `Normal
let is_infix = function `Infix _ -> true | _ -> false
let is_mixfix = function `Mixfix _ -> true | _ -> false
let is_kwdop = function `Letop _ | `Andop _ -> true | _ -> false
let first_is c str =
str <> "" && str.[0] = c
let last_is c str =
str <> "" && str.[String.length str - 1] = c
let first_is_in cs str =
str <> "" && List.mem str.[0] cs
let needs_parens txt =
let fix = fixity_of_string txt in
is_infix fix
|| is_mixfix fix
|| is_kwdop fix
|| first_is_in prefix_symbols txt
let needs_spaces txt =
first_is '*' txt || last_is '*' txt
let tyvar_of_name s =
if String.length s >= 2 && s.[1] = '\'' then
"' " ^ s
else if Lexer.is_keyword s then
"'\\#" ^ s
else if String.equal s "_" then
s
else
"'" ^ s
module Doc_internal = struct
let ident_of_name ppf txt =
let format : (_, _, _) format =
if Lexer.is_keyword txt then "\\#%s"
else if not (needs_parens txt) then "%s"
else if needs_spaces txt then "(@;%s@;)"
else "(%s)"
in Format_doc.fprintf ppf format txt
let protect_longident ppf print_longident longprefix txt =
if not (needs_parens txt) then
Format_doc.fprintf ppf "%a.%a"
print_longident longprefix
ident_of_name txt
else if needs_spaces txt then
Format_doc.fprintf ppf "%a.(@;%s@;)" print_longident longprefix txt
else
Format_doc.fprintf ppf "%a.(%s)" print_longident longprefix txt
let rec longident f = function
| Lident s -> ident_of_name f s
| Ldot(y,s) -> protect_longident f longident y s
| Lapply (y,s) ->
Format_doc.fprintf f "%a(%a)" longident y longident s
let tyvar ppf s =
Format_doc.fprintf ppf "%s" (tyvar_of_name s)
end
let longident ppf l = Format_doc.compat Doc_internal.longident ppf l
let ident_of_name ppf i = Format_doc.compat Doc_internal.ident_of_name ppf i
let is_curry_attr attr =
attr.attr_name.txt = Builtin_attributes.curry_attr_name
let split_out_curry_attr attrs =
let curry, non_curry = List.partition is_curry_attr attrs in
let is_curry =
match curry with
| [] -> false
| _ :: _ -> true
in
is_curry, non_curry
type space_formatter = (unit, Format.formatter, unit) format
let override = function
| Override -> "!"
| Fresh -> ""
let type_variance = function
| NoVariance -> ""
| Covariant -> "+"
| Contravariant -> "-"
let type_injectivity = function
| NoInjectivity -> ""
| Injective -> "!"
type construct =
[ `cons of expression list
| `list of expression list
| `nil
| `normal
| `simple of Longident.t
| `tuple
| `btrue
| `bfalse ]
let view_expr x =
match x.pexp_desc with
| Pexp_construct ( {txt= Lident "()"; _},_) -> `tuple
| Pexp_construct ( {txt= Lident "true"; _},_) -> `btrue
| Pexp_construct ( {txt= Lident "false"; _},_) -> `bfalse
| Pexp_construct ( {txt= Lident "[]";_},_) -> `nil
| Pexp_construct ( {txt= Lident"::";_},Some _) ->
let rec loop exp acc = match exp with
| {pexp_desc=Pexp_construct ({txt=Lident "[]";_},_);
pexp_attributes = []} ->
(List.rev acc,true)
| {pexp_desc=
Pexp_construct ({txt=Lident "::";_},
Some ({pexp_desc= Pexp_tuple([None, e1;None, e2]);
pexp_attributes = []}));
pexp_attributes = []}
->
loop e2 (e1::acc)
| e -> (List.rev (e::acc),false) in
let (ls,b) = loop x [] in
if b then
`list ls
else `cons ls
| Pexp_construct (x,None) -> `simple (x.txt)
| _ -> `normal
let is_simple_construct :construct -> bool = function
| `nil | `tuple | `list _ | `simple _ | `btrue | `bfalse -> true
| `cons _ | `normal -> false
let pp = fprintf
type ctxt = {
pipe : bool;
semi : bool;
ifthenelse : bool;
functionrhs : bool;
}
let reset_ctxt = { pipe=false; semi=false; ifthenelse=false; functionrhs=false }
let under_pipe ctxt = { ctxt with pipe=true }
let under_semi ctxt = { ctxt with semi=true }
let under_ifthenelse ctxt = { ctxt with ifthenelse=true }
let under_functionrhs ctxt = { ctxt with functionrhs = true }
let list : 'a . ?sep:space_formatter -> ?first:space_formatter ->
?last:space_formatter -> (Format.formatter -> 'a -> unit) ->
Format.formatter -> 'a list -> unit
= fun ?sep ?first ?last fu f xs ->
let first = match first with Some x -> x |None -> ("": _ format6)
and last = match last with Some x -> x |None -> ("": _ format6)
and sep = match sep with Some x -> x |None -> ("@ ": _ format6) in
let aux f = function
| [] -> ()
| [x] -> fu f x
| xs ->
let rec loop f = function
| [x] -> fu f x
| x::xs -> fu f x; pp f sep; loop f xs;
| _ -> assert false in begin
pp f first; loop f xs; pp f last;
end in
aux f xs
let option : 'a. ?first:space_formatter -> ?last:space_formatter ->
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a option -> unit
= fun ?first ?last fu f a ->
let first = match first with Some x -> x | None -> ("": _ format6)
and last = match last with Some x -> x | None -> ("": _ format6) in
match a with
| None -> ()
| Some x -> pp f first; fu f x; pp f last
let paren: 'a . ?first:space_formatter -> ?last:space_formatter ->
bool -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a -> unit
= fun ?(first=("": _ format6)) ?(last=("": _ format6)) b fu f x ->
if b then (pp f "("; pp f first; fu f x; pp f last; pp f ")")
else fu f x
let longident_loc f x = pp f "%a" longident x.txt
let constant f = function
| Pconst_char i ->
pp f "%C" i
| Pconst_untagged_char i ->
pp f "#%C" i
| Pconst_string (i, _, None) ->
pp f "%S" i
| Pconst_string (i, _, Some delim) ->
pp f "{%s|%s|%s}" delim i delim
| Pconst_integer (i, None) ->
paren (first_is '-' i) (fun f -> pp f "%s") f i
| Pconst_integer (i, Some m) ->
paren (first_is '-' i) (fun f (i, m) -> pp f "%s%c" i m) f (i,m)
| Pconst_float (i, None) ->
paren (first_is '-' i) (fun f -> pp f "%s") f i
| Pconst_float (i, Some m) ->
paren (first_is '-' i) (fun f (i,m) -> pp f "%s%c" i m) f (i,m)
| Pconst_unboxed_float (x, None) ->
paren (first_is '-' x) (fun f -> pp f "%s") f
(Misc.format_as_unboxed_literal x)
| Pconst_unboxed_float (x, Some suffix)
| Pconst_unboxed_integer (x, suffix) ->
paren (first_is '-' x) (fun f (x, suffix) -> pp f "%s%c" x suffix) f
(Misc.format_as_unboxed_literal x, suffix)
let bool f = function
| false -> pp f "false"
| true -> pp f "true"
let mutable_flag f = function
| Immutable -> ()
| Mutable -> pp f "mutable@;"
let virtual_flag f = function
| Concrete -> ()
| Virtual -> pp f "virtual@;"
let rec_flag f rf =
match rf with
| Nonrecursive -> ()
| Recursive -> pp f "rec "
let nonrec_flag f rf =
match rf with
| Nonrecursive -> pp f "nonrec "
| Recursive -> ()
let direction_flag f = function
| Upto -> pp f "to@ "
| Downto -> pp f "downto@ "
let private_flag f = function
| Public -> ()
| Private -> pp f "private@ "
let iter_loc f ctxt {txt; loc = _} = f ctxt txt
let constant_string f s = pp f "%S" s
let tyvar ppf v = Format_doc.compat Doc_internal.tyvar ppf v
let string_loc ppf x = fprintf ppf "%s" x.txt
let string_quot f x = pp f "`%a" ident_of_name x
let mode f { txt = Mode s; _ } =
pp_print_string f s
let modes f m =
pp_print_list ~pp_sep:(fun f () -> pp f " ") mode f m
let optional_at_modes f m =
match m with
| [] -> ()
| m -> pp f " %@ %a" modes m
let modality f m =
let {txt = Modality txt; _} = m in
pp_print_string f txt
let modalities f m =
pp_print_list ~pp_sep:(fun f () -> pp f " ") modality f m
let optional_modalities ?(pre = fun _ () -> ()) ?(post = fun _ () -> ()) f m =
match m with
| [] -> ()
| m ->
pre f ();
pp f "%a" modalities m;
post f ()
let optional_space_atat_modalities f m =
let pre f () = Format.fprintf f "@ %@%@@ " in
optional_modalities ~pre f m
let optional_atat_modalities_newline f m =
let pre f () = Format.fprintf f "%@%@@ " in
optional_modalities ~pre ~post:pp_print_newline f m
let modalities_type pty ctxt f pca =
let m = pca.pca_modalities in
pp f "%a%a"
(pty ctxt) pca.pca_type
optional_space_atat_modalities m
let include_kind f = function
| Functor -> pp f "@ functor"
| Structure -> ()
type function_delimiter = Arrow | Equals
let rec class_params_def f = function
| [] -> ()
| l ->
pp f "[%a] "
(list type_param ~sep:",") l
and core_type1_with_optional_modes pty ctxt f (c, m) =
match m with
| [] -> pty ctxt f c
| _ :: _ ->
pp f "%a%a" (core_type1 ctxt) c optional_at_modes m
and type_with_label ctxt f (label, c, mode) =
match label with
| Nolabel ->
core_type1_with_optional_modes core_type1 ctxt f (c, mode)
| Labelled s ->
pp f "%a:%a" ident_of_name s
(core_type1_with_optional_modes core_type1 ctxt) (c, mode)
| Optional s ->
pp f "?%a:%a" ident_of_name s
(core_type1_with_optional_modes core_type1 ctxt) (c, mode)
and jkind_annotation ?(nested = false) ctxt f k = match k.pjka_desc with
| Pjk_default -> pp f "_"
| Pjk_abbreviation (s, sa) ->
longident_loc f s;
List.iter (fun a -> pp f " %s" a.Location.txt) sa
| Pjk_mod (t, modes) ->
begin match modes with
| [] -> Misc.fatal_error "malformed jkind annotation"
| _ :: _ ->
Misc.pp_parens_if nested (fun f (t, modes) ->
pp f "%a mod %a"
(jkind_annotation ~nested:true ctxt) t
(pp_print_list ~pp_sep:pp_print_space mode) modes
) f (t, modes)
end
| Pjk_with (t, ty, modalities) ->
Misc.pp_parens_if nested (fun f (t, ty, modalities) ->
pp f "%a with %a%a"
(jkind_annotation ~nested:true ctxt) t
(core_type ctxt) ty
optional_space_atat_modalities modalities;
) f (t, ty, modalities)
| Pjk_kind_of ty -> pp f "(kind_of_ %a)" (core_type ctxt) ty
| Pjk_product ts ->
Misc.pp_parens_if nested (fun f ts ->
pp f "@[%a@]" (list (jkind_annotation ~nested:true ctxt) ~sep:"@ & ") ts
) f ts
and tyvar_jkind tyvar f (str, jkind) =
match jkind with
| None -> tyvar f str
| Some lay -> pp f "(%a : %a)" tyvar str (jkind_annotation reset_ctxt) lay
and tyvar_loc_jkind tyvar f (str, jkind) = tyvar_jkind tyvar f (str.txt,jkind)
and tyvar_loc_repr tyvar f str = pp f "(repr_@ %a)" tyvar str.txt
and tyvar_loc_option_jkind f (str, jkind) =
match jkind with
| None -> tyvar_loc_option f str
| Some jkind ->
pp f "(%a : %a)"
tyvar_loc_option str
(jkind_annotation reset_ctxt) jkind
and name_jkind f (name, jkind) =
match jkind with
| None -> ident_of_name f name
| Some jkind ->
pp f "(%a : %a)"
ident_of_name name
(jkind_annotation reset_ctxt) jkind
and name_loc_jkind f (str, jkind) = name_jkind f (str.txt,jkind)
and core_type ctxt f x =
if x.ptyp_attributes <> [] then begin
pp f "((%a)%a)" (core_type ctxt) {x with ptyp_attributes=[]}
(attributes ctxt) x.ptyp_attributes
end
else match x.ptyp_desc with
| Ptyp_arrow (l, ct1, ct2, m1, m2) ->
pp f "@[<2>%a@;->@;%a@]"
(type_with_label ctxt) (l,ct1,m1) (return_type ctxt) (ct2,m2)
| Ptyp_alias (ct, s, j) ->
pp f "@[<2>%a@;as@;%a@]" (core_type1 ctxt) ct
tyvar_loc_option_jkind (s, j)
| Ptyp_poly ([], ct) | Ptyp_repr ([], ct) | Ptyp_newlayout ([], ct) ->
core_type ctxt f ct
| Ptyp_poly (sl, ct) ->
pp f "@[<2>%a%a@]"
(fun f l -> match l with
| [] -> ()
| _ ->
pp f "%a@;.@;"
(list
(tyvar_loc_jkind tyvar) ~sep:"@;")
l)
sl (core_type ctxt) ct
| Ptyp_repr (lv, ct) ->
pp f "@[<2>%a%a@]"
(fun f l -> match l with
| [] -> ()
| _ ->
pp f "%a@;.@;"
(list
(tyvar_loc_repr tyvar) ~sep:"@;")
l)
lv (core_type ctxt) ct
| Ptyp_newlayout (lv, ct) ->
pp f "@[<2>layout_@;%a.@;%a@]"
(list string_loc ~sep:"@;") lv
(core_type ctxt) ct
| Ptyp_of_kind jkind ->
pp f "@[(type@ :@ %a)@]" (jkind_annotation reset_ctxt) jkind
| _ -> pp f "@[<2>%a@]" (core_type1 ctxt) x
and core_type1 ctxt f x =
if x.ptyp_attributes <> [] then core_type ctxt f x
else
match x.ptyp_desc with
| Ptyp_any jkind -> tyvar_loc_option_jkind f (None, jkind)
| Ptyp_var (s, jkind) -> (tyvar_jkind tyvar) f (s, jkind)
| Ptyp_tuple tl ->
pp f "(%a)" (list (labeled_core_type1 ctxt) ~sep:"@;*@;") tl
| Ptyp_unboxed_tuple l ->
core_type1_labeled_tuple ctxt f ~unboxed:true l
| Ptyp_constr (li, l) ->
pp f "%a%a"
(fun f l -> match l with
|[] -> ()
|[x]-> pp f "%a@;" (core_type1 ctxt) x
| _ -> list ~first:"(" ~last:")@;" (core_type ctxt) ~sep:",@;" f l)
l longident_loc li
| Ptyp_variant (l, closed, low) ->
let first_is_inherit = match l with
| {Parsetree.prf_desc = Rinherit _}::_ -> true
| _ -> false in
let type_variant_helper f x =
match x.prf_desc with
| Rtag (l, _, ctl) ->
pp f "@[<2>%a%a@;%a@]" (iter_loc string_quot) l
(fun f l -> match l with
|[] -> ()
| _ -> pp f "@;of@;%a"
(list (core_type ctxt) ~sep:"&") ctl) ctl
(attributes ctxt) x.prf_attributes
| Rinherit ct -> core_type ctxt f ct in
pp f "@[<2>[%a%a]@]"
(fun f l ->
match l, closed with
| [], Closed -> ()
| [], Open -> pp f ">"
| _ ->
pp f "%s@;%a"
(match (closed,low) with
| (Closed,None) -> if first_is_inherit then " |" else ""
| (Closed,Some _) -> "<"
| (Open,_) -> ">")
(list type_variant_helper ~sep:"@;<1 -2>| ") l) l
(fun f low -> match low with
|Some [] |None -> ()
|Some xs ->
pp f ">@ %a"
(list string_quot) xs) low
| Ptyp_object (l, o) ->
let core_field_type f x = match x.pof_desc with
| Otag (l, ct) ->
pp f "@[<hov2>%a: %a@ %a@ @]" ident_of_name l.txt
(core_type ctxt) ct (attributes ctxt) x.pof_attributes
| Oinherit ct ->
pp f "@[<hov2>%a@ @]" (core_type ctxt) ct
in
let field_var f = function
| Asttypes.Closed -> ()
| Asttypes.Open ->
match l with
| [] -> pp f ".."
| _ -> pp f " ;.."
in
pp f "@[<hov2><@ %a%a@ > @]"
(list core_field_type ~sep:";") l
field_var o
| Ptyp_class (li, l) ->
pp f "@[<hov2>%a@;#%a@]"
(list (core_type ctxt) ~sep:"," ~first:"(" ~last:")") l
longident_loc li
| Ptyp_package (lid, cstrs) ->
let aux f (s, ct) =
pp f "type %a@ =@ %a" longident_loc s (core_type ctxt) ct in
(match cstrs with
|[] -> pp f "@[<hov2>(module@ %a)@]" longident_loc lid
|_ ->
pp f "@[<hov2>(module@ %a@ with@ %a)@]" longident_loc lid
(list aux ~sep:"@ and@ ") cstrs)
| Ptyp_open(li, ct) ->
pp f "@[<hov2>%a.(%a)@]" longident_loc li (core_type ctxt) ct
| Ptyp_quote t ->
pp f "@[<hov2><[%a]>@]" (core_type ctxt) t
| Ptyp_splice t ->
pp f "@[<hov2>$(%a)@]" (core_type ctxt) t
| Ptyp_extension e -> extension ctxt f e
| (Ptyp_arrow _ | Ptyp_alias _ | Ptyp_poly _ | Ptyp_repr _
| Ptyp_newlayout _ | Ptyp_of_kind _) ->
paren true (core_type ctxt) f x
and core_type2 ctxt f x =
if x.ptyp_attributes <> [] then core_type ctxt f x
else
match x.ptyp_desc with
| Ptyp_poly (sl, ct) ->
pp f "@[<2>%a%a@]"
(fun f l -> match l with
| [] -> ()
| _ ->
pp f "%a@;.@;"
(list
(tyvar_loc_jkind tyvar) ~sep:"@;")
l)
sl (core_type1 ctxt) ct
| _ -> core_type1 ctxt f x
and tyvar_option f = function
| None -> pp f "_"
| Some name -> tyvar f name
and tyvar_loc_option f str = tyvar_option f (Option.map Location.get_txt str)
and core_type1_labeled_tuple ctxt f ~unboxed tl =
pp f "%s(%a)" (if unboxed then "#" else "")
(list (labeled_core_type1 ctxt) ~sep:"@;*@;") tl
and labeled_core_type1 ctxt f (label, ty) =
begin match label with
| None -> ()
| Some s -> pp f "%s:" s
end;
core_type1 ctxt f ty
and return_type ctxt f (x, m) =
let is_curry, ptyp_attributes = split_out_curry_attr x.ptyp_attributes in
let x = {x with ptyp_attributes} in
if is_curry then core_type1_with_optional_modes core_type1 ctxt f (x, m)
else core_type1_with_optional_modes core_type ctxt f (x, m)
and core_type2_with_optional_modes ctxt f (ty, modes) =
match modes with
| [] -> core_type ctxt f ty
| _ :: _ -> pp f "%a%a" (core_type2 ctxt) ty optional_at_modes modes
and pattern ctxt f x =
if x.ppat_attributes <> [] then begin
pp f "((%a)%a)" (pattern ctxt) {x with ppat_attributes=[]}
(attributes ctxt) x.ppat_attributes
end
else match x.ppat_desc with
| Ppat_alias (p, s) ->
pp f "@[<2>%a@;as@;%a@]" (pattern ctxt) p ident_of_name s.txt
| _ -> pattern_or ctxt f x
and pattern_or ctxt f x =
let rec left_associative x acc = match x with
| {ppat_desc=Ppat_or (p1,p2); ppat_attributes = []} ->
left_associative p1 (p2 :: acc)
| x -> x :: acc
in
match left_associative x [] with
| [] -> assert false
| [x] -> pattern1 ctxt f x
| orpats ->
pp f "@[<hov0>%a@]" (list ~sep:"@ | " (pattern1 ctxt)) orpats
and pattern1 ctxt (f:Format.formatter) (x:pattern) : unit =
let rec pattern_list_helper f p = match p with
| {ppat_desc =
Ppat_construct
({ txt = Lident("::") ;_},
Some ([], inner_pat));
ppat_attributes = []} ->
begin match inner_pat.ppat_desc with
| Ppat_tuple([None, pat1; None, pat2], Closed) ->
pp f "%a::%a" (simple_pattern ctxt) pat1 pattern_list_helper pat2
| _ -> pattern1 ctxt f p
end
| _ -> pattern1 ctxt f p
in
if x.ppat_attributes <> [] then pattern ctxt f x
else match x.ppat_desc with
| Ppat_variant (l, Some p) ->
pp f "@[<2>`%a@;%a@]" ident_of_name l (simple_pattern ctxt) p
| Ppat_construct (({txt=Lident("()"|"[]"|"true"|"false");_}), _) ->
simple_pattern ctxt f x
| Ppat_construct (({txt;_} as li), po) ->
if txt = Lident "::" then
pp f "%a" pattern_list_helper x
else
(match po with
| Some ([], x) ->
pp f "%a@;%a" longident_loc li (simple_pattern ctxt) x
| Some (vl, x) ->
pp f "%a@ (type %a)@;%a" longident_loc li
(list ~sep:"@ " name_loc_jkind) vl
(simple_pattern ctxt) x
| None -> pp f "%a" longident_loc li)
| _ -> simple_pattern ctxt f x
and labeled_pattern1 ctxt (f:Format.formatter) (label, x) : unit =
let simple_name = match x with
| {ppat_desc = Ppat_var { txt=s; _ }; ppat_attributes = []; _} -> Some s
| _ -> None
in
match label, simple_name with
| None, _ ->
pattern1 ctxt f x
| Some lbl, Some simple_name when String.equal simple_name lbl ->
pp f "~%s" lbl
| Some lbl, _ ->
pp f "~%s:" lbl;
pattern1 ctxt f x
and simple_pattern ctxt (f:Format.formatter) (x:pattern) : unit =
if x.ppat_attributes <> [] then pattern ctxt f x
else match x.ppat_desc with
| Ppat_construct (({txt=Lident ("()"|"[]"|"true"|"false" as x);_}), None) ->
pp f "%s" x
| Ppat_any -> pp f "_";
| Ppat_var ({txt = txt;_}) -> ident_of_name f txt
| Ppat_array (mut, l) ->
let punct =
match mut with
| Mutable -> '|'
| Immutable -> ':'
in
pp f "@[<2>[%c%a%c]@]"
punct
(list (pattern1 ctxt) ~sep:";") l
punct
| Ppat_unpack { txt = None } ->
pp f "(module@ _)@ "
| Ppat_unpack { txt = Some s } ->
pp f "(module@ %s)@ " s
| Ppat_type li ->
pp f "#%a" longident_loc li
| Ppat_record (l, closed) ->
record_pattern ctxt f ~unboxed:false l closed
| Ppat_record_unboxed_product (l, closed) ->
record_pattern ctxt f ~unboxed:true l closed
| Ppat_unboxed_unit -> pp f "#()"
| Ppat_unboxed_bool b -> pp f "#%a" bool b
| Ppat_tuple (l, closed) ->
labeled_tuple_pattern ctxt f ~unboxed:false l closed
| Ppat_unboxed_tuple (l, closed) ->
labeled_tuple_pattern ctxt f ~unboxed:true l closed
| Ppat_constant (c) -> pp f "%a" constant c
| Ppat_interval (c1, c2) -> pp f "%a..%a" constant c1 constant c2
| Ppat_variant (l,None) -> pp f "`%a" ident_of_name l
| Ppat_constraint (p, ct, _) ->
pp f "@[<2>(%a@;:@;%a)@]" (pattern1 ctxt) p (core_type ctxt) (Option.get ct)
| Ppat_lazy p ->
pp f "@[<2>(lazy@;%a)@]" (simple_pattern ctxt) p
| Ppat_exception p ->
pp f "@[<2>exception@;%a@]" (pattern1 ctxt) p
| Ppat_extension e -> extension ctxt f e
| Ppat_open (lid, p) ->
let with_paren =
match p.ppat_desc with
| Ppat_array _ | Ppat_record _ | Ppat_record_unboxed_product _
| Ppat_construct (({txt=Lident ("()"|"[]"|"true"|"false");_}), None) ->
false
| _ -> true in
pp f "@[<2>%a.%a @]" longident_loc lid
(paren with_paren @@ pattern1 ctxt) p
| _ -> paren true (pattern ctxt) f x
and record_pattern ctxt f ~unboxed l closed =
let longident_x_pattern f (li, p) =
match (li,p) with
| ({txt=Lident s;_ },
{ppat_desc=Ppat_var {txt;_};
ppat_attributes=[]; _})
when s = txt ->
pp f "@[<2>%a@]" longident_loc li
| _ ->
pp f "@[<2>%a@;=@;%a@]" longident_loc li (pattern1 ctxt) p
in
let hash = if unboxed then "#" else "" in
match closed with
| Closed ->
pp f "@[<2>%s{@;%a@;}@]" hash (list longident_x_pattern ~sep:";@;") l
| Open ->
pp f "@[<2>%s{@;%a;_}@]" hash (list longident_x_pattern ~sep:";@;") l
and labeled_tuple_pattern ctxt f ~unboxed l closed =
let closed_flag ppf = function
| Closed -> ()
| Open -> pp ppf ",@;.."
in
pp f "@[<1>%s(%a%a)@]"
(if unboxed then "#" else "")
(list ~sep:",@;" (labeled_pattern1 ctxt)) l
closed_flag closed
(** for special treatment of modes in labeled expressions *)
and pattern2 ctxt f p =
match p.ppat_desc with
| Ppat_constraint(p, ct, m) ->
begin match ct with
| Some ct ->
pp f "@[<2>%a@;:@;%a@]"
(simple_pattern ctxt) p
(core_type2_with_optional_modes ctxt) (ct, m)
| None ->
pp f "@[<2>%a%a@]"
(simple_pattern ctxt) p
optional_at_modes m
end
| _ -> pattern1 ctxt f p
(** for special treatment of modes in labeled expressions *)
and simple_pattern1 ctxt f p =
match p.ppat_desc with
| Ppat_constraint _ ->
pp f "(%a)" (pattern2 ctxt) p
| _ -> simple_pattern ctxt f p
and label_exp ctxt f (l,opt,p) =
match l with
| Nolabel ->
pp f "%a" (simple_pattern1 ctxt) p
| Optional rest ->
begin match p with
| {ppat_desc = Ppat_var {txt;_}; ppat_attributes = []}
when txt = rest ->
(match opt with
| Some o ->
pp f "?(%a=@;%a)" ident_of_name rest (expression ctxt) o
| None -> pp f "?%a" ident_of_name rest)
| _ ->
(match opt with
| Some o ->
pp f "?%a:(%a=@;%a)@;"
ident_of_name rest (pattern2 ctxt) p (expression ctxt) o
| None -> pp f "?%a:%a@;" ident_of_name rest (simple_pattern1 ctxt) p)
end
| Labelled l -> match p with
| {ppat_desc = Ppat_var {txt;_}; ppat_attributes = []}
when txt = l ->
pp f "~%a" ident_of_name l
| _ -> pp f "~%a:%a" ident_of_name l (simple_pattern1 ctxt) p
and sugar_expr ctxt f e =
if e.pexp_attributes <> [] then false
else match e.pexp_desc with
| Pexp_apply ({ pexp_desc = Pexp_ident {txt = id; _};
pexp_attributes=[]; _}, args)
when List.for_all (fun (lab, _) -> lab = Nolabel) args -> begin
let print_indexop a path_prefix assign left sep right print_index indices
rem_args =
let print_path ppf = function
| None -> ()
| Some m -> pp ppf ".%a" longident m in
match assign, rem_args with
| false, [] ->
pp f "@[%a%a%s%a%s@]"
(simple_expr ctxt) a print_path path_prefix
left (list ~sep print_index) indices right; true
| true, [v] ->
pp f "@[%a%a%s%a%s@ <-@;<1 2>%a@]"
(simple_expr ctxt) a print_path path_prefix
left (list ~sep print_index) indices right
(simple_expr ctxt) v; true
| _ -> false in
match id, List.map snd args with
| Lident "!", [e] ->
pp f "@[<hov>!%a@]" (simple_expr ctxt) e; true
| Ldot (path, ("get"|"set" as func)), a :: other_args -> begin
let assign = func = "set" in
let print = print_indexop a None assign in
match path, other_args with
| Lident "Array", i :: rest ->
print ".(" "" ")" (expression ctxt) [i] rest
| Lident "String", i :: rest ->
print ".[" "" "]" (expression ctxt) [i] rest
| Ldot (Lident "Bigarray", "Array1"), i1 :: rest ->
print ".{" "," "}" (simple_expr ctxt) [i1] rest
| Ldot (Lident "Bigarray", "Array2"), i1 :: i2 :: rest ->
print ".{" "," "}" (simple_expr ctxt) [i1; i2] rest
| Ldot (Lident "Bigarray", "Array3"), i1 :: i2 :: i3 :: rest ->
print ".{" "," "}" (simple_expr ctxt) [i1; i2; i3] rest
| Ldot (Lident "Bigarray", "Genarray"),
{pexp_desc = Pexp_array (_, indexes); pexp_attributes = []} :: rest ->
print ".{" "," "}" (simple_expr ctxt) indexes rest
| _ -> false
end
| (Lident s | Ldot(_,s)) , a :: i :: rest
when first_is '.' s ->
let multi_indices = String.contains s ';' in
let i =
match i.pexp_desc with
| Pexp_array (_, l) when multi_indices -> l
| _ -> [ i ] in
let assign = last_is '-' s in
let kind =
let n = String.length s in
if assign then s.[n - 3] else s.[n - 1] in
let left, right = match kind with
| ')' -> '(', ")"
| ']' -> '[', "]"
| '}' -> '{', "}"
| _ -> assert false in
let path_prefix = match id with
| Ldot(m,_) -> Some m
| _ -> None in
let left = String.sub s 0 (1+String.index s left) in
print_indexop a path_prefix assign left ";" right
(if multi_indices then expression ctxt else simple_expr ctxt)
i rest
| _ -> false
end
| _ -> false
and expression ctxt f x =
if x.pexp_attributes <> [] then
pp f "((%a)@,%a)" (expression ctxt) {x with pexp_attributes=[]}
(attributes ctxt) x.pexp_attributes
else match x.pexp_desc with
| Pexp_function _ | Pexp_match _ | Pexp_try _ | Pexp_sequence _
| Pexp_newtype _
when ctxt.pipe || ctxt.semi ->
paren true (expression reset_ctxt) f x
| Pexp_ifthenelse _ | Pexp_sequence _ when ctxt.ifthenelse ->
paren true (expression reset_ctxt) f x
| Pexp_let _ | Pexp_letmodule _ | Pexp_open _
| Pexp_letexception _ | Pexp_letop _
when ctxt.semi ->
paren true (expression reset_ctxt) f x
| Pexp_newtype (lid, jkind, e) ->
pp f "@[<2>fun@;(type@;%a)@;%a@]"
name_jkind (lid.txt, jkind)
(pp_print_pexp_newtype ctxt "->") e
| Pexp_function (params, constraint_, body) ->
begin match params, constraint_ with
| [], {ret_type_constraint = None; ret_mode_annotations = []; _} ->
let should_paren =
match body with
| Pfunction_cases _ -> ctxt.functionrhs
| Pfunction_body _ -> false
in
let ctxt' = if should_paren then reset_ctxt else ctxt in
pp f "@[<2>%a@]" (paren should_paren (function_body ctxt')) body
| [], constraint_ ->
pp f "@[<2>(%a%a)@]"
(function_body ctxt) body
(function_constraint ~followed_by_arrow:false ctxt) constraint_
| _ :: _, _ ->
pp f "@[<2>fun@;%t@]"
(fun f ->
function_params_then_body
ctxt f params constraint_ body ~delimiter:Arrow)
end
| Pexp_match (e, l) ->
pp f "@[<hv0>@[<hv0>@[<2>match %a@]@ with@]%a@]"
(expression reset_ctxt) e (case_list ctxt) l
| Pexp_try (e, l) ->
pp f "@[<0>@[<hv2>try@ %a@]@ @[<0>with%a@]@]"
(expression reset_ctxt) e (case_list ctxt) l
| Pexp_let (mf, rf, l, e) ->
pp f "@[<2>%a in@;<1 -2>%a@]"
(bindings reset_ctxt) (mf,rf,l)
(expression ctxt) e
| Pexp_apply
({ pexp_desc = Pexp_extension({txt = "extension.exclave"}, PStr []) },
[Nolabel, sbody]) ->
pp f "@[<2>exclave_ %a@]" (expression ctxt) sbody
| Pexp_apply (e, l) ->
begin if not (sugar_expr ctxt f x) then
match view_fixity_of_exp e with
| `Infix s ->
begin match l with
| [ (Nolabel, _) as arg1; (Nolabel, _) as arg2 ] ->
pp f "@[<2>%a@;%s@;%a@]"
(label_x_expression_param reset_ctxt) arg1 s
(label_x_expression_param ctxt) arg2
| _ ->
pp f "@[<2>%a %a@]"
(simple_expr ctxt) e
(list (label_x_expression_param ctxt)) l
end
| `Prefix s ->
let s =
if List.mem s ["~+";"~-";"~+.";"~-."] &&
(match l with
|[(_,{pexp_desc=Pexp_constant _})] -> false
| _ -> true)
then String.sub s 1 (String.length s -1)
else s in
begin match l with
| [(Nolabel, x)] ->
pp f "@[<2>%s@;%a@]" s (simple_expr ctxt) x
| _ ->
pp f "@[<2>%a %a@]" (simple_expr ctxt) e
(list (label_x_expression_param ctxt)) l
end
| _ ->
pp f "@[<hov2>%a@]" begin fun f (e,l) ->
pp f "%a@ %a" (expression2 ctxt) e
(list (label_x_expression_param reset_ctxt)) l
end (e,l)
end
| Pexp_stack e ->
pp f "@[<hov2>stack_@ %a@]" (expression2 reset_ctxt) e
| Pexp_construct (li, Some eo)
when not (is_simple_construct (view_expr x))->
(match view_expr x with
| `cons ls -> list (simple_expr ctxt) f ls ~sep:"@;::@;"
| `normal ->
pp f "@[<2>%a@;%a@]" longident_loc li
(simple_expr ctxt) eo
| _ -> assert false)
| Pexp_setfield (e1, li, e2) ->
pp f "@[<2>%a.%a@ <-@ %a@]"
(simple_expr ctxt) e1 longident_loc li (simple_expr ctxt) e2
| Pexp_ifthenelse (e1, e2, eo) ->
let fmt:(_,_,_)format ="@[<hv0>@[<2>if@ %a@]@;@[<2>then@ %a@]%a@]" in
let expression_under_ifthenelse = expression (under_ifthenelse ctxt) in
pp f fmt expression_under_ifthenelse e1 expression_under_ifthenelse e2
(fun f eo -> match eo with
| Some x ->
pp f "@;@[<2>else@;%a@]" (expression (under_semi ctxt)) x
| None -> () ) eo
| Pexp_sequence _ ->
let rec sequence_helper acc = function
| {pexp_desc=Pexp_sequence(e1,e2); pexp_attributes = []} ->
sequence_helper (e1::acc) e2
| v -> List.rev (v::acc) in
let lst = sequence_helper [] x in
pp f "@[<hv>%a@]"
(list (expression (under_semi ctxt)) ~sep:";@;") lst
| Pexp_new (li) ->
pp f "@[<hov2>new@ %a@]" longident_loc li;
| Pexp_setvar (s, e) ->
pp f "@[<hov2>%a@ <-@ %a@]" ident_of_name s.txt (expression ctxt) e
| Pexp_override l ->
let string_x_expression f (s, e) =
pp f "@[<hov2>%a@ =@ %a@]" ident_of_name s.txt (expression ctxt) e in
pp f "@[<hov2>{<%a>}@]"
(list string_x_expression ~sep:";" ) l;
| Pexp_letmodule (s, me, e) ->
pp f "@[<hov2>let@ module@ %s@ =@ %a@ in@ %a@]"
(Option.value s.txt ~default:"_")
(module_expr reset_ctxt) me (expression ctxt) e
| Pexp_letexception (cd, e) ->
pp f "@[<hov2>let@ exception@ %a@ in@ %a@]"
(extension_constructor ctxt) cd
(expression ctxt) e
| Pexp_assert e ->
pp f "@[<hov2>assert@ %a@]" (simple_expr ctxt) e
| Pexp_lazy (e) ->
pp f "@[<hov2>lazy@ %a@]" (simple_expr ctxt) e
| Pexp_poly (e, None) ->
pp f "@[<hov2>!poly!@ %a@]" (simple_expr ctxt) e
| Pexp_poly (e, Some ct) ->
pp f "@[<hov2>(!poly!@ %a@ : %a)@]"
(simple_expr ctxt) e (core_type ctxt) ct
| Pexp_open (o, e) ->
pp f "@[<2>let open%s %a in@;%a@]"
(override o.popen_override) (module_expr ctxt) o.popen_expr
(expression ctxt) e
| Pexp_variant (l,Some eo) ->
pp f "@[<2>`%a@;%a@]" ident_of_name l (simple_expr ctxt) eo
| Pexp_letop {let_; ands; body} ->
pp f "@[<2>@[<v>%a@,%a@] in@;<1 -2>%a@]"
(binding_op ctxt) let_
(list ~sep:"@," (binding_op ctxt)) ands
(expression ctxt) body
| Pexp_extension e -> extension ctxt f e
| Pexp_unreachable -> pp f "."
| Pexp_overwrite (e1, e2) ->
pp f "@[<hov2>overwrite_@ %a@ with@ %a@]"
(expression2 reset_ctxt) e1
(expression2 reset_ctxt) e2
| Pexp_quote e ->
pp f "@[<hov2><[%a]>@]" (expression ctxt) e
| Pexp_splice e ->
pp f "@[$%a@]" (simple_expr ctxt) e
| Pexp_hole -> pp f "_"
| Pexp_borrow e ->
pp f "@[<hov2>borrow_@ %a@]" (expression2 reset_ctxt) e
| _ -> expression1 ctxt f x
and expression1 ctxt f x =
if x.pexp_attributes <> [] then expression ctxt f x
else match x.pexp_desc with
| Pexp_object cs -> pp f "%a" (class_structure ctxt) cs
| _ -> expression2 ctxt f x
and expression2 ctxt f x =
if x.pexp_attributes <> [] then expression ctxt f x
else match x.pexp_desc with
| Pexp_field (e, li) ->
pp f "@[<hov2>%a.%a@]" (simple_expr ctxt) e longident_loc li
| Pexp_unboxed_field (e, li) ->
pp f "@[<hov2>%a.#%a@]" (simple_expr ctxt) e longident_loc li
| Pexp_send (e, s) ->
pp f "@[<hov2>%a#%a@]" (simple_expr ctxt) e ident_of_name s.txt
| _ -> simple_expr ctxt f x
and simple_expr ctxt f x =
if x.pexp_attributes <> [] then expression ctxt f x
else match x.pexp_desc with
| Pexp_construct _ when is_simple_construct (view_expr x) ->
(match view_expr x with
| `nil -> pp f "[]"
| `tuple -> pp f "()"
| `btrue -> pp f "true"
| `bfalse -> pp f "false"
| `list xs ->
pp f "@[<hv0>[%a]@]"
(list (expression (under_semi ctxt)) ~sep:";@;") xs
| `simple x -> longident f x
| _ -> assert false)
| Pexp_ident li ->
longident_loc f li
| Pexp_constant c -> constant f c;
| Pexp_pack me ->
pp f "(module@;%a)" (module_expr ctxt) me
| Pexp_unboxed_unit -> pp f "#()"
| Pexp_unboxed_bool b -> pp f "#%a" bool b
| Pexp_tuple l ->
labeled_tuple_expr ctxt f ~unboxed:false l
| Pexp_unboxed_tuple l ->
labeled_tuple_expr ctxt f ~unboxed:true l
| Pexp_constraint (e, ct, m) ->
begin match ct with
| None ->
pp f "(%a : %@ %a)" (expression ctxt) e modes m
| Some ct ->
pp f "(%a : %a)"
(expression ctxt) e
(core_type2_with_optional_modes ctxt) (ct, m)
end
| Pexp_coerce (e, cto1, ct) ->
pp f "(%a%a :> %a)" (expression ctxt) e
(option (core_type ctxt) ~first:" : " ~last:" ") cto1
(core_type ctxt) ct
| Pexp_variant (l, None) -> pp f "`%a" ident_of_name l
| Pexp_record (l, eo) ->
record_expr ctxt f ~unboxed:false l eo
| Pexp_record_unboxed_product (l, eo) ->
record_expr ctxt f ~unboxed:true l eo
| Pexp_array (mut, l) ->
let punct = match mut with
| Immutable -> ':'
| Mutable -> '|'
in
pp f "@[<0>@[<2>[%c%a%c]@]@]"
punct
(list (simple_expr (under_semi ctxt)) ~sep:";") l
punct
| Pexp_idx (ba, uas) ->
pp f "(%a%a)" (block_access ctxt) ba (list unboxed_access ~sep:"") uas
| Pexp_comprehension comp -> comprehension_expr ctxt f comp
| Pexp_while (e1, e2) ->
let fmt : (_,_,_) format = "@[<2>while@;%a@;do@;%a@;done@]" in
pp f fmt (expression ctxt) e1 (expression ctxt) e2
| Pexp_for (s, e1, e2, df, e3) ->
let fmt:(_,_,_)format =
"@[<hv0>@[<hv2>@[<2>for %a =@;%a@;%a%a@;do@]@;%a@]@;done@]" in
let expression = expression ctxt in
pp f fmt (pattern ctxt) s expression e1 direction_flag
df expression e2 expression e3
| Pexp_hole -> pp f "_"
| _ -> paren true (expression ctxt) f x
and attributes ctxt f l =
List.iter (attribute ctxt f) l
and item_attributes ctxt f l =
List.iter (item_attribute ctxt f) l
and attribute ctxt f a =
pp f "@[<2>[@@%s@ %a]@]" a.attr_name.txt (payload ctxt) a.attr_payload
and item_attribute ctxt f a =
pp f "@[<2>[@@@@%s@ %a]@]" a.attr_name.txt (payload ctxt) a.attr_payload
and floating_attribute ctxt f a =
pp f "@[<2>[@@@@@@%s@ %a]@]" a.attr_name.txt (payload ctxt) a.attr_payload
and value_description ctxt f x =
pp f "@[<hov2>%a%a%a@]" (core_type ctxt) x.pval_type
optional_space_atat_modalities x.pval_modalities
(fun f x ->
if x.pval_prim <> []
then pp f "@ =@ %a" (list constant_string) x.pval_prim
) x
and extension ctxt f (s, e) =
pp f "@[<2>[%%%s@ %a]@]" s.txt (payload ctxt) e
and item_extension ctxt f (s, e) =
pp f "@[<2>[%%%%%s@ %a]@]" s.txt (payload ctxt) e
and exception_declaration ctxt f x =
pp f "@[<hov2>exception@ %a@]%a"
(extension_constructor ctxt) x.ptyexn_constructor
(item_attributes ctxt) x.ptyexn_attributes
and class_type_field ctxt f x =
match x.pctf_desc with
| Pctf_inherit (ct) ->
pp f "@[<2>inherit@ %a@]%a" (class_type ctxt) ct
(item_attributes ctxt) x.pctf_attributes
| Pctf_val (s, mf, vf, ct) ->
pp f "@[<2>val @ %a%a%a@ :@ %a@]%a"
mutable_flag mf virtual_flag vf
ident_of_name s.txt (core_type ctxt) ct
(item_attributes ctxt) x.pctf_attributes
| Pctf_method (s, pf, vf, ct) ->
pp f "@[<2>method %a %a%a :@;%a@]%a"
private_flag pf virtual_flag vf
ident_of_name s.txt (core_type ctxt) ct
(item_attributes ctxt) x.pctf_attributes
| Pctf_constraint (ct1, ct2) ->
pp f "@[<2>constraint@ %a@ =@ %a@]%a"
(core_type ctxt) ct1 (core_type ctxt) ct2
(item_attributes ctxt) x.pctf_attributes
| Pctf_attribute a -> floating_attribute ctxt f a
| Pctf_extension e ->
item_extension ctxt f e;
item_attributes ctxt f x.pctf_attributes
and class_signature ctxt f { pcsig_self = ct; pcsig_fields = l ;_} =
pp f "@[<hv0>@[<hv2>object@[<1>%a@]@ %a@]@ end@]"
(fun f -> function
{ptyp_desc=Ptyp_any None; ptyp_attributes=[]; _} -> ()
| ct -> pp f " (%a)" (core_type ctxt) ct) ct
(list (class_type_field ctxt) ~sep:"@;") l
and class_type ctxt f x =
match x.pcty_desc with
| Pcty_signature cs ->
class_signature ctxt f cs;
attributes ctxt f x.pcty_attributes
| Pcty_constr (li, l) ->
pp f "%a%a%a"
(fun f l -> match l with
| [] -> ()
| _ -> pp f "[%a]@ " (list (core_type ctxt) ~sep:"," ) l) l
longident_loc li
(attributes ctxt) x.pcty_attributes
| Pcty_arrow (l, co, cl) ->
pp f "@[<2>%a@;->@;%a@]"
(type_with_label ctxt) (l,co,[])
(class_type ctxt) cl
| Pcty_extension e ->
extension ctxt f e;
attributes ctxt f x.pcty_attributes
| Pcty_open (o, e) ->
pp f "@[<2>let open%s %a in@;%a@]"
(override o.popen_override) longident_loc o.popen_expr
(class_type ctxt) e
and class_type_declaration_list ctxt f l =
let class_type_declaration kwd f x =
let { pci_params=ls; pci_name={ txt; _ }; _ } = x in
pp f "@[<2>%s %a%a%a@ =@ %a@]%a" kwd
virtual_flag x.pci_virt
class_params_def ls
ident_of_name txt
(class_type ctxt) x.pci_expr
(item_attributes ctxt) x.pci_attributes
in
match l with
| [] -> ()
| [x] -> class_type_declaration "class type" f x
| x :: xs ->
pp f "@[<v>%a@,%a@]"
(class_type_declaration "class type") x
(list ~sep:"@," (class_type_declaration "and")) xs
and class_field ctxt f x =
match x.pcf_desc with
| Pcf_inherit (ovf, ce, so) ->
pp f "@[<2>inherit@ %s@ %a%a@]%a" (override ovf)
(class_expr ctxt) ce
(fun f so -> match so with
| None -> ();
| Some (s) -> pp f "@ as %a" ident_of_name s.txt ) so
(item_attributes ctxt) x.pcf_attributes
| Pcf_val (s, mf, Cfk_concrete (ovf, e)) ->
pp f "@[<2>val%s %a%a =@;%a@]%a" (override ovf)
mutable_flag mf
ident_of_name s.txt
(expression ctxt) e
(item_attributes ctxt) x.pcf_attributes
| Pcf_method (s, pf, Cfk_virtual ct) ->
pp f "@[<2>method virtual %a %a :@;%a@]%a"
private_flag pf
ident_of_name s.txt
(core_type ctxt) ct
(item_attributes ctxt) x.pcf_attributes
| Pcf_val (s, mf, Cfk_virtual ct) ->
pp f "@[<2>val virtual %a%a :@ %a@]%a"
mutable_flag mf
ident_of_name s.txt
(core_type ctxt) ct
(item_attributes ctxt) x.pcf_attributes
| Pcf_method (s, pf, Cfk_concrete (ovf, e)) ->
let bind e =
binding ctxt f
{pvb_pat=
{ppat_desc=Ppat_var s;
ppat_loc=Location.none;
ppat_loc_stack=[];
ppat_attributes=[]};
pvb_expr=e;
pvb_constraint=None;
pvb_is_poly=false;
pvb_attributes=[];
pvb_modes=[];
pvb_loc=Location.none;
}
in
pp f "@[<2>method%s %a%a@]%a"
(override ovf)
private_flag pf
(fun f -> function
| {pexp_desc=Pexp_poly (e, Some ct); pexp_attributes=[]; _} ->
pp f "%a :@;%a=@;%a"
ident_of_name s.txt (core_type ctxt) ct (expression ctxt) e
| {pexp_desc=Pexp_poly (e, None); pexp_attributes=[]; _} ->
bind e
| _ -> bind e) e
(item_attributes ctxt) x.pcf_attributes
| Pcf_constraint (ct1, ct2) ->
pp f "@[<2>constraint %a =@;%a@]%a"
(core_type ctxt) ct1
(core_type ctxt) ct2
(item_attributes ctxt) x.pcf_attributes
| Pcf_initializer (e) ->
pp f "@[<2>initializer@ %a@]%a"
(expression ctxt) e
(item_attributes ctxt) x.pcf_attributes
| Pcf_attribute a -> floating_attribute ctxt f a
| Pcf_extension e ->
item_extension ctxt f e;
item_attributes ctxt f x.pcf_attributes
and class_structure ctxt f { pcstr_self = p; pcstr_fields = l } =
pp f "@[<hv0>@[<hv2>object%a@;%a@]@;end@]"
(fun f p -> match p.ppat_desc with
| Ppat_any -> ()
| Ppat_constraint _ -> pp f " %a" (pattern ctxt) p
| _ -> pp f " (%a)" (pattern ctxt) p) p
(list (class_field ctxt)) l
and class_expr ctxt f x =
if x.pcl_attributes <> [] then begin
pp f "((%a)%a)" (class_expr ctxt) {x with pcl_attributes=[]}
(attributes ctxt) x.pcl_attributes
end else
match x.pcl_desc with
| Pcl_structure (cs) -> class_structure ctxt f cs
| Pcl_fun (l, eo, p, e) ->
pp f "fun@ %a@ ->@ %a"
(label_exp ctxt) (l,eo,p)
(class_expr ctxt) e
| Pcl_let (rf, l, ce) ->
pp f "%a@ in@ %a"
(bindings ctxt) (Immutable,rf,l)
(class_expr ctxt) ce
| Pcl_apply (ce, l) ->
pp f "((%a)@ %a)"
(class_expr ctxt) ce
(list (label_x_expression_param ctxt)) l
| Pcl_constr (li, l) ->
pp f "%a%a"
(fun f l-> if l <>[] then
pp f "[%a]@ "
(list (core_type ctxt) ~sep:",") l) l
longident_loc li
| Pcl_constraint (ce, ct) ->
pp f "(%a@ :@ %a)"
(class_expr ctxt) ce
(class_type ctxt) ct
| Pcl_extension e -> extension ctxt f e
| Pcl_open (o, e) ->
pp f "@[<2>let open%s %a in@;%a@]"
(override o.popen_override) longident_loc o.popen_expr
(class_expr ctxt) e
and include_ : 'a. ctxt -> formatter ->
contents:(ctxt -> formatter -> 'a -> unit) ->
'a include_infos ->
unit =
fun ctxt f ~contents incl ->
pp f "@[<hov2>include%a@ %a@]%a"
include_kind incl.pincl_kind
(contents ctxt) incl.pincl_mod
(item_attributes ctxt) incl.pincl_attributes
and sig_include ctxt f incl moda =
include_ ctxt f ~contents:module_type incl;
optional_space_atat_modalities f moda
and jkind_declaration ctxt f jd =
begin match jd.pjkind_manifest with
| None -> ()
| Some jkind -> pp f "@;%a" (jkind_annotation ctxt) jkind
end
and jkind_def ctxt f jd =
let eq = if jd.pjkind_manifest = None then "" else " =" in
pp f "@[<2>kind_@ %a%s%a@]%a"
string_loc jd.pjkind_name
eq
(jkind_declaration ctxt) jd
(item_attributes ctxt) jd.pjkind_attributes
and module_type_with_optional_modes ctxt f (mty, mm) =
match mm with
| [] -> module_type ctxt f mty
| _ :: _ -> pp f "%a%a" (module_type1 ctxt) mty optional_at_modes mm
and module_type1_with_optional_modes ctxt f (mty, mm) =
match mm with
| [] -> module_type1 ctxt f mty
| _ :: _ -> pp f "%a%a" (module_type1 ctxt) mty optional_at_modes mm
and module_type ctxt f x =
if x.pmty_attributes <> [] then begin
pp f "((%a)%a)" (module_type ctxt) {x with pmty_attributes=[]}
(attributes ctxt) x.pmty_attributes
end else
match x.pmty_desc with
| Pmty_functor (Unit, mt2, mm2) ->
pp f "@[<hov2>() ->@ %a@]" (module_type_with_optional_modes ctxt) (mt2, mm2)
| Pmty_functor (Named (s, mt1, mm1), mt2, mm2) ->
begin match s.txt with
| None ->
pp f "@[<hov2>%a@ ->@ %a@]"
(module_type1_with_optional_modes ctxt) (mt1, mm1)
(module_type_with_optional_modes ctxt) (mt2, mm2)
| Some name ->
pp f "@[<hov2>functor@ (%s@ :@ %a)@ ->@ %a@]" name
(module_type_with_optional_modes ctxt) (mt1, mm1)
(module_type_with_optional_modes ctxt) (mt2, mm2)
end
| Pmty_with (mt, []) -> module_type ctxt f mt
| Pmty_with (mt, l) ->
pp f "@[<hov2>%a@ with@ %a@]"
(module_type1 ctxt) mt
(list (with_constraint ctxt) ~sep:"@ and@ ") l
| Pmty_strengthen (mty, mod_id) ->
pp f "@[<hov2>%a@ with@ %a@]"
(module_type1 ctxt) mty
longident_loc mod_id
| _ -> module_type1 ctxt f x
and with_constraint ctxt f = function
| Pwith_type (li, ({ptype_params= ls ;_} as td)) ->
pp f "type@ %a %a =@ %a"
type_params ls
longident_loc li (type_declaration ctxt) td
| Pwith_module (li, li2) ->
pp f "module %a =@ %a" longident_loc li longident_loc li2;
| Pwith_modtype (li, mty) ->
pp f "module type %a =@ %a" longident_loc li (module_type ctxt) mty;
| Pwith_jkind (li, jd) ->
pp f "kind_ %a =@ %a" longident_loc li (jkind_declaration ctxt) jd;
| Pwith_typesubst (li, ({ptype_params=ls;_} as td)) ->
pp f "type@ %a %a :=@ %a"
type_params ls
longident_loc li
(type_declaration ctxt) td
| Pwith_modsubst (li, li2) ->
pp f "module %a :=@ %a" longident_loc li longident_loc li2
| Pwith_modtypesubst (li, mty) ->
pp f "module type %a :=@ %a" longident_loc li (module_type ctxt) mty;
| Pwith_jkindsubst (li, jd) ->
pp f "kind_ %a :=@ %a" longident_loc li (jkind_declaration ctxt) jd;
and module_type1 ctxt f x =
if x.pmty_attributes <> [] then module_type ctxt f x
else match x.pmty_desc with
| Pmty_ident li ->
pp f "%a" longident_loc li;
| Pmty_alias li ->
pp f "(module %a)" longident_loc li;
| Pmty_signature {psg_items; psg_modalities} ->
pp f "@[<hv0>@[<hv2>sig%a@ %a@]@ end@]"
optional_space_atat_modalities psg_modalities
(list (signature_item ctxt)) psg_items
| Pmty_typeof me ->
pp f "@[<hov2>module@ type@ of@ %a@]" (module_expr ctxt) me
| Pmty_extension e -> extension ctxt f e
| _ -> paren true (module_type ctxt) f x
and signature ctxt f {psg_items; psg_modalities} =
optional_atat_modalities_newline f psg_modalities;
signature_items ctxt f psg_items
and signature_items ctxt f items =
list ~sep:"@\n" (signature_item ctxt) f items
and signature_item ctxt f x : unit =
match x.psig_desc with
| Psig_type (rf, l) ->
type_def_list ctxt f (rf, true, l)
| Psig_typesubst l ->
type_def_list ctxt f (Recursive, false, l)
| Psig_value vd ->
let intro = if vd.pval_prim = [] then "val" else "external" in
if vd.pval_prim <> [] then assert (not vd.pval_poly);
let poly_str = if vd.pval_poly then "poly_ " else "" in
pp f "@[<2>%s@ %s%a@ :@ %a@]%a" intro poly_str
ident_of_name vd.pval_name.txt
(value_description ctxt) vd
(item_attributes ctxt) vd.pval_attributes
| Psig_typext te ->
type_extension ctxt f te
| Psig_exception ed ->
exception_declaration ctxt f ed
| Psig_class l ->
let class_description kwd f ({pci_params=ls;pci_name={txt;_};_} as x) =
pp f "@[<2>%s %a%a%a@;:@;%a@]%a" kwd
virtual_flag x.pci_virt
class_params_def ls
ident_of_name txt
(class_type ctxt) x.pci_expr
(item_attributes ctxt) x.pci_attributes
in begin
match l with
| [] -> ()
| [x] -> class_description "class" f x
| x :: xs ->
pp f "@[<v>%a@,%a@]"
(class_description "class") x
(list ~sep:"@," (class_description "and")) xs
end
| Psig_module ({pmd_type={pmty_desc=Pmty_alias alias;
pmty_attributes=[]; _}; _} as pmd) ->
pp f "@[<hov>module@ %s@ =@ %a%a@]%a"
(Option.value pmd.pmd_name.txt ~default:"_")
longident_loc alias
optional_space_atat_modalities pmd.pmd_modalities
(item_attributes ctxt) pmd.pmd_attributes
| Psig_module pmd ->
pp f "@[<hov>module@ %s@ :@ %a%a@]%a"
(Option.value pmd.pmd_name.txt ~default:"_")
(module_type ctxt) pmd.pmd_type
optional_space_atat_modalities pmd.pmd_modalities
(item_attributes ctxt) pmd.pmd_attributes
| Psig_modsubst pms ->
pp f "@[<hov>module@ %s@ :=@ %a@]%a" pms.pms_name.txt
longident_loc pms.pms_manifest
(item_attributes ctxt) pms.pms_attributes
| Psig_open od ->
pp f "@[<hov2>open%s@ %a@]%a"
(override od.popen_override)
longident_loc od.popen_expr
(item_attributes ctxt) od.popen_attributes
| Psig_include (incl, modalities) ->
sig_include ctxt f incl modalities
| Psig_modtype {pmtd_name=s; pmtd_type=md; pmtd_attributes=attrs} ->
pp f "@[<hov2>module@ type@ %s%a@]%a"
s.txt
(fun f md -> match md with
| None -> ()
| Some mt ->
pp_print_space f () ;
pp f "@ =@ %a" (module_type ctxt) mt
) md
(item_attributes ctxt) attrs
| Psig_modtypesubst {pmtd_name=s; pmtd_type=md; pmtd_attributes=attrs} ->
let md = match md with
| None -> assert false
| Some mt -> mt in
pp f "@[<hov2>module@ type@ %s@ :=@ %a@]%a"
s.txt (module_type ctxt) md
(item_attributes ctxt) attrs
| Psig_class_type (l) -> class_type_declaration_list ctxt f l
| Psig_recmodule decls ->
let rec string_x_module_type_list f ?(first=true) l =
match l with
| [] -> () ;
| pmd :: tl ->
if not first then
pp f "@ @[<hov2>and@ %s:@ %a%a@]%a"
(Option.value pmd.pmd_name.txt ~default:"_")
(module_type1 ctxt) pmd.pmd_type
optional_space_atat_modalities pmd.pmd_modalities
(item_attributes ctxt) pmd.pmd_attributes
else
pp f "@[<hov2>module@ rec@ %s:@ %a%a@]%a"
(Option.value pmd.pmd_name.txt ~default:"_")
(module_type1 ctxt) pmd.pmd_type
optional_space_atat_modalities pmd.pmd_modalities
(item_attributes ctxt) pmd.pmd_attributes;
string_x_module_type_list f ~first:false tl
in
string_x_module_type_list f decls
| Psig_attribute a -> floating_attribute ctxt f a
| Psig_extension(e, a) ->
item_extension ctxt f e;
item_attributes ctxt f a
| Psig_jkind kd ->
jkind_def ctxt f kd
and module_expr ctxt f x =
if x.pmod_attributes <> [] then
pp f "((%a)%a)" (module_expr ctxt) {x with pmod_attributes=[]}
(attributes ctxt) x.pmod_attributes
else match x.pmod_desc with
| Pmod_structure (s) ->
pp f "@[<hv2>struct@;@[<0>%a@]@;<1 -2>end@]"
(list (structure_item ctxt) ~sep:"@\n") s;
| Pmod_constraint (me, mt, mm) ->
begin match mt with
| None ->
pp f "@[<hov2>(%a%a)@]"
(module_expr ctxt) me
optional_at_modes mm
| Some mt ->
pp f "@[<hov2>(%a@ :@ %a)@]"
(module_expr ctxt) me
(module_type_with_optional_modes ctxt) (mt, mm)
end
| Pmod_ident (li) ->
pp f "%a" longident_loc li;
| Pmod_functor (Unit, me) ->
pp f "functor ()@;->@;%a" (module_expr ctxt) me
| Pmod_functor (Named (s, mt, mm), me) ->
pp f "functor@ (%s@ :@ %a)@;->@;%a"
(Option.value s.txt ~default:"_")
(module_type_with_optional_modes ctxt) (mt, mm) (module_expr ctxt) me
| Pmod_apply (me1, me2) ->
pp f "(%a)(%a)" (module_expr ctxt) me1 (module_expr ctxt) me2
| Pmod_apply_unit me1 ->
pp f "(%a)()" (module_expr ctxt) me1
| Pmod_unpack e ->
pp f "(val@ %a)" (expression ctxt) e
| Pmod_extension ({ txt; _ }, _) when txt = Ast_helper.hole_txt ->
pp f "_"
| Pmod_extension e -> extension ctxt f e
| Pmod_instance i ->
pp f "(%a [@jane.non_erasable.instances])"(instance ctxt) i
and structure ctxt f x = list ~sep:"@\n" (structure_item ctxt) f x
and payload ctxt f = function
| PStr [{pstr_desc = Pstr_eval (e, attrs)}] ->
pp f "@[<2>%a@]%a"
(expression ctxt) e
(item_attributes ctxt) attrs
| PStr x -> structure ctxt f x
| PTyp x -> pp f ":@ "; core_type ctxt f x
| PSig x -> pp f ":@ "; signature ctxt f x
| PPat (x, None) -> pp f "?"; pattern ctxt f x
| PPat (x, Some e) ->
pp f "?@ "; pattern ctxt f x;
pp f " when "; expression ctxt f e
and pp_print_pexp_newtype ctxt sep f x =
if x.pexp_attributes <> [] then pp f "%s@;%a" sep (expression ctxt) x
else
match x.pexp_desc with
| Pexp_newtype (str, jkind, e) ->
pp f "(type@ %a)@ %a" name_jkind (str.txt, jkind)
(pp_print_pexp_newtype ctxt sep) e
| _ ->
pp f "%s@;%a" sep (expression ctxt) x
and pp_print_params_then_equals ctxt f x =
if x.pexp_attributes <> [] then pp f "=@;%a" (expression ctxt) x
else
match x.pexp_desc with
| Pexp_function (params, constraint_, body) ->
function_params_then_body ctxt f params constraint_ body
~delimiter:Equals
| _ -> pp_print_pexp_newtype ctxt "=" f x
and poly_type ctxt core_type f (vars, typ) =
pp f "type@;%a.@;%a"
(list ~sep:"@;" (tyvar_loc_jkind pp_print_string)) vars
(core_type ctxt) typ
and poly_type_with_optional_modes ctxt f (vars, typ, modes) =
match modes with
| [] -> poly_type ctxt core_type f (vars, typ)
| _ :: _ -> pp f "%a%a" (poly_type ctxt core_type1) (vars, typ)
optional_at_modes modes
and binding ctxt f {pvb_pat=p; pvb_expr=x; pvb_constraint = ct; pvb_modes = modes; _} =
match ct with
| Some (Pvc_constraint { locally_abstract_univars = []; typ }) ->
pp f "%a@;:@;%a@;=@;%a"
(simple_pattern ctxt) p
(core_type2_with_optional_modes ctxt) (typ, modes)
(expression ctxt) x
| Some (Pvc_constraint { locally_abstract_univars = vars; typ }) ->
pp f "%a@;: %a@;=@;%a"
(simple_pattern ctxt) p
(poly_type_with_optional_modes ctxt)
(List.map (fun x -> (x, None)) vars, typ, modes)
(expression ctxt) x
| Some (Pvc_coercion {ground=None; coercion }) ->
pp f "%a@;:>@;%a@;=@;%a"
(simple_pattern ctxt) p
(core_type ctxt) coercion
(expression ctxt) x
| Some (Pvc_coercion {ground=Some ground; coercion }) ->
pp f "%a@;:%a@;:>@;%a@;=@;%a"
(simple_pattern ctxt) p
(core_type ctxt) ground
(core_type ctxt) coercion
(expression ctxt) x
| None ->
let tyvars_str tyvars = List.map (fun v -> v.txt) tyvars in
let tyvars_jkind_str tyvars = List.map (fun (v, _jkind) -> v.txt) tyvars in
let is_desugared_gadt p e =
let gadt_pattern =
match p with
| {ppat_desc=Ppat_constraint({ppat_desc=Ppat_var _} as pat,
Some {ptyp_desc=Ptyp_poly (args_tyvars, rt)}, _);
ppat_attributes=[]}->
Some (pat, args_tyvars, rt)
| _ -> None in
let rec gadt_exp tyvars e =
match e with
| {pexp_desc=Pexp_newtype (tyvar, _jkind, e); pexp_attributes=[]} ->
gadt_exp (tyvar :: tyvars) e
| {pexp_desc=Pexp_constraint (e, Some ct, _); pexp_attributes=[]} ->
Some (List.rev tyvars, e, ct)
| _ -> None in
let gadt_exp = gadt_exp [] e in
match gadt_pattern, gadt_exp with
| Some (p, pt_tyvars, pt_ct), Some (e_tyvars, e, e_ct)
when tyvars_jkind_str pt_tyvars = tyvars_str e_tyvars ->
let ety = Ast_helper.Typ.varify_constructors e_tyvars e_ct in
if ety = pt_ct then
Some (p, pt_tyvars, e_ct, e) else None
| _ -> None
in
begin match is_desugared_gadt p x with
| Some (p, (_ :: _ as tyvars), ct, e) ->
pp f "%a@;: %a@;=@;%a"
(simple_pattern ctxt) p
(poly_type_with_optional_modes ctxt)
(tyvars, ct, modes)
(expression ctxt) e
| _ ->
begin match p with
| {ppat_desc=Ppat_var _; ppat_attributes=[]} ->
begin match modes with
| [] ->
pp f "%a@ %a"
(simple_pattern ctxt) p
(pp_print_params_then_equals ctxt) x
| _ ->
pp f "(%a%a)@ %a"
(simple_pattern ctxt) p
optional_at_modes modes
(pp_print_params_then_equals ctxt) x
end
| _ ->
pp f "%a%a@;=@;%a"
(pattern ctxt) p
optional_at_modes modes
(expression ctxt) x
end
end
and bindings ctxt f (mf,rf,l) =
let binding kwd mf rf f x =
let poly_str = if x.pvb_is_poly then "poly_ " else "" in
pp f "@[<2>%s %a%s%a%a@]%a" kwd mutable_flag mf poly_str rec_flag rf
(binding ctxt) x
(item_attributes ctxt) x.pvb_attributes
in
match l with
| [] -> ()
| [x] -> binding "let" mf rf f x
| x::xs ->
pp f "@[<v>%a@,%a@]"
(binding "let" mf rf) x
(list ~sep:"@," (binding "and" Immutable Nonrecursive)) xs
and binding_op ctxt f x =
match x.pbop_pat, x.pbop_exp with
| {ppat_desc = Ppat_var { txt=pvar; _ }; ppat_attributes = []; _},
{pexp_desc = Pexp_ident { txt=Lident evar; _}; pexp_attributes = []; _}
when pvar = evar ->
pp f "@[<2>%s %s@]" x.pbop_op.txt evar
| pat, exp ->
pp f "@[<2>%s %a@;=@;%a@]"
x.pbop_op.txt (pattern ctxt) pat (expression ctxt) exp
and structure_item ctxt f x =
match x.pstr_desc with
| Pstr_eval (e, attrs) ->
pp f "@[<hov2>;;%a@]%a"
(expression ctxt) e
(item_attributes ctxt) attrs
| Pstr_type (_, []) -> assert false
| Pstr_type (rf, l) -> type_def_list ctxt f (rf, true, l)
| Pstr_value (rf, l) ->
pp f "@[<2>%a@]" (bindings ctxt) (Immutable,rf,l)
| Pstr_typext te -> type_extension ctxt f te
| Pstr_exception ed -> exception_declaration ctxt f ed
| Pstr_module x ->
let rec module_helper = function
| {pmod_desc=Pmod_functor(arg_opt,me'); pmod_attributes = []} ->
begin match arg_opt with
| Unit -> pp f "()"
| Named (s, mt, mm) ->
pp f "(%s:%a)" (Option.value s.txt ~default:"_")
(module_type_with_optional_modes ctxt) (mt, mm)
end;
module_helper me'
| me -> me
in
pp f "@[<hov2>module %s%a@]%a"
(Option.value x.pmb_name.txt ~default:"_")
(fun f me ->
let me = module_helper me in
match me with
| {pmod_desc=
Pmod_constraint
(me',
Some ({pmty_desc=(Pmty_ident (_)
| Pmty_signature (_));_} as mt), mm);
pmod_attributes = []} ->
pp f " :@;%a@;=@;%a@;"
(module_type_with_optional_modes ctxt) (mt, mm) (module_expr ctxt) me'
| _ -> pp f " =@ %a" (module_expr ctxt) me
) x.pmb_expr
(item_attributes ctxt) x.pmb_attributes
| Pstr_open od ->
pp f "@[<2>open%s@;%a@]%a"
(override od.popen_override)
(module_expr ctxt) od.popen_expr
(item_attributes ctxt) od.popen_attributes
| Pstr_modtype {pmtd_name=s; pmtd_type=md; pmtd_attributes=attrs} ->
pp f "@[<hov2>module@ type@ %s%a@]%a"
s.txt
(fun f md -> match md with
| None -> ()
| Some mt ->
pp_print_space f () ;
pp f "@ =@ %a" (module_type ctxt) mt
) md
(item_attributes ctxt) attrs
| Pstr_class l ->
let cl =
let rec loop acc = function
| {pcl_desc=Pcl_fun (l, eo, p, cl'); pcl_attributes = []} ->
loop ((l,eo,p) :: acc) cl'
| cl -> List.rev acc, cl
in
let args, cl = loop [] cl in
let constr, cl =
match cl with
| {pcl_desc=Pcl_constraint (cl', ct); pcl_attributes = []} ->
Some ct, cl'
| _ -> None, cl
in
args, constr, cl
in
let class_constraint f ct = pp f ": @[%a@] " (class_type ctxt) ct in
let class_declaration kwd f
({pci_params=ls; pci_name={txt;_}; _} as x) =
let args, constr, cl = extract_class_args x.pci_expr in
pp f "@[<2>%s %a%a%a %a%a=@;%a@]%a" kwd
virtual_flag x.pci_virt
class_params_def ls
ident_of_name txt
(list (label_exp ctxt) ~last:"@ ") args
(option class_constraint) constr
(class_expr ctxt) cl
(item_attributes ctxt) x.pci_attributes
in begin
match l with
| [] -> ()
| [x] -> class_declaration "class" f x
| x :: xs ->
pp f "@[<v>%a@,%a@]"
(class_declaration "class") x
(list ~sep:"@," (class_declaration "and")) xs
end
| Pstr_class_type l -> class_type_declaration_list ctxt f l
| Pstr_primitive vd ->
assert (not vd.pval_poly);
pp f "@[<hov2>external@ %a@ :@ %a@]%a"
ident_of_name vd.pval_name.txt
(value_description ctxt) vd
(item_attributes ctxt) vd.pval_attributes
| Pstr_include incl ->
include_ ctxt f ~contents:module_expr incl
| Pstr_recmodule decls ->
let aux f = function
| ({pmb_expr={pmod_desc=Pmod_constraint (expr, Some typ, mm)}} as pmb) ->
pp f "@[<hov2>@ and@ %s:%a@ =@ %a@]%a"
(Option.value pmb.pmb_name.txt ~default:"_")
(module_type_with_optional_modes ctxt) (typ, mm)
(module_expr ctxt) expr
(item_attributes ctxt) pmb.pmb_attributes
| ({pmb_expr={pmod_desc=Pmod_constraint (expr, None, mm)}} as pmb) ->
pp f "@[<hov2>@ and@ %s%a@ =@ %a@]%a"
(Option.value pmb.pmb_name.txt ~default:"_")
optional_at_modes mm
(module_expr ctxt) expr
(item_attributes ctxt) pmb.pmb_attributes
| pmb ->
pp f "@[<hov2>@ and@ %s@ =@ %a@]%a"
(Option.value pmb.pmb_name.txt ~default:"_")
(module_expr ctxt) pmb.pmb_expr
(item_attributes ctxt) pmb.pmb_attributes
in
begin match decls with
| ({pmb_expr={pmod_desc=Pmod_constraint (expr, Some typ, mm)}} as pmb) :: l2 ->
pp f "@[<hv>@[<hov2>module@ rec@ %s:%a@ =@ %a@]%a@ %a@]"
(Option.value pmb.pmb_name.txt ~default:"_")
(module_type_with_optional_modes ctxt) (typ, mm)
(module_expr ctxt) expr
(item_attributes ctxt) pmb.pmb_attributes
(fun f l2 -> List.iter (aux f) l2) l2
| ({pmb_expr={pmod_desc=Pmod_constraint (expr, None, mm)}} as pmb) :: l2 ->
pp f "@[<hv>@[<hov2>module@ rec@ %s%a@ =@ %a@]%a@ %a@]"
(Option.value pmb.pmb_name.txt ~default:"_")
optional_at_modes mm
(module_expr ctxt) expr
(item_attributes ctxt) pmb.pmb_attributes
(fun f l2 -> List.iter (aux f) l2) l2
| pmb :: l2 ->
pp f "@[<hv>@[<hov2>module@ rec@ %s@ =@ %a@]%a@ %a@]"
(Option.value pmb.pmb_name.txt ~default:"_")
(module_expr ctxt) pmb.pmb_expr
(item_attributes ctxt) pmb.pmb_attributes
(fun f l2 -> List.iter (aux f) l2) l2
| _ -> assert false
end
| Pstr_attribute a -> floating_attribute ctxt f a
| Pstr_extension(e, a) ->
item_extension ctxt f e;
item_attributes ctxt f a
| Pstr_jkind jd ->
jkind_def ctxt f jd
and core_type_param f ct =
let attrs = attributes reset_ctxt in
match ct.ptyp_desc with
| Ptyp_any None -> pp f "_%a" attrs ct.ptyp_attributes
| Ptyp_any (Some jk) ->
pp f "_%a : %a" attrs ct.ptyp_attributes (jkind_annotation reset_ctxt) jk
| Ptyp_var (s, None) ->
pp f "%a%a" tyvar s attrs ct.ptyp_attributes
| Ptyp_var (s, Some jk) ->
pp f "%a%a : %a" tyvar s attrs ct.ptyp_attributes
(jkind_annotation reset_ctxt) jk
| _ -> Misc.fatal_error "unexpected type in core_type_param"
and type_param f (ct, (a,b)) =
pp f "%s%s%a" (type_variance a) (type_injectivity b) core_type_param ct
and type_params f = function
| [] -> ()
| [{ ptyp_desc = Ptyp_any (Some _) | Ptyp_var (_, Some _) }, _ as param] ->
pp f "(%a) " type_param param
| l -> pp f "%a " (list type_param ~first:"(" ~last:")" ~sep:",@;") l
and type_def_list ctxt f (rf, exported, l) =
let type_decl kwd rf f x =
let eq =
if (x.ptype_kind = Ptype_abstract)
&& (x.ptype_manifest = None) then ""
else if exported then " ="
else " :="
in
let layout_annot =
match x.ptype_jkind_annotation with
| None -> Format.dprintf ""
| Some jkind ->
Format.dprintf " : %a" (jkind_annotation ctxt) jkind
in
pp f "@[<2>%s %a%a%a%t%s%a@]%a" kwd
nonrec_flag rf
type_params x.ptype_params
ident_of_name x.ptype_name.txt
layout_annot eq
(type_declaration ctxt) x
(item_attributes ctxt) x.ptype_attributes
in
match l with
| [] -> assert false
| [x] -> type_decl "type" rf f x
| x :: xs -> pp f "@[<v>%a@,%a@]"
(type_decl "type" rf) x
(list ~sep:"@," (type_decl "and" Recursive)) xs
and record_declaration ctxt f ~unboxed lbls =
let type_record_field f pld =
pp f "@[<2>%a%a:@;%a%a@;%a@]"
mutable_flag pld.pld_mutable
ident_of_name pld.pld_name.txt
(core_type ctxt) pld.pld_type
optional_space_atat_modalities pld.pld_modalities
(attributes ctxt) pld.pld_attributes
in
let hash = if unboxed then "#" else "" in
pp f "%s{@\n%a}"
hash (list type_record_field ~sep:";@\n" ) lbls
and type_declaration ctxt f x =
let priv f =
match x.ptype_private with
| Public -> ()
| Private -> pp f "@;private"
in
let manifest f =
match x.ptype_manifest with
| None -> ()
| Some y ->
if x.ptype_kind = Ptype_abstract then
pp f "%t@;%a" priv (core_type ctxt) y
else
pp f "@;%a" (core_type ctxt) y
in
let constructor_declaration f pcd =
pp f "|@;";
constructor_declaration ctxt f
(pcd.pcd_name.txt, pcd.pcd_vars, pcd.pcd_args, pcd.pcd_res,
pcd.pcd_attributes)
in
let repr f =
let intro f =
if x.ptype_manifest = None then ()
else pp f "@;="
in
match x.ptype_kind with
| Ptype_variant xs ->
let variants fmt xs =
if xs = [] then pp fmt " |" else
pp fmt "@\n%a" (list ~sep:"@\n" constructor_declaration) xs
in pp f "%t%t%a" intro priv variants xs
| Ptype_abstract -> ()
| Ptype_record l ->
pp f "%t%t@;%a" intro priv (record_declaration ctxt ~unboxed:false) l
| Ptype_record_unboxed_product l ->
pp f "%t%t@;%a" intro priv (record_declaration ctxt ~unboxed:true) l
| Ptype_open -> pp f "%t%t@;.." intro priv
in
let constraints f =
List.iter
(fun (ct1,ct2,_) ->
pp f "@[<hov2>@ constraint@ %a@ =@ %a@]"
(core_type ctxt) ct1 (core_type ctxt) ct2)
x.ptype_cstrs
in
pp f "%t%t%t" manifest repr constraints
and type_extension ctxt f x =
let extension_constructor f x =
pp f "@\n|@;%a" (extension_constructor ctxt) x
in
pp f "@[<2>type %a%a += %a@ %a@]%a"
type_params x.ptyext_params
longident_loc x.ptyext_path
private_flag x.ptyext_private
(list ~sep:"" extension_constructor)
x.ptyext_constructors
(item_attributes ctxt) x.ptyext_attributes
and constructor_declaration ctxt f (name, vars_jkinds, args, res, attrs) =
let name =
match name with
| "::" -> "(::)"
| s -> s in
let pp_vars f vls =
match vls with
| [] -> ()
| _ -> pp f "%a@;.@;" (list (tyvar_loc_jkind tyvar) ~sep:"@;")
vls
in
match res with
| None ->
pp f "%s%a@;%a" name
(fun f -> function
| Pcstr_tuple [] -> ()
| Pcstr_tuple l ->
pp f "@;of@;%a" (list (modalities_type core_type1 ctxt) ~sep:"@;*@;") l
| Pcstr_record l ->
pp f "@;of@;%a" (record_declaration ctxt ~unboxed:false) l
) args
(attributes ctxt) attrs
| Some r ->
pp f "%s:@;%a%a@;%a" name
pp_vars vars_jkinds
(fun f -> function
| Pcstr_tuple [] -> core_type1 ctxt f r
| Pcstr_tuple l -> pp f "%a@;->@;%a"
(list (modalities_type core_type1 ctxt) ~sep:"@;*@;") l
(core_type1 ctxt) r
| Pcstr_record l ->
pp f "%a@;->@;%a" (record_declaration ctxt ~unboxed:false) l
(core_type1 ctxt) r
)
args
(attributes ctxt) attrs
and extension_constructor ctxt f x =
match x.pext_kind with
| Pext_decl(v, l, r) ->
constructor_declaration ctxt f
(x.pext_name.txt, v, l, r, x.pext_attributes)
| Pext_rebind li ->
pp f "%s@;=@;%a%a" x.pext_name.txt
longident_loc li
(attributes ctxt) x.pext_attributes
and case_list ctxt f l : unit =
let aux f {pc_lhs; pc_guard; pc_rhs} =
pp f "@;| @[<2>%a%a@;->@;%a@]"
(pattern ctxt) pc_lhs (option (expression ctxt) ~first:"@;when@;")
pc_guard (expression (under_pipe ctxt)) pc_rhs
in
list aux f l ~sep:""
and label_x_expression_param ctxt f (l,e) =
let simple_name = match e with
| {pexp_desc=Pexp_ident {txt=Lident l;_};
pexp_attributes=[]} -> Some l
| _ -> None
in match l with
| Nolabel -> expression2 ctxt f e
| Optional str ->
if Some str = simple_name then
pp f "?%a" ident_of_name str
else
pp f "?%a:%a" ident_of_name str (simple_expr ctxt) e
| Labelled lbl ->
if Some lbl = simple_name then
pp f "~%a" ident_of_name lbl
else
pp f "~%a:%a" ident_of_name lbl (simple_expr ctxt) e
and tuple_component ctxt f (l,e) =
let simple_name = match e with
| {pexp_desc=Pexp_ident {txt=Lident l;_};
pexp_attributes=[]} -> Some l
| _ -> None
in match (simple_name, l) with
| Some simple_name, Some lbl when String.equal simple_name lbl -> pp f "~%s" lbl
| _, Some lbl -> pp f "~%s:%a" lbl (simple_expr ctxt) e
| _, None -> expression2 ctxt f e
and directive_argument f x =
match x.pdira_desc with
| Pdir_string (s) -> pp f "@ %S" s
| Pdir_int (n, None) -> pp f "@ %s" n
| Pdir_int (n, Some m) -> pp f "@ %s%c" n m
| Pdir_ident (li) -> pp f "@ %a" longident li
| Pdir_bool (b) -> pp f "@ %s" (string_of_bool b)
and block_access ctxt f = function
| Baccess_field li ->
pp f ".%a" longident_loc li
| Baccess_block (mut, index) ->
let s =
match mut with
| Mutable -> "idx_mut"
| Immutable -> "idx_imm"
in
pp f ".%s(%a)" s (expression ctxt) index
and unboxed_access f = function
| Uaccess_unboxed_field li ->
pp f ".#%a" longident_loc li
and comprehension_expr ctxt f cexp =
let punct, comp = match cexp with
| Pcomp_list_comprehension comp ->
"", comp
| Pcomp_array_comprehension (amut, comp) ->
let punct = match amut with
| Mutable -> "|"
| Immutable -> ":"
in
punct, comp
in
comprehension ctxt f ~open_:("[" ^ punct) ~close:(punct ^ "]") comp
and comprehension ctxt f ~open_ ~close cexp =
let { pcomp_body = body; pcomp_clauses = clauses } = cexp in
pp f "@[<hv0>@[<hv2>%s%a@ @[<hv2>%a@]%s@]@]"
open_
(expression ctxt) body
(list ~sep:"@ " (comprehension_clause ctxt)) clauses
close
and comprehension_clause ctxt f x =
match x with
| Pcomp_for bindings ->
pp f "@[for %a@]" (list ~sep:"@]@ @[and " (comprehension_binding ctxt)) bindings
| Pcomp_when cond ->
pp f "@[when %a@]" (expression ctxt) cond
and comprehension_binding ctxt f x =
let { pcomp_cb_pattern = pat;
pcomp_cb_iterator = iterator;
pcomp_cb_attributes = attrs } = x in
pp f "%a%a %a"
(attributes ctxt) attrs
(pattern ctxt) pat
(comprehension_iterator ctxt) iterator
and comprehension_iterator ctxt f x =
match x with
| Pcomp_range { start; stop; direction } ->
pp f "=@ %a %a%a"
(expression ctxt) start
direction_flag direction
(expression ctxt) stop
| Pcomp_in seq ->
pp f "in %a" (expression ctxt) seq
and function_param ctxt f { pparam_desc; pparam_loc = _ } =
match pparam_desc with
| Pparam_val (a, b, c) -> label_exp ctxt f (a, b, c)
| Pparam_newtype (ty, None) -> pp f "(type %a)" ident_of_name ty.txt
| Pparam_newtype (ty, Some annot) ->
pp f "(type %a : %a)" ident_of_name ty.txt (jkind_annotation ctxt) annot
and function_body ctxt f x =
match x with
| Pfunction_body body -> expression ctxt f body
| Pfunction_cases (cases, _, attrs) ->
pp f "@[<hv>function%a%a@]"
(item_attributes ctxt) attrs
(case_list ctxt) cases
and function_constraint ~followed_by_arrow ctxt f x =
match[@ocaml.warning "+9"] x with
| { ret_type_constraint = Some (Pconstraint ty); ret_mode_annotations; _ } ->
let print_ty =
if followed_by_arrow
then core_type1_with_optional_modes core_type1 ctxt
else core_type2_with_optional_modes ctxt
in
pp f "@;:@;%a@;" print_ty (ty, ret_mode_annotations)
| { ret_type_constraint = Some (Pcoerce (ty1, ty2)); _ } ->
pp f "@;%a:>@;%a"
(option ~first:":@;" (core_type ctxt)) ty1
(core_type ctxt) ty2
| { ret_type_constraint = None; ret_mode_annotations; _} ->
pp f "%a" optional_at_modes ret_mode_annotations
and function_params_then_body ctxt f params constraint_ body ~delimiter =
let followed_by_arrow, delimiter =
match delimiter with
| Arrow -> true, "->"
| Equals -> false, "="
in
let pp_params f =
match params with
| [] -> ()
| _ :: _ -> pp f "%a@;" (list (function_param ctxt) ~sep:"@ ") params
in
pp f "%t%a%s@;%a"
pp_params
(function_constraint ~followed_by_arrow ctxt) constraint_
delimiter
(function_body (under_functionrhs ctxt)) body
and labeled_tuple_expr ctxt f ~unboxed x =
pp f "@[<hov2>%s(%a)@]" (if unboxed then "#" else "")
(list (tuple_component ctxt) ~sep:",@;") x
and record_expr ctxt f ~unboxed l eo =
let longident_x_expression f ( li, e) =
match e with
| {pexp_desc=Pexp_ident {txt;_};
pexp_attributes=[]; _} when li.txt = txt ->
pp f "@[<hov2>%a@]" longident_loc li
| _ ->
pp f "@[<hov2>%a@;=@;%a@]" longident_loc li (simple_expr ctxt) e
in
let hash = if unboxed then "#" else "" in
pp f "@[<hv0>@[<hv2>%s{@;%a%a@]@;}@]"
hash (option ~last:" with@;" (simple_expr ctxt)) eo
(list longident_x_expression ~sep:";@;") l
and instance ctxt f x =
match x with
| { pmod_instance_head = head; pmod_instance_args = [] } -> pp f "%s" head
| { pmod_instance_head = head; pmod_instance_args = args } ->
pp f "@[<2>%s %a@]" head (list (instance_arg ctxt)) args
and instance_arg ctxt f (param, value) =
pp f "@[<1>(%s)@;(%a)@]" param (instance ctxt) value
let Language_extension.For_pprintast.{ print_with_maximal_extensions } =
Language_extension.For_pprintast.make_printer_exporter ()
let print_reset_with_maximal_extensions f =
print_with_maximal_extensions (f reset_ctxt)
let toplevel_phrase f x =
match x with
| Ptop_def (s) ->pp f "@[<hov0>%a@]" (list (structure_item reset_ctxt)) s
| Ptop_dir {pdir_name; pdir_arg = None; _} ->
pp f "@[<hov2>#%s@]" pdir_name.txt
| Ptop_dir {pdir_name; pdir_arg = Some pdir_arg; _} ->
pp f "@[<hov2>#%s@ %a@]" pdir_name.txt directive_argument pdir_arg
let toplevel_phrase = print_with_maximal_extensions toplevel_phrase
let expression f x =
pp f "@[%a@]" (expression reset_ctxt) x
let expression = print_with_maximal_extensions expression
let string_of_expression x =
ignore (flush_str_formatter ()) ;
let f = str_formatter in
expression f x;
flush_str_formatter ()
let structure = print_reset_with_maximal_extensions structure
let string_of_structure x =
ignore (flush_str_formatter ());
let f = str_formatter in
structure f x;
flush_str_formatter ()
let top_phrase f x =
pp_print_newline f ();
toplevel_phrase f x;
pp f ";;";
pp_print_newline f ()
let longident = print_with_maximal_extensions longident
let core_type = print_reset_with_maximal_extensions core_type
let pattern = print_reset_with_maximal_extensions pattern
let signature = print_reset_with_maximal_extensions signature
let module_expr = print_reset_with_maximal_extensions module_expr
let module_type = print_reset_with_maximal_extensions module_type
let class_field = print_reset_with_maximal_extensions class_field
let class_type_field = print_reset_with_maximal_extensions class_type_field
let class_expr = print_reset_with_maximal_extensions class_expr
let class_type = print_reset_with_maximal_extensions class_type
let class_signature = print_reset_with_maximal_extensions class_signature
let structure_item = print_reset_with_maximal_extensions structure_item
let signature_item = print_reset_with_maximal_extensions signature_item
let signature_items = print_reset_with_maximal_extensions signature_items
let binding = print_reset_with_maximal_extensions binding
let payload = print_reset_with_maximal_extensions payload
let type_declaration = print_reset_with_maximal_extensions type_declaration
let jkind_annotation = print_reset_with_maximal_extensions jkind_annotation
module Doc = struct
include Doc_internal
let jkind_annotation ppf jkind =
Format_doc.deprecated_printer (fun fmt -> jkind_annotation fmt jkind) ppf
end
let case_list = print_reset_with_maximal_extensions case_list
module Style = Misc.Style
let prepare_error err =
let source = Location.Parser in
let open Syntaxerr in
match err with
| Unclosed(opening_loc, opening, closing_loc, closing) ->
Location.errorf ~source
~loc:closing_loc
~sub:[
Location.msg ~loc:opening_loc
"This %a might be unmatched" Style.inline_code opening
]
"Syntax error: %a expected" Style.inline_code closing
| Expecting (loc, nonterm) ->
Location.errorf ~source ~loc "Syntax error: %a expected."
Style.inline_code nonterm
| Not_expecting (loc, nonterm) ->
Location.errorf ~source ~loc "Syntax error: %a not expected."
Style.inline_code nonterm
| Applicative_path loc ->
Location.errorf ~source ~loc
"Syntax error: applicative paths of the form %a \
are not supported when the option %a is set."
Style.inline_code "F(X).t"
Style.inline_code "-no-app-func"
| Variable_in_scope (loc, var) ->
Location.errorf ~source ~loc
"In this scoped type, variable %a \
is reserved for the local type %a."
(Style.as_inline_code Doc.tyvar) var
Style.inline_code var
| Other loc ->
Location.errorf ~source ~loc "Syntax error"
| Ill_formed_ast (loc, s) ->
Location.errorf ~source ~loc
"broken invariant in parsetree: %s" s
| Invalid_package_type (loc, ipt) ->
let invalid ppf ipt = match ipt with
| Syntaxerr.Parameterized_types ->
Format_doc.fprintf ppf "parametrized types are not supported"
| Constrained_types ->
Format_doc.fprintf ppf "constrained types are not supported"
| Private_types ->
Format_doc.fprintf ppf "private types are not supported"
| Not_with_type ->
Format_doc.fprintf ppf "only %a constraints are supported"
Style.inline_code "with type t ="
| Neither_identifier_nor_with_type ->
Format_doc.fprintf ppf
"only module type identifier and %a constraints are supported"
Style.inline_code "with type"
| Misplaced_attribute ->
Format_doc.fprintf ppf "an attribute cannot go here"
in
Location.errorf ~loc "invalid package type: %a" invalid ipt
| Removed_string_set loc ->
Location.errorf ~source ~loc
"Syntax error: strings are immutable, there is no assignment \
syntax for them.\n\
@{<hint>Hint@}: Mutable sequences of bytes are available in \
the Bytes module.\n\
@{<hint>Hint@}: Did you mean to use %a?"
Style.inline_code "Bytes.set"
| Missing_unboxed_literal_suffix loc ->
Location.errorf ~source ~loc
"Syntax error: Unboxed integer literals require width suffixes."
| Malformed_instance_identifier loc ->
Location.errorf ~source ~loc
"Syntax error: Unexpected in module instance"
| Quotation_reserved (loc, symb) ->
Location.errorf ~source ~loc
"Syntax error: `%s` is reserved for use in runtime metaprogramming."
symb
| Unspliceable loc ->
Location.errorf ~loc
"Syntax error: expression cannot be spliced.\n\
@{<hint>Hint@}: consider putting parentheses around the \
expression."
| Let_mutable_not_allowed_at_structure_level loc ->
Location.errorf ~source ~loc
"Syntax error: Mutable let bindings are not allowed \
at the structure level."
| Let_mutable_not_allowed_in_class_definition loc ->
Location.errorf ~source ~loc
"Syntax error: Mutable let bindings are not allowed \
inside class definitions."
| Let_mutable_not_allowed_with_function_bindings loc ->
Location.errorf ~source ~loc
"Syntax error: Mutable let is not allowed with function bindings.\n\
@{<hint>Hint@}: If you really want a mutable function variable, \
use the de-sugared syntax:\n %a"
Style.inline_code "let mutable f = fun x -> .."
| Block_access_bad_paren loc ->
Location.errorf ~source ~loc
"Syntax error: A parenthesis here can only follow one of: \n \
%a, %a, %a, %a, %a, %a, %a, %a, %a, %a."
Style.inline_code "." Style.inline_code ".L" Style.inline_code ".l"
Style.inline_code ".n" Style.inline_code ".:" Style.inline_code ".:L"
Style.inline_code ".:l" Style.inline_code ".:n"
Style.inline_code ".idx_imm" Style.inline_code ".idx_mut"
let () =
Location.register_error_of_exn
(function
| Syntaxerr.Error err -> Some (prepare_error err)
| _ -> None
)