Source file js_output.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
open! Stdlib
let stats = Debug.find "output"
open Javascript
module PP = Pretty_print
module Make (D : sig
val push_mapping : Pretty_print.pos -> Source_map.map -> unit
val get_file_index : string -> int
val get_name_index : string -> int
val source_map_enabled : bool
val accept_unnamed_var : bool
end) =
struct
open D
let nane_of_label = function
| Javascript.Label.L _ -> assert false
| Javascript.Label.S n -> n
let debug_enabled = Config.Flag.debuginfo ()
let output_debug_info f loc =
(if debug_enabled
then
match loc with
| Pi { Parse_info.src = None | Some ""; name = None | Some ""; _ } | N -> ()
| U ->
PP.non_breaking_space f;
PP.string f "/*<<?>>*/";
PP.non_breaking_space f
| Pi { Parse_info.src; name; line; col; _ } ->
let file =
match name, src with
| (None | Some ""), Some file -> file
| Some file, (None | Some "") -> file
| Some file, Some _file -> file
| None, None -> assert false
in
PP.non_breaking_space f;
PP.string f (Format.sprintf "/*<<%s:%d:%d>>*/" file line col);
PP.non_breaking_space f);
if source_map_enabled
then
match loc with
| N -> ()
| U | Pi { Parse_info.src = None | Some ""; _ } ->
push_mapping (PP.pos f) (Source_map.Gen { gen_line = -1; gen_col = -1 })
| Pi { Parse_info.src = Some file; line; col; _ } ->
push_mapping
(PP.pos f)
(Source_map.Gen_Ori
{ gen_line = -1
; gen_col = -1
; ori_source = get_file_index file
; ori_line = line
; ori_col = col
})
let output_debug_info_ident f nm loc =
if source_map_enabled
then
match loc with
| None | Some { Parse_info.src = Some "" | None; _ } -> ()
| Some { Parse_info.src = Some file; line; col; _ } ->
push_mapping
(PP.pos f)
(Source_map.Gen_Ori_Name
{ gen_line = -1
; gen_col = -1
; ori_source = get_file_index file
; ori_line = line
; ori_col = col
; ori_name = get_name_index nm
})
let ident f = function
| S { name = Utf8 name; var = Some v; _ } ->
output_debug_info_ident f name (Code.Var.get_loc v);
if false then PP.string f (Printf.sprintf "/* %d */" (Code.Var.idx v));
PP.string f name
| S { name = Utf8 name; var = None; loc = Pi pi } ->
output_debug_info_ident f name (Some pi);
PP.string f name
| S { name = Utf8 name; var = None; loc = U | N } -> PP.string f name
| V v ->
assert accept_unnamed_var;
PP.string f ("<" ^ Code.Var.to_string v ^ ">")
let opt_identifier f i =
match i with
| None -> ()
| Some i ->
PP.space f;
ident f i
let early_error _ = assert false
type prec =
| Expression
| AssignementExpression
| ConditionalExpression
| ShortCircuitExpression
| CoalesceExpression
| LogicalORExpression
| LogicalANDExpression
| BitwiseORExpression
| BitwiseXORExpression
| BitwiseANDExpression
| EqualityExpression
| RelationalExpression
| ShiftExpression
| AdditiveExpression
| MultiplicativeExpression
| ExponentiationExpression
| UnaryExpression
| UpdateExpression
| LeftHandSideExpression
| NewExpression
| CallOrMemberExpression
| MemberExpression
module Prec = struct
let compare (a : prec) (b : prec) = Poly.compare a b
[@@@ocaml.warning "-32"]
let ( <= ) a b = compare a b <= 0
let ( >= ) a b = compare a b >= 0
let ( < ) a b = compare a b < 0
let ( > ) a b = compare a b > 0
let ( = ) a b = compare a b = 0
end
let op_prec op =
match op with
| Eq
| StarEq
| SlashEq
| ModEq
| PlusEq
| MinusEq
| LslEq
| AsrEq
| LsrEq
| BandEq
| BxorEq
| BorEq
| OrEq
| AndEq
| ExpEq
| CoalesceEq -> AssignementExpression, LeftHandSideExpression, AssignementExpression
| Coalesce -> CoalesceExpression, BitwiseORExpression, BitwiseORExpression
| Or -> LogicalORExpression, LogicalORExpression, LogicalORExpression
| And -> LogicalANDExpression, LogicalANDExpression, LogicalANDExpression
| Bor -> BitwiseORExpression, BitwiseORExpression, BitwiseORExpression
| Bxor -> BitwiseXORExpression, BitwiseXORExpression, BitwiseXORExpression
| Band -> BitwiseANDExpression, BitwiseANDExpression, BitwiseANDExpression
| EqEq | NotEq | EqEqEq | NotEqEq ->
EqualityExpression, EqualityExpression, RelationalExpression
| Gt | GtInt | Ge | GeInt | Lt | LtInt | Le | LeInt | InstanceOf | In ->
RelationalExpression, RelationalExpression, ShiftExpression
| Lsl | Lsr | Asr -> ShiftExpression, ShiftExpression, AdditiveExpression
| Plus | Minus -> AdditiveExpression, AdditiveExpression, MultiplicativeExpression
| Mul | Div | Mod ->
MultiplicativeExpression, MultiplicativeExpression, ExponentiationExpression
| Exp -> ExponentiationExpression, UpdateExpression, ExponentiationExpression
let op_str op =
match op with
| Eq -> "="
| StarEq -> "*="
| SlashEq -> "/="
| ModEq -> "%="
| PlusEq -> "+="
| MinusEq -> "-="
| Or -> "||"
| OrEq -> "||="
| And -> "&&"
| AndEq -> "&&="
| Bor -> "|"
| Bxor -> "^"
| Band -> "&"
| EqEq -> "=="
| NotEq -> "!="
| EqEqEq -> "==="
| NotEqEq -> "!=="
| LslEq -> "<<="
| AsrEq -> ">>="
| LsrEq -> ">>>="
| BandEq -> "&="
| BxorEq -> "^="
| BorEq -> "|="
| Lt | LtInt -> "<"
| Le | LeInt -> "<="
| Gt | GtInt -> ">"
| Ge | GeInt -> ">="
| Lsl -> "<<"
| Lsr -> ">>>"
| Asr -> ">>"
| Plus -> "+"
| Minus -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "%"
| Exp -> "**"
| ExpEq -> "**="
| CoalesceEq -> "??="
| Coalesce -> "??"
| InstanceOf | In -> assert false
let unop_str op =
match op with
| Not -> "!"
| Neg -> "-"
| Pl -> "+"
| Bnot -> "~"
| IncrA | IncrB | DecrA | DecrB | Typeof | Void | Delete | Await -> assert false
let rec ends_with_if_without_else st =
match fst st with
| Labelled_statement (_, st)
| If_statement (_, _, Some st)
| While_statement (_, st)
| For_statement (_, _, _, st)
| With_statement (_, st)
| ForIn_statement (_, _, st) -> ends_with_if_without_else st
| ForOf_statement (_, _, st) -> ends_with_if_without_else st
| ForAwaitOf_statement (_, _, st) -> ends_with_if_without_else st
| If_statement (_, _, None) -> true
| Block _
| Variable_statement _
| Empty_statement
| Expression_statement _
| Continue_statement _
| Break_statement _
| Return_statement _
| Throw_statement _
| Do_while_statement _
| Switch_statement _
| Try_statement _
| Function_declaration _
| Class_declaration _
| Debugger_statement
| Import _
| Export _ -> false
let starts_with ~obj ~funct ~class_ ~let_identifier ~async_identifier l e =
let rec traverse l e =
match e with
| EObj _ -> obj
| EFun _ -> funct
| EClass _ -> class_
| EVar (S { name = Utf8 "let"; _ }) -> let_identifier
| EVar (S { name = Utf8 "async"; _ }) -> async_identifier
| ESeq (e, _) -> Prec.(l <= Expression) && traverse Expression e
| ECond (e, _, _) ->
Prec.(l <= ConditionalExpression) && traverse ShortCircuitExpression e
| EAssignTarget (ObjectTarget _) -> obj
| EAssignTarget (ArrayTarget _) -> false
| EBin (op, e, _) ->
let out, lft, _rght = op_prec op in
Prec.(l <= out) && traverse lft e
| EUn ((IncrA | DecrA), e) ->
Prec.(l <= UpdateExpression) && traverse LeftHandSideExpression e
| ECallTemplate (EFun _, _, _) ->
false
| ECallTemplate (e, _, _)
| ECall (e, _, _, _)
| EAccess (e, _, _)
| EDot (e, _, _)
| EDotPrivate (e, _, _) -> traverse CallOrMemberExpression e
| EArrow _
| EVar _
| EStr _
| ETemplate _
| EArr _
| EBool _
| ENum _
| ERegexp _
| EUn _
| ENew _
| EYield _
| EPrivName _ -> false
| CoverCallExpressionAndAsyncArrowHead e
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e
in
traverse l e
let contains ~in_ l e =
let rec traverse l e =
match e with
| EObj _ -> false
| EFun _ -> false
| EClass _ -> false
| EVar (S { name = Utf8 "in"; _ }) -> true
| ESeq (e1, e2) ->
Prec.(l <= Expression) && (traverse Expression e1 || traverse Expression e2)
| ECond (e1, e2, e3) ->
Prec.(l <= ConditionalExpression)
&& (traverse ShortCircuitExpression e1
|| traverse ShortCircuitExpression e2
|| traverse ShortCircuitExpression e3)
| EAssignTarget (ObjectTarget _) -> false
| EAssignTarget (ArrayTarget _) -> false
| EBin (op, e1, e2) ->
let out, lft, rght = op_prec op in
Prec.(l <= out) && (Poly.(op = In && in_) || traverse lft e1 || traverse rght e2)
| EUn ((IncrA | DecrA | IncrB | DecrB), e) ->
Prec.(l <= UpdateExpression) && traverse LeftHandSideExpression e
| EUn (_, e) -> Prec.(l <= UnaryExpression) && traverse UnaryExpression e
| ECallTemplate (EFun _, _, _) ->
false
| ECallTemplate (e, _, _)
| ECall (e, _, _, _)
| EAccess (e, _, _)
| EDot (e, _, _)
| EDotPrivate (e, _, _) -> traverse CallOrMemberExpression e
| EArrow _
| EVar _
| EStr _
| ETemplate _
| EArr _
| EBool _
| ENum _
| ERegexp _
| ENew _
| EYield _
| EPrivName _ -> false
| CoverCallExpressionAndAsyncArrowHead e
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e
in
traverse l e
let best_string_quote s =
let simple = ref 0 and double = ref 0 in
for i = 0 to String.length s - 1 do
match s.[i] with
| '\'' -> incr simple
| '"' -> incr double
| _ -> ()
done;
if !simple < !double then '\'' else '"'
let pp_string f ?(quote = '"') s =
let l = String.length s in
let b = Buffer.create (String.length s + 2) in
Buffer.add_char b quote;
for i = 0 to l - 1 do
let c = s.[i] in
match c with
| '\000' when i = l - 1 || not (Char.is_num s.[i + 1]) -> Buffer.add_string b "\\0"
| '\b' -> Buffer.add_string b "\\b"
| '\t' -> Buffer.add_string b "\\t"
| '\n' -> Buffer.add_string b "\\n"
| '\012' -> Buffer.add_string b "\\f"
| '/' when i > 0 && Char.equal s.[i - 1] '<' -> Buffer.add_string b "\\/"
| '\r' -> Buffer.add_string b "\\r"
| '\000' .. '\031' | '\127' ->
Buffer.add_string b "\\x";
Buffer.add_char_hex b c
| _ ->
if Char.equal c quote
then (
Buffer.add_char b '\\';
Buffer.add_char b c)
else Buffer.add_char b c
done;
Buffer.add_char b quote;
PP.string f (Buffer.contents b)
let pp_string_lit f (Stdlib.Utf8_string.Utf8 s) =
let quote = best_string_quote s in
pp_string f ~quote s
let pp_ident_or_string_lit f (Stdlib.Utf8_string.Utf8 s_lit as s) =
if is_ident s_lit then PP.string f s_lit else pp_string_lit f s
let rec comma_list f ~force_last_comma f_elt l =
match l with
| [] -> ()
| [ x ] ->
PP.start_group f 0;
f_elt f x;
if force_last_comma x then PP.string f ",";
PP.end_group f
| x :: r ->
PP.start_group f 0;
f_elt f x;
PP.end_group f;
PP.string f ",";
PP.space f;
comma_list f ~force_last_comma f_elt r
let comma_list_rest f ~force_last_comma f_elt l f_rest rest =
match l, rest with
| [], None -> ()
| [], Some rest ->
PP.start_group f 0;
PP.string f "...";
f_rest f rest;
PP.end_group f
| l, None -> comma_list f ~force_last_comma f_elt l
| l, Some r ->
comma_list f ~force_last_comma:(fun _ -> false) f_elt l;
PP.string f ",";
PP.space f;
PP.start_group f 0;
PP.string f "...";
f_rest f r;
PP.end_group f
let rec expression (l : prec) f e =
match e with
| EVar v -> ident f v
| ESeq (e1, e2) ->
if Prec.(l > Expression)
then (
PP.start_group f 1;
PP.string f "(");
expression Expression f e1;
PP.string f ",";
PP.space f;
expression Expression f e2;
if Prec.(l > Expression)
then (
PP.string f ")";
PP.end_group f)
| EFun (i, decl) -> function_declaration' f i decl
| EClass (i, cl_decl) -> class_declaration f i cl_decl
| EArrow ((k, p, b, pc), consise, _) ->
if Prec.(l > AssignementExpression)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 1;
PP.start_group f 0;
(match k with
| { async = true; generator = false } ->
PP.string f "async";
PP.non_breaking_space f
| { async = false; generator = false } -> ()
| { async = true | false; generator = true } -> assert false);
(match p with
| { list = [ ((BindingIdent _, None) as x) ]; rest = None } ->
formal_parameter f x;
PP.string f "=>"
| _ ->
PP.start_group f 1;
PP.string f "(";
formal_parameter_list f p;
PP.string f ")=>";
PP.end_group f);
PP.end_group f;
(match b, consise with
| [ (Return_statement (Some e), loc) ], true ->
PP.start_group f 1;
PP.break1 f;
output_debug_info f loc;
parenthesized_expression ~obj:true AssignementExpression f e;
PP.end_group f
| l, _ ->
let b =
match l with
| [ (Block l, _) ] -> l
| l -> l
in
PP.string f "{";
PP.break f;
function_body f b;
output_debug_info f pc;
PP.string f "}");
PP.end_group f;
if Prec.(l > AssignementExpression)
then (
PP.string f ")";
PP.end_group f)
| ECall (e, access_kind, el, loc) ->
if Prec.(l = NewExpression || l > CallOrMemberExpression)
then (
PP.start_group f 1;
PP.string f "(");
output_debug_info f loc;
PP.start_group f 1;
expression CallOrMemberExpression f e;
PP.break f;
PP.start_group f 1;
(match access_kind with
| ANormal -> PP.string f "("
| ANullish -> PP.string f "?.(");
arguments f el;
PP.string f ")";
PP.end_group f;
PP.end_group f;
if Prec.(l = NewExpression || l > CallOrMemberExpression)
then (
PP.string f ")";
PP.end_group f)
| ECallTemplate (e, t, loc) ->
if Prec.(l = NewExpression || l > CallOrMemberExpression)
then (
PP.start_group f 1;
PP.string f "(");
output_debug_info f loc;
PP.start_group f 1;
parenthesized_expression ~funct:true CallOrMemberExpression f e;
PP.break f;
PP.start_group f 1;
template f t;
PP.end_group f;
PP.end_group f;
if Prec.(l = NewExpression || l > CallOrMemberExpression)
then (
PP.string f ")";
PP.end_group f)
| EStr x -> pp_string_lit f x
| ETemplate l -> template f l
| EBool b -> PP.string f (if b then "true" else "false")
| ENum num ->
let s = Num.to_string num in
let need_parent =
if Num.is_neg num
then
Prec.(l > UnaryExpression)
else
Prec.(l >= CallOrMemberExpression)
&& (not (Char.equal s.[0] 'I'))
&& not (Char.equal s.[0] 'N')
in
if need_parent then PP.string f "(";
PP.string f s;
if need_parent then PP.string f ")"
| EUn (((Typeof | Void | Delete | Await) as op), e) ->
let p = UnaryExpression in
if Prec.(l > p)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 0;
let name =
match op with
| Typeof -> "typeof"
| Void -> "void"
| Delete -> "delete"
| Await -> "await"
| _ -> assert false
in
PP.string f name;
PP.space f;
expression p f e;
PP.end_group f;
if Prec.(l > p)
then (
PP.string f ")";
PP.end_group f)
| EUn (((IncrB | DecrB) as op), e) ->
let p = UpdateExpression in
if Prec.(l > p)
then (
PP.start_group f 1;
PP.string f "(");
if Poly.(op = IncrB) then PP.string f "++" else PP.string f "--";
expression UnaryExpression f e;
if Prec.(l > p)
then (
PP.string f ")";
PP.end_group f)
| EUn (((IncrA | DecrA) as op), e) ->
let p = UpdateExpression in
if Prec.(l > p)
then (
PP.start_group f 1;
PP.string f "(");
expression LeftHandSideExpression f e;
if Poly.(op = IncrA) then PP.string f "++" else PP.string f "--";
if Prec.(l > p)
then (
PP.string f ")";
PP.end_group f)
| EUn (op, e) ->
let p = UnaryExpression in
let need_parent = Prec.(l > p) in
if need_parent
then (
PP.start_group f 1;
PP.string f "(");
PP.string f (unop_str op);
PP.space f;
expression p f e;
if need_parent
then (
PP.string f ")";
PP.end_group f)
| EBin (((InstanceOf | In) as op), e1, e2) ->
let out, lft, rght = op_prec InstanceOf in
if Prec.(l > out)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 0;
expression lft f e1;
PP.space f;
let name =
match op with
| InstanceOf -> "instanceof"
| In -> "in"
| _ -> assert false
in
PP.string f name;
PP.space f;
expression rght f e2;
PP.end_group f;
if Prec.(l > out)
then (
PP.string f ")";
PP.end_group f)
| EBin
( (( Eq
| StarEq
| SlashEq
| ModEq
| PlusEq
| MinusEq
| LslEq
| AsrEq
| LsrEq
| BandEq
| BxorEq
| BorEq
| OrEq
| AndEq
| ExpEq
| CoalesceEq ) as op)
, e1
, e2 ) ->
let out, lft, rght = op_prec op in
let lft =
match e1, op with
| EBin (Coalesce, _, _), Coalesce -> CoalesceExpression
| _ -> lft
in
PP.start_group f 0;
if Prec.(l > out) then PP.string f "(";
PP.start_group f 0;
expression lft f e1;
PP.space f;
PP.string f (op_str op);
PP.end_group f;
PP.start_group f 1;
PP.space f;
expression rght f e2;
PP.end_group f;
if Prec.(l > out) then PP.string f ")";
PP.end_group f
| EBin (op, e1, e2) ->
let out, lft, rght = op_prec op in
let lft =
match e1, op with
| EBin (Coalesce, _, _), Coalesce -> CoalesceExpression
| _ -> lft
in
PP.start_group f 0;
if Prec.(l > out) then PP.string f "(";
expression lft f e1;
PP.space f;
PP.start_group f 1;
PP.string f (op_str op);
PP.space f;
expression rght f e2;
if Prec.(l > out) then PP.string f ")";
PP.end_group f;
PP.end_group f
| EAssignTarget t -> (
let property f p =
match p with
| TargetPropertyId (Prop_and_ident id, None) -> ident f id
| TargetPropertyId (Prop_and_ident id, Some (e, _)) ->
ident f id;
PP.space f;
PP.string f "=";
PP.space f;
expression AssignementExpression f e
| TargetProperty (pn, e, None) ->
PP.start_group f 0;
property_name f pn;
PP.string f ":";
PP.space f;
expression AssignementExpression f e;
PP.end_group f
| TargetProperty (pn, e, Some (ini, _)) ->
PP.start_group f 0;
property_name f pn;
PP.string f ":";
PP.space f;
expression AssignementExpression f e;
PP.space f;
PP.string f "=";
PP.space f;
expression AssignementExpression f ini;
PP.end_group f
| TargetPropertySpread e ->
PP.string f "...";
expression AssignementExpression f e
| TargetPropertyMethod (n, m) -> method_ f property_name n m
in
let element f p =
match p with
| TargetElementHole -> ()
| TargetElementId (id, None) -> ident f id
| TargetElementId (id, Some (e, _)) ->
ident f id;
PP.space f;
PP.string f "=";
PP.space f;
expression AssignementExpression f e
| TargetElement e -> expression AssignementExpression f e
| TargetElementSpread e ->
PP.string f "...";
expression AssignementExpression f e
in
match t with
| ObjectTarget list ->
PP.start_group f 1;
PP.string f "{";
comma_list f ~force_last_comma:(fun _ -> false) property list;
PP.string f "}";
PP.end_group f
| ArrayTarget list ->
PP.start_group f 1;
PP.string f "[";
comma_list
f
~force_last_comma:(function
| TargetElementHole -> true
| _ -> false)
element
list;
PP.string f "]";
PP.end_group f)
| EArr el ->
PP.start_group f 1;
PP.string f "[";
element_list f el;
PP.string f "]";
PP.end_group f
| EAccess (e, access_kind, e') ->
PP.start_group f 1;
let l' =
match l with
| NewExpression | MemberExpression -> MemberExpression
| _ -> CallOrMemberExpression
in
expression l' f e;
PP.break f;
PP.start_group f 1;
(match access_kind with
| ANormal -> PP.string f "["
| ANullish -> PP.string f "?.[");
expression Expression f e';
PP.string f "]";
PP.end_group f;
PP.end_group f
| EDot (e, access_kind, Utf8 nm) ->
let l' =
match l with
| NewExpression | MemberExpression -> MemberExpression
| _ -> CallOrMemberExpression
in
expression l' f e;
(match access_kind with
| ANormal -> PP.string f "."
| ANullish -> PP.string f "?.");
PP.string f nm
| EDotPrivate (e, access_kind, Utf8 nm) ->
let l' =
match l with
| NewExpression | MemberExpression -> MemberExpression
| _ -> CallOrMemberExpression
in
expression l' f e;
(match access_kind with
| ANormal -> PP.string f ".#"
| ANullish -> PP.string f "?.#");
PP.string f nm
| ENew (e, None) ->
if Prec.(l > NewExpression)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 1;
PP.string f "new";
PP.space f;
expression NewExpression f e;
PP.end_group f;
if Prec.(l > NewExpression)
then (
PP.string f ")";
PP.end_group f)
| ENew (e, Some el) ->
PP.start_group f 1;
PP.string f "new";
PP.space f;
expression MemberExpression f e;
PP.break f;
PP.start_group f 1;
PP.string f "(";
arguments f el;
PP.string f ")";
PP.end_group f;
PP.end_group f
| ECond (e, e1, e2) ->
if Prec.(l > ConditionalExpression)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 1;
PP.start_group f 0;
expression ShortCircuitExpression f e;
PP.end_group f;
PP.space f;
PP.start_group f 1;
PP.start_group f 0;
PP.string f "?";
PP.space f;
PP.end_group f;
expression AssignementExpression f e1;
PP.end_group f;
PP.space f;
PP.start_group f 1;
PP.start_group f 0;
PP.string f ":";
PP.space f;
PP.end_group f;
expression AssignementExpression f e2;
PP.end_group f;
PP.end_group f;
if Prec.(l > ConditionalExpression)
then (
PP.string f ")";
PP.end_group f)
| EObj lst ->
PP.start_group f 1;
PP.string f "{";
property_list f lst;
PP.string f "}";
PP.end_group f
| ERegexp (s, opt) -> (
PP.string f "/";
PP.string f s;
PP.string f "/";
match opt with
| None -> ()
| Some o -> PP.string f o)
| EYield { delegate; expr = e } -> (
let kw =
match delegate with
| false -> "yield"
| true -> "yield*"
in
match e with
| None -> PP.string f kw
| Some e ->
if Prec.(l > AssignementExpression)
then (
PP.start_group f 1;
PP.string f "(");
PP.start_group f 7;
PP.string f kw;
PP.non_breaking_space f;
PP.start_group f 0;
expression AssignementExpression f e;
PP.end_group f;
PP.end_group f;
if Prec.(l > AssignementExpression)
then (
PP.end_group f;
PP.string f ")"
))
| EPrivName (Utf8 i) ->
PP.string f "#";
PP.string f i
| CoverCallExpressionAndAsyncArrowHead e
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e
and template f l =
PP.string f "`";
List.iter l ~f:(function
| TStr (Utf8 s) -> PP.string f s
| TExp e ->
PP.string f "${";
expression AssignementExpression f e;
PP.string f "}");
PP.string f "`"
and property_name f n =
match n with
| PNI (Utf8 s) -> PP.string f s
| PNS s -> pp_string_lit f s
| PNN v -> expression Expression f (ENum v)
| PComputed e ->
PP.string f "[";
expression Expression f e;
PP.string f "]"
and property_list f l = comma_list f ~force_last_comma:(fun _ -> false) property l
and property f p =
match p with
| Property (pn, e) ->
PP.start_group f 0;
property_name f pn;
PP.string f ":";
PP.space f;
expression AssignementExpression f e;
PP.end_group f
| PropertySpread e ->
PP.string f "...";
expression AssignementExpression f e
| PropertyMethod (n, m) -> method_ f property_name n m
| CoverInitializedName (e, _, _) -> early_error e
and method_ : 'a. _ -> (PP.t -> 'a -> unit) -> 'a -> method_ -> unit =
fun (type a) f (name : PP.t -> a -> unit) (n : a) (m : method_) ->
match m with
| MethodGet (k, l, b, loc') | MethodSet (k, l, b, loc') ->
(match k with
| { async = false; generator = false } -> ()
| _ -> assert false);
let prefix =
match m with
| MethodGet _ -> "get"
| MethodSet _ -> "set"
| _ -> assert false
in
function_declaration f prefix name (Some n) l b loc'
| Method (k, l, b, loc') ->
let fpn f () =
(match k with
| { async = false; generator = false } -> ()
| { async = false; generator = true } ->
PP.string f "*";
PP.space f
| { async = true; generator = false } ->
PP.string f "async";
PP.non_breaking_space f
| { async = true; generator = true } ->
PP.string f "async*";
PP.space f);
name f n
in
function_declaration f "" fpn (Some ()) l b loc'
and element_list f el =
comma_list
f
~force_last_comma:(function
| ElementHole -> true
| _ -> false)
element
el
and element f (e : element) =
match e with
| ElementHole -> ()
| Element e ->
PP.start_group f 0;
expression AssignementExpression f e;
PP.end_group f
| ElementSpread e ->
PP.start_group f 0;
PP.string f "...";
expression AssignementExpression f e;
PP.end_group f
and formal_parameter f e = binding_element f e
and formal_parameter_list f { list; rest } =
comma_list_rest
f
~force_last_comma:(fun _ -> false)
formal_parameter
list
binding
rest
and function_body f b = statement_list f ~skip_last_semi:true b
and argument f a =
PP.start_group f 0;
(match a with
| Arg e -> expression AssignementExpression f e
| ArgSpread e ->
PP.string f "...";
expression AssignementExpression f e);
PP.end_group f
and arguments f l = comma_list f ~force_last_comma:(fun _ -> false) argument l
and variable_declaration f ?(in_ = true) x =
match x with
| DeclIdent (i, None) -> ident f i
| DeclIdent (i, Some (e, loc)) ->
PP.start_group f 1;
output_debug_info f loc;
PP.start_group f 0;
ident f i;
PP.space f;
PP.string f "=";
PP.end_group f;
PP.start_group f 1;
PP.space f;
let p = (not in_) && contains ~in_:true Expression e in
if p
then (
PP.start_group f 1;
PP.string f "(");
expression AssignementExpression f e;
if p
then (
PP.string f ")";
PP.end_group f);
PP.end_group f;
PP.end_group f
| DeclPattern (p, (e, loc)) ->
PP.start_group f 1;
output_debug_info f loc;
PP.start_group f 0;
pattern f p;
PP.space f;
PP.string f "=";
PP.end_group f;
PP.start_group f 1;
PP.space f;
let p = (not in_) && contains ~in_:true Expression e in
if p
then (
PP.start_group f 1;
PP.string f "(");
expression AssignementExpression f e;
if p
then (
PP.string f ")";
PP.end_group f);
PP.end_group f;
PP.end_group f
and binding_property f x =
match x with
| Prop_binding (pn, e) ->
property_name f pn;
PP.string f ":";
PP.space f;
binding_element f e
| Prop_ident (Prop_and_ident i, None) -> ident f i
| Prop_ident (Prop_and_ident i, Some (e, loc)) ->
ident f i;
PP.space f;
PP.string f "=";
PP.space f;
output_debug_info f loc;
expression AssignementExpression f e
and binding_element f (b, (e : initialiser option)) =
match e with
| None -> binding f b
| Some (e, loc) ->
output_debug_info f loc;
binding f b;
PP.space f;
PP.string f "=";
PP.space f;
expression AssignementExpression f e
and binding f x =
match x with
| BindingIdent id -> ident f id
| BindingPattern p -> pattern f p
and binding_array_elt f x =
match x with
| None -> ()
| Some e -> binding_element f e
and pattern f p =
match p with
| ObjectBinding { list; rest } ->
PP.start_group f 1;
PP.string f "{";
comma_list_rest
f
~force_last_comma:(fun _ -> false)
binding_property
list
ident
rest;
PP.string f "}";
PP.end_group f
| ArrayBinding { list; rest } ->
PP.start_group f 1;
PP.string f "[";
comma_list_rest
f
~force_last_comma:(function
| None -> true
| Some _ -> false)
binding_array_elt
list
binding
rest;
PP.string f "]";
PP.end_group f
and variable_declaration_list_aux f ?in_ l =
match l with
| [] -> assert false
| [ d ] -> variable_declaration f ?in_ d
| d :: r ->
variable_declaration f ?in_ d;
PP.string f ",";
PP.space f;
variable_declaration_list_aux f ?in_ r
and variable_declaration_kind f kind =
match kind with
| Var -> PP.string f "var"
| Let -> PP.string f "let"
| Const -> PP.string f "const"
and variable_declaration_list ?in_ kind close f = function
| [] -> ()
| [ x ] ->
let x, loc =
match x with
| DeclIdent (_, None) as x -> x, N
| DeclIdent (i, Some (e, loc)) -> DeclIdent (i, Some (e, N)), loc
| DeclPattern (p, (e, loc)) -> DeclPattern (p, (e, N)), loc
in
PP.start_group f 1;
output_debug_info f loc;
variable_declaration_kind f kind;
PP.space f;
variable_declaration f ?in_ x;
if close then PP.string f ";";
PP.end_group f
| l ->
PP.start_group f 1;
variable_declaration_kind f kind;
PP.space f;
variable_declaration_list_aux f ?in_ l;
if close then PP.string f ";";
PP.end_group f
and parenthesized_expression
?(last_semi = fun () -> ())
?(obj = false)
?(funct = false)
?(class_ = false)
?(let_identifier = false)
?(async_identifier = false)
?(force = false)
l
f
e =
if force || starts_with ~obj ~funct ~class_ ~let_identifier ~async_identifier l e
then (
PP.start_group f 1;
PP.string f "(";
expression l f e;
PP.string f ")";
last_semi ();
PP.end_group f)
else (
PP.start_group f 0;
expression l f e;
last_semi ();
PP.end_group f)
and for_binding f k v =
variable_declaration_kind f k;
PP.space f;
binding f v
and statement1 ?last f s =
match s with
| Block _, _ -> statement ?last f s
| _ ->
PP.space ~indent:1 f;
PP.start_group f 0;
statement ?last f s;
PP.end_group f
and statement ?(last = false) f (s, loc) =
let can_omit_semi = PP.compact f && last in
let last_semi () = if can_omit_semi then () else PP.string f ";" in
output_debug_info f loc;
match s with
| Block b -> block f b
| Variable_statement (k, l) -> variable_declaration_list k (not can_omit_semi) f l
| Function_declaration (i, decl) -> function_declaration' f (Some i) decl
| Class_declaration (i, cl_decl) -> class_declaration f (Some i) cl_decl
| Empty_statement -> PP.string f ";"
| Debugger_statement ->
PP.string f "debugger";
last_semi ()
| Expression_statement e ->
parenthesized_expression
~last_semi
~obj:true
~funct:true
~class_:true
~let_identifier:true
Expression
f
e
| If_statement (e, s1, (Some _ as s2)) when ends_with_if_without_else s1 ->
statement ~last f (If_statement (e, (Block [ s1 ], N), s2), N)
| If_statement (e, s1, s2) ->
let rec ite kw e s1 s2 =
(let last_in_s1 =
match s2 with
| None -> Some last
| Some _ -> None
in
PP.start_group f 0;
PP.start_group f 1;
PP.string f kw;
PP.break f;
PP.start_group f 1;
PP.string f "(";
expression Expression f e;
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ?last:last_in_s1 f s1);
match s2 with
| None -> PP.end_group f
| Some (If_statement (e, s1, s2), _) when not (ends_with_if_without_else s1) ->
PP.space f;
ite "else if" e s1 s2;
PP.end_group f
| Some s2 ->
PP.space f;
PP.string f "else";
statement1 ~last f s2;
PP.end_group f
in
ite "if" e s1 s2
| While_statement (e, s) ->
PP.start_group f 0;
PP.start_group f 0;
PP.string f "while";
PP.break f;
PP.start_group f 1;
PP.string f "(";
expression Expression f e;
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ~last f s;
PP.end_group f
| Do_while_statement (s, e) ->
PP.start_group f 0;
PP.string f "do";
statement1 f s;
PP.break f;
PP.string f "while";
PP.break1 f;
PP.start_group f 1;
PP.string f "(";
expression Expression f e;
PP.string f ")";
last_semi ();
PP.end_group f;
PP.end_group f
| For_statement (e1, e2, e3, s) ->
PP.start_group f 0;
PP.start_group f 0;
PP.string f "for";
PP.break f;
PP.start_group f 1;
PP.string f "(";
(match e1 with
| Left None -> ()
| Left (Some e) ->
let force = contains ~in_:true Expression e in
parenthesized_expression ~force ~let_identifier:true Expression f e
| Right (k, l) -> variable_declaration_list k ~in_:false false f l);
PP.string f ";";
(match e2 with
| None -> ()
| Some e2 ->
PP.space f;
expression Expression f e2);
PP.string f ";";
(match e3 with
| None -> ()
| Some e3 ->
PP.space f;
expression Expression f e3);
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ~last f s;
PP.end_group f
| ForIn_statement (e1, e2, s) ->
PP.start_group f 0;
PP.start_group f 0;
PP.string f "for";
PP.break f;
PP.start_group f 1;
PP.string f "(";
(match e1 with
| Left e ->
parenthesized_expression ~let_identifier:true LeftHandSideExpression f e
| Right (k, v) -> for_binding f k v);
PP.space f;
PP.string f "in";
PP.break f;
PP.space f;
expression Expression f e2;
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ~last f s;
PP.end_group f
| ForOf_statement (e1, e2, s) ->
PP.start_group f 0;
PP.start_group f 0;
PP.string f "for";
PP.break f;
PP.start_group f 1;
PP.string f "(";
(match e1 with
| Left e ->
parenthesized_expression
~let_identifier:true
~async_identifier:true
LeftHandSideExpression
f
e
| Right (k, v) -> for_binding f k v);
PP.space f;
PP.string f "of";
PP.break f;
PP.space f;
expression AssignementExpression f e2;
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ~last f s;
PP.end_group f
| ForAwaitOf_statement (e1, e2, s) ->
PP.start_group f 0;
PP.start_group f 0;
PP.string f "for await";
PP.break f;
PP.start_group f 1;
PP.string f "(";
(match e1 with
| Left e ->
parenthesized_expression ~let_identifier:true LeftHandSideExpression f e
| Right (k, v) -> for_binding f k v);
PP.space f;
PP.string f "of";
PP.break f;
PP.space f;
expression AssignementExpression f e2;
PP.string f ")";
PP.end_group f;
PP.end_group f;
statement1 ~last f s;
PP.end_group f
| Continue_statement None ->
PP.string f "continue";
last_semi ()
| Continue_statement (Some s) ->
PP.string f "continue ";
let (Utf8 l) = nane_of_label s in
PP.string f l;
last_semi ()
| Break_statement None ->
PP.string f "break";
last_semi ()
| Break_statement (Some s) ->
PP.string f "break ";
let (Utf8 l) = nane_of_label s in
PP.string f l;
last_semi ()
| Return_statement e -> (
match e with
| None ->
PP.string f "return";
last_semi ()
| Some (EFun (i, ({ async = false; generator = false }, l, b, pc))) ->
PP.start_group f 1;
PP.start_group f 0;
PP.start_group f 0;
PP.string f "return function";
opt_identifier f i;
PP.end_group f;
PP.break f;
PP.start_group f 1;
PP.string f "(";
formal_parameter_list f l;
PP.string f ")";
PP.end_group f;
PP.end_group f;
PP.string f "{";
PP.break f;
function_body f b;
output_debug_info f pc;
PP.string f "}";
last_semi ();
PP.end_group f
| Some e ->
PP.start_group f 7;
PP.string f "return";
PP.non_breaking_space f;
PP.start_group f 0;
expression Expression f e;
last_semi ();
PP.end_group f;
PP.end_group f
)
| Labelled_statement (i, s) ->
let (Utf8 l) = nane_of_label i in
PP.string f l;
PP.string f ":";
PP.space f;
statement ~last f s
| Switch_statement (e, cc, def, cc') ->
PP.start_group f 1;
PP.start_group f 0;
PP.string f "switch";
PP.break f;
PP.start_group f 1;
PP.string f "(";
expression Expression f e;
PP.string f ")";
PP.end_group f;
PP.end_group f;
PP.start_group f 1;
PP.string f "{";
PP.break f;
let output_one last (e, sl) =
PP.start_group f 1;
PP.string f "case";
PP.space f;
expression Expression f e;
PP.string f ":";
PP.end_group f;
PP.start_group f 1;
(match sl with
| _ :: _ -> PP.space f
| [] -> PP.break f);
PP.start_group f 0;
statement_list ~skip_last_semi:last f sl;
PP.end_group f;
PP.end_group f
in
let rec loop last = function
| [] -> ()
| [ x ] ->
output_one last x;
if not last then PP.break f
| x :: xs ->
output_one false x;
PP.break f;
loop last xs
in
loop (Option.is_none def && List.is_empty cc') cc;
(match def with
| None -> ()
| Some def ->
PP.start_group f 1;
PP.string f "default:";
PP.space f;
PP.start_group f 0;
let last = List.is_empty cc' in
statement_list ~skip_last_semi:last f def;
PP.end_group f;
PP.end_group f;
if not last then PP.break f);
loop true cc';
PP.end_group f;
PP.end_group f;
PP.break f;
PP.string f "}"
| Throw_statement e ->
PP.start_group f 6;
PP.string f "throw";
PP.non_breaking_space f;
PP.start_group f 0;
expression Expression f e;
last_semi ();
PP.end_group f;
PP.end_group f
| Try_statement (b, ctch, fin) ->
PP.start_group f 0;
PP.string f "try";
block f b;
(match ctch with
| None -> ()
| Some (i, b) ->
PP.break f;
(match i with
| None -> PP.string f "catch"
| Some i ->
PP.string f "catch(";
formal_parameter f i;
PP.string f ")");
block f b);
(match fin with
| None -> ()
| Some b ->
PP.break f;
PP.string f "finally";
block f b);
PP.end_group f
| With_statement (e, s) ->
PP.start_group f 0;
PP.string f "with(";
expression Expression f e;
PP.string f ")";
PP.break f;
statement f s;
PP.end_group f
| Import ({ kind; from }, _loc) ->
PP.start_group f 0;
PP.string f "import";
(match kind with
| SideEffect -> ()
| Default i ->
PP.space f;
ident f i
| Namespace (def, i) ->
Option.iter def ~f:(fun def ->
PP.space f;
ident f def;
PP.string f ",");
PP.space f;
PP.string f "* as ";
ident f i
| Named (def, l) ->
Option.iter def ~f:(fun def ->
PP.space f;
ident f def;
PP.string f ",");
PP.space f;
PP.string f "{";
PP.space f;
comma_list
f
~force_last_comma:(fun _ -> false)
(fun f (s, i) ->
if match i with
| S { name; _ } when Stdlib.Utf8_string.equal name s -> true
| _ -> false
then ident f i
else (
pp_ident_or_string_lit f s;
PP.string f " as ";
ident f i))
l;
PP.space f;
PP.string f "}");
(match kind with
| SideEffect -> ()
| _ ->
PP.space f;
PP.string f "from");
PP.space f;
pp_string_lit f from;
PP.string f ";";
PP.end_group f
| Export (e, _loc) ->
PP.start_group f 0;
PP.string f "export";
(match e with
| ExportNames l ->
PP.space f;
PP.string f "{";
PP.space f;
comma_list
~force_last_comma:(fun _ -> false)
f
(fun f (i, s) ->
if match i with
| S { name; _ } when Stdlib.Utf8_string.equal name s -> true
| _ -> false
then ident f i
else (
ident f i;
PP.string f " as ";
pp_ident_or_string_lit f s))
l;
PP.space f;
PP.string f "};"
| ExportFrom { kind; from } ->
PP.space f;
(match kind with
| Export_all None -> PP.string f "*"
| Export_all (Some s) ->
PP.string f "* as ";
pp_ident_or_string_lit f s
| Export_names l ->
PP.string f "{";
PP.space f;
comma_list
~force_last_comma:(fun _ -> false)
f
(fun f (a, b) ->
if Stdlib.Utf8_string.equal a b
then pp_ident_or_string_lit f a
else (
pp_ident_or_string_lit f a;
PP.string f " as ";
pp_ident_or_string_lit f b))
l;
PP.space f;
PP.string f "}");
PP.space f;
PP.string f "from";
PP.space f;
pp_string_lit f from;
PP.string f ";"
| ExportDefaultExpression e ->
PP.space f;
PP.string f "default";
PP.space f;
parenthesized_expression
~last_semi
~obj:true
~funct:true
~class_:true
~let_identifier:true
Expression
f
e
| ExportDefaultFun (i, decl) ->
PP.space f;
PP.string f "default";
PP.space f;
function_declaration' f i decl
| ExportDefaultClass (id, decl) ->
PP.space f;
PP.string f "default";
PP.space f;
class_declaration f id decl
| ExportFun (id, decl) ->
PP.space f;
function_declaration' f (Some id) decl
| ExportClass (id, decl) ->
PP.space f;
class_declaration f (Some id) decl
| ExportVar (k, l) ->
PP.space f;
variable_declaration_list k (not can_omit_semi) f l
| CoverExportFrom e -> early_error e);
PP.end_group f
and statement_list f ?skip_last_semi b =
match b with
| [] -> ()
| [ s ] -> statement f ?last:skip_last_semi s
| s :: r ->
statement f s;
PP.space f;
statement_list f ?skip_last_semi r
and block f b =
PP.start_group f 0;
PP.start_group f 1;
PP.string f "{";
PP.break f;
statement_list ~skip_last_semi:true f b;
PP.end_group f;
PP.break f;
PP.string f "}";
PP.end_group f
and function_declaration :
type a. 'pp -> string -> ('pp -> a -> unit) -> a option -> _ -> _ -> _ -> unit =
fun f prefix (pp_name : _ -> a -> unit) (name : a option) l body loc ->
PP.start_group f 0;
PP.start_group f 0;
PP.start_group f 0;
PP.string f prefix;
(match name with
| None -> ()
| Some name ->
if not (String.is_empty prefix) then PP.space f;
pp_name f name);
PP.end_group f;
PP.break f;
PP.start_group f 1;
PP.string f "(";
formal_parameter_list f l;
PP.string f ")";
PP.end_group f;
PP.end_group f;
PP.start_group f 1;
PP.string f "{";
PP.break f;
function_body f body;
PP.end_group f;
PP.break f;
output_debug_info f loc;
PP.string f "}";
PP.end_group f
and function_declaration' f (name : _ option) (k, l, b, loc') =
let prefix =
match k with
| { async = false; generator = false } -> "function"
| { async = true; generator = false } -> "async function"
| { async = true; generator = true } -> "async function*"
| { async = false; generator = true } -> "function*"
in
function_declaration f prefix ident name l b loc'
and class_declaration f i x =
PP.start_group f 1;
PP.start_group f 0;
PP.start_group f 0;
PP.string f "class";
(match i with
| None -> ()
| Some i ->
PP.space f;
ident f i);
PP.end_group f;
Option.iter x.extends ~f:(fun e ->
PP.space f;
PP.string f "extends";
PP.space f;
expression LeftHandSideExpression f e;
PP.space f);
PP.end_group f;
PP.start_group f 2;
PP.string f "{";
PP.break f;
List.iter_last x.body ~f:(fun last x ->
(match x with
| CEMethod (static, n, m) ->
PP.start_group f 0;
if static
then (
PP.string f "static";
PP.space f);
method_ f class_element_name n m;
PP.end_group f
| CEField (static, n, i) ->
PP.start_group f 0;
if static
then (
PP.string f "static";
PP.space f);
class_element_name f n;
(match i with
| None -> ()
| Some (e, loc) ->
PP.space f;
PP.string f "=";
PP.space f;
output_debug_info f loc;
expression AssignementExpression f e);
PP.string f ";";
PP.end_group f
| CEStaticBLock l ->
PP.start_group f 0;
PP.string f "static";
PP.space f;
block f l;
PP.end_group f);
if not last then PP.break f);
PP.end_group f;
PP.break f;
PP.string f "}";
PP.end_group f
and class_element_name f x =
match x with
| PropName n -> property_name f n
| PrivName (Utf8 i) ->
PP.string f "#";
PP.string f i
and program f s = statement_list f s
end
let part_of_ident =
let a =
Array.init 256 ~f:(fun i ->
match Char.chr i with
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '$' -> true
| _ -> false)
in
fun c -> Array.unsafe_get a (Char.code c)
let need_space a b =
(part_of_ident a && part_of_ident b)
||
match a, b with
| '/', '/'
| '-', '-'
| '+', '+' -> true
| _, _ -> false
let hashtbl_to_list htb =
Hashtbl.fold (fun k v l -> (k, v) :: l) htb []
|> List.sort ~cmp:(fun (_, a) (_, b) -> compare a b)
|> List.map ~f:fst
let program ?(accept_unnamed_var = false) f ?source_map p =
let temp_mappings = ref [] in
let files = Hashtbl.create 17 in
let names = Hashtbl.create 17 in
let contents : string option list ref option =
match source_map with
| None | Some { Source_map.sources_content = None; _ } -> None
| Some { Source_map.sources_content = Some _; _ } -> Some (ref [])
in
let push_mapping, get_file_index, get_name_index, source_map_enabled =
let source_map_enabled =
match source_map with
| None -> false
| Some sm ->
let rec loop s sc =
match s, sc with
| [], _ -> ()
| x :: xs, [] ->
Hashtbl.add files x (Hashtbl.length files);
Option.iter contents ~f:(fun r -> r := None :: !r);
loop xs []
| x :: xs, y :: ys ->
Hashtbl.add files x (Hashtbl.length files);
Option.iter contents ~f:(fun r -> r := y :: !r);
loop xs ys
in
loop sm.sources (Option.value ~default:[] sm.sources_content);
List.iter sm.Source_map.names ~f:(fun f ->
Hashtbl.add names f (Hashtbl.length names));
true
in
let find_source file =
match Builtins.find file with
| Some f -> Some (Builtins.File.content f)
| None ->
if Sys.file_exists file && not (Sys.is_directory file)
then
let content = Fs.read_file file in
Some content
else None
in
( (fun pos m -> temp_mappings := (pos, m) :: !temp_mappings)
, (fun file ->
try Hashtbl.find files file
with Not_found ->
let pos = Hashtbl.length files in
Hashtbl.add files file pos;
Option.iter contents ~f:(fun r -> r := find_source file :: !r);
pos)
, (fun name ->
try Hashtbl.find names name
with Not_found ->
let pos = Hashtbl.length names in
Hashtbl.add names name pos;
pos)
, source_map_enabled )
in
let module O = Make (struct
let push_mapping = push_mapping
let get_name_index = get_name_index
let get_file_index = get_file_index
let source_map_enabled = source_map_enabled
let accept_unnamed_var = accept_unnamed_var
end) in
PP.set_needed_space_function f need_space;
if Config.Flag.effects () then PP.set_adjust_indentation_function f (fun n -> n mod 40);
PP.start_group f 0;
O.program f p;
PP.end_group f;
PP.newline f;
let sm =
match source_map with
| None -> None
| Some sm ->
let sources = hashtbl_to_list files in
let names = hashtbl_to_list names in
let sources_content =
match contents with
| None -> None
| Some r -> Some (List.rev !r)
in
let sources =
List.map sources ~f:(fun filename ->
match Builtins.find filename with
| None -> filename
| Some _ -> Filename.concat "/builtin" filename)
in
let mappings =
List.rev_append_map !temp_mappings sm.mappings ~f:(fun (pos, m) ->
let gen_line = pos.PP.p_line + 1 in
let gen_col = pos.PP.p_col in
match m with
| Source_map.Gen { gen_col = _; gen_line = _ } ->
Source_map.Gen { gen_col; gen_line }
| Source_map.Gen_Ori
{ gen_line = _; gen_col = _; ori_source; ori_line; ori_col } ->
Source_map.Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
| Source_map.Gen_Ori_Name
{ gen_line = _; gen_col = _; ori_source; ori_line; ori_col; ori_name }
->
Source_map.Gen_Ori_Name
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
in
Some { sm with Source_map.sources; names; sources_content; mappings }
in
PP.check f;
(if stats ()
then
let size i = Printf.sprintf "%.2fKo" (float_of_int i /. 1024.) in
let _percent n d =
Printf.sprintf "%.1f%%" (float_of_int n *. 100. /. float_of_int d)
in
let total_s = PP.total f in
Format.eprintf "total size : %s@." (size total_s));
sm