fatou 0.5.0

A language server, formatter, and linter for Julia
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
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
//! CST → JuliaSyntax.jl s-expression projector (the parser oracle).
//!
//! Walks a Fatou [`SyntaxNode`] and emits a string in the textual shape of
//! JuliaSyntax's `SyntaxNode` s-expression — the same form produced by
//! `JuliaSyntax.parseall(SyntaxNode, code)` and printed by its `show`. Exposed
//! via [`to_juliasyntax_sexpr`] and the `fatou parse --to sexpr` CLI mode; it
//! drives the differential parser harness in `tests/juliasyntax_oracle.rs`.
//!
//! **Faithful translation, not reshaping.** The projector translates only
//! *encoding* differences — Fatou's wrapper nodes (`NAME`, `LITERAL`, `ARG`,
//! `CONDITION`, `SIGNATURE`, `PAREN_EXPR`), delimiter tokens, and trivia — into
//! JuliaSyntax's surface. It never reshapes Fatou's tree *topology*: where Fatou
//! genuinely models a construct differently than JuliaSyntax (comparison chains
//! staying nested, header passthrough kept as loose tokens), the divergence is
//! emitted faithfully so the harness surfaces it (routing the case to
//! `blocked.txt`) rather than hiding it.
//!
//! Coverage is intentionally incremental. An unsupported node emits a visible
//! `(unsupported KIND)` sentinel so a gap stays loud rather than silently
//! dropping content.

use crate::parser::diagnostics::{DiagnosticKind, ParseDiagnostic};
use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
use rowan::NodeOrToken;
use std::cell::RefCell;

use SyntaxKind::*;

// The projector reconstructs JuliaSyntax's `(error-t)`/`(error)` error shapes
// from the parse diagnostics rather than from dedicated CST nodes (the
// rust-analyzer model: recovery lives in a side-channel, not the tree). Since
// `project` recurses through ~50 free helpers, the diagnostics are stashed in a
// thread-local set once at the entry point and queried by byte position at the
// handful of sites that need them, rather than threaded through every signature.
thread_local! {
    static PROJ_DIAGS: RefCell<Vec<(DiagnosticKind, usize, usize)>> =
        const { RefCell::new(Vec::new()) };
}

/// Render the given Fatou CST as a JuliaSyntax-native s-expression string.
///
/// `diags` is the parse's diagnostics side-channel; the projector reads it to
/// reconstruct error shapes (`(error-t)` markers, recovery `(error-t …)` heads).
/// The root projects to `(toplevel …)`, mirroring `parseall`. Pair with
/// [`normalize_sexpr`] when comparing against captured Julia output to ignore
/// pretty-print whitespace differences.
pub fn to_juliasyntax_sexpr(tree: &SyntaxNode, diags: &[ParseDiagnostic]) -> String {
    PROJ_DIAGS.with(|d| {
        *d.borrow_mut() = diags.iter().map(|x| (x.kind, x.start, x.end)).collect();
    });
    project(tree)
}

/// Count diagnostics of `kind` recorded as a zero-width point at byte `pos`.
fn diag_count_at(pos: usize, kind: DiagnosticKind) -> usize {
    PROJ_DIAGS.with(|d| {
        d.borrow()
            .iter()
            .filter(|(k, s, e)| *k == kind && *s == pos && *e == pos)
            .count()
    })
}

/// Whether a zero-width diagnostic of `kind` is recorded at byte `pos`.
fn diag_at(pos: usize, kind: DiagnosticKind) -> bool {
    diag_count_at(pos, kind) > 0
}

/// Count diagnostics of `kind` whose range *starts* at byte `pos` (ignoring the
/// end). Used for the keyword-anchored truncation diagnostics, which span the
/// opening keyword rather than a zero-width point.
fn diag_count_from(pos: usize, kind: DiagnosticKind) -> usize {
    PROJ_DIAGS.with(|d| {
        d.borrow()
            .iter()
            .filter(|(k, s, _)| *k == kind && *s == pos)
            .count()
    })
}

/// The construct's opening-keyword start byte — the anchor the parser uses for
/// truncation diagnostics (`MissingEnd`, `MissingTryHandler`). For most block
/// forms that is the first non-trivia token, but a `do`-block leads with its
/// callee, so the `do` keyword is anchored instead.
fn keyword_start(node: &SyntaxNode) -> usize {
    if node.kind() == DO_EXPR
        && let Some(do_kw) = node
            .children_with_tokens()
            .filter_map(|el| el.into_token())
            .find(|t| t.kind() == DO_KW)
    {
        return usize::from(do_kw.text_range().start());
    }
    node.descendants_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| !is_trivia(t.kind()))
        .map(|t| usize::from(t.text_range().start()))
        .unwrap_or_else(|| usize::from(node.text_range().start()))
}

/// Whether `node` is a byte-bearing recovery `ERROR` node (a stray-closer run,
/// trailing junk, or an import recovery `:` clause) — JuliaSyntax renders those
/// with the `(error-t …)` head, unlike the plain `(error …)` of other recovery.
/// A zero-width (empty) error node is never one, so the synthesized leading
/// `(error)` of a stray closer keeps its plain head.
fn is_recovery_error(node: &SyntaxNode) -> bool {
    let r = node.text_range();
    if r.start() == r.end() {
        return false;
    }
    let (s, e) = (usize::from(r.start()), usize::from(r.end()));
    PROJ_DIAGS.with(|d| {
        d.borrow().iter().any(|(k, ds, de)| {
            matches!(
                k,
                DiagnosticKind::StrayCloser
                    | DiagnosticKind::TrailingJunk
                    | DiagnosticKind::ImportRecoveryColon
            ) && *ds >= s
                && *de <= e
        })
    })
}

/// The keyword text of a stray-block-keyword `ERROR` node (one carrying a
/// `StrayKeyword` diagnostic at its range), or `None` for any other error node.
/// JuliaSyntax renders such a node as `(error <kw>)` (`@doc x\nend` ⇒
/// `(macrocall @doc x) (error end)`), so the keyword token's text is surfaced
/// rather than dropped as a structural keyword.
fn stray_keyword_text(node: &SyntaxNode) -> Option<String> {
    let r = node.text_range();
    let (s, e) = (usize::from(r.start()), usize::from(r.end()));
    let has_diag = PROJ_DIAGS.with(|d| {
        d.borrow()
            .iter()
            .any(|(k, ds, de)| matches!(k, DiagnosticKind::StrayKeyword) && *ds >= s && *de <= e)
    });
    if !has_diag {
        return None;
    }
    node.children_with_tokens().find_map(|el| match el {
        NodeOrToken::Token(t) if is_keyword(t.kind()) => Some(t.text().to_string()),
        _ => None,
    })
}

/// Canonical form of a JuliaSyntax s-expression string. Tokenizes on whitespace
/// and parentheses (preserving `"…"` string literals as atoms) and rejoins with
/// single-space separation, so pretty-print spacing no longer affects equality.
pub fn normalize_sexpr(s: &str) -> String {
    let mut tokens: Vec<String> = Vec::new();
    let bytes = s.as_bytes();
    let mut i = 0usize;
    while i < bytes.len() {
        let c = bytes[i];
        match c {
            b' ' | b'\t' | b'\n' | b'\r' => i += 1,
            b'(' | b')' => {
                tokens.push((c as char).to_string());
                i += 1;
            }
            b'"' => {
                let start = i;
                i += 1;
                while i < bytes.len() {
                    match bytes[i] {
                        b'\\' if i + 1 < bytes.len() => i += 2,
                        b'"' => {
                            i += 1;
                            break;
                        }
                        _ => i += 1,
                    }
                }
                tokens.push(s[start..i].to_string());
            }
            _ => {
                let start = i;
                while i < bytes.len() {
                    if matches!(bytes[i], b' ' | b'\t' | b'\n' | b'\r' | b'(' | b')' | b'"') {
                        break;
                    }
                    i += 1;
                }
                if i > start {
                    tokens.push(s[start..i].to_string());
                }
            }
        }
    }
    tokens.join(" ")
}

// --- Core dispatch ---------------------------------------------------------

fn project(node: &SyntaxNode) -> String {
    match node.kind() {
        ROOT => sexp("toplevel", stmt_strings(node)),
        // A logical line carrying a top-level `;` groups its statements into a
        // `(toplevel-; …)` node (mirroring JuliaSyntax); an empty `;` line is
        // `(toplevel-;)`.
        TOPLEVEL_SEMICOLON => sexp("toplevel-;", stmt_strings(node)),
        // A string-literal statement directly followed by another statement folds
        // into a docstring `(doc <string> <target>)` (JuliaSyntax `parse_docstring`).
        DOC => sexp("doc", stmt_strings(node)),
        BLOCK => sexp("block", stmt_strings(node)),
        // `begin … end` wraps a `BLOCK`; project that directly so it lowers to a
        // single `(block …)` rather than a doubled `(block (block …))`.
        BEGIN_EXPR => project_block_child_folding_error(node),

        NAME => name_text(node),
        LITERAL => project_literal(node),
        STRING_LITERAL => project_string(node),
        NONSTANDARD_IDENTIFIER => project_var(node),
        CMD_LITERAL => project_cmd(node),
        // A standalone interpolation projects to a `$` node (`$x` → `($ x)`);
        // inside a string the inner value is used instead (via `string_parts`).
        // A bare `$` with no operand (`$\n`) is the `$` symbol itself.
        INTERPOLATION => {
            let inner = project_interpolation(node);
            if inner.is_empty() && !node.children_with_tokens().any(|el| el.kind() == LPAREN) {
                "$".to_string()
            } else {
                format!("($ {inner})")
            }
        }

        PAREN_EXPR | CONDITION => match first_node(node) {
            Some(inner) => project(&inner),
            // A lone operator in parens is the operator as a value/symbol, e.g.
            // `(+)` → `+` or, quoted, `:(=)` → `(quote-: =)`.
            None => significant(node)
                .iter()
                .find_map(|el| match el {
                    NodeOrToken::Token(t) if is_operator(t.kind()) => Some(t.text().to_string()),
                    _ => None,
                })
                .unwrap_or_else(|| "(block)".to_string()),
        },

        BINARY_EXPR => project_binary(node),
        RANGE_EXPR => project_range(node),
        COMPARISON_EXPR => project_comparison(node),
        ASSIGNMENT_EXPR => project_assignment(node),
        UNARY_EXPR => project_unary(node),
        POSTFIX_EXPR => project_postfix(node),
        SPLAT_EXPR => sexp("...", vec![project_first(node)]),
        TYPE_ANNOTATION => project_type_annotation(node),
        WHERE_EXPR => project_where(node),
        ARROW_EXPR => sexp("->", project_each(child_nodes(node))),
        JUXTAPOSE_EXPR => project_juxtapose(node),
        TERNARY_EXPR => project_ternary(node),

        CALL_EXPR => project_call("call", node),
        INDEX_EXPR => project_call("ref", node),
        CURLY_EXPR => project_call("curly", node),
        DOT_CALL_EXPR => project_dot_call(node),
        BRACES => sexp("braces", project_args(node)),

        TUPLE_EXPR => sexp("tuple-p", project_args(node)),
        PAREN_BLOCK => sexp("block-p", project_block_args(node)),
        BARE_TUPLE_EXPR => sexp("tuple", project_each(child_nodes(node))),
        VECT_EXPR => sexp("vect", project_args(node)),
        MATRIX_EXPR => project_matrix(node),
        TYPED_MATRIX_EXPR => project_typed_matrix(node),
        BRACESCAT_EXPR => project_bracescat(node),

        COMPREHENSION => sexp("comprehension", vec![project_generator(node)]),
        BRACES_COMPREHENSION => sexp("braces", vec![project_generator(node)]),
        TYPED_COMPREHENSION => project_typed_comprehension(node),
        GENERATOR => project_generator(node),

        IF_EXPR => project_if(node),
        WHILE_EXPR => sexp("while", project_each(child_nodes(node))),
        FOR_EXPR => {
            let mut parts = vec![project_for_binding(node), project_block_child(node)];
            push_trailing_errors(node, &mut parts);
            sexp("for", parts)
        }
        FUNCTION_DEF => project_function_like("function", node),
        MACRO_DEF => project_function_like("macro", node),
        LET_EXPR => project_let(node),
        QUOTE_EXPR => sexp("quote", vec![project_block_child_folding_error(node)]),
        QUOTE_SYM => project_quote_sym(node),
        TRY_EXPR => project_try(node),
        STRUCT_DEF => project_struct(node),
        ABSTRACT_DEF => sexp("abstract", vec![project_signature(node)]),
        PRIMITIVE_DEF => project_primitive(node),
        MODULE_DEF => project_module(node),
        DO_EXPR => project_do(node),

        RETURN_EXPR => project_keyword_stmt("return", node),
        BREAK_EXPR => "(break)".to_string(),
        CONTINUE_EXPR => "(continue)".to_string(),
        CONST_STMT => {
            // A `const` whose declaration is not a plain `=` assignment is wrapped
            // in `(error …)` (`const x` ⇒ `(error (const x))`); the parser records
            // the diagnostic at the `const` keyword start.
            let decl = project_decl("const", node);
            if diag_at(
                usize::from(node.text_range().start()),
                DiagnosticKind::ConstNotAssignment,
            ) {
                format!("(error {decl})")
            } else {
                decl
            }
        }
        GLOBAL_STMT => project_decl("global", node),
        LOCAL_STMT => project_decl("local", node),
        IMPORT_STMT => project_import("import", node),
        USING_STMT => project_import("using", node),
        EXPORT_STMT => project_export(node),
        PUBLIC_STMT => project_public(node),
        IMPORT_PATH => project_import_path(node),
        IMPORT_ALIAS => project_import_alias(node),

        MACRO_CALL => project_macrocall(node),

        END_MARKER => "end".to_string(),
        BEGIN_MARKER => "begin".to_string(),
        OPERATOR_ATOM => project_operator_atom(node),

        // The sole error node kind. JuliaSyntax distinguishes the bare `(error)`
        // (a missing required element, an invalid `as` rename) from the
        // `TRIVIA_FLAG`-tagged `(error-t)` (a recovered run); `is_recovery_error`
        // recovers that distinction from the diagnostics side-channel. A stray
        // closing delimiter recovered into the node renders as `✘`.
        ERROR => {
            // A stray middle/closing block keyword (`end`, `else`, `elseif`,
            // `catch`, `finally`) where a statement was expected is wrapped
            // alone in `(error <kw>)` — the keyword text is rendered, unlike the
            // dropped-keyword default of a recovery run.
            if let Some(kw) = stray_keyword_text(node) {
                format!("(error {kw})")
            } else {
                project_error(
                    if is_recovery_error(node) {
                        "error-t"
                    } else {
                        "error"
                    },
                    node,
                )
            }
        }

        other => format!("(unsupported {other:?})"),
    }
}

// --- Operator tables -------------------------------------------------------

/// How a `BINARY_EXPR`/`ASSIGNMENT_EXPR` operator token projects.
enum InfixHead {
    /// `(call-i lhs OP rhs)` — ordinary infix operator (OP is the source text).
    CallI(&'static str),
    /// `(OP lhs rhs)` — operator is its own head (`&&`, `||`, `<:`, `>:`, `=`).
    Special(&'static str),
    /// `(. lhs (quote rhs))` — field access.
    Dot,
    /// `(dotcall-i lhs OP rhs)` — broadcast infix (OP is the *undotted* text).
    DotCallI(&'static str),
}

fn infix_head(kind: SyntaxKind) -> InfixHead {
    use InfixHead::*;
    match kind {
        PLUS => CallI("+"),
        MINUS => CallI("-"),
        STAR => CallI("*"),
        // Invalid doubled operators: JuliaSyntax heads the infix call with the
        // error token itself (`a**b` ⇒ `(call-i a (Error**) b)`, `a--b` ⇒
        // `(call-i a (ErrorInvalidOperator) b)`).
        STAR_STAR => CallI("(Error**)"),
        MINUS_MINUS => CallI("(ErrorInvalidOperator)"),
        SLASH => CallI("/"),
        BACKSLASH => CallI("\\"),
        SLASH_SLASH => CallI("//"),
        CARET => CallI("^"),
        PERCENT => CallI("%"),
        COLON => CallI(":"),
        DOT_DOT => CallI(".."),
        FAT_ARROW => CallI("=>"),
        PIPE_GT => CallI("|>"),
        PIPE_LT => CallI("<|"),
        LONG_ARROW => Special("-->"),
        LEFT_RIGHT_ARROW => CallI("<-->"),
        LEFT_LONG_ARROW => CallI("<--"),
        SHL => CallI("<<"),
        SHR => CallI(">>"),
        USHR => CallI(">>>"),
        AMP => CallI("&"),
        PIPE => CallI("|"),
        EQ_EQ => CallI("=="),
        NOT_EQ => CallI("!="),
        EQ_EQ_EQ => CallI("==="),
        NOT_EQ_EQ => CallI("!=="),
        LT => CallI("<"),
        LE => CallI("<="),
        GT => CallI(">"),
        GE => CallI(">="),
        TILDE => CallI("~"),

        AND_AND => Special("&&"),
        OR_OR => Special("||"),
        DOT_AND_AND => Special(".&&"),
        DOT_OR_OR => Special(".||"),
        SUBTYPE => Special("<:"),
        SUPERTYPE => Special(">:"),
        EQ => Special("="),
        DOT_EQ => Special(".="),

        DOT => Dot,

        DOT_PLUS => DotCallI("+"),
        DOT_MINUS => DotCallI("-"),
        DOT_STAR => DotCallI("*"),
        DOT_STAR_STAR => DotCallI("(Error**)"),
        DOT_MINUS_MINUS => DotCallI("(ErrorInvalidOperator)"),
        DOT_SLASH => DotCallI("/"),
        DOT_BACKSLASH => DotCallI("\\"),
        DOT_SLASH_SLASH => DotCallI("//"),
        DOT_CARET => DotCallI("^"),
        DOT_PERCENT => DotCallI("%"),
        DOT_TILDE => DotCallI("~"),
        DOT_EQ_EQ => DotCallI("=="),
        DOT_NOT_EQ => DotCallI("!="),
        DOT_EQ_EQ_EQ => DotCallI("==="),
        DOT_NOT_EQ_EQ => DotCallI("!=="),
        DOT_LT => DotCallI("<"),
        DOT_LE => DotCallI("<="),
        DOT_GT => DotCallI(">"),
        DOT_GE => DotCallI(">="),
        DOT_SUBTYPE => DotCallI("<:"),
        DOT_SUPERTYPE => DotCallI(">:"),
        DOT_FAT_ARROW => DotCallI("=>"),
        DOT_LONG_ARROW => DotCallI("-->"),
        DOT_LEFT_LONG_ARROW => DotCallI("<--"),
        DOT_LEFT_RIGHT_ARROW => DotCallI("<-->"),
        DOT_PIPE_GT => DotCallI("|>"),
        DOT_AMP => DotCallI("&"),
        DOT_PIPE => DotCallI("|"),

        // Fallback: treat as an ordinary infix call using the raw text. Leaked
        // in faithfully so an unmapped operator surfaces as a divergence.
        _ => CallI("?"),
    }
}

/// A bare operator used as a value atom (`+` → `+`, `.&` → `(. &)`, `:` → `:`).
/// Broadcast operators project to `(. op)` (via `operator_func_repr`); every
/// other operator projects to its raw token text, so kinds without an
/// `infix_head` entry (the Unicode radicals `√`/`¬`, `..`) project faithfully.
fn project_operator_atom(node: &SyntaxNode) -> String {
    significant(node)
        .iter()
        .find_map(|el| match el {
            NodeOrToken::Token(t) if matches!(infix_head(t.kind()), InfixHead::DotCallI(_)) => {
                // A suffixed broadcast operator (`.+₁`) keeps its suffix, which
                // `operator_func_repr` (keyed on kind) would drop; strip the
                // leading broadcast dot from the text instead.
                Some(if op_has_suffix(t.text()) {
                    format!("(. {})", &t.text()[1..])
                } else {
                    operator_func_repr(t.kind())
                })
            }
            // Dotted broadcast operators whose head is not `DotCallI` — the
            // short-circuit `.&&`/`.||` and the assignment forms `.=`/`.+=` quote
            // as `(. op)` too (`:.&&` ⇒ `(quote-: (. &&))`). Strip the leading
            // broadcast dot; `..`/`...` lead with a doubled dot and are excluded.
            NodeOrToken::Token(t)
                if t.text().starts_with('.')
                    && t.text().len() > 1
                    && t.text().as_bytes()[1] != b'.' =>
            {
                Some(format!("(. {})", &t.text()[1..]))
            }
            NodeOrToken::Token(t) => Some(t.text().to_string()),
            _ => None,
        })
        .unwrap_or_else(|| "(operator)".to_string())
}

/// The function-name representation of an operator used as a callee, e.g. in
/// `*(x)` → `(call * x)` or `.*(x)` → `(call (. *) x)`. Broadcast operators
/// project to `(. op)`; everything else to the bare operator text.
fn operator_func_repr(kind: SyntaxKind) -> String {
    // `!` is unary-only (no `infix_head` entry), but it is a valid call callee:
    // `!(a, b)` → `(call ! a b)`.
    if kind == BANG {
        return "!".to_string();
    }
    match infix_head(kind) {
        InfixHead::CallI(s) | InfixHead::Special(s) => s.to_string(),
        InfixHead::DotCallI(s) => format!("(. {s})"),
        InfixHead::Dot => ".".to_string(),
    }
}

/// Whether an operator's text carries a trailing sub/superscript or prime suffix
/// (`+₁`, `-->₁`). A base operator never ends in a suffix character, so checking
/// the final character is sufficient.
fn op_has_suffix(text: &str) -> bool {
    text.chars()
        .next_back()
        .is_some_and(super::lexer::is_op_suffix_char)
}

fn is_operator(kind: SyntaxKind) -> bool {
    kind.is_operator()
}

// --- Binary / unary / assignment -------------------------------------------

/// `(? cond then else)`. Whitespace errors around `?`/`:` are recorded as
/// `TernaryQWhitespace` (anchored at the `?`'s end) and `TernaryColonWhitespace`
/// (anchored at the true-branch's end); each replays as a `(error-t)` between the
/// respective operands (`a?b:c` ⇒ `(? a (error-t) (error-t) b (error-t) (error-t) c)`).
fn project_ternary(node: &SyntaxNode) -> String {
    let nodes = child_nodes(node);
    let q_end = node
        .children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| t.kind() == QUESTION)
        .map(|t| usize::from(t.text_range().end()));
    // An incomplete ternary terminated by a closing block keyword is re-headed
    // `?` → `if` with one trailing `(error-t)` per missing piece (`x ? true end`
    // ⇒ `(if x true (error-t) (error-t))`, `x ? true : elseif …` ⇒
    // `(if x true (error-t))`).
    if let Some(qe) = q_end {
        let markers = diag_count_at(qe, DiagnosticKind::IncompleteTernaryIf);
        if markers > 0 {
            let mut parts: Vec<String> = nodes.iter().map(project).collect();
            for _ in 0..markers {
                parts.push("(error-t)".to_string());
            }
            return sexp("if", parts);
        }
    }
    let mut parts = Vec::new();
    for (i, n) in nodes.iter().enumerate() {
        parts.push(project(n));
        if i == 0 {
            if let Some(qe) = q_end {
                for _ in 0..diag_count_at(qe, DiagnosticKind::TernaryQWhitespace) {
                    parts.push("(error-t)".to_string());
                }
            }
        } else if i == 1 {
            let then_end = usize::from(n.text_range().end());
            for _ in 0..diag_count_at(then_end, DiagnosticKind::TernaryColonWhitespace) {
                parts.push("(error-t)".to_string());
            }
        }
    }
    sexp("?", parts)
}

/// `(juxtapose lhs rhs)`. An *invalid* string juxtaposition (`"a"x`, `2"b"`)
/// records a `StringJuxtapose` diagnostic at the left operand's end, projecting
/// a `(error-t)` between the operands (`(juxtapose "a" (error-t) x)`).
fn project_juxtapose(node: &SyntaxNode) -> String {
    let nodes = child_nodes(node);
    let mut parts = Vec::new();
    for (i, n) in nodes.iter().enumerate() {
        parts.push(project(n));
        if i == 0
            && diag_at(
                usize::from(n.text_range().end()),
                DiagnosticKind::StringJuxtapose,
            )
        {
            parts.push("(error-t)".to_string());
        }
    }
    sexp("juxtapose", parts)
}

fn project_binary(node: &SyntaxNode) -> String {
    // Word operators `in`/`isa` are lexed as identifiers, so the operator is the
    // sole loose `IDENT` token child (both operands are wrapped in nodes). They
    // head an ordinary infix call: `i in rhs` ⇒ `(call-i i in rhs)`.
    if let Some(word) = node
        .children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| t.kind() == IDENT && matches!(t.text(), "in" | "isa"))
    {
        let operands = child_nodes(node);
        if operands.len() == 2 {
            return format!(
                "(call-i {} {} {})",
                project(&operands[0]),
                word.text(),
                project(&operands[1])
            );
        }
    }
    let op = match operator_token(node) {
        Some(t) => t,
        None => return format!("(unsupported {:?})", node.kind()),
    };
    // A range colon glued to a single `<`/`>` (`InvalidGluedOperator` recorded at
    // the colon) heads the call with both operator tokens error-wrapped: `a :< b`
    // ⇒ `(call-i a (error : <) b)`, the missing-rhs form `a :<` ⇒ `(call-i a
    // (error : <) (error))`.
    if op.kind() == COLON
        && diag_at(
            usize::from(op.text_range().start()),
            DiagnosticKind::InvalidGluedOperator,
        )
    {
        let head: Vec<String> = node
            .children_with_tokens()
            .filter_map(|el| el.into_token())
            .filter(|t| is_operator(t.kind()))
            .map(|t| t.text().to_string())
            .collect();
        let operands = child_nodes(node);
        let lhs = project(&operands[0]);
        let rhs = operands
            .get(1)
            .map(project)
            .unwrap_or_else(|| "(error)".to_string());
        return format!("(call-i {lhs} (error {}) {rhs})", head.join(" "));
    }
    // A field-access dot with disallowed leading whitespace (`x .y`) records a
    // `DotWhitespace` diagnostic at the dot's end; splice `(error-t)` before the
    // quoted field name in the `Dot` arm below.
    let dot_error = if diag_at(
        usize::from(op.text_range().end()),
        DiagnosticKind::DotWhitespace,
    ) {
        "(error-t) "
    } else {
        ""
    };
    let operands = child_nodes(node);
    // Missing right operand: JuliaSyntax keeps the operator node and synthesizes
    // a zero-width `(error)` for the absent operand (`a +` ⇒ `(call-i a +
    // (error))`, `a &&` ⇒ `(&& a (error))`). Reconstructed from the
    // `MissingOperand` diagnostic at the operator; a field-access dot has no
    // suffix-quoting to do, so it gets the bare `(. lhs (error))`.
    if operands.len() == 1 && operator_missing_rhs(&op) {
        let lhs = project(&operands[0]);
        return infix_call_string(&op, &lhs, "(error)")
            .unwrap_or_else(|| format!("(. {lhs} {dot_error}(error))"));
    }
    // A flat arithmetic chain (`a + b + c` ⇒ `(call-i a + b c)`): two or more
    // operands joined by a single repeated `+`/`*`, projecting as one variadic
    // infix call. A trailing dangling operator (`a + b +`) keeps two operands and
    // a `MissingOperand` diagnostic on the last operator, replayed as `(error)`.
    let last_op_missing = node
        .children_with_tokens()
        .filter_map(|el| el.into_token())
        .filter(|t| is_operator(t.kind()))
        .last()
        .is_some_and(|t| operator_missing_rhs(&t));
    if operands.len() >= 3 || (operands.len() == 2 && last_op_missing) {
        return project_flat_arith(&op, &operands, last_op_missing);
    }
    if operands.len() != 2 {
        return project_flat(significant(node));
    }
    let lhs = project(&operands[0]);
    let rhs = &operands[1];
    let rhs_str = project(rhs);
    // The non-field-access heads (unicode, suffixed, `call-i`/`dotcall-i`/special)
    // are shared with the missing-operand path above.
    if let Some(s) = infix_call_string(&op, &lhs, &rhs_str) {
        return s;
    }
    match infix_head(op.kind()) {
        // Field access. A plain field name is quoted (`f.x` → `(. f (quote x))`);
        // an interpolated field name is inert-quoted (`f.$x` →
        // `(. f (inert ($ x)))`), so the interpolation projects through `($ …)`.
        InfixHead::Dot if rhs.kind() == INTERPOLATION => {
            format!("(. {lhs} {dot_error}(inert {rhs_str}))")
        }
        // A quoted field name (`a.:b`) is already a `(quote-: …)` symbol; emit it
        // directly rather than wrapping it in another `(quote …)`.
        InfixHead::Dot if rhs.kind() == QUOTE_SYM => {
            format!("(. {lhs} {dot_error}{rhs_str})")
        }
        InfixHead::Dot => format!("(. {lhs} {dot_error}(quote {}))", name_text(rhs)),
        // Non-dot heads are handled by `infix_call_string` above.
        _ => unreachable!("non-dot infix head handled by infix_call_string"),
    }
}

/// Project a flat arithmetic chain (`a + b + c` ⇒ `(call-i a + b c)`): the
/// single repeated operator heads a variadic infix call over all operands. A
/// trailing dangling operator appends a zero-width `(error)`. The operator is a
/// `CallI` head (`+`/`*`); any unexpected non-`CallI` operator still joins
/// faithfully under `call-i` so a divergence surfaces.
fn project_flat_arith(op: &SyntaxToken, operands: &[SyntaxNode], last_op_missing: bool) -> String {
    let (head, op_text) = match infix_head(op.kind()) {
        InfixHead::CallI(text) => ("call-i", text.to_string()),
        InfixHead::DotCallI(text) => ("dotcall-i", text.to_string()),
        InfixHead::Special(text) => ("call-i", text.to_string()),
        InfixHead::Dot => ("call-i", op.text().to_string()),
    };
    let mut parts = Vec::with_capacity(operands.len() + 2);
    parts.push(project(&operands[0]));
    parts.push(op_text);
    parts.extend(operands[1..].iter().map(project));
    if last_op_missing {
        parts.push("(error)".to_string());
    }
    sexp(head, parts)
}

/// Format a non-field-access infix operator from its operator token and
/// already-projected operand strings. Returns `None` for a field-access dot,
/// whose right operand needs structural inspection (handled by the caller).
/// Shared by the normal and missing-right-operand projection paths.
fn infix_call_string(op: &SyntaxToken, lhs: &str, rhs: &str) -> Option<String> {
    // Unicode operators carry their own text: the `call-i` tiers head an ordinary
    // infix call, and the assignment tier (`≔ ≕ ⩴`) heads the node with the
    // operator itself, just like the ASCII `Special` forms.
    match op.kind() {
        // A broadcast Unicode operator (`.…`, `.×`) carries a leading `.`: strip
        // it and head `dotcall-i`, like the ASCII broadcast forms.
        UNICODE_OP if op.text().starts_with('.') => {
            return Some(format!("(dotcall-i {lhs} {} {rhs})", &op.text()[1..]));
        }
        UNICODE_OP => return Some(format!("(call-i {lhs} {} {rhs})", op.text())),
        UNICODE_ASSIGN_OP => return Some(format!("({} {lhs} {rhs})", op.text())),
        _ => {}
    }
    // A suffixed operator (`a +₁ b`, `x -->₁ y`) carries its sub/superscript
    // suffix in the token text and always projects as a generic infix call —
    // even operators that are otherwise syntactic (`-->`). Broadcast operators
    // keep the `dotcall-i` head with the leading `.` stripped from the function
    // name. Mirrors JuliaSyntax, where a suffix makes the operator non-syntactic.
    if op_has_suffix(op.text()) {
        let text = op.text();
        return Some(match infix_head(op.kind()) {
            InfixHead::DotCallI(_) => {
                format!("(dotcall-i {lhs} {} {rhs})", text.trim_start_matches('.'))
            }
            _ => format!("(call-i {lhs} {text} {rhs})"),
        });
    }
    match infix_head(op.kind()) {
        InfixHead::CallI(text) => Some(format!("(call-i {lhs} {text} {rhs})")),
        InfixHead::Special(text) => Some(format!("({text} {lhs} {rhs})")),
        InfixHead::DotCallI(text) => Some(format!("(dotcall-i {lhs} {text} {rhs})")),
        InfixHead::Dot => None,
    }
}

/// Whether the operator token heads a node whose right operand is absent — a
/// `MissingOperand` diagnostic recorded spanning the operator. JuliaSyntax
/// synthesizes a zero-width `(error)` for the missing operand.
fn operator_missing_rhs(op: &SyntaxToken) -> bool {
    diag_count_from(
        usize::from(op.text_range().start()),
        DiagnosticKind::MissingOperand,
    ) > 0
}

/// A stepped range `a:b:c` is a single infix colon call over three operands:
/// `(call-i a : b c)`. Mirrors JuliaSyntax, which folds the range with step into
/// one `(call ...)` node rather than nesting two binary colons.
fn project_range(node: &SyntaxNode) -> String {
    let operands = child_nodes(node);
    // A stepped range with its third operand absent (`1:2:` ⇒
    // `(call-i 1 : 2 (error))`) keeps two operands and a `MissingOperand`
    // diagnostic on the trailing colon.
    if operands.len() == 2 {
        if node
            .children_with_tokens()
            .filter_map(|el| el.into_token())
            .filter(|t| t.kind() == COLON)
            .last()
            .is_some_and(|colon| operator_missing_rhs(&colon))
        {
            return format!(
                "(call-i {} : {} (error))",
                project(&operands[0]),
                project(&operands[1]),
            );
        }
        return project_flat(significant(node));
    }
    if operands.len() != 3 {
        return project_flat(significant(node));
    }
    format!(
        "(call-i {} : {} {})",
        project(&operands[0]),
        project(&operands[1]),
        project(&operands[2]),
    )
}

/// Project a comparison chain (`a < b <= c` ⇒ `(comparison a < b <= c)`). The
/// operands and operator tokens are emitted in source order; a plain operator
/// renders as its text, a dotted-broadcast comparison as `(. op)`
/// (`a .< b .< c` ⇒ `(comparison a (. <) b (. <) c)`). A dangling trailing
/// operator with no right operand replays the zero-width `(error)` from the
/// `MissingOperand` diagnostic (`a < b <` ⇒ `(comparison a < b < (error))`).
fn project_comparison(node: &SyntaxNode) -> String {
    let mut parts = Vec::new();
    let mut last_op = None;
    for el in significant(node) {
        match el {
            NodeOrToken::Node(n) => parts.push(project(&n)),
            NodeOrToken::Token(t) => {
                let text = t.text();
                if text.starts_with('.') && text.len() > 1 && text.as_bytes()[1] != b'.' {
                    parts.push(format!("(. {})", &text[1..]));
                } else {
                    parts.push(text.to_string());
                }
                last_op = Some(t);
            }
        }
    }
    if last_op.is_some_and(|op| operator_missing_rhs(&op)) {
        parts.push("(error)".to_string());
    }
    sexp("comparison", parts)
}

fn project_assignment(node: &SyntaxNode) -> String {
    // The operator's own text is its JuliaSyntax head verbatim: `=`, `.=`, `+=`,
    // `.+=`, … all project as `(<op> lhs rhs)`.
    let op = operator_token(node);
    let head = match &op {
        Some(t) => t.text().to_string(),
        None => "=".to_string(),
    };
    let operands = child_nodes(node);
    // Missing right operand: `<: =` ⇒ `(= <: (error))`, `a +=` ⇒ `(+= a (error))`.
    if operands.len() == 1 && op.is_some_and(|t| operator_missing_rhs(&t)) {
        return format!("({head} {} (error))", project(&operands[0]));
    }
    sexp(&head, project_each(operands))
}

fn project_unary(node: &SyntaxNode) -> String {
    // Invalid prefix: a binary-only operator used in prefix position is
    // error-wrapped and applied as a prefix call (`/x` ⇒ `(call-pre (error /) x)`).
    // A broadcast operator heads `dotcall-pre` instead (`.*x` ⇒
    // `(dotcall-pre (error (. *)) x)`). The error-wrapped operator (an
    // `OPERATOR_ATOM` inside the `ERROR`) projects to `/` or `(. *)`.
    if let Some(err) = node.children().find(|c| c.kind() == ERROR) {
        let operand = child_nodes(node)
            .iter()
            .find(|c| c.kind() != ERROR)
            .map(project)
            .unwrap_or_default();
        let head = if invalid_prefix_is_dotted(&err) {
            "dotcall-pre"
        } else {
            "call-pre"
        };
        return format!("({head} {} {operand})", project(&err));
    }
    let op = match operator_token(node) {
        Some(t) => t,
        None => return format!("(unsupported {:?})", node.kind()),
    };
    let operand = project_first(node);
    match op.kind() {
        SUBTYPE => format!("(<:-pre {operand})"),
        SUPERTYPE => format!("(>:-pre {operand})"),
        // The address-of `&x` heads the node with the operator itself (a
        // syntactic prefix), not the generic `call-pre`.
        AMP => format!("(& {operand})"),
        DOT_PLUS => format!("(dotcall-pre + {operand})"),
        DOT_MINUS => format!("(dotcall-pre - {operand})"),
        DOT_TILDE => format!("(dotcall-pre ~ {operand})"),
        _ => format!("(call-pre {} {operand})", op.text()),
    }
}

/// Whether the operator error-wrapped inside an invalid-prefix `ERROR` node is a
/// broadcast (dotted) operator (`.*`, `./`, `.<:`) — those head `dotcall-pre`.
/// A leading `..` (the range operator) is not dotted in this sense.
fn invalid_prefix_is_dotted(err: &SyntaxNode) -> bool {
    err.descendants_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| is_operator(t.kind()))
        .map(|t| {
            let b = t.text().as_bytes();
            b.first() == Some(&b'.') && b.len() > 1 && b[1] != b'.'
        })
        .unwrap_or(false)
}

fn project_postfix(node: &SyntaxNode) -> String {
    // `A'` → `(call-post A ')`. The postfix token text is the operator (`'`).
    let operand = project_first(node);
    let op = significant(node)
        .into_iter()
        .filter_map(|el| el.into_token())
        .find(|t| t.kind() == TRANSPOSE)
        .map(|t| t.text().to_string())
        .unwrap_or_else(|| "'".to_string());
    format!("(call-post {operand} {op})")
}

fn project_type_annotation(node: &SyntaxNode) -> String {
    let operands = child_nodes(node);
    match operands.len() {
        2 => format!("(::-i {} {})", project(&operands[0]), project(&operands[1])),
        1 => format!("(::-pre {})", project(&operands[0])),
        _ => project_flat(significant(node)),
    }
}

fn project_where(node: &SyntaxNode) -> String {
    let nodes = child_nodes(node);
    if nodes.is_empty() {
        return "(unsupported WHERE_EXPR)".to_string();
    }
    let mut parts = vec![project(&nodes[0])];
    parts.extend(nodes[1..].iter().map(project));
    sexp("where", parts)
}

// --- Calls / args ----------------------------------------------------------

/// Project a broadcast call `f.(args)` → `(dotcall f args…)`. A broadcast applied
/// to a macro name (`@M.(x)`) is invalid — a macro cannot be broadcast — so
/// JuliaSyntax re-heads it as a macrocall wrapping the dotcall and splices a
/// zero-width `(error-t)` after the name (`@M.(x)` ⇒
/// `(macrocall (dotcall @M (error-t) x))`). The parser records `MacroDotBroadcast`
/// at the broadcast opener.
fn project_dot_call(node: &SyntaxNode) -> String {
    if let Some(callee) = node.children().next()
        && callee.kind() == MACRO_CALL
        && let Some(arg_list) = node.children().find(|c| c.kind() == ARG_LIST)
        && diag_at(
            usize::from(arg_list.text_range().start()),
            DiagnosticKind::MacroDotBroadcast,
        )
    {
        let name = callee
            .children()
            .find(|c| c.kind() == MACRO_NAME)
            .map(|c| project_macro_name(&c))
            .unwrap_or_else(|| "@?".to_string());
        let mut parts = vec![name, "(error-t)".to_string()];
        parts.extend(project_args(&arg_list));
        return format!("(macrocall (dotcall {}))", parts.join(" "));
    }
    project_call("dotcall", node)
}

fn project_call(head: &str, node: &SyntaxNode) -> String {
    let mut parts = Vec::new();
    let mut head = head.to_string();
    // The callee is the first significant element. Usually a node (`f(x)`), but
    // for operator-as-call functions (`*(x)`, `.*(x)`) it is a bare operator
    // token that projects to its function name (`*`, `(. *)`).
    for el in significant(node) {
        match el {
            NodeOrToken::Node(n) => {
                parts.push(project(&n));
                break;
            }
            // The type operators `<:`/`>:` in call syntax (`<:(a, b)` →
            // `(<: a b)`) are syntactic: JuliaSyntax heads the node with the
            // operator itself rather than wrapping it in a `call`. In a `curly`
            // callee (`<:{T}` → `(curly <: T)`) the operator is an ordinary part,
            // so this head override only applies to `call`.
            NodeOrToken::Token(t) if head == "call" && matches!(t.kind(), SUBTYPE | SUPERTYPE) => {
                head = operator_func_repr(t.kind());
                break;
            }
            NodeOrToken::Token(t) if is_operator(t.kind()) => {
                // A suffixed operator callee (`+₁(x)` → `(call +₁ x)`) keeps its
                // suffix, which `operator_func_repr` (keyed on kind) would drop.
                parts.push(if op_has_suffix(t.text()) {
                    match t.text().strip_prefix('.') {
                        Some(rest) if matches!(infix_head(t.kind()), InfixHead::DotCallI(_)) => {
                            format!("(. {rest})")
                        }
                        _ => t.text().to_string(),
                    }
                } else {
                    operator_func_repr(t.kind())
                });
                break;
            }
            NodeOrToken::Token(_) => {}
        }
    }
    if let Some(arg_list) = node.children().find(|c| c.kind() == ARG_LIST) {
        // Disallowed whitespace before the opener records `OpenerWhitespace` at
        // the opener's start (`f (a)` → `(call f (error-t) a)`); the marker sits
        // between the callee and the arguments.
        if diag_at(
            usize::from(arg_list.text_range().start()),
            DiagnosticKind::OpenerWhitespace,
        ) {
            parts.push("(error-t)".to_string());
        }
        // A unary-prefix operator callee with a space before its call-form `(`
        // flags the space as a zero-width `(error)` (`+ (a,b)` → `(call +
        // (error) a b)`), unlike the `(error-t)` of an identifier callee.
        if diag_at(
            usize::from(arg_list.text_range().start()),
            DiagnosticKind::PrefixOpenerWhitespace,
        ) {
            parts.push("(error)".to_string());
        }
        parts.extend(project_args(&arg_list));
    } else if let Some(generator) = node.children().find(|c| c.kind() == GENERATOR) {
        // A bare generator argument: `sum(x for x in xs)` → `(call sum (generator …))`.
        parts.push(project_generator(&generator));
    }
    sexp(&head, parts)
}

/// Project a typed comprehension `T[x for x in xs]` →
/// `(typed_comprehension T (generator x (= x xs)))`. The callee is the first
/// child; the bracketed body and clauses form the `GENERATOR` child.
fn project_typed_comprehension(node: &SyntaxNode) -> String {
    let mut parts = Vec::new();
    if let Some(callee) = node.children().next() {
        parts.push(project(&callee));
    }
    if let Some(generator) = node.children().find(|c| c.kind() == GENERATOR) {
        parts.push(project_generator(&generator));
    }
    sexp("typed_comprehension", parts)
}

/// Project the argument-like direct children of `container` (an `ARG_LIST`,
/// `TUPLE_EXPR`, `VECT_EXPR`, `BRACES`, or `MATRIX_ROW`): unwrap `ARG`, turn
/// `KEYWORD_ARG` into `(= name val)`, `PARAMETERS` into `(parameters …)`, and
/// pass `end` tokens through.
fn project_args(container: &SyntaxNode) -> Vec<String> {
    let mut out = Vec::new();
    for el in significant(container) {
        match el {
            NodeOrToken::Node(n) => match n.kind() {
                ARG => {
                    out.push(project_first(&n));
                    // A misplaced-`end` recovery in a bracket that collapsed to a
                    // `vect` (`[1 end]` ⇒ `(vect 1 (error-t))`) splices a zero-width
                    // marker after the last real element, like its matrix sibling.
                    let end = usize::from(n.text_range().end());
                    for _ in 0..diag_count_at(end, DiagnosticKind::MatrixKeywordRecovery) {
                        out.push("(error-t)".to_string());
                    }
                }
                KEYWORD_ARG => out.push(project_keyword_arg(&n)),
                PARAMETERS => out.push(project_parameters(&n)),
                _ => out.push(project(&n)),
            },
            NodeOrToken::Token(t) => {
                if t.kind() == END_KW {
                    out.push("end".to_string());
                }
            }
        }
    }
    // An argument list with no closing delimiter (`f(a`) records `UnterminatedArgList`
    // at the opener's start, projecting a trailing `(error-t)` (`(call f a (error-t))`).
    if diag_at(
        usize::from(container.text_range().start()),
        DiagnosticKind::UnterminatedArgList,
    ) {
        out.push("(error-t)".to_string());
    }
    out
}

/// Project a `PAREN_BLOCK`'s children as a flat statement list. The block is
/// parsed with the arg-list machinery, so positional statements are `ARG`s,
/// assignments are `KEYWORD_ARG`s, and statements after the first `;` live in a
/// `PARAMETERS` node — all flattened away here, since a block has no parameters
/// (`(a; b; c)` ⇒ `(block-p a b c)`, `(a=1; b=2)` ⇒ `(block-p (= a 1) (= b 2))`).
fn project_block_args(container: &SyntaxNode) -> Vec<String> {
    let mut out = Vec::new();
    let push_stmt = |n: &SyntaxNode, out: &mut Vec<String>| match n.kind() {
        ARG => out.push(project_first(n)),
        KEYWORD_ARG => out.push(project_keyword_arg(n)),
        _ => out.push(project(n)),
    };
    for el in significant(container) {
        if let NodeOrToken::Node(n) = el {
            if n.kind() == PARAMETERS {
                for inner in significant(&n) {
                    if let NodeOrToken::Node(m) = inner {
                        push_stmt(&m, &mut out);
                    }
                }
            } else {
                push_stmt(&n, &mut out);
            }
        }
    }
    out
}

fn project_keyword_arg(node: &SyntaxNode) -> String {
    let nodes = child_nodes(node);
    match nodes.as_slice() {
        [name, value] => format!("(= {} {})", project(name), project(value)),
        [name] => format!("(= {})", project(name)),
        _ => project_flat(significant(node)),
    }
}

fn project_parameters(node: &SyntaxNode) -> String {
    let mut out = Vec::new();
    for el in significant(node) {
        if let NodeOrToken::Node(n) = el {
            match n.kind() {
                ARG => out.push(project_first(&n)),
                KEYWORD_ARG => out.push(project_keyword_arg(&n)),
                _ => out.push(project(&n)),
            }
        }
    }
    sexp("parameters", out)
}

// --- Matrices --------------------------------------------------------------

/// Project the outer `[...]` concatenation node. The CST nests rows by
/// dimension (`MATRIX_ROW` groups, bare `ARG` elements); the dimension `d` of a
/// group is recovered from the separator tokens between its direct child nodes:
/// a run of `;` contributes its length, a row-separating newline contributes 1,
/// a space contributes 0. The top head is `hcat`/`vcat`/`ncat-d`
/// (`d` = 0/1/≥2); nested groups are `row` (`d` = 0) or `nrow-d` (`d` ≥ 1),
/// mirroring JuliaSyntax. An element-free `[; …]` is `ncat-d` with `d` the
/// longest semicolon run.
fn project_matrix(node: &SyntaxNode) -> String {
    let (head, children) = matrix_head_and_children(node);
    sexp(&head, children)
}

/// The bracket-concatenation head (`hcat`/`vcat`/`ncat-d`, or `ncat-d` for an
/// element-free `[; …]`) and the projected child s-expressions. Shared by the
/// plain (`[...]`) and typed (`T[...]`) concatenation projectors.
fn matrix_head_and_children(node: &SyntaxNode) -> (String, Vec<String>) {
    let children: Vec<SyntaxNode> = node
        .children()
        .filter(|c| matches!(c.kind(), ARG | MATRIX_ROW))
        .collect();
    if children.is_empty() {
        return (format!("ncat-{}", max_semicolon_run(node)), Vec::new());
    }
    let d = group_dimension(node);
    let head = match d {
        0 => "hcat".to_string(),
        1 => "vcat".to_string(),
        _ => format!("ncat-{d}"),
    };
    (head, project_cat_children(&children))
}

/// Project a sequence of concatenation children (`ARG` elements and `MATRIX_ROW`
/// groups), splicing a zero-width `(error-t)` after any bare element whose end
/// carries an `ArraySeparatorMismatch` diagnostic — a space/`;;` separator-order
/// conflict (`[a b ;; c]` ⇒ `(ncat-2 (row a b (error-t)) c)`). The marker is only
/// appended after `ARG` elements: when the offending element is the last in a
/// row, the diagnostic's byte anchor also coincides with the enclosing
/// `MATRIX_ROW`'s end, and the recursion handles it inside that row instead.
fn project_cat_children(children: &[SyntaxNode]) -> Vec<String> {
    let mut out = Vec::new();
    for child in children {
        out.push(project_cat_child(child));
        if child.kind() == ARG {
            let end = usize::from(child.text_range().end());
            for _ in 0..diag_count_at(end, DiagnosticKind::ArraySeparatorMismatch) {
                out.push("(error-t)".to_string());
            }
            for _ in 0..diag_count_at(end, DiagnosticKind::MatrixKeywordRecovery) {
                out.push("(error-t)".to_string());
            }
        }
    }
    out
}

/// Project a typed concatenation `T[...]`: the same shape as the inner
/// `MATRIX_EXPR`, but with the type expression prepended and the head prefixed
/// `typed_` (`T[x y]` → `(typed_hcat T x y)`, `T[;]` → `(typed_ncat-1 T)`).
fn project_typed_matrix(node: &SyntaxNode) -> String {
    let mut children = node.children();
    let type_node = children.next().expect("typed concat has a type child");
    let matrix = children
        .find(|c| c.kind() == MATRIX_EXPR)
        .expect("typed concat has a matrix body");
    let (head, mut args) = matrix_head_and_children(&matrix);
    args.insert(0, project(&type_node));
    sexp(&format!("typed_{head}"), args)
}

/// Project a brace concatenation `{...}`: always headed `bracescat`. A
/// dimension-1 (vcat-like) layout keeps its children directly; a horizontal or
/// higher-dimensional layout (and the element-free `{; …}` form) becomes a
/// single nested `row`/`nrow-d` child, since `bracescat` is itself the
/// dimension-1 container (`{x y}` → `(bracescat (row x y))`, `{a;b}` →
/// `(bracescat a b)`, `{;;}` → `(bracescat (nrow-2))`).
fn project_bracescat(node: &SyntaxNode) -> String {
    let children: Vec<SyntaxNode> = node
        .children()
        .filter(|c| matches!(c.kind(), ARG | MATRIX_ROW))
        .collect();
    if children.is_empty() {
        let d = max_semicolon_run(node);
        return sexp("bracescat", vec![sexp(&format!("nrow-{d}"), Vec::new())]);
    }
    let d = group_dimension(node);
    let items: Vec<String> = children.iter().map(project_cat_child).collect();
    match d {
        1 => sexp("bracescat", items),
        0 => sexp("bracescat", vec![sexp("row", items)]),
        _ => sexp("bracescat", vec![sexp(&format!("nrow-{d}"), items)]),
    }
}

/// Project one direct child of a concatenation group: a bare `ARG` element
/// unwraps to its inner expression; a `MATRIX_ROW` becomes a `row` (dimension 0)
/// or `nrow-d` group, recursing on its own children.
fn project_cat_child(node: &SyntaxNode) -> String {
    match node.kind() {
        ARG => project_first(node),
        MATRIX_ROW => {
            let d = group_dimension(node);
            let head = if d == 0 {
                "row".to_string()
            } else {
                format!("nrow-{d}")
            };
            let items: Vec<SyntaxNode> = node
                .children()
                .filter(|c| matches!(c.kind(), ARG | MATRIX_ROW))
                .collect();
            sexp(&head, project_cat_children(&items))
        }
        _ => project(node),
    }
}

/// The concatenation dimension of a group: the largest separator dimension among
/// the runs lying *between* direct child nodes (a `;` run counts its length, a
/// newline counts 1), plus any trailing semicolon run after the last child
/// (a trailing newline does not separate, so it is ignored).
///
/// A `;;` immediately followed by a newline (`;; \n`) inside a row-major group —
/// one where a plain space separator was already seen — is a *line continuation*
/// that JuliaSyntax folds into the row, so it counts as dimension 0 rather than
/// 2 (`[a b ;; \n c]` ⇒ `(hcat a b c)`). We re-derive the row-major order locally
/// the same way JuliaSyntax does (first space ⇒ row-major).
fn group_dimension(node: &SyntaxNode) -> usize {
    let mut d = 0;
    let mut seen_node = false;
    let mut row_major = false;
    let mut semis = 0usize;
    let mut newline = false;
    let mut newline_after_semis = false;
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Node(n) if matches!(n.kind(), ARG | MATRIX_ROW) => {
                if seen_node {
                    let is_space = semis == 0 && !newline;
                    let continuation = semis == 2 && newline_after_semis && row_major;
                    let run = if continuation {
                        0
                    } else if semis > 0 {
                        semis
                    } else {
                        usize::from(newline)
                    };
                    if is_space {
                        row_major = true;
                    }
                    d = d.max(run);
                }
                seen_node = true;
                semis = 0;
                newline = false;
                newline_after_semis = false;
            }
            NodeOrToken::Token(t) => match t.kind() {
                SEMICOLON => {
                    semis += 1;
                    newline_after_semis = false;
                }
                NEWLINE => {
                    newline = true;
                    if semis > 0 {
                        newline_after_semis = true;
                    }
                }
                _ => {}
            },
            _ => {}
        }
    }
    // Trailing run after the last child: only semicolons separate (`[x;]` is a
    // `vcat`, but `[x\n]` is just a `vect`).
    if seen_node && semis > 0 {
        d = d.max(semis);
    }
    d
}

/// The length of the longest consecutive `;` run among a node's direct tokens
/// (used for element-free `[; …]` concatenations).
fn max_semicolon_run(node: &SyntaxNode) -> usize {
    let mut max = 0;
    let mut run = 0;
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) if t.kind() == SEMICOLON => {
                run += 1;
                max = max.max(run);
            }
            NodeOrToken::Token(t) if is_trivia(t.kind()) => {}
            _ => run = 0,
        }
    }
    max
}

// --- Comprehensions / generators -------------------------------------------

fn project_generator(node: &SyntaxNode) -> String {
    // Fatou is flat: `body (FOR_BINDING [COMPREHENSION_IF])…`. JuliaSyntax nests
    // `(generator body <clause>…)`, where each `for` clause is one `(= v it)` (or
    // a `(cartesian_iterator …)` for comma-separated specs), and a trailing `if`
    // wraps the immediately preceding clause in a `(filter <clause> cond)`.
    let mut body = String::new();
    // `for` glued to the body splices a zero-width `(error-t)` marker between
    // the body and the first clause (`[(x)for x in xs]` → `(generator x
    // (error-t) (= x xs))`); keep clauses and markers in source order.
    let mut clauses: Vec<String> = Vec::new();
    for child in node.children() {
        match child.kind() {
            FOR_BINDING => {
                // A `for` glued to the body records `GluedFor` at the `for`'s
                // start, projecting `(error-t)` before the first clause.
                if diag_at(
                    usize::from(child.text_range().start()),
                    DiagnosticKind::GluedFor,
                ) {
                    clauses.push("(error-t)".to_string());
                }
                clauses.push(project_for_binding_node(&child));
            }
            COMPREHENSION_IF => {
                if let (Some(cond), Some(last)) = (first_node(&child), clauses.last().cloned()) {
                    let n = clauses.len();
                    clauses[n - 1] = format!("(filter {last} {})", project(&cond));
                }
            }
            _ if body.is_empty() => body = project(&child),
            _ => {}
        }
    }
    let mut parts = vec![body];
    parts.extend(clauses);
    sexp("generator", parts)
}

// --- For binding -----------------------------------------------------------

fn project_for_binding(node: &SyntaxNode) -> String {
    match node.children().find(|c| c.kind() == FOR_BINDING) {
        Some(binding) => project_for_binding_node(&binding),
        None => "(unsupported FOR_BINDING)".to_string(),
    }
}

fn project_for_binding_node(binding: &SyntaxNode) -> String {
    // Split the clause's specs on top-level commas (kept as tokens). One spec
    // projects directly; several become a `(cartesian_iterator …)`.
    let mut specs: Vec<Vec<SyntaxElement>> = vec![Vec::new()];
    for el in binding.children_with_tokens() {
        match &el {
            NodeOrToken::Token(t) if t.kind() == COMMA => specs.push(Vec::new()),
            NodeOrToken::Token(t) if is_drop_token(t.kind()) => {}
            _ => specs.last_mut().expect("non-empty").push(el),
        }
    }
    let projected: Vec<String> = specs.iter().map(|s| project_for_spec(s)).collect();
    match projected.as_slice() {
        [one] => one.clone(),
        _ => sexp("cartesian_iterator", projected),
    }
}

fn project_for_spec(elems: &[SyntaxElement]) -> String {
    // `j = 1:3` keeps a proper ASSIGNMENT_EXPR; `i in xs` keeps the iterator as
    // loose passthrough tokens after an `in` keyword-identifier.
    if let [NodeOrToken::Node(n)] = elems
        && n.kind() == ASSIGNMENT_EXPR
    {
        return project(n);
    }
    // Otherwise the loose `var in iter` form: split on the `in`/`∈` token.
    let split = elems
        .iter()
        .position(|el| matches!(el, NodeOrToken::Token(t) if t.text() == "in" || t.text() == ""));
    match split {
        Some(idx) => {
            let var = project_flat(elems[..idx].to_vec());
            let iter = project_flat(elems[idx + 1..].to_vec());
            format!("(= {var} {iter})")
        }
        None => project_flat(elems.to_vec()),
    }
}

// --- Control flow ----------------------------------------------------------

/// The condition slot of an `if`/`elseif` whose `CONDITION` node is absent. An
/// empty condition (`if end`, `if; end`) is recovery: JuliaSyntax synthesizes a
/// zero-width `(error)`, recorded here as a `MissingCondition` diagnostic anchored
/// at the opening keyword. Without the diagnostic the slot stays empty (defensive;
/// the parser always records one when the condition is missing).
fn missing_condition(node: &SyntaxNode) -> String {
    if diag_count_from(keyword_start(node), DiagnosticKind::MissingCondition) > 0 {
        "(error)".to_string()
    } else {
        String::new()
    }
}

fn project_if(node: &SyntaxNode) -> String {
    let cond = node
        .children()
        .find(|c| c.kind() == CONDITION)
        .map(|c| project(&c))
        .unwrap_or_else(|| missing_condition(node));
    let then_block = node
        .children()
        .find(|c| c.kind() == BLOCK)
        .map(|c| project(&c))
        .unwrap_or_else(|| "(block)".to_string());
    let clauses: Vec<SyntaxNode> = node
        .children()
        .filter(|c| matches!(c.kind(), ELSEIF_CLAUSE | ELSE_CLAUSE))
        .collect();
    let mut parts = vec![cond, then_block];
    // A trailing-junk `ERROR` glued after the then-block is a sibling of the
    // block inside the `if` (`if c\n x y\n end` ⇒ `(if c (block x) (error-t y))`).
    if let Some(err) = node.children().find(|c| c.kind() == ERROR) {
        parts.push(project(&err));
    }
    // `else if` recovery: a zero-width `(error-t)` for the missing else block
    // sits between the then-block and the recovered `elseif` clause.
    for _ in 0..diag_count_from(keyword_start(node), DiagnosticKind::ElseIf) {
        parts.push("(error-t)".to_string());
    }
    if let Some(tail) = project_if_tail(&clauses) {
        parts.push(tail);
    }
    push_trailing_errors(node, &mut parts);
    sexp("if", parts)
}

fn project_if_tail(clauses: &[SyntaxNode]) -> Option<String> {
    let first = clauses.first()?;
    match first.kind() {
        ELSE_CLAUSE => first
            .children()
            .find(|c| c.kind() == BLOCK)
            .map(|c| project(&c)),
        ELSEIF_CLAUSE => {
            let cond = first
                .children()
                .find(|c| c.kind() == CONDITION)
                .map(|c| project(&c))
                .unwrap_or_else(|| missing_condition(first));
            let block = first
                .children()
                .find(|c| c.kind() == BLOCK)
                .map(|c| project(&c))
                .unwrap_or_else(|| "(block)".to_string());
            let mut parts = vec![cond, block];
            if let Some(tail) = project_if_tail(&clauses[1..]) {
                parts.push(tail);
            }
            Some(sexp("elseif", parts))
        }
        _ => None,
    }
}

fn project_try(node: &SyntaxNode) -> String {
    let mut parts = Vec::new();
    parts.push(project_block_child(node));
    for clause in node.children() {
        match clause.kind() {
            CATCH_CLAUSE => {
                // The catch-variable is the first child node before the body
                // block; it may be a plain NAME, a `$`-interpolation, or a
                // `var"…"` non-standard identifier. Absent ⇒ `false`. A
                // non-identifier variable (`catch e+3`) is flagged invalid by
                // the post-build walk and error-wrapped here.
                let var = clause
                    .children()
                    .find(|c| c.kind() != BLOCK)
                    .map(|c| {
                        let projected = project(&c);
                        if diag_at(
                            usize::from(c.text_range().start()),
                            DiagnosticKind::CatchVarNotIdentifier,
                        ) {
                            format!("(error {projected})")
                        } else {
                            projected
                        }
                    })
                    .unwrap_or_else(|| "false".to_string());
                let block = project_block_child(&clause);
                parts.push(format!("(catch {var} {block})"));
            }
            FINALLY_CLAUSE => parts.push(format!("(finally {})", project_block_child(&clause))),
            ELSE_CLAUSE => {
                // `else` without a preceding `catch` is error-recovery: the else
                // block is wrapped in an `(error …)` node in the CST.
                if let Some(err) = clause.children().find(|c| c.kind() == ERROR) {
                    parts.push(format!("(else {})", project(&err)));
                } else {
                    parts.push(format!("(else {})", project_block_child(&clause)));
                }
            }
            _ => {}
        }
    }
    // Truncation markers land in document order: a missing `catch`/`finally`
    // handler then a missing `end` (`try x` ⇒ `(try (block x) (error-t) (error-t))`).
    // Both diagnostics anchor at the `try` keyword.
    let kw = keyword_start(node);
    for _ in 0..diag_count_from(kw, DiagnosticKind::MissingTryHandler) {
        parts.push("(error-t)".to_string());
    }
    for _ in 0..diag_count_from(kw, DiagnosticKind::MissingEnd) {
        parts.push("(error-t)".to_string());
    }
    sexp("try", parts)
}

fn project_struct(node: &SyntaxNode) -> String {
    let mutable = node
        .children_with_tokens()
        .any(|el| el.kind() == MUTABLE_KW);
    let head = if mutable { "struct-mut" } else { "struct" };
    let mut parts = vec![project_signature(node), project_block_child(node)];
    push_trailing_errors(node, &mut parts);
    sexp(head, parts)
}

fn project_primitive(node: &SyntaxNode) -> String {
    // `(primitive <spec> <bits>)`: the spec is the `SIGNATURE` child, the bit
    // size is the sibling expression node that follows it.
    let spec = project_signature(node);
    let size = node
        .children()
        .find(|c| c.kind() != SIGNATURE)
        .map(|c| project(&c))
        .unwrap_or_default();
    sexp("primitive", vec![spec, size])
}

fn project_module(node: &SyntaxNode) -> String {
    let bare = node
        .children_with_tokens()
        .any(|el| el.kind() == BAREMODULE_KW);
    let head = if bare { "module-bare" } else { "module" };
    let mut parts = vec![project_signature(node), project_block_child(node)];
    push_trailing_errors(node, &mut parts);
    sexp(head, parts)
}

fn project_quote_sym(node: &SyntaxNode) -> String {
    // `:foo`/`:(expr)` → `(quote-: …)`. The quoted form is the first significant
    // child after the `:` — a `NAME`/paren node, or a bare keyword token.
    // Whitespace between the `:` and the symbol records a `QuoteColonWhitespace`
    // diagnostic at the `:`'s end and projects a leading `(error-t)`
    // (`: foo` ⇒ `(quote-: (error-t) foo)`).
    let mut prefix = "";
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Node(n) => return format!("(quote-: {prefix}{})", project(&n)),
            NodeOrToken::Token(t) if t.kind() == COLON => {
                if diag_at(
                    usize::from(t.text_range().end()),
                    DiagnosticKind::QuoteColonWhitespace,
                ) {
                    prefix = "(error-t) ";
                }
            }
            // `:(end)`/`:(else)`/`:(catch)` — a quote-paren whose body can't start
            // an expression. The `(` is a loose child carrying an
            // `EmptyQuoteParen` diagnostic; the quoted form is a zero-width
            // `(error-t)` (the keyword spilled to the trailing-junk driver).
            NodeOrToken::Token(t)
                if t.kind() == LPAREN
                    && diag_at(
                        usize::from(t.text_range().end()),
                        DiagnosticKind::EmptyQuoteParen,
                    ) =>
            {
                return format!("(quote-: {prefix}(error-t))");
            }
            NodeOrToken::Token(t) if is_trivia(t.kind()) => continue,
            NodeOrToken::Token(t) => return format!("(quote-: {prefix}{})", t.text()),
        }
    }
    "(quote-:)".to_string()
}

fn project_let(node: &SyntaxNode) -> String {
    let bindings = match node.children().find(|c| c.kind() == LET_BINDINGS) {
        Some(b) => sexp("block", project_let_bindings(&b)),
        None => "(block)".to_string(),
    };
    let mut parts = vec![bindings, project_block_child(node)];
    push_trailing_errors(node, &mut parts);
    sexp("let", parts)
}

fn project_let_bindings(node: &SyntaxNode) -> Vec<String> {
    // Each comma-separated binding is its own expression node (an
    // `ASSIGNMENT_EXPR`, a bare name, a destructuring tuple, …); the `,`
    // separators are loose tokens. Project each binding node in order.
    node.children().map(|c| project(&c)).collect()
}

fn project_do(node: &SyntaxNode) -> String {
    let call = node
        .children()
        .next()
        .map(|c| project(&c))
        .unwrap_or_default();
    let params = match node.children().find(|c| c.kind() == DO_PARAMS) {
        Some(p) => sexp("tuple", do_param_strings(&p)),
        None => "(tuple)".to_string(),
    };
    let block = project_block_child(node);
    let mut parts = vec![call, params, block];
    push_trailing_errors(node, &mut parts);
    sexp("do", parts)
}

fn do_param_strings(node: &SyntaxNode) -> Vec<String> {
    significant(node)
        .into_iter()
        .filter_map(|el| match el {
            NodeOrToken::Node(n) => Some(project(&n)),
            NodeOrToken::Token(t) if t.kind() == IDENT => Some(t.text().to_string()),
            NodeOrToken::Token(_) => None,
        })
        .collect()
}

// --- Statements / declarations ---------------------------------------------

fn project_keyword_stmt(head: &str, node: &SyntaxNode) -> String {
    match first_node(node) {
        Some(inner) => format!("({head} {})", project(&inner)),
        None => format!("({head})"),
    }
}

fn project_decl(head: &str, node: &SyntaxNode) -> String {
    // A `global`/`local`/`const` whose body is a single bare comma list parses to
    // one `BARE_TUPLE_EXPR`; JuliaSyntax splices its elements directly into the
    // declaration (`global a, b` ⇒ `(global a b)`, not `(global (tuple a b))`). An
    // assignment (`global a, b = 1, 2` ⇒ `(global (= (tuple a b) (tuple 1 2)))`)
    // or a parenthesized tuple (`global (a, b)` ⇒ `(global (tuple-p a b))`) stays
    // one child.
    let nodes = child_nodes(node);
    if let [only] = nodes.as_slice()
        && only.kind() == BARE_TUPLE_EXPR
    {
        return sexp(head, project_each(child_nodes(only)));
    }
    // `const x = 1` / `local y = 2` wrap a single assignment; `global a, b`
    // carries a bare name list. Both fall out of collecting every operand.
    let items: Vec<String> = significant(node)
        .into_iter()
        .filter_map(|el| match el {
            NodeOrToken::Node(n) => Some(project(&n)),
            NodeOrToken::Token(t) if t.kind() == IDENT => Some(t.text().to_string()),
            NodeOrToken::Token(_) => None,
        })
        .collect();
    sexp(head, items)
}

fn project_export(node: &SyntaxNode) -> String {
    // Each item is a bare name (`export x`), an interpolation/macro name, or a
    // parenthesized item. A parenthesized item that wraps a single symbol is
    // unwrapped by `project` (`(x)` ⇒ `x`, `(+)` ⇒ `+`); anything else is flagged
    // `InvalidExportItem` by the post-build walk and error-wrapped here (`(x::T)`
    // ⇒ `(error (::-i x T))`, `(x, y)` ⇒ `(error (tuple-p x y))`).
    let items: Vec<String> = significant(node)
        .into_iter()
        .filter_map(|el| match el {
            NodeOrToken::Node(n) if matches!(n.kind(), PAREN_EXPR | TUPLE_EXPR) => {
                let projected = project(&n);
                if diag_at(
                    usize::from(n.text_range().start()),
                    DiagnosticKind::InvalidExportItem,
                ) {
                    Some(format!("(error {projected})"))
                } else {
                    Some(projected)
                }
            }
            _ => name_run_item(el),
        })
        .collect();
    sexp("export", items)
}

fn project_public(node: &SyntaxNode) -> String {
    // The leading `public` contextual keyword is a plain identifier token in the
    // CST (it stays an identifier elsewhere), so drop the first element before
    // reading the name list exactly like `export`. A name may itself be a
    // contextual keyword (`public export`), which `significant` would drop, so
    // filter only trivia and the comma separators here.
    let items: Vec<String> = node
        .children_with_tokens()
        .filter(|el| match el {
            NodeOrToken::Node(_) => true,
            NodeOrToken::Token(t) => !is_trivia(t.kind()) && t.kind() != COMMA,
        })
        .skip(1)
        .filter_map(name_run_item)
        .collect();
    sexp("public", items)
}

fn project_import(head: &str, node: &SyntaxNode) -> String {
    // `import A` / `using A.B` / `import A: b, c as d`. The path tree is built by
    // the parser: each clause is an `IMPORT_PATH` or `IMPORT_ALIAS` node, and a
    // top-level `:` token (when present) splits the base path from the list of
    // imported names. Read those nodes directly.
    // The base/names `:` split is the *first* separator after the base path; a
    // `:` after a comma is recovery (no grouping), e.g. `import A, B: y` ⇒
    // `(import (importpath A) (importpath B) (error-t (importpath y)))`.
    let mut first_sep: Option<SyntaxKind> = None;
    let mut clauses: Vec<String> = Vec::new();
    for el in node.children_with_tokens() {
        match el {
            // `ERROR` wraps an invalid `as` rename (`using A as B`, projected
            // `(error …)`) or a clause after a recovery `:` (`import A, B: y`,
            // projected `(error-t …)` via the `ImportRecoveryColon` diagnostic).
            NodeOrToken::Node(n) if matches!(n.kind(), IMPORT_PATH | IMPORT_ALIAS | ERROR) => {
                clauses.push(project(&n));
            }
            NodeOrToken::Token(t)
                if matches!(t.kind(), COLON | COMMA)
                    && !clauses.is_empty()
                    && first_sep.is_none() =>
            {
                first_sep = Some(t.kind());
            }
            _ => {}
        }
    }

    if first_sep == Some(COLON) && !clauses.is_empty() {
        // `(: <base> <name> …)` — the first clause is the base path.
        format!("({head} {})", sexp(":", clauses))
    } else {
        format!("({head} {})", clauses.join(" "))
    }
}

/// `(importpath . . A B)` — leading relative dots (each `.`/`..`/`...` token
/// expands to one dot per character) followed by the dotted name components. The
/// dots that *separate* name components carry no meaning in JuliaSyntax's shape,
/// so only the leading dots (before the first name) are emitted.
fn project_import_path(node: &SyntaxNode) -> String {
    let mut parts: Vec<String> = Vec::new();
    let mut seen_name = false;
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) => match t.kind() {
                DOT if !seen_name => parts.push(".".to_string()),
                DOT_DOT if !seen_name => parts.extend([".".to_string(), ".".to_string()]),
                DOT_DOT_DOT if !seen_name => {
                    parts.extend([".".to_string(), ".".to_string(), ".".to_string()])
                }
                IDENT => {
                    parts.push(t.text().to_string());
                    seen_name = true;
                }
                // After a name, a `...` is a separator dot fused with the `..`
                // range operator as a component (`import A...` → `(importpath A ..)`).
                DOT_DOT_DOT if seen_name => parts.push("..".to_string()),
                // Separator dots/colons between components carry no meaning here.
                DOT | DOT_DOT | DOT_DOT_DOT | COLON => {}
                // An operator-symbol name component (`import A.==`, `import A: +`).
                // A fused dotted operator (`.==`, `.⋆`) carries a leading `.`. After
                // a name it is a separator we strip (`import A.==` → `… ==`); before
                // any name it is a *relative-import* dot emitted on its own (`import
                // .==` → `(importpath . ==)`), so keep one `.` part in that case.
                k if is_operator(k) => {
                    if !seen_name && t.text().starts_with('.') {
                        parts.push(".".to_string());
                    }
                    parts.push(t.text().trim_start_matches('.').to_string());
                    seen_name = true;
                }
                _ => {}
            },
            NodeOrToken::Node(n) if n.kind() == NAME => {
                parts.push(name_text(&n));
                seen_name = true;
            }
            // A quoted operator symbol component (`import A.:+` → `(quote-: +)`).
            NodeOrToken::Node(n) if n.kind() == QUOTE_SYM => {
                parts.push(project_quote_sym(&n));
                seen_name = true;
            }
            // A parenthesized quoted symbol (`import A.(:+)` → `(quote-: +)`); the
            // paren unwraps to its inner quote.
            NodeOrToken::Node(n) if n.kind() == PAREN_EXPR => {
                parts.push(project(&n));
                seen_name = true;
            }
            // An interpolated path root (`import $A` → `($ A)`).
            NodeOrToken::Node(n) if n.kind() == INTERPOLATION => {
                parts.push(project(&n));
                seen_name = true;
            }
            // A macro-name component (`import A.@x` → `(importpath A @x)`).
            NodeOrToken::Node(n) if n.kind() == MACRO_NAME => {
                parts.push(project_macro_name(&n));
                seen_name = true;
            }
            _ => {}
        }
    }
    sexp("importpath", parts)
}

/// `(as (importpath …) <name>)` — an `as` rename wrapping an import path.
fn project_import_alias(node: &SyntaxNode) -> String {
    let path = node
        .children()
        .find(|c| c.kind() == IMPORT_PATH)
        .map(|c| project_import_path(&c))
        .unwrap_or_default();
    // The alias is the bare identifier after the `as` keyword (the path's own
    // identifiers are nested inside the `IMPORT_PATH` child, not direct tokens).
    let alias = node
        .children_with_tokens()
        .filter_map(|el| match el {
            NodeOrToken::Token(t) if t.kind() == IDENT && t.text() != "as" => {
                Some(t.text().to_string())
            }
            _ => None,
        })
        .last()
        .unwrap_or_default();
    format!("(as {path} {alias})")
}

// --- Macros ----------------------------------------------------------------

fn project_macrocall(node: &SyntaxNode) -> String {
    let name = node
        .children()
        .find(|c| c.kind() == MACRO_NAME)
        .map(|c| project_macro_name(&c))
        .unwrap_or_else(|| "@?".to_string());

    // Paren form `@m(…)` carries a direct `ARG_LIST` child → `macrocall-p`;
    // the space form `@m a b` carries bare argument nodes → `macrocall`.
    if let Some(arg_list) = node.children().find(|c| c.kind() == ARG_LIST) {
        let mut parts = vec![name];
        parts.extend(project_args(&arg_list));
        return sexp("macrocall-p", parts);
    }
    let mut parts = vec![name];
    parts.extend(
        node.children()
            .filter(|c| c.kind() != MACRO_NAME)
            .map(|c| project(&c)),
    );
    sexp("macrocall", parts)
}

fn project_macro_name(node: &SyntaxNode) -> String {
    // An invalid bracketed macro name (`@[x]` ⇒ `(error (vect x))`, `@{x}` ⇒
    // `(error (braces x))`): the parser parses the bracketed expression as a child
    // node and records `InvalidMacroName` at its start. Error-wrap its projection.
    if let Some(child) = node.children().next()
        && diag_at(
            usize::from(child.text_range().start()),
            DiagnosticKind::InvalidMacroName,
        )
    {
        return format!("(error {})", project(&child));
    }

    // A `var"…"` non-standard identifier name (`@var"#"` ⇒ `(var @#)`): the `@`
    // sigil prefixes the identifier content. JuliaSyntax folds the `@` into the
    // `var` name itself rather than wrapping it as a separate macro-name token.
    let var_name = node
        .children()
        .find(|c| c.kind() == NONSTANDARD_IDENTIFIER)
        .map(|c| format!("(var @{})", raw_content(&c)));

    // Trailing form (`A.@x`, `A.B.@x`, `$A.@x`, `A.$B.@x`): the module path is a
    // single leading node (`NAME`, `BINARY_EXPR`, or `INTERPOLATION`) that
    // `project` already nests correctly, then `. @ name`. Reuse `project` for the
    // module so dotted access and interpolation splits (`(inert ($ B))`) stay
    // consistent with plain field access.
    if let Some(module) = node
        .children()
        .find(|c| matches!(c.kind(), NAME | BINARY_EXPR | INTERPOLATION))
    {
        // Misplaced macro sigil (`A.@B.x`): the `@` names a non-final component
        // with a trailing `.ident` continuation (recorded as `MacroSigilTrailing`).
        // JuliaSyntax relocates the sigil to the final component and splices a
        // zero-width `(error-t)` before it: `A.@B.x` ⇒
        // `(. (. A (quote B)) (error-t) (quote @x))`.
        if var_name.is_none()
            && let Some(at_start) = node
                .children_with_tokens()
                .filter_map(|el| el.into_token())
                .find(|t| t.kind() == AT)
                .map(|t| usize::from(t.text_range().start()))
            && diag_at(at_start, DiagnosticKind::MacroSigilTrailing)
        {
            let mut after_at = false;
            let comps: Vec<String> = node
                .children_with_tokens()
                .filter_map(|el| el.into_token())
                .filter_map(|t| {
                    if t.kind() == AT {
                        after_at = true;
                        return None;
                    }
                    if after_at && (t.kind() == IDENT || is_macro_name_part_token(t.kind())) {
                        Some(t.text().to_string())
                    } else {
                        None
                    }
                })
                .collect();
            // The first component after the module is the one the sigil named
            // (plain `(quote B)`); every later step gets an `(error-t)`, and the
            // final one also carries the relocated `@`.
            if let [first, rest @ ..] = comps.as_slice() {
                let mut path = format!("(. {} (quote {first}))", project(&module));
                if let Some((last, mids)) = rest.split_last() {
                    for c in mids {
                        path = format!("(. {path} (error-t) (quote {c}))");
                    }
                    path = format!("(. {path} (error-t) (quote @{last}))");
                }
                return path;
            }
        }
        let name = match &var_name {
            Some(v) => v.clone(),
            None => format!("@{}", macro_name_after_at(node)),
        };
        return format!("(. {} (quote {name}))", project(&module));
    }

    // Prefix form with a `var"…"` name and no module (`@var"#"` ⇒ `(var @#)`).
    if let Some(v) = var_name {
        return v;
    }

    // Prefix form with a `$`/inner-`@` dotted path (`@A.$x`, `@A.$x.y`,
    // `@A.B.@x`): JuliaSyntax builds the dotted module path and relocates the
    // macro sigil onto the final component. A `$` final or any inner/extra `@`
    // is recovered with zero-width `(error-t)`/`(error …)` (recorded as
    // `MacroSigilLeading`); a non-final `$` is a *valid* interpolation
    // (`@A.$x.y`), so the extended path is entered on structure (a `.$`/`.@`
    // step), not the diagnostic alone. Everything is derived from the lossless
    // token sequence; the diagnostic only gates the error pieces.
    let sig_toks: Vec<SyntaxToken> = node
        .children_with_tokens()
        .filter_map(|el| el.into_token())
        .filter(|t| matches!(t.kind(), IDENT | DOT | DOLLAR | AT))
        .collect();
    if sig_toks
        .windows(2)
        .any(|w| w[0].kind() == DOT && matches!(w[1].kind(), DOLLAR | AT))
    {
        return project_leading_macro_path(&sig_toks);
    }

    // Prefix form (`@m`, `@A.x`, `@A.B.x`): a flat run of component tokens after
    // the `@`. The last component is the macro name; the rest form the module
    // path, nested left-to-right the same way field access is.
    let comps: Vec<String> = node
        .children_with_tokens()
        .filter_map(|el| match el {
            NodeOrToken::Token(t) if t.kind() == IDENT => Some(t.text().to_string()),
            // An operator, `$`, or keyword name token (`@+`, `@$`, `@end`). The
            // qualifying `.` and broadcast `@.` dot are excluded so they don't
            // count as a name component.
            NodeOrToken::Token(t) if is_macro_name_part_token(t.kind()) => {
                Some(t.text().to_string())
            }
            _ => None,
        })
        .collect();

    match comps.as_slice() {
        // `@.` — broadcast macro: `@` then the lone broadcast dot, no ident.
        [] => "@.".to_string(),
        // Simple `@m`.
        [one] => format!("@{one}"),
        // Qualified `@Mod.mac` / `@A.B.x` → `(. <module> (quote @macro))`.
        rest => {
            let (macro_name, module) = rest.split_last().unwrap();
            let mut path = module[0].clone();
            for c in &module[1..] {
                path = format!("(. {path} (quote {c}))");
            }
            format!("(. {path} (quote @{macro_name}))")
        }
    }
}

/// Project a leading-`@` qualified macro name whose dotted path carries a `$`
/// interpolation or an inner/extra `@` sigil (`sig_toks` is the name's flat run
/// of significant tokens `@`, `IDENT`, `.`, `$`/`@`, …). JuliaSyntax builds the
/// module path left-to-right and relocates the macro sigil onto the final
/// component:
/// - a non-final ident is `(quote B)`, the final ident `(quote @x)` (the sigil);
/// - a non-final `$x` is the valid `(inert ($ x))`, a final `$x` the recovered
///   `(inert (error x))` (an interpolation cannot take the sigil);
/// - any `@x` component is a misplaced/extra sigil: inner ⇒ `(quote (error-t) B)`,
///   final ⇒ `(quote (error-t) @x)`, and an inner `@` also splices a zero-width
///   `(error-t)` into the final dot step.
///
/// The error pieces are gated on the recorded `MacroSigilLeading` diagnostic; a
/// fully valid path (`@A.$x.y`) carries none and renders without recovery.
fn project_leading_macro_path(sig_toks: &[SyntaxToken]) -> String {
    let at_start = usize::from(sig_toks[0].text_range().start());
    let invalid = diag_at(at_start, DiagnosticKind::MacroSigilLeading);

    // Walk the dotted components after the leading `@` sigil (`sig_toks[0]`).
    let mut comps: Vec<(SyntaxKind, String)> = Vec::new();
    let mut i = 1;
    if sig_toks.get(i).map(|t| t.kind()) == Some(IDENT) {
        comps.push((IDENT, sig_toks[i].text().to_string()));
        i += 1;
    }
    while sig_toks.get(i).map(|t| t.kind()) == Some(DOT) {
        i += 1; // `.`
        match sig_toks.get(i).map(|t| t.kind()) {
            Some(IDENT) => {
                comps.push((IDENT, sig_toks[i].text().to_string()));
                i += 1;
            }
            Some(k @ (DOLLAR | AT)) => {
                let name = sig_toks
                    .get(i + 1)
                    .map(|t| t.text().to_string())
                    .unwrap_or_default();
                comps.push((k, name));
                i += 2;
            }
            _ => break,
        }
    }

    let Some(((_, root), steps)) = comps.split_first() else {
        return "@.".to_string();
    };
    let last = steps.len().saturating_sub(1);
    let inner_at = steps[..last].iter().any(|(k, _)| *k == AT);

    let mut path = root.clone();
    for (idx, (kind, name)) in steps.iter().enumerate() {
        let is_final = idx == last;
        let step = match (*kind, is_final) {
            (DOLLAR, true) if invalid => format!("(inert (error {name}))"),
            (DOLLAR, _) => format!("(inert ($ {name}))"),
            (AT, true) if invalid => format!("(quote (error-t) @{name})"),
            (AT, false) if invalid => format!("(quote (error-t) {name})"),
            (_, true) if invalid && inner_at => format!("(error-t) (quote @{name})"),
            (_, true) => format!("(quote @{name})"),
            (_, false) => format!("(quote {name})"),
        };
        path = format!("(. {path} {step})");
    }
    path
}

/// The macro-name component text in a trailing-form `MACRO_NAME` — the token
/// immediately after the `@` (`A.B.@x` → `x`).
fn macro_name_after_at(node: &SyntaxNode) -> String {
    let mut after_at = false;
    for el in node.children_with_tokens() {
        if let NodeOrToken::Token(t) = el {
            if after_at {
                return t.text().to_string();
            }
            if t.kind() == AT {
                after_at = true;
            }
        }
    }
    String::new()
}

/// Whether `kind` is a macro-name component token other than an identifier — an
/// operator name (`+`, `!`, `..`), the `$` sigil, or a keyword (`end`). `DOT` is
/// excluded: it is the qualifier dot or the broadcast `@.`, never a name part.
fn is_macro_name_part_token(kind: SyntaxKind) -> bool {
    (is_operator(kind) && kind != DOT) || kind == DOLLAR || is_keyword(kind)
}

// --- Literals / strings ----------------------------------------------------

fn project_literal(node: &SyntaxNode) -> String {
    let toks: Vec<_> = node
        .children_with_tokens()
        .filter_map(|el| el.into_token())
        .collect();
    // A folded signed numeric literal (`-2`, `+2.0`): the leading `+`/`-` sign
    // token precedes the number. `-` stays in the literal; `+` is a no-op and is
    // dropped (`+2.0` → `2.0`, matching JuliaSyntax's glued literal).
    if let [sign, num] = toks.as_slice() {
        let n = literal_token_text(num);
        return if sign.text() == "-" {
            format!("-{n}")
        } else {
            n
        };
    }
    match toks.first() {
        Some(tok) => literal_token_text(tok),
        None => "(unsupported LITERAL)".to_string(),
    }
}

/// Render a single literal token the way JuliaSyntax displays it. Integer
/// literals are normalized to their parsed *value* (underscores stripped;
/// hex/octal/binary shown as zero-padded hex at Julia's width tier), matching
/// how JuliaSyntax shows the leaf value rather than the source text — the same
/// value-rendering the string/char paths already do. Floats are left as source
/// text for now (float canonicalization is a separate, deferred follow-up).
fn literal_token_text(tok: &SyntaxToken) -> String {
    match tok.kind() {
        CHAR => project_char(tok),
        TRUE_KW => "true".to_string(),
        FALSE_KW => "false".to_string(),
        INTEGER => tok.text().replace('_', ""),
        HEX_INT => normalize_based_int(tok.text(), 16),
        OCT_INT => normalize_based_int(tok.text(), 8),
        BIN_INT => normalize_based_int(tok.text(), 2),
        _ => tok.text().to_string(),
    }
}

/// Normalize a base-prefixed integer literal (`0x…`/`0o…`/`0b…`) to JuliaSyntax's
/// display: the parsed value as lowercase hex, zero-padded to the width of the
/// unsigned type Julia selects from the digit count. Hex counts 4 bits/digit,
/// binary 1 bit/digit, and octal `bits(leading) + 3·(ndigits−1)`; that bit count
/// rounds up to the next of {8,16,32,64,128} (UInt8…UInt128). Values wider than
/// 128 bits are `BigInt` (shown as decimal) — not handled here, so the source
/// text is kept (a deferred divergence).
fn normalize_based_int(text: &str, base: u32) -> String {
    let digits: String = text[2..].chars().filter(|&c| c != '_').collect();
    let nbits = match base {
        16 => 4 * digits.len(),
        2 => digits.len(),
        _ => octal_bits(&digits),
    };
    let tier_bits = match nbits {
        0..=8 => 8,
        9..=16 => 16,
        17..=32 => 32,
        33..=64 => 64,
        65..=128 => 128,
        _ => return text.replace('_', ""),
    };
    match u128::from_str_radix(&digits, base) {
        Ok(v) => format!("0x{:0width$x}", v, width = tier_bits / 4),
        Err(_) => text.replace('_', ""),
    }
}

/// Bit width of an octal literal's digit string: the leading digit contributes
/// only its significant bits (`0`→0, `1`→1, `2–3`→2, `4–7`→3) and each remaining
/// digit a full 3 bits, matching Julia's octal type selection (`0o200`→`0x80`,
/// `0o400`→`0x0100`, leading zeros widen: `0o00007`→`0x0007`).
fn octal_bits(digits: &str) -> usize {
    let lead = digits.as_bytes()[0] - b'0';
    let lead_bits = (8 - lead.leading_zeros()) as usize;
    lead_bits + 3 * (digits.len() - 1)
}

/// Project a char-literal token (`'…'`) to `(char '…')`, decoding the source
/// escapes to a single codepoint and re-displaying it the way JuliaSyntax shows
/// a `Char`. Content `decode_char` rejects is one of JuliaSyntax's error shapes:
/// empty `''` ⇒ `(char (error))`, a malformed escape `'\xq'` ⇒
/// `(char (ErrorInvalidEscapeSequence))`, a lone non-UTF-8 byte `'\xff'` stays a
/// valid one-byte `Char`, and anything else multi-codepoint `'ab'` ⇒
/// `(char (ErrorOverLongCharacter))`.
fn project_char(tok: &SyntaxToken) -> String {
    let text = tok.text();
    // An unterminated char (no closing quote) is flagged with `UnterminatedLiteral`
    // at the opening quote. JuliaSyntax reads it as a char and recovers with a
    // missing-close marker: empty content `'` ⇒ `(char (error))` (the bare empty
    // shape, no `(error-t)`), non-empty `'a` ⇒ `(char 'a' (error-t))`.
    let start = usize::from(tok.text_range().start());
    if diag_at(start, DiagnosticKind::UnterminatedLiteral) {
        let inner = text.strip_prefix('\'').unwrap_or(text);
        if inner.is_empty() {
            return "(char (error))".to_string();
        }
        return match decode_char_body(inner) {
            Some(c) => format!("(char '{}' (error-t))", display_char(c)),
            None => match classify_char_body(inner) {
                CharError::Empty => "(char (error))".to_string(),
                CharError::BadEscape => "(char (ErrorInvalidEscapeSequence) (error-t))".to_string(),
                CharError::OverLong => "(char (ErrorOverLongCharacter) (error-t))".to_string(),
                CharError::SingleByte(b) => format!("(char '\\x{b:02x}' (error-t))"),
            },
        };
    }
    match decode_char(text) {
        Some(c) => format!("(char '{}')", display_char(c)),
        None => match classify_char_error(text) {
            CharError::Empty => "(char (error))".to_string(),
            CharError::BadEscape => "(char (ErrorInvalidEscapeSequence))".to_string(),
            CharError::OverLong => "(char (ErrorOverLongCharacter))".to_string(),
            CharError::SingleByte(b) => format!("(char '\\x{b:02x}')"),
        },
    }
}

/// Why `decode_char` rejected a char-literal body, mapped to JuliaSyntax's
/// error-token classification.
enum CharError {
    /// Empty body (`''`).
    Empty,
    /// A malformed backslash escape (`'\xq'`, `'\q'`, `'\400'`).
    BadEscape,
    /// A single byte that is not valid UTF-8 (`'\xff'`, `'\377'`) — still a
    /// valid one-byte Julia `Char`, displayed `\xNN`.
    SingleByte(u8),
    /// A well-formed body holding more than one codepoint (`'ab'`, `'\xff\xff'`).
    OverLong,
}

/// Classify a char-literal body that `decode_char` could not reduce to a single
/// codepoint. Mirrors `decode_char`'s byte accumulation but reports *why* it
/// failed; a malformed escape wins over over-long (matching JuliaSyntax).
fn classify_char_error(text: &str) -> CharError {
    let inner = text
        .strip_prefix('\'')
        .and_then(|s| s.strip_suffix('\''))
        .unwrap_or("");
    classify_char_body(inner)
}

/// Classify a char-literal *body* (the text between the quotes, already stripped).
/// Shared by the terminated (`classify_char_error`) and unterminated paths.
fn classify_char_body(inner: &str) -> CharError {
    if inner.is_empty() {
        return CharError::Empty;
    }
    let mut bytes: Vec<u8> = Vec::new();
    let mut buf = [0u8; 4];
    let mut chars = inner.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            bytes.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
        } else if decode_escape_into(&mut chars, &mut bytes).is_none() {
            return CharError::BadEscape;
        }
    }
    match bytes.as_slice() {
        [b] => CharError::SingleByte(*b),
        _ => CharError::OverLong,
    }
}

/// Decode a char literal's source text (`'\xce\xb1'`) to its single codepoint.
/// Byte escapes (`\xNN`, octal) and literal characters accumulate as bytes that
/// are then read as UTF-8; `\u`/`\U` and named escapes contribute a codepoint
/// directly. Returns `None` for empty, malformed, or over-long content.
fn decode_char(text: &str) -> Option<char> {
    let inner = text.strip_prefix('\'')?.strip_suffix('\'')?;
    decode_char_body(inner)
}

/// Decode a char-literal *body* (already stripped of quotes) to its single
/// codepoint. Shared by the terminated and unterminated paths.
fn decode_char_body(inner: &str) -> Option<char> {
    if inner.is_empty() {
        return None;
    }
    let mut bytes: Vec<u8> = Vec::new();
    let mut buf = [0u8; 4];
    let mut chars = inner.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            bytes.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
            continue;
        }
        decode_escape_into(&mut chars, &mut bytes)?;
    }
    let s = std::str::from_utf8(&bytes).ok()?;
    let mut it = s.chars();
    let first = it.next()?;
    it.next().is_none().then_some(first)
}

/// Decode a single backslash escape (the backslash already consumed) into `bytes`,
/// the way Julia reads a `Char`/`String` literal: byte escapes (`\xNN`, octal) push
/// one byte; `\u`/`\U` push a codepoint's UTF-8 bytes; named escapes push their
/// control byte. Returns `None` on a malformed or unknown escape.
fn decode_escape_into(
    chars: &mut std::iter::Peekable<std::str::Chars>,
    bytes: &mut Vec<u8>,
) -> Option<()> {
    let mut buf = [0u8; 4];
    match chars.next()? {
        'n' => bytes.push(b'\n'),
        't' => bytes.push(b'\t'),
        'r' => bytes.push(b'\r'),
        'a' => bytes.push(0x07),
        'b' => bytes.push(0x08),
        'f' => bytes.push(0x0c),
        'v' => bytes.push(0x0b),
        'e' => bytes.push(0x1b),
        '\\' => bytes.push(b'\\'),
        '\'' => bytes.push(b'\''),
        '"' => bytes.push(b'"'),
        '$' => bytes.push(b'$'),
        'x' => bytes.push(take_hex(chars, 2)? as u8),
        'u' => {
            let cp = char::from_u32(take_hex(chars, 4)?)?;
            bytes.extend_from_slice(cp.encode_utf8(&mut buf).as_bytes());
        }
        'U' => {
            let cp = char::from_u32(take_hex(chars, 8)?)?;
            bytes.extend_from_slice(cp.encode_utf8(&mut buf).as_bytes());
        }
        d @ '0'..='7' => {
            let mut val = d.to_digit(8)?;
            for _ in 0..2 {
                match chars.peek().and_then(|c| c.to_digit(8)) {
                    Some(o) => {
                        val = val * 8 + o;
                        chars.next();
                    }
                    None => break,
                }
            }
            // Julia caps an octal escape at one byte; `\400` and up overflow.
            bytes.push(u8::try_from(val).ok()?);
        }
        _ => return None,
    }
    Some(())
}

/// Consume up to `max` hex digits from `chars` and return their value; `None`
/// if there is not at least one digit.
fn take_hex(chars: &mut std::iter::Peekable<std::str::Chars>, max: usize) -> Option<u32> {
    let mut val = 0u32;
    let mut n = 0;
    while n < max {
        match chars.peek().and_then(|c| c.to_digit(16)) {
            Some(d) => {
                val = val * 16 + d;
                chars.next();
                n += 1;
            }
            None => break,
        }
    }
    (n > 0).then_some(val)
}

/// JuliaSyntax's escape for a control/non-printable codepoint, shared by `Char`
/// and `String` show: the named control escapes, a `\xNN` form for the remaining
/// C0 controls and DEL, a `\u`/`\U` form for other non-printable codepoints.
/// Returns `None` for a printable char (which shows as itself).
fn control_escape(c: char) -> Option<String> {
    Some(match c {
        '\0' => "\\0".to_string(),
        '\u{7}' => "\\a".to_string(),
        '\u{8}' => "\\b".to_string(),
        '\t' => "\\t".to_string(),
        '\n' => "\\n".to_string(),
        '\u{b}' => "\\v".to_string(),
        '\u{c}' => "\\f".to_string(),
        '\r' => "\\r".to_string(),
        '\u{1b}' => "\\e".to_string(),
        c if (c as u32) < 0x20 || c as u32 == 0x7f => format!("\\x{:02x}", c as u32),
        c if c.is_control() => {
            let v = c as u32;
            if v <= 0xffff {
                format!("\\u{v:04x}")
            } else {
                format!("\\U{v:08x}")
            }
        }
        _ => return None,
    })
}

/// Render a single codepoint as JuliaSyntax shows a `Char` body: `\\`/`\'`, the
/// shared control escapes, else literal.
fn display_char(c: char) -> String {
    match c {
        '\\' => "\\\\".to_string(),
        '\'' => "\\'".to_string(),
        c => control_escape(c).unwrap_or_else(|| c.to_string()),
    }
}

fn project_string(node: &SyntaxNode) -> String {
    // String macro: a prefix (`r`, `raw`, `b`, `v`) makes it a raw `@<p>_str`
    // macrocall rather than an interpolating `(string …)`.
    if let Some(prefix) = string_token(node, STRING_PREFIX) {
        // A triple-quoted raw string still gets JuliaSyntax's dedent + per-line
        // chunking; only the unescaping differs (raw content's backslashes and
        // quotes stay literal), so its chunks are display-escaped as raw bytes.
        let body = if matches!(string_token(node, STRING_DELIM_OPEN), Some(d) if d.len() >= 3) {
            sexp(
                "string-s-r",
                with_error_trivia(node, triple_string_parts(node, true)),
            )
        } else {
            sexp(
                "string-r",
                with_error_trivia(node, vec![quote_raw(&raw_content(node))]),
            )
        };
        let mut parts = vec![format!("@{prefix}_str"), body];
        if let Some(suffix) = string_token(node, STRING_SUFFIX) {
            parts.push(quote_raw(&suffix));
        } else if let Some(num) = numeric_suffix(node) {
            // A glued numeric suffix (`x"s"2`) is an extra macrocall argument,
            // rendered as the numeric literal itself (not a flag string).
            parts.push(num);
        }
        return sexp("macrocall", parts);
    }

    // Triple-quoted strings get JuliaSyntax's dedent + per-line chunking applied
    // to compute their literal value (a faithful encoding of what the literal
    // means, mirroring `SyntaxNode`'s String children).
    if matches!(string_token(node, STRING_DELIM_OPEN), Some(d) if d.len() >= 3) {
        return sexp(
            "string-s",
            with_error_trivia(node, triple_string_parts(node, false)),
        );
    }

    let mut parts = string_parts(node);
    if parts.is_empty() {
        // An empty literal still carries one empty String child (`"" → (string "")`).
        parts.push("\"\"".to_string());
    }
    sexp("string", with_error_trivia(node, parts))
}

/// One piece of a triple-quoted string's processed content: either a literal
/// text chunk (JuliaSyntax keeps one `String` per line) or an interpolation.
enum TripleItem {
    Text(String),
    Interp(String),
}

/// Project a triple-quoted string's content the way JuliaSyntax does: normalize
/// line endings to `\n`, split into per-line chunks, strip the common leading
/// indentation, drop the leading newline right after `"""`, then display-escape.
fn triple_string_parts(node: &SyntaxNode, raw: bool) -> Vec<String> {
    // Build the content as a sequence of lines (split on normalized newlines);
    // each line is a run of text/interpolation items.
    let mut lines: Vec<Vec<TripleItem>> = vec![Vec::new()];
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) if t.kind() == STRING_CONTENT => {
                let text = normalize_newlines(t.text());
                let mut segs = text.split('\n');
                if let Some(first) = segs.next() {
                    lines
                        .last_mut()
                        .unwrap()
                        .push(TripleItem::Text(first.to_string()));
                }
                for seg in segs {
                    lines.push(vec![TripleItem::Text(seg.to_string())]);
                }
            }
            NodeOrToken::Node(n) if n.kind() == INTERPOLATION => {
                lines
                    .last_mut()
                    .unwrap()
                    .push(TripleItem::Interp(project_interpolation(&n)));
            }
            _ => {}
        }
    }

    chunk_triple_lines(lines, raw)
}

/// A triple-backtick command's content as raw text lines (commands keep
/// `$`-interpolation as literal source, so every child — content and
/// interpolation alike — is folded into one raw string before dedenting), then
/// JuliaSyntax's dedent + per-line chunking is applied with raw escaping.
fn triple_cmd_parts(node: &SyntaxNode) -> Vec<String> {
    let body = normalize_newlines(&cmd_raw_body(node));
    let lines: Vec<Vec<TripleItem>> = body
        .split('\n')
        .map(|seg| vec![TripleItem::Text(seg.to_string())])
        .collect();
    chunk_triple_lines(lines, true)
}

/// Apply JuliaSyntax's triple-quoted dedent and per-line chunking to a sequence
/// of content lines, emitting one display-escaped `String`/interpolation part per
/// surviving chunk.
fn chunk_triple_lines(lines: Vec<Vec<TripleItem>>, raw: bool) -> Vec<String> {
    let last_idx = lines.len() - 1;

    // Common leading whitespace over lines 2..end. Whitespace-only lines are
    // skipped except the last (the closing-delimiter line), which always counts.
    let candidates: Vec<String> = lines
        .iter()
        .enumerate()
        .filter(|(i, line)| *i != 0 && (!line_is_blank(line) || *i == last_idx))
        .map(|(_, line)| line_lead_ws(line))
        .collect();
    let dedent_len = common_prefix_len(&candidates);

    let mut chunks: Vec<TripleItem> = Vec::new();
    for (i, mut line) in lines.into_iter().enumerate() {
        let has_newline = i != last_idx;
        if i == 0 {
            // The opening line is never dedented; an empty one (`"""` directly
            // followed by a newline) is dropped along with its newline.
            if line_is_empty(&line) {
                continue;
            }
        } else if let Some(TripleItem::Text(t)) = line.first_mut() {
            *t = strip_leading_ws(t, dedent_len);
        }
        if has_newline {
            match line.last_mut() {
                Some(TripleItem::Text(t)) => t.push('\n'),
                _ => line.push(TripleItem::Text("\n".to_string())),
            }
        }
        chunks.extend(line);
    }

    let mut out: Vec<String> = Vec::new();
    for chunk in chunks {
        match chunk {
            TripleItem::Text(t) if t.is_empty() => {}
            TripleItem::Text(t) => out.push(format!("\"{}\"", escape_display(&t, raw))),
            TripleItem::Interp(s) => out.push(s),
        }
    }
    if out.is_empty() {
        out.push("\"\"".to_string());
    }
    out
}

/// Collapse CRLF and lone CR line endings to LF (JuliaSyntax normalizes both).
fn normalize_newlines(s: &str) -> String {
    s.replace("\r\n", "\n").replace('\r', "\n")
}

/// Leading run of spaces/tabs at the start of a line (empty if it opens with an
/// interpolation).
fn line_lead_ws(line: &[TripleItem]) -> String {
    match line.first() {
        Some(TripleItem::Text(t)) => t.chars().take_while(|c| *c == ' ' || *c == '\t').collect(),
        _ => String::new(),
    }
}

/// A line with no interpolation whose text is entirely spaces/tabs (or empty).
fn line_is_blank(line: &[TripleItem]) -> bool {
    line.iter().all(|it| match it {
        TripleItem::Text(t) => t.chars().all(|c| c == ' ' || c == '\t'),
        TripleItem::Interp(_) => false,
    })
}

/// A line with no interpolation and no text at all.
fn line_is_empty(line: &[TripleItem]) -> bool {
    line.iter()
        .all(|it| matches!(it, TripleItem::Text(t) if t.is_empty()))
}

/// Longest common prefix length (in bytes) over the given whitespace strings.
fn common_prefix_len(strs: &[String]) -> usize {
    let mut iter = strs.iter();
    let Some(first) = iter.next() else {
        return 0;
    };
    let mut len = first.len();
    for s in iter {
        len = first
            .bytes()
            .zip(s.bytes())
            .take(len)
            .take_while(|(a, b)| a == b)
            .count();
    }
    len
}

/// Strip up to `n` leading whitespace bytes (spaces/tabs are single-byte).
fn strip_leading_ws(t: &str, n: usize) -> String {
    let mut idx = 0;
    for c in t.chars() {
        if idx >= n || (c != ' ' && c != '\t') {
            break;
        }
        idx += 1;
    }
    t[idx..].to_string()
}

/// Escape the control characters JuliaSyntax shows as backslash escapes. For an
/// interpolating string the source escapes (`\n`, `\\`) already display as
/// themselves, so only literal control chars need escaping; for a `raw` string
/// the content is literal bytes, so backslashes/quotes/`$` are escaped too.
fn escape_display(s: &str, raw: bool) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            '\r' => out.push_str("\\r"),
            '\\' if raw => out.push_str("\\\\"),
            '"' if raw => out.push_str("\\\""),
            '$' if raw => out.push_str("\\$"),
            _ => out.push(c),
        }
    }
    out
}

/// A `var"name"` non-standard identifier projects to `(var name)`, heading the
/// node with the raw delimited content. (Escape-processing of the name —
/// `var"\""` → `(var ")` — follows Julia's raw-string rules and is deferred, so
/// only escape-free names match the oracle today.)
fn project_var(node: &SyntaxNode) -> String {
    let content = unescape_raw_string(&raw_content(node));
    let parts = if content.is_empty() {
        vec![]
    } else {
        vec![content]
    };
    sexp("var", with_error_trivia(node, parts))
}

/// Unescape a raw-string body the way Julia's `unescape_raw_string` does: a run
/// of `n` backslashes immediately before a `"` (or at the end of the body, where
/// the closing delimiter is the implied `"`) yields `n / 2` backslashes, plus a
/// literal `"` when `n` is odd (`\"` ⇒ `"`, `\\\"` ⇒ `\` then `"`, trailing
/// `\\` ⇒ `\`); any other backslash run is literal. Used for `var"…"` identifier
/// content, whose name is the unescaped value rather than the raw source bytes.
fn unescape_raw_string(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = String::with_capacity(s.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' {
            let mut run = 0;
            while i + run < bytes.len() && bytes[i + run] == b'\\' {
                run += 1;
            }
            let at_end = i + run >= bytes.len();
            let before_quote = !at_end && bytes[i + run] == b'"';
            if before_quote || at_end {
                for _ in 0..run / 2 {
                    out.push('\\');
                }
                i += run;
                if run % 2 == 1 {
                    out.push('"');
                    i += 1;
                }
            } else {
                for _ in 0..run {
                    out.push('\\');
                }
                i += run;
            }
        } else {
            let ch = s[i..].chars().next().unwrap();
            out.push(ch);
            i += ch.len_utf8();
        }
    }
    out
}

fn project_cmd(node: &SyntaxNode) -> String {
    // Command literals lower to a macrocall over a raw cmdstring. A bare literal
    // (`` `cmd` ``) uses the built-in `core_@cmd`; a prefix names a custom command
    // macro (`` x`str` `` ⇒ `@x_cmd`). Commands are raw: JuliaSyntax keeps
    // `$`-interpolation as literal source (escaped `\$`) and defers expansion to
    // the macro, so reconstruct the raw body from content and interpolation text.
    let triple = matches!(string_token(node, CMD_DELIM_OPEN), Some(d) if d.len() >= 3);
    let head = match string_token(node, STRING_PREFIX) {
        Some(prefix) => format!("@{prefix}_cmd"),
        None => "core_@cmd".to_string(),
    };
    // A triple-quoted command gets JuliaSyntax's dedent + per-line chunking (raw),
    // matching the triple-string path; a single-quoted one is one raw chunk.
    let body = if triple {
        sexp(
            "cmdstring-s-r",
            with_error_trivia(node, triple_cmd_parts(node)),
        )
    } else {
        sexp(
            "cmdstring-r",
            with_error_trivia(node, vec![quote_raw(&cmd_raw_body(node))]),
        )
    };
    let mut parts = vec![head, body];
    if let Some(suffix) = string_token(node, STRING_SUFFIX) {
        // A flag glued to the closing delimiter (`` x`str`flag ``) is an extra
        // macrocall argument.
        parts.push(quote_raw(&suffix));
    }
    sexp("macrocall", parts)
}

fn cmd_raw_body(node: &SyntaxNode) -> String {
    let mut body = String::new();
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) if t.kind() == STRING_CONTENT => body.push_str(t.text()),
            NodeOrToken::Node(n) if n.kind() == INTERPOLATION => {
                body.push_str(&n.text().to_string())
            }
            _ => {}
        }
    }
    body
}

fn string_parts(node: &SyntaxNode) -> Vec<String> {
    // JuliaSyntax projects a single-quoted string to its *value*: escapes are
    // decoded and re-shown JuliaSyntax-style, and a `\`-newline line continuation
    // splits the content into separate `String` chunks (dropping the backslash,
    // the newline, and the following indentation). On any malformed escape we fall
    // back to echoing the raw source — those are deferred error shapes that stay
    // un-allowlisted, matching the prior behavior.
    decoded_string_parts(node).unwrap_or_else(|| raw_string_parts(node))
}

fn decoded_string_parts(node: &SyntaxNode) -> Option<Vec<String>> {
    let mut parts = Vec::new();
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) if t.kind() == STRING_CONTENT => {
                match decode_string_chunks(t.text()) {
                    Ok(chunks) => {
                        for chunk in chunks {
                            if !chunk.is_empty() {
                                parts.push(format!("\"{}\"", escape_string_value(&chunk)));
                            }
                        }
                    }
                    // A malformed escape collapses the whole content token to one
                    // JuliaSyntax error part — `"ok\xqq"` ⇒ `(string
                    // (ErrorInvalidEscapeSequence))`, the valid surrounding text is
                    // dropped, matching JuliaSyntax's per-`String`-token error.
                    Err(StringDecodeError::BadEscape) => {
                        parts.push("(ErrorInvalidEscapeSequence)".to_string());
                    }
                    // Valid bytes that aren't UTF-8 (`"\xff"`) stay a faithful
                    // byte-string: fall back to the raw-source rendering for the
                    // whole literal (matches JuliaSyntax's `\xff` show).
                    Err(StringDecodeError::BadUtf8) => return None,
                }
            }
            NodeOrToken::Node(n) if n.kind() == INTERPOLATION => {
                parts.push(project_interpolation(&n));
            }
            _ => {}
        }
    }
    Some(parts)
}

/// Why `decode_string_chunks` could not reduce a `STRING_CONTENT` token to its
/// literal value. The two failures project differently: a malformed escape is
/// JuliaSyntax's `(ErrorInvalidEscapeSequence)`, while well-formed bytes that
/// aren't UTF-8 are a valid byte-string we render from the raw source.
enum StringDecodeError {
    /// A malformed backslash escape (`\xq`, `\q`, `\400`).
    BadEscape,
    /// Well-formed bytes that don't decode as UTF-8 (`\xff`).
    BadUtf8,
}

fn raw_string_parts(node: &SyntaxNode) -> Vec<String> {
    let mut parts = Vec::new();
    for el in node.children_with_tokens() {
        match el {
            NodeOrToken::Token(t) if t.kind() == STRING_CONTENT => {
                parts.push(format!("\"{}\"", t.text()));
            }
            NodeOrToken::Node(n) if n.kind() == INTERPOLATION => {
                parts.push(project_interpolation(&n));
            }
            _ => {}
        }
    }
    parts
}

/// Decode one `STRING_CONTENT` token's source into its literal value, split into
/// chunks at each `\`-newline line continuation (JuliaSyntax keeps one `String`
/// per chunk). A continuation drops the backslash, the newline (`\n`/`\r`/`\r\n`),
/// and the run of spaces/tabs that follow. Returns a `StringDecodeError` on a
/// malformed escape (`BadEscape`) or valid-but-non-UTF-8 bytes (`BadUtf8`).
fn decode_string_chunks(text: &str) -> Result<Vec<String>, StringDecodeError> {
    let mut chunks: Vec<Vec<u8>> = vec![Vec::new()];
    let mut buf = [0u8; 4];
    let mut chars = text.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            let last = chunks.last_mut().unwrap();
            last.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
            continue;
        }
        match chars.peek() {
            Some('\n') | Some('\r') => {
                let nl = chars.next().unwrap();
                if nl == '\r' && chars.peek() == Some(&'\n') {
                    chars.next();
                }
                while matches!(chars.peek(), Some(' ') | Some('\t')) {
                    chars.next();
                }
                chunks.push(Vec::new());
            }
            _ => decode_escape_into(&mut chars, chunks.last_mut().unwrap())
                .ok_or(StringDecodeError::BadEscape)?,
        }
    }
    chunks
        .into_iter()
        .map(|c| String::from_utf8(c).map_err(|_| StringDecodeError::BadUtf8))
        .collect()
}

/// Escape a decoded string value the way JuliaSyntax's `String` show does:
/// `\\`/`\"`/`\$`, the shared control escapes, else literal.
fn escape_string_value(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '$' => out.push_str("\\$"),
            c => match control_escape(c) {
                Some(esc) => out.push_str(&esc),
                None => out.push(c),
            },
        }
    }
    out
}

fn project_interpolation(node: &SyntaxNode) -> String {
    // `$name` → the bare identifier; `$(expr)` → the projected sub-expression. A
    // `$(…)` whose parens hold a multi-value form is invalid: JuliaSyntax renders
    // a block (`$(x;y)`), tuple (`$(x,y)`, empty `$()`), or generator
    // (`$(x for …)`) operand as `(error …)`, flattening block/tuple children and
    // keeping the generator nested. A single expression is a `PAREN_EXPR` the
    // normal `project` unwraps.
    if let Some(inner) = first_node(node) {
        return match inner.kind() {
            PAREN_BLOCK => sexp("error", project_block_args(&inner)),
            TUPLE_EXPR => sexp("error", project_args(&inner)),
            GENERATOR => sexp("error", vec![project_generator(&inner)]),
            _ => project(&inner),
        };
    }
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| t.kind() == IDENT)
        .map(|t| t.text().to_string())
        .unwrap_or_default()
}

fn raw_content(node: &SyntaxNode) -> String {
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        .filter(|t| t.kind() == STRING_CONTENT)
        .map(|t| t.text().to_string())
        .collect()
}

/// A numeric literal token glued after a string macro's close delimiter
/// (`x"s"2`), captured into the `STRING_LITERAL` node as a trailing macrocall
/// argument. Returns the token's source text verbatim.
fn numeric_suffix(node: &SyntaxNode) -> Option<String> {
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| matches!(t.kind(), INTEGER | FLOAT | FLOAT32))
        .map(|t| t.text().to_string())
}

fn string_token(node: &SyntaxNode, kind: SyntaxKind) -> Option<String> {
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| t.kind() == kind)
        .map(|t| t.text().to_string())
}

/// Escape a raw string/command body for display the way JuliaSyntax's `show`
/// does: backslashes, double-quotes, and `$` are escaped.
fn quote_raw(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '$' => out.push_str("\\$"),
            _ => out.push(c),
        }
    }
    out.push('"');
    out
}

// --- Signatures ------------------------------------------------------------

/// Project a `function`/`macro` definition. A bare-name signature
/// (`function f end`, `macro m end`) is a forward declaration with no body in
/// JuliaSyntax (`(function f)`), distinct from a method/macro with a call
/// signature and block (`function f() end` → `(function (call f) (block))`).
fn project_function_like(head: &str, node: &SyntaxNode) -> String {
    if is_forward_declaration(node) {
        return sexp(head, vec![project_signature(node)]);
    }
    // A bare-identifier signature with a non-empty body is invalid (`function f
    // body end`); JuliaSyntax error-wraps the name (`(function (error f) (block
    // body))`). The `InvalidFunctionSignature` diagnostic, anchored at the
    // `SIGNATURE`'s start, marks exactly that case.
    let sig = if !node.children().any(|c| c.kind() == SIGNATURE) {
        // No signature at all (`function`, `function;end`) is JuliaSyntax's
        // empty-signature recovery: an error wrapping an empty error,
        // `(error (error))`. (`struct`/`module` differ — handled elsewhere.)
        "(error (error))".to_string()
    } else if invalid_bare_signature(node) {
        format!("(error {})", project_signature(node))
    } else {
        project_signature(node)
    };
    let mut parts = vec![sig, project_block_child(node)];
    push_trailing_errors(node, &mut parts);
    sexp(head, parts)
}

/// A `function`/`macro` header is a forward declaration when its signature is a
/// bare name (`f`, `$f`) rather than a call (`f()`) or other expression — and the
/// body is empty enough to keep it a declaration. A bare name with a body is
/// instead an invalid signature (`invalid_bare_signature`), not a declaration.
fn is_forward_declaration(node: &SyntaxNode) -> bool {
    signature_is_bare_name(node) && !invalid_bare_signature(node)
}

/// Whether the signature's first node is a bare name (`f`, `$f`) rather than a
/// call or other expression.
fn signature_is_bare_name(node: &SyntaxNode) -> bool {
    node.children()
        .find(|c| c.kind() == SIGNATURE)
        .and_then(|sig| first_node(&sig))
        .map(|inner| matches!(inner.kind(), NAME | INTERPOLATION))
        .unwrap_or(false)
}

/// Whether the signature is a bare name marked invalid by the parser (a bare-name
/// header with a non-empty body — `InvalidFunctionSignature`, anchored at the
/// `SIGNATURE`'s start).
fn invalid_bare_signature(node: &SyntaxNode) -> bool {
    node.children()
        .find(|c| c.kind() == SIGNATURE)
        .map(|sig| {
            diag_at(
                usize::from(sig.text_range().start()),
                DiagnosticKind::InvalidFunctionSignature,
            )
        })
        .unwrap_or(false)
}

fn project_signature(node: &SyntaxNode) -> String {
    match node.children().find(|c| c.kind() == SIGNATURE) {
        Some(sig) => match first_node(&sig) {
            Some(inner) => project(&inner),
            // Bare or loose signature (`struct Point`, `struct Foo <: Bar`).
            None => project_flat(significant(&sig)),
        },
        // Some forms (`module M`) put the name directly; fall back to loose.
        None => project_flat(significant(node)),
    }
}

// --- Generic helpers -------------------------------------------------------

/// Append the unclosed-delimiter `(error-t)` marker to a literal body's parts
/// when an `UnterminatedLiteral`/`StringSuffixSpace` diagnostic is anchored at
/// the literal's start. JuliaSyntax emits no empty `""` content placeholder for
/// an unterminated literal, so drop a sole filler `""` before appending
/// (`"` → `(string (error-t))`, not `(string "")`).
fn with_error_trivia(node: &SyntaxNode, mut parts: Vec<String>) -> Vec<String> {
    let s = usize::from(node.text_range().start());
    if diag_at(s, DiagnosticKind::UnterminatedLiteral)
        || diag_at(s, DiagnosticKind::StringSuffixSpace)
    {
        if parts == ["\"\""] {
            parts.clear();
        }
        parts.push("(error-t)".to_string());
    }
    parts
}

fn project_error(head: &str, node: &SyntaxNode) -> String {
    // A delimiter recovered into an error node is JuliaSyntax's `✘` error-token
    // glyph: a recovery run bumps brackets, commas, and `@` as flat error tokens
    // (`var"x")` ⇒ `(error-t ✘)`, `x y, z` ⇒ `x (error-t y ✘ z)`, `x@y` ⇒
    // `x (error-t ✘ y)`). Other recovered tokens and child nodes project
    // normally, trivia/structure are dropped.
    let parts: Vec<String> = node
        .children_with_tokens()
        .filter_map(|el| match &el {
            NodeOrToken::Token(t) if is_error_glyph(t.kind()) => Some("".to_string()),
            // A middle/closing block keyword (`end`/`else`/`elseif`/`catch`/
            // `finally`) recovered into a trailing-junk run renders verbatim
            // (`: end` ⇒ `(error-t end)`, `: catch z` ⇒ `(error-t catch z)`),
            // unlike a structural keyword (dropped below).
            NodeOrToken::Token(t) if is_closing_block_keyword_kind(t.kind()) => {
                Some(t.text().to_string())
            }
            // A misplaced prime (`f.'` ⇒ `f (error-t ')`) recovered into the
            // junk run renders as its `'` glyph; the leading field-access `.`
            // that JuliaSyntax bundles with it is dropped (it shows only `'`).
            NodeOrToken::Token(t) if t.kind() == TRANSPOSE => Some(t.text().to_string()),
            NodeOrToken::Token(t) if t.kind() == DOT => None,
            NodeOrToken::Token(t) if is_drop_token(t.kind()) => None,
            _ => project_element(&el),
        })
        .collect();
    sexp(head, parts)
}

/// Delimiter tokens that render as JuliaSyntax's `✘` error-token glyph when
/// recovered into an error node: brackets (open and close), commas, and the
/// macro `@` — each is bumped as a bare error token during recovery.
fn is_error_glyph(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        LPAREN | RPAREN | LBRACKET | RBRACKET | LBRACE | RBRACE | COMMA | AT
    )
}

/// Whether `kind` is a middle/closing block keyword (`end`/`else`/`elseif`/
/// `catch`/`finally`). When recovered into a trailing-junk `(error-t …)` run such
/// a keyword is rendered verbatim rather than dropped as a structural keyword.
fn is_closing_block_keyword_kind(kind: SyntaxKind) -> bool {
    matches!(kind, END_KW | ELSE_KW | ELSEIF_KW | CATCH_KW | FINALLY_KW)
}

fn sexp(head: &str, parts: Vec<String>) -> String {
    if parts.is_empty() {
        format!("({head})")
    } else {
        format!("({head} {})", parts.join(" "))
    }
}

/// Project the statement nodes of a `ROOT`/`BLOCK`/`BEGIN_EXPR` container.
fn stmt_strings(node: &SyntaxNode) -> Vec<String> {
    child_nodes(node).iter().map(project).collect()
}

/// The `BLOCK` child of a block-bearing construct, projected (empty if absent).
fn project_block_child(node: &SyntaxNode) -> String {
    match node.children().find(|c| c.kind() == BLOCK) {
        Some(block) => {
            let mut parts = stmt_strings(&block);
            // A block form truncated before its `end` (a `MissingEnd` diagnostic)
            // with an *empty* body gets a zero-width `(error)` placeholder for the
            // missing first statement: JuliaSyntax tries to parse a statement, hits
            // the truncation, and synthesizes one (`function f()` ⇒ `(function
            // (call f) (block (error)) (error-t))`). A body with content, or a
            // properly closed empty body (`function f() end` ⇒ `(block)`), gets no
            // such marker.
            if parts.is_empty()
                && diag_count_from(keyword_start(node), DiagnosticKind::MissingEnd) > 0
            {
                parts.push("(error)".to_string());
            }
            sexp("block", parts)
        }
        None => "(block)".to_string(),
    }
}

/// Append `(error-t)` to `parts` for each `MissingEnd` diagnostic anchored at the
/// construct's opening keyword — the truncation marker for a block form missing
/// its `end` (`if c\n x` ⇒ `(if c (block x) (error-t))`).
fn push_trailing_errors(node: &SyntaxNode, parts: &mut Vec<String>) {
    for _ in 0..diag_count_from(keyword_start(node), DiagnosticKind::MissingEnd) {
        parts.push("(error-t)".to_string());
    }
}

/// Project a block-form's `BLOCK` child, folding a trailing missing-`end` marker
/// *into* the block — for constructs that JuliaSyntax models *as* the block
/// (`begin`, `quote`), so the truncation marker lands inside it
/// (`begin\n x` ⇒ `(block x (error-t))`).
fn project_block_child_folding_error(node: &SyntaxNode) -> String {
    let Some(block) = node.children().find(|c| c.kind() == BLOCK) else {
        return "(block)".to_string();
    };
    let mut parts = stmt_strings(&block);
    // A trailing-junk `ERROR` sibling of the block folds *into* it for the forms
    // JuliaSyntax models *as* the block, so the recovery lands inside
    // (`begin x y end` ⇒ `(block x (error-t y))`).
    for err in node.children().filter(|c| c.kind() == ERROR) {
        parts.push(project(&err));
    }
    push_trailing_errors(node, &mut parts);
    sexp("block", parts)
}

fn child_nodes(node: &SyntaxNode) -> Vec<SyntaxNode> {
    node.children().collect()
}

fn project_each(nodes: Vec<SyntaxNode>) -> Vec<String> {
    nodes.iter().map(project).collect()
}

fn first_node(node: &SyntaxNode) -> Option<SyntaxNode> {
    node.children().next()
}

fn project_first(node: &SyntaxNode) -> String {
    first_node(node).map(|n| project(&n)).unwrap_or_default()
}

fn name_text(node: &SyntaxNode) -> String {
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        // A `NAME` normally wraps an `IDENT`; a reserved keyword misused as a
        // signature name (`struct try end` ⇒ `(error try)`) is wrapped here too,
        // so fall back to its keyword text.
        .find(|t| t.kind() == IDENT || is_keyword(t.kind()))
        .map(|t| t.text().to_string())
        .unwrap_or_default()
}

fn operator_token(node: &SyntaxNode) -> Option<SyntaxToken> {
    node.children_with_tokens()
        .filter_map(|el| el.into_token())
        .find(|t| is_operator(t.kind()))
}

/// Project one element of an `export`/`public` name list: a bare identifier, a
/// `NAME` node, an interpolated name (`$a`), or a macro name (`@a`).
fn name_run_item(el: SyntaxElement) -> Option<String> {
    match el {
        NodeOrToken::Token(t) if t.kind() == IDENT => Some(t.text().to_string()),
        NodeOrToken::Node(n) if n.kind() == NAME => Some(name_text(&n)),
        // An interpolated name (`export $a, $(a*b)`) → `($ …)`.
        NodeOrToken::Node(n) if n.kind() == INTERPOLATION => Some(project(&n)),
        // A macro name (`export @a`) → `@a`.
        NodeOrToken::Node(n) if n.kind() == MACRO_NAME => Some(project_macro_name(&n)),
        // An operator used as a name (`export +, ==`, `export ⊕`) → the bare
        // operator text.
        NodeOrToken::Token(t) if is_operator(t.kind()) => Some(t.text().to_string()),
        // A contextual keyword used as a name (`public export` ⇒ `(public
        // export)`): the parser places it in the name slot, so render its text.
        NodeOrToken::Token(t) if is_keyword(t.kind()) => Some(t.text().to_string()),
        _ => None,
    }
}

/// Project a flat sequence of significant elements representing a simple
/// (single-operator) expression. Used for the loose-token header passthrough
/// Fatou keeps for some constructs (for-loop ranges, struct subtypes, later
/// `let` bindings). Multi-operator runs fall back to a space join, which
/// diverges from JuliaSyntax and routes the case to `blocked.txt`.
fn project_flat(elems: Vec<SyntaxElement>) -> String {
    match elems.as_slice() {
        [one] => project_element(one).unwrap_or_default(),
        [lhs, NodeOrToken::Token(op), rhs] if is_operator(op.kind()) => {
            let l = project_element(lhs).unwrap_or_default();
            let r = project_element(rhs).unwrap_or_default();
            match infix_head(op.kind()) {
                InfixHead::CallI(text) => format!("(call-i {l} {text} {r})"),
                InfixHead::Special(text) => format!("({text} {l} {r})"),
                InfixHead::DotCallI(text) => format!("(dotcall-i {l} {text} {r})"),
                InfixHead::Dot => format!("(. {l} (quote {r}))"),
            }
        }
        _ => elems
            .iter()
            .filter_map(project_element)
            .collect::<Vec<_>>()
            .join(" "),
    }
}

fn project_element(el: &SyntaxElement) -> Option<String> {
    match el {
        NodeOrToken::Node(n) => Some(project(n)),
        NodeOrToken::Token(t) => match t.kind() {
            IDENT | INTEGER | BIN_INT | OCT_INT | HEX_INT | FLOAT | FLOAT32 => {
                Some(t.text().to_string())
            }
            CHAR => Some(project_char(t)),
            TRUE_KW => Some("true".to_string()),
            FALSE_KW => Some("false".to_string()),
            END_KW => Some("end".to_string()),
            k if is_operator(k) => Some(t.text().to_string()),
            _ => None,
        },
    }
}

/// The significant children of `node`: nodes, plus tokens that are neither
/// trivia, structural delimiters, nor keywords. Operators, identifiers, and
/// literal tokens survive.
fn significant(node: &SyntaxNode) -> Vec<SyntaxElement> {
    node.children_with_tokens()
        .filter(|el| match el {
            NodeOrToken::Node(_) => true,
            NodeOrToken::Token(t) => !is_drop_token(t.kind()),
        })
        .collect()
}

fn is_drop_token(kind: SyntaxKind) -> bool {
    is_trivia(kind) || is_delimiter(kind) || is_keyword(kind) || kind == DOLLAR
}

fn is_trivia(kind: SyntaxKind) -> bool {
    matches!(kind, WHITESPACE | NEWLINE | COMMENT | BLOCK_COMMENT)
}

fn is_delimiter(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        LPAREN | RPAREN | LBRACKET | RBRACKET | LBRACE | RBRACE | COMMA | SEMICOLON | AT
    )
}

fn is_keyword(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        FUNCTION_KW
            | MACRO_KW
            | END_KW
            | IF_KW
            | ELSEIF_KW
            | ELSE_KW
            | BEGIN_KW
            | WHILE_KW
            | FOR_KW
            | LET_KW
            | QUOTE_KW
            | TRY_KW
            | CATCH_KW
            | FINALLY_KW
            | STRUCT_KW
            | MUTABLE_KW
            | MODULE_KW
            | BAREMODULE_KW
            | DO_KW
            | RETURN_KW
            | BREAK_KW
            | CONTINUE_KW
            | CONST_KW
            | GLOBAL_KW
            | LOCAL_KW
            | IMPORT_KW
            | USING_KW
            | EXPORT_KW
            | WHERE_KW
    )
}