xdy 0.13.0

Complex RPG dice expression evaluator with histogram support.
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
//! # Diagnostics
//!
//! Herein is the diagnostic layer for the `xDy` parser and
//! [validator](Validator). The [`diagnose`] function analyzes a source string
//! by parsing it once in the parser's recovery mode, which diagnoses and
//! repairs every syntactic error in turn; it runs the semantic validator
//! instead whenever the source parses cleanly. This produces structured
//! [`Diagnostic`]s with specific error kinds, source spans highlighting,
//! human-readable messages, secondary [related labels](RelatedLabel) for
//! cross-referenced positions, and suggested fixes — edits of the original
//! source, in the manner of an editor's quick fixes, with highlighted
//! placeholder regions for the defaults of ambiguous ones.
//!
//! # Architecture
//!
//! The diagnostic layer is a consumer of the parser's
//! [`ParseError`](crate::parser::ParseError) and the validator's
//! [`CompilationError`], not part of either. The [`compile`](crate::compile)
//! and [`compile_unoptimized`](crate::compile_unoptimized) functions continue
//! to deal in the typed errors directly; diagnostics are a separate concern for
//! clients that want richer error information (e.g., for IDE integration,
//! syntax highlighting on every keystroke, or autocompletion).
//!
//! # Error Recovery Strategy
//!
//! The doctor runs one of two passes. If the source parses cleanly, a
//! **semantic pass** runs the [`Validator`] against the AST. Otherwise, a
//! **recovering parse** collects the syntactic errors. The [rustdoc for
//! `diagnose`](diagnose) carries a Mermaid rendering of the complete pipeline;
//! the ASCII sketch below summarizes the flow for casual skimming.
//!
//! ```text
//! ┌───────────────────────────────────────────────────────────┐
//! │ Parser::parse(source)                                     │
//! │   ├─ success → Validator::validate(ast) → diagnostics     │
//! │   └─ error   → Parser::parse_recovering(source, doctor)   │
//! │                  └─ each failure → diagnose → repair      │
//! └───────────────────────────────────────────────────────────┘
//! ```
//!
//! The parser never edits the source. Wherever the recovering parse cannot
//! continue, it consults the doctor, which diagnoses the failure from where
//! it lies in the grammar, the expectations of its error, and the text around
//! it. If the diagnostic has a fix, the doctor answers the repair that makes
//! the parse continue as though the source had been edited by the fix, e.g.,
//! as though a missing `)` were present. The parser reads stray whitespace,
//! which may not separate tokens, e.g., U+00A0 NO-BREAK SPACE, as spaces of
//! the same length, and the doctor diagnoses each stray itself. Every position
//! is a position of the original source, so the spans of diagnostics and the
//! edits of suggestions need no mapping, and the whole diagnosis takes time
//! linear in the length of the source, however many errors it holds. The parse
//! ends when it succeeds or when an unfixable error is encountered.
//!
//! The semantic pass runs **only when the original source parsed cleanly** —
//! that is, when no fixes were applied. Semantic diagnostics (e.g., duplicate
//! parameters) point at spans the user actually typed; running the validator on
//! a fix-synthesized source would attach diagnostics to characters the user
//! never wrote, so semantic checks wait for a syntactically valid source.

use std::{
	borrow::Cow,
	collections::HashSet,
	fmt::{self, Display, Formatter},
	iter
};

use crate::{
	CompilationError, Parser, Validator,
	ast::{Event, Expression, Function, Node, Parameter, Walk},
	parser::{
		FailureSite, Recovery, Repair, Site, canonical_name,
		is_bare_word_continue, is_bare_word_start, is_token_space,
		unclosed_name_ends
	},
	span::SourceSpan
};

////////////////////////////////////////////////////////////////////////////////
//                                   Types.                                   //
////////////////////////////////////////////////////////////////////////////////

/// The specific kind of diagnostic, indicating what went wrong.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiagnosticKind
{
	/// An opening delimiter has no matching close.
	///
	/// # Examples
	/// `(3D6`, `{x`, `[1:6`
	UnclosedDelimiter
	{
		/// The opening delimiter character.
		opener: char,
		/// The expected closing delimiter character.
		expected_closer: char
	},

	/// A delimited construct is closed, but lacks the expression, name, or
	/// face within it.
	///
	/// # Examples
	/// `( )`, `{ }`, `[1: ]`, `3D[ ]`
	MissingExpression
	{
		/// The opening delimiter character.
		opener: char
	},

	/// A closing delimiter has no matching open.
	///
	/// # Examples
	/// `3D6)`, `x}`
	UnopenedDelimiter
	{
		/// The unmatched closing delimiter character.
		closer: char
	},

	/// A binary operator is missing its right operand.
	///
	/// # Examples
	/// `3D6 +`, `1 + * 2`
	MissingRightOperand
	{
		/// The binary operator character.
		operator: char
	},

	/// A binary operator is missing its left operand.
	///
	/// # Examples
	/// `+ 3D6`
	MissingLeftOperand
	{
		/// The binary operator character.
		operator: char
	},

	/// A bare identifier appears where only `{identifier}` is legal.
	///
	/// # Examples
	/// `xD6`, `abcD12`
	BareIdentifier,

	/// A dice operator has no face specification.
	///
	/// # Examples
	/// `3D`, `3d`
	MissingDiceFaces,

	/// A `drop` keyword is not followed by `lowest` or `highest`.
	///
	/// # Examples
	/// `4D6 drop`
	IncompleteDropClause,

	/// A drop clause follows something other than a dice expression.
	///
	/// # Examples
	/// `3 drop lowest`, `1 + drop highest`
	MisplacedDropClause,

	/// A parameter definition is missing the `:` separator and body.
	///
	/// # Examples
	/// `{x}, {y}`, `{x}, {y} {x} + {y}`
	IncompleteParameterDefinition,

	/// A comma in the formal parameters is followed by another comma or by
	/// the `:`, rather than by a parameter.
	///
	/// # Examples
	/// `{x},: 1`, `{x}, , {y}: 1`
	MissingParameter,

	/// A valid expression is followed by unparseable input.
	///
	/// # Examples
	/// `3D6)`, `3D6 hello`
	TrailingInput,

	/// The input is empty or contains only whitespace.
	EmptyExpression,

	/// An unexpected token was encountered.
	UnexpectedToken,

	/// The input ended unexpectedly.
	UnexpectedEof,

	/// A formal parameter name was declared more than once. Unlike the kinds
	/// above — which are produced from
	/// [`ParseError`](crate::parser::ParseError) during the recovering parse —
	/// this kind is produced by the [`Validator`] semantic pass after a
	/// clean parse. The diagnostic's primary [`span`](Diagnostic::span) points
	/// at the duplicate occurrence; the [`related`](Diagnostic::related) list
	/// carries the span of the first occurrence.
	///
	/// # Examples
	/// `{x}, {x}: {x}`, `{a}, {b}, {b}: {a}`
	DuplicateParameter
	{
		/// The duplicated parameter name.
		name: String
	},

	/// A [local binding](crate::ast::Binding) uses a name that is already
	/// declared as a formal parameter. Not produced by the recovering parse —
	/// this kind is produced by the [`Validator`] semantic pass after a clean
	/// parse. The diagnostic's primary [`span`](Diagnostic::span) points at the
	/// binding-site name; the [`related`](Diagnostic::related) list carries the
	/// span of the colliding parameter declaration.
	///
	/// # Examples
	/// `{x}: {x}@(3D6) + {x}`
	BindingCollidesWithParameter
	{
		/// The colliding name.
		name: String
	},

	/// The same name is bound more than once by
	/// [local bindings](crate::ast::Binding) within a single function body.
	/// Not produced by the recovering parse — this kind is produced by the
	/// [`Validator`] semantic pass after a clean parse. The diagnostic's
	/// primary [`span`](Diagnostic::span) points at the duplicate binding-site
	/// name; the [`related`](Diagnostic::related) list carries the span of the
	/// first binding.
	///
	/// # Examples
	/// `{x}@(3D6) + {x}@(1D4)`
	DuplicateBinding
	{
		/// The rebound name.
		name: String
	},

	/// A [variable reference](crate::ast::Variable) appears lexically before
	/// the [binding](crate::ast::Binding) that introduces its name. Not
	/// produced by the recovering parse — this kind is produced by the
	/// [`Validator`] semantic pass after a clean parse. The diagnostic's
	/// primary [`span`](Diagnostic::span) points at the offending reference;
	/// the [`related`](Diagnostic::related) list carries the span of the later
	/// binding site.
	///
	/// # Examples
	/// `{x} + {x}@(3D6)`, `{x}@({x})` (self-reference inside the bound
	/// expression)
	UseBeforeBind
	{
		/// The name that was referenced before being bound.
		name: String
	}
}

impl Display for DiagnosticKind
{
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
	{
		match self
		{
			Self::UnclosedDelimiter { opener, .. } =>
			{
				write!(f, "unclosed `{}`", opener)
			},
			Self::MissingExpression { opener } =>
			{
				write!(f, "missing expression in `{}`", opener)
			},
			Self::UnopenedDelimiter { closer } =>
			{
				write!(f, "unexpected `{}`", closer)
			},
			Self::MissingRightOperand { operator } =>
			{
				write!(f, "missing right operand of `{}`", operator)
			},
			Self::MissingLeftOperand { operator } =>
			{
				write!(f, "missing left operand of `{}`", operator)
			},
			Self::BareIdentifier => write!(f, "bare identifier"),
			Self::MissingDiceFaces => write!(f, "missing dice faces"),
			Self::IncompleteDropClause =>
			{
				write!(f, "incomplete drop clause")
			},
			Self::MisplacedDropClause => write!(f, "misplaced drop clause"),
			Self::IncompleteParameterDefinition =>
			{
				write!(f, "incomplete parameter definition")
			},
			Self::MissingParameter => write!(f, "missing parameter"),
			Self::TrailingInput => write!(f, "trailing input"),
			Self::EmptyExpression => write!(f, "empty expression"),
			Self::UnexpectedToken => write!(f, "unexpected token"),
			Self::UnexpectedEof => write!(f, "unexpected end of input"),
			Self::DuplicateParameter { name } =>
			{
				write!(f, "duplicate parameter `{}`", name)
			},
			Self::BindingCollidesWithParameter { name } =>
			{
				write!(
					f,
					"local binding `{}` collides with formal parameter",
					name
				)
			},
			Self::DuplicateBinding { name } =>
			{
				write!(f, "duplicate local binding `{}`", name)
			},
			Self::UseBeforeBind { name } =>
			{
				write!(f, "reference to `{}` precedes its binding", name)
			}
		}
	}
}

/// A diagnostic produced by the [`doctor`](diagnose).
///
/// # Notes
/// The `span` field refers to byte offsets in the **original** source string
/// passed to [`diagnose`], not in the corrected source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic
{
	/// The specific kind of error.
	pub kind: DiagnosticKind,

	/// The region of source text that is erroneous, as byte offsets in the
	/// original source.
	pub span: SourceSpan,

	/// A human-readable error message.
	pub message: String,

	/// Secondary spans that enrich the diagnostic with related context, such
	/// as the prior occurrence of a duplicate name. Each entry is a
	/// [`RelatedLabel`] whose span is in the **original** source coordinates.
	/// Empty for most parse-error diagnostics; populated by semantic
	/// diagnostics that naturally reference multiple source positions.
	pub related: Vec<RelatedLabel>,

	/// Suggested fixes, from most to least specific.
	pub suggestions: Vec<Suggestion>
}

/// A secondary source label attached to a [`Diagnostic`]. Used to point at
/// related positions in the source — for example, the prior declaration of a
/// duplicated parameter — without expanding the primary span of the
/// diagnostic.
///
/// # Notes
/// The `span` refers to byte offsets in the **original** source string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelatedLabel
{
	/// The byte range of the related region in the original source.
	pub span: SourceSpan,

	/// A short human-readable explanation of the relationship, such as
	/// `"first declared here"`.
	pub message: String
}

impl Display for Diagnostic
{
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
	{
		write!(f, "{} ({}): {}", self.kind, self.span, self.message)
	}
}

/// A suggested fix for a diagnostic: a set of [edits](Edit) to the original
/// source, in the manner of a quick fix in an editor.
///
/// # Notes
/// Each suggestion stands alone: its edits apply to the original source passed
/// to [`diagnose`], whether or not the fixes of other diagnostics are applied
/// too. The [`corrected_source`](DiagnoseResult::corrected_source) of the
/// [result](DiagnoseResult) is the source with the first suggestion of every
/// diagnostic applied. Where two fixes interact, e.g., where one removes
/// trailing input that begins with text that the other inserted, their edits
/// may overlap or abut, and `corrected_source` resolves the interaction.
///
/// For unambiguous fixes (e.g., `{x` → `{x}`), no edit has
/// [placeholders](Edit::placeholders). For ambiguous fixes (e.g., `3D` →
/// `3D6`), placeholders identify the inserted defaults that the user may want
/// to replace.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Suggestion
{
	/// A human-readable description of the fix.
	pub description: String,

	/// The edits that make the fix, in ascending order of position. They do
	/// not overlap, though insertions may abut.
	pub edits: Vec<Edit>
}

impl Suggestion
{
	/// Apply the suggestion to the original source.
	///
	/// # Parameters
	/// - `source`: The source string originally passed to [`diagnose`].
	///
	/// # Returns
	/// The source with the [edits](Suggestion::edits) applied.
	///
	/// # Panics
	/// If an edit's [span](Edit::span) is out of bounds or does not fall on
	/// character boundaries of `source`, i.e., if `source` is not the source
	/// that was diagnosed.
	///
	/// # Examples
	/// ```rust
	/// use xdy::diagnostics::diagnose;
	///
	/// let result = diagnose("3D");
	/// let suggestion = &result.diagnostics[0].suggestions[0];
	/// assert_eq!(suggestion.apply("3D"), "3D6");
	/// ```
	pub fn apply(&self, source: &str) -> String
	{
		let mut corrected = String::with_capacity(source.len());
		let mut copied = 0;
		for edit in &self.edits
		{
			corrected.push_str(&source[copied..edit.span.start]);
			corrected.push_str(&edit.replacement);
			copied = edit.span.end;
		}
		corrected.push_str(&source[copied..]);
		corrected
	}
}

/// A replacement of one region of the original source, as part of a
/// [suggestion](Suggestion).
///
/// # Examples
/// Inserting `)` at the end of `(1`:
///
/// ```rust
/// use xdy::{SourceSpan, diagnostics::diagnose};
///
/// let result = diagnose("(1");
/// let edit = &result.diagnostics[0].suggestions[0].edits[0];
/// assert_eq!(edit.span, SourceSpan { start: 2, end: 2 });
/// assert_eq!(edit.replacement, ")");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edit
{
	/// The byte range of the original source to replace. It is empty for an
	/// insertion.
	pub span: SourceSpan,

	/// The replacement text. It is empty for a deletion.
	pub replacement: String,

	/// The regions of the [replacement](Edit::replacement) that are defaults,
	/// which the user may want to replace. Empty for unambiguous fixes.
	pub placeholders: Vec<Placeholder>
}

/// A placeholder region in the [replacement](Edit::replacement) text of an
/// [edit](Edit).
///
/// A sophisticated UI can highlight these regions and present the
/// `valid_kinds` as a picker, allowing the user to type over the default
/// value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Placeholder
{
	/// The byte range of the placeholder, relative to the start of the
	/// [replacement](Edit::replacement) text that contains it.
	pub span: SourceSpan,

	/// A description of what belongs here (e.g., "face count", "operand").
	pub description: &'static str,

	/// The kinds of expressions that are valid at this position.
	pub valid_kinds: &'static [&'static str]
}

/// The result of diagnosing a source string.
///
/// # Notes
/// If all errors are fixable, `corrected_source` contains a parseable source
/// string with all fixes applied. If any error is unfixable, `corrected_source`
/// is `None`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiagnoseResult
{
	/// The diagnostics collected during analysis, in source order.
	pub diagnostics: Vec<Diagnostic>,

	/// The fully corrected source string, if all errors were fixable. This
	/// string will parse cleanly via [`Parser::parse`].
	pub corrected_source: Option<String>
}

/// Valid expression kinds for operand positions.
const OPERAND_KINDS: &[&str] =
	&["integer", "{variable}", "(expression)", "dice expression"];

/// Valid expression kinds for face count positions.
const FACE_COUNT_KINDS: &[&str] = &["integer", "{variable}", "(expression)"];

/// Valid expression kinds for positions that only a dice expression may fill.
const DICE_KINDS: &[&str] = &["dice expression"];

/// Valid kinds for drop direction.
const DROP_DIRECTION_KINDS: &[&str] = &["lowest", "highest"];

////////////////////////////////////////////////////////////////////////////////
//                              Error analysis.                               //
////////////////////////////////////////////////////////////////////////////////

/// The doctor's [recovery policy](Recovery). The parser consults it at each
/// [failure](FailureSite) of a single recovering parse of the source; it
/// diagnoses the failure and, if it can, answers the [repair](Repair) that
/// makes the parse continue as though the source had been edited by the first
/// suggestion of the diagnostic.
///
/// The parser reads the source with its [stray whitespace](stray_whitespace)
/// replaced by spaces of the same length, which the doctor diagnoses itself, so
/// every position is a position of the original source, and the diagnostics
/// and their edits need no mapping. To keep the whole diagnosis linear in the
/// length of the source, the doctor inspects the text around a failure in
/// constant amortized time: the whitespace before the failure, which it
/// remembers for the next failure at the same position, and the token at the
/// failure, which the repair consumes. Only a failure after formal parameters,
/// which happens at most once, inspects the parameters in full, and only a
/// variable without its `}` inspects its name in full, which ends where the
/// next name can begin.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text.
#[derive(Debug)]
struct Doctor<'src>
{
	/// The source text that the parser reads: the original source, with its
	/// stray whitespace replaced by spaces.
	source: &'src str,

	/// The original source.
	original: &'src str,

	/// The byte offsets of the start and end of each character of [stray
	/// whitespace](stray_whitespace) in the original source, in order.
	strays: Vec<(usize, usize)>,

	/// The byte offset of the end of the source without its trailing
	/// whitespace. A failure at or beyond it is at the end of the input.
	content_end: usize,

	/// The diagnostics, in the order of the failures.
	diagnostics: Vec<Diagnostic>,

	/// The edits of the first suggestions of the diagnostics, in the order of
	/// the failures.
	fixes: Vec<Edit>,

	/// The compound fix whose tokens the parser is still supplying, if any.
	compound: Option<Compound>,

	/// The state of the diagnosis.
	state: State,

	/// The last position whose preceding character the doctor looked for, and
	/// that character and its byte offset, if any.
	preceding: Option<(usize, Option<(char, usize)>)>,

	/// The byte offsets of the start and end of the last [bare
	/// word](crate::parser::bare_word) that the doctor scanned.
	bare_word: Option<(usize, usize)>,

	/// Whether a fix removes trailing input.
	trailing: bool,

	/// The byte offset of a misplaced `drop`, before which a fix supplied a
	/// dice expression, if the parser has yet to read the `d` of the `drop`
	/// as its dice operator, and fail at its faces.
	reread: Option<usize>,

	/// The byte offsets of the start and end of the last run of unopened
	/// closing delimiters that fixes discarded, separated by whitespace only.
	skipped: Option<(usize, usize)>
}

/// The state of a [diagnosis](Doctor).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum State
{
	/// Every failure so far has a fix.
	Fixing,

	/// A fix replaced the rest of the source, so no later failure matters.
	Supplanted,

	/// A failure has no fix.
	Unfixable
}

/// Where a [bare identifier](DiagnosticKind::BareIdentifier) lies.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Place
{
	/// At the start of the input, where formal parameters may begin.
	Start,

	/// After the `,` of a formal parameter.
	Parameters,

	/// Within an expression.
	Expression
}

/// The [doctor's](Doctor) analysis of a failure.
#[derive(Debug)]
enum Analysis
{
	/// A diagnostic, and the repair that corresponds to its first suggestion,
	/// or [`Stop`](Repair::Stop) if it has none.
	Diagnosed(Diagnostic, Repair),

	/// A diagnostic whose first suggestion replaces the rest of the source,
	/// and whose repair is therefore [`Fix`](Repair::Fix), which supplies the
	/// first token of the replacement.
	Supplanting(Diagnostic),

	/// The first token of a compound fix, whose repair is
	/// [`Fix`](Repair::Fix).
	Compound(Compound)
}

impl<'src> Doctor<'src>
{
	/// Create a doctor for the specified source.
	///
	/// # Parameters
	/// - `source`: The source text that the parser reads: the original source,
	///   with its stray whitespace replaced by spaces of the same length.
	/// - `original`: The original source.
	/// - `strays`: The byte offsets of the start and end of each character of
	///   [stray whitespace](stray_whitespace) in the original source, in order.
	///
	/// # Returns
	/// The doctor.
	fn new(
		source: &'src str,
		original: &'src str,
		strays: Vec<(usize, usize)>
	) -> Self
	{
		Self {
			source,
			original,
			strays,
			content_end: source.trim_end_matches(is_token_space).len(),
			diagnostics: Vec::new(),
			fixes: Vec::new(),
			compound: None,
			state: State::Fixing,
			preceding: None,
			bare_word: None,
			trailing: false,
			reread: None,
			skipped: None
		}
	}

	/// Finish the diagnosis, once the recovering parse is over.
	///
	/// # Parameters
	/// - `recovered`: Whether the recovering parse succeeded.
	///
	/// # Returns
	/// The result of the diagnosis.
	fn finish(mut self, recovered: bool) -> DiagnoseResult
	{
		self.finish_compound();
		// The parser declines a repair that it cannot make, in which case the
		// parse fails although the doctor fixed every failure.
		let fixed = match self.state
		{
			State::Fixing => recovered,
			State::Supplanted => true,
			State::Unfixable => false
		};
		self.diagnose_strays();
		let corrected_source = fixed.then(|| {
			// An insertion precedes a replacement at the same position, whose
			// text it therefore cannot have followed.
			self.fixes
				.sort_by_key(|edit| (edit.span.start, edit.span.end));
			let mut corrected = apply(self.original, &self.fixes);
			// The removal of trailing input leaves no trailing whitespace, not
			// even whitespace that an earlier fix inserted.
			if self.trailing
			{
				corrected
					.truncate(corrected.trim_end_matches(is_token_space).len());
			}
			corrected
		});
		DiagnoseResult {
			diagnostics: self.diagnostics,
			corrected_source
		}
	}

	/// Diagnose the [stray whitespace](stray_whitespace), and record the fixes,
	/// but not where another fix replaces it, e.g., within trailing input that
	/// a fix removes. The diagnostics of the strays join the others in source
	/// order: each precedes the first other diagnostic that begins after it.
	fn diagnose_strays(&mut self)
	{
		let mut replaced = self
			.fixes
			.iter()
			.filter(|edit| edit.span.start < edit.span.end)
			.map(|edit| (edit.span.start, edit.span.end))
			.collect::<Vec<_>>();
		replaced.sort_unstable();
		let mut replaced = replaced.into_iter().peekable();
		// The greatest end of the regions that begin at or before a stray.
		let mut reach = 0;
		let mut strays = Vec::new();
		for &(start, end) in &self.strays
		{
			while let Some((_, region_end)) =
				replaced.next_if(|&(region_start, _)| region_start <= start)
			{
				reach = reach.max(region_end);
			}
			if reach < end
			{
				let diagnostic =
					make_stray_whitespace(self.original, start, end);
				self.fixes.extend(diagnostic.suggestions[0].edits.clone());
				strays.push(diagnostic);
			}
		}
		let mut strays = strays.into_iter().peekable();
		let others = std::mem::take(&mut self.diagnostics);
		for diagnostic in others
		{
			self.diagnostics.extend(iter::from_fn(|| {
				strays.next_if(|stray| stray.span.start < diagnostic.span.start)
			}));
			self.diagnostics.push(diagnostic);
		}
		self.diagnostics.extend(strays);
	}

	/// Record the diagnostic of the pending compound fix, if any.
	fn finish_compound(&mut self)
	{
		if let Some(compound) = self.compound.take()
		{
			self.record(compound.diagnostic(self.source));
		}
	}

	/// Record a diagnostic, and the edits of its first suggestion, if any.
	///
	/// # Parameters
	/// - `diagnostic`: The diagnostic.
	fn record(&mut self, diagnostic: Diagnostic)
	{
		if let Some(first) = diagnostic.suggestions.first()
		{
			self.fixes.extend(first.edits.iter().cloned());
		}
		self.diagnostics.push(diagnostic);
	}

	/// Answer the nearest character before a position that is not whitespace,
	/// and its byte offset. The answer for the last position asked about is
	/// remembered, since several failures may lie at the same position.
	/// Otherwise, since every failure but one at the end of the input lies at
	/// a character that is not whitespace, the whitespace scanned for
	/// different failures does not overlap.
	///
	/// # Parameters
	/// - `pos`: The byte offset of the position.
	///
	/// # Returns
	/// The character and its byte offset, if any.
	fn preceding(&mut self, pos: usize) -> Option<(char, usize)>
	{
		if let Some((at, preceding)) = self.preceding
			&& at == pos
		{
			return preceding;
		}
		let before = |end: usize| {
			self.source[..end]
				.char_indices()
				.rev()
				.find(|(_, c)| !is_token_space(*c))
				.map(|(i, c)| (c, i))
		};
		// Look past discarded delimiters, as though they were whitespace.
		let preceding = match (before(pos), self.skipped)
		{
			(Some((_, i)), Some((start, end))) if start <= i && i < end =>
			{
				before(start)
			},
			(preceding, _) => preceding
		};
		self.preceding = Some((pos, preceding));
		preceding
	}

	/// Record that a fix discards an unopened closing delimiter.
	///
	/// # Parameters
	/// - `start`: The byte offset of the delimiter.
	/// - `end`: The byte offset of the end of the delimiter.
	fn skip(&mut self, start: usize, end: usize)
	{
		self.skipped = match self.skipped
		{
			Some((first, last))
				if self.source[last..start]
					.trim_matches(is_token_space)
					.is_empty() =>
			{
				Some((first, end))
			},
			_ => Some((start, end))
		};
	}

	/// Answer the byte offset of the end of the [bare
	/// word](crate::parser::bare_word) that begins at a position, if any. A
	/// bare word that begins within the last bare word scanned ends where that
	/// one does, so its end is not scanned again.
	///
	/// # Parameters
	/// - `pos`: The byte offset of the position.
	///
	/// # Returns
	/// The byte offset of the end of the bare word, if one begins at `pos`.
	fn bare_word_end(&mut self, pos: usize) -> Option<usize>
	{
		if !self.source[pos..].starts_with(is_bare_word_start)
		{
			return None;
		}
		if let Some((start, end)) = self.bare_word
			&& start <= pos
			&& pos < end
		{
			return Some(end);
		}
		let remaining = &self.source[pos..];
		let end = pos
			+ remaining
				.find(|c: char| !is_bare_word_continue(c))
				.unwrap_or(remaining.len());
		self.bare_word = Some((pos, end));
		Some(end)
	}

	/// Analyze a failure, choosing the diagnostic and repair from the site of
	/// the failure, the expectations of its error, and the text around it.
	///
	/// # Parameters
	/// - `site`: The failure.
	///
	/// # Returns
	/// The analysis.
	fn analyze(&mut self, site: &FailureSite<'src>) -> Analysis
	{
		let source = self.source;
		let pos = site.position();
		let at_eof = pos >= self.content_end;
		let is_goal = matches!(site.site, Site::Goal { .. });

		// Collect all expectations at the position of the failure.
		let expectations = site
			.error
			.errors
			.iter()
			.flat_map(|(_, kind)| kind.expectations())
			.collect::<Vec<_>>();

		// Check for specific patterns based on the failure and the source.
		let expects_expression = expectations.iter().any(|e| {
			let s = e.as_ref();
			s == "integer"
				|| s == "dice expression"
				|| s == "`(`"
				|| s == "`{`"
				|| s == "`[`"
				|| s == "`-`"
		});
		let expects_delimiter =
			expectations.iter().find_map(|e| match e.as_ref()
			{
				"`)`" => Some(('(', ')')),
				"`]`" => Some(('[', ']')),
				"`}`" => Some(('{', '}')),
				_ => None
			});

		// Pattern: closing brace of a variable expected → unclosed delimiter.
		// The name reads everything up to the next brace or the end of the
		// input, so break it early, before an operator or delimiter, if it has
		// one; otherwise, close it at the failure. The name ends at the
		// failure, and the next one begins at or after it, so no two scans of
		// names overlap.
		if let Site::Closer {
			opener: opener_pos,
			closer: '}'
		} = site.site
		{
			let name = source[opener_pos + 1..pos].trim_start();
			let start = pos - name.len();
			let ends = unclosed_name_ends(name)
				.into_iter()
				.flatten()
				.map(|length| start + length)
				.collect::<Vec<_>>();
			let repair = ends
				.first()
				.map_or(Repair::Fix, |&end| Repair::Break { end });
			return Analysis::Diagnosed(
				make_unclosed_brace(source, opener_pos, &ends, pos, at_eof),
				repair
			);
		}

		// Pattern: closing delimiter expected at EOF → unclosed delimiter.
		if at_eof
			&& let Some((opener, closer)) = expects_delimiter
			&& let Site::Closer {
				opener: opener_pos, ..
			} = site.site
		{
			return Analysis::Diagnosed(
				make_unclosed_delimiter(opener, closer, opener_pos, pos),
				Repair::Fix
			);
		}

		// Pattern: a drop clause that follows no dice expression, e.g.,
		// `3 drop lowest`, whose `d` the parser read as a dice operator, or
		// `1 + drop lowest`, where an expression was expected, rather than a
		// bare name that splits `drop` at its `d`. After an operand, the parse
		// supplies faces and reads the `drop` again. Where an expression was
		// expected, the parse supplies an operand, which it reads as the count
		// of a dice expression whose operator is the `d` of `drop`, and the
		// faces then fail, as after an operand.
		let misplaced_drop = match site.site
		{
			Site::Faces => self
				.preceding(pos)
				.filter(|&(c, d_pos)| c == 'd' && begins_drop(&source[d_pos..]))
				.map(|(_, d_pos)| d_pos),
			Site::Goal { .. }
				if expects_expression && begins_drop(&source[pos..]) =>
			{
				Some(pos)
			},
			_ => None
		};
		if let Some(drop_pos) = misplaced_drop
		{
			let after_operand = site.site == Site::Faces;
			let repair = if after_operand
			{
				Repair::Reread
			}
			else
			{
				self.reread = Some(drop_pos);
				Repair::Fix
			};
			return Analysis::Diagnosed(
				make_misplaced_drop(source, drop_pos, after_operand),
				repair
			);
		}

		// Pattern: expression expected, and the text at the error position is
		// an identifier → bare identifier in expression context. The
		// identifier must begin the goal or the faces, which a `-` might
		// begin instead.
		let reads_variable = match site.site
		{
			Site::Goal { .. } => true,
			Site::Faces => self.preceding(pos).is_none_or(|(c, _)| c != '-'),
			_ => false
		};
		if expects_expression && !at_eof && reads_variable
		{
			// Only a name at the start of the input may begin formal
			// parameters.
			let place = if is_goal && self.preceding(pos).is_none()
			{
				Place::Start
			}
			else
			{
				Place::Expression
			};
			if let Some((diagnostic, end)) = self.bare_identifier_at(pos, place)
			{
				return Analysis::Diagnosed(
					diagnostic,
					Repair::Variable { end }
				);
			}
		}

		// Pattern: formal parameter expected after `,`, and the text after
		// the `,` is an identifier → bare identifier as formal parameter; or
		// the text after the `,` is another `,` or the `:` → a stray comma.
		if site.site == Site::ParameterName
		{
			let start = source.len()
				- source[pos..].trim_start_matches(is_token_space).len();
			if let Some((diagnostic, end)) =
				self.bare_identifier_at(start, Place::Parameters)
			{
				return Analysis::Diagnosed(
					diagnostic,
					Repair::Variable { end }
				);
			}
			if source[start..].starts_with([',', ':'])
				&& source[..pos].ends_with(',')
			{
				return Analysis::Diagnosed(
					make_missing_parameter(pos - 1, start),
					Repair::Retract
				);
			}
		}

		// Pattern: expression or identifier expected after delimiter/operator.
		let expects_identifier =
			expectations.iter().any(|e| e.as_ref() == "identifier");
		if (expects_expression || expects_identifier)
			&& let Some((prev, prev_pos)) = self.preceding(pos)
		{
			if (prev == 'd' || prev == 'D')
				&& (is_goal || site.site == Site::Faces)
			{
				return Analysis::Diagnosed(
					make_missing_dice_faces(source, prev_pos),
					Repair::Fix
				);
			}
			if "+-*×/÷%^".contains(prev) && is_goal
			{
				return Analysis::Diagnosed(
					make_missing_right_operand(source, prev, prev_pos, pos),
					Repair::Fix
				);
			}
			// Opening delimiter with no expression inside:
			// e.g., `(` → `(0)`, `[` → `[0:0]`, `{` → `{x}`.
			let shape = match (prev, site.site)
			{
				('(', Site::Goal { .. }) => Some(Shape::Group),
				('[', Site::Goal { .. }) => Some(Shape::Range),
				('[', Site::FaceValue { .. }) => Some(Shape::CustomFaces),
				('{', Site::VariableName { .. }) => Some(Shape::Variable),
				_ => None
			};
			if let Some(shape) = shape
			{
				return Analysis::Compound(Compound::new(shape, prev_pos, pos));
			}
			// After `:` inside a range, e.g., `[1:` → insert end expression and
			// `]`.
			if prev == ':'
				&& let Site::Goal {
					range: Some(bracket),
					..
				} = site.site
			{
				return Analysis::Compound(Compound::new(
					Shape::RangeEnd,
					bracket,
					pos
				));
			}
		}

		// Pattern: drop direction expected → incomplete drop clause. The drop
		// keyword ends just before the whitespace before the direction.
		if site.site == Site::Direction
			&& expectations
				.iter()
				.any(|e| e.as_ref() == "`lowest`" || e.as_ref() == "`highest`")
			&& let Some((last, last_pos)) = self.preceding(pos)
		{
			let drop_end = last_pos + last.len_utf8();
			return Analysis::Diagnosed(
				make_incomplete_drop(drop_end.saturating_sub(4), drop_end),
				Repair::Fix
			);
		}

		// Pattern: operator at start of input, perhaps after unopened closing
		// delimiters that earlier fixes discarded → missing left operand.
		if !at_eof && (pos == 0 || self.skipped == Some((0, pos))) && is_goal
		{
			let ch = source[pos..].chars().next().unwrap_or('\0');
			if "+-*×/÷%^".contains(ch)
			{
				return Analysis::Diagnosed(
					make_missing_left_operand(source, ch, pos),
					Repair::Fix
				);
			}
		}

		// Pattern: empty or whitespace-only input.
		if at_eof && is_goal && self.preceding(pos).is_none()
		{
			// The expression supplants the whitespace, but not the delimiters
			// that earlier fixes discarded.
			let start = self.skipped.map_or(0, |(_, end)| end);
			return Analysis::Diagnosed(
				make_empty_expression(start, source.len()),
				Repair::Fix
			);
		}

		// Pattern: formal parameters, missing `:` and perhaps body.
		if site.site == Site::ParameterColon
			&& expectations
				.iter()
				.any(|e| e.as_ref() == "`,`" || e.as_ref() == "`:`")
			&& let Some(analysis) = self.incomplete_parameters(pos)
		{
			return analysis;
		}

		// Pattern: bare closing delimiter where an expression was expected →
		// the user has a spurious `)`, `]`, or `}` with no matching opener.
		// This is more specific than the catch-all `UnexpectedToken` and
		// produces a `remove the unopened` suggestion. Only fires when an
		// expression was expected at `pos`; if the parser had already
		// consumed a complete expression, the trailing-input branch below
		// handles it instead.
		if expects_expression && !at_eof && is_goal
		{
			let ch = source[pos..].chars().next().unwrap_or('\0');
			if ch == ')' || ch == ']' || ch == '}'
			{
				let end = pos + ch.len_utf8();
				self.skip(pos, end);
				return Analysis::Diagnosed(
					make_unopened_delimiter(ch, pos),
					Repair::Skip { end }
				);
			}
		}

		// Pattern: end of input from all_consuming → trailing input.
		if site.site == Site::TrailingInput
			&& expectations.iter().any(|e| e.as_ref() == "end of input")
			&& !at_eof
		{
			self.trailing = true;
			return Analysis::Diagnosed(self.trailing_input(pos), Repair::Fix);
		}

		// Catch-all: unexpected token or EOF.
		let diagnostic = if at_eof
		{
			Diagnostic {
				kind: DiagnosticKind::UnexpectedEof,
				span: SourceSpan {
					start: pos,
					end: pos
				},
				message: "unexpected end of input".into(),
				related: vec![],
				suggestions: vec![]
			}
		}
		else
		{
			let (start, end, token) = unexpected_token(source, pos);
			Diagnostic {
				kind: DiagnosticKind::UnexpectedToken,
				span: SourceSpan { start, end },
				message: format!("unexpected `{}`", token),
				related: vec![],
				suggestions: vec![]
			}
		};
		Analysis::Diagnosed(diagnostic, Repair::Stop)
	}

	/// Detect a bare identifier at the position of a failure where an
	/// expression or a formal parameter was expected. The user almost
	/// certainly forgot to wrap it in `{}`, or wrote a formal parameter or a
	/// local binding as the language once did, without braces, e.g., `x, y: x
	/// + y` or `x@(3D6)`. A formal parameter or a local binding is wrapped
	/// whole; a variable may instead be split before a `d` or `D`, e.g., `xD6`.
	///
	/// # Parameters
	/// - `pos`: The position of the identifier.
	/// - `place`: Where the identifier lies.
	///
	/// # Returns
	/// A `BareIdentifier` diagnostic, if the text at `pos` is a [bare
	/// word](crate::parser::bare_word), and the byte offset of the end of the
	/// name that its first suggestion makes.
	fn bare_identifier_at(
		&mut self,
		pos: usize,
		place: Place
	) -> Option<(Diagnostic, usize)>
	{
		let ident_end = self.bare_word_end(pos)?;
		let name = self.source[pos..ident_end].trim_end_matches(is_token_space);
		if name.is_empty()
		{
			return None;
		}
		// The identifier may have swallowed inline whitespace after the name,
		// which both fixes leave where it is, e.g., `{hello} * 3`.
		let name_end = pos + name.len();
		let mut suggestions = Vec::new();

		// A formal parameter precedes a `,` or `:`, and the name of a local
		// binding precedes its `@`.
		let follows =
			self.source[name_end..].trim_start_matches(is_token_space);
		let what = match place
		{
			Place::Parameters => "parameter",
			Place::Start if follows.starts_with([',', ':']) => "parameter",
			_ if follows.starts_with('@') => "binding",
			_ => "variable"
		};

		// Check if a variable contains `d`/`D` — if so, offer both a split
		// fix (e.g., `{x}D6`) and a whole-variable fix (e.g., `{xD6}`). The
		// split fix leaves the whitespace before the `d` outside the braces,
		// e.g., `{abc} d6`.
		let split_pos = name.char_indices().find(|&(i, c)| {
			what == "variable"
				&& (c == 'd' || c == 'D')
				&& i > 0
				&& name[..i].starts_with(is_bare_word_start)
		});
		let prefix =
			split_pos.map(|(i, _)| name[..i].trim_end_matches(is_token_space));
		if let Some(prefix) = prefix
		{
			suggestions.push(Suggestion {
				description: format!(
					"wrap `{}` in braces",
					canonical_name(prefix)
				),
				edits: vec![
					insertion(pos, "{"),
					insertion(pos + prefix.len(), "}"),
				]
			});
		}
		// Always offer wrapping the whole identifier.
		suggestions.push(Suggestion {
			description: format!(
				"use `{}` as a {} name",
				canonical_name(name),
				what
			),
			edits: vec![insertion(pos, "{"), insertion(name_end, "}")]
		});

		// Use the first (most specific) suggestion's description for the
		// message. If there's a `d`/`D` split, the bare name is the prefix.
		// Text shows the name that the braced fix would denote, e.g., not the
		// U+00A0 NO-BREAK SPACE that the fix braces after `hit points`.
		let bare_name = prefix.unwrap_or(name);
		let end = pos + bare_name.len();
		Some((
			Diagnostic {
				kind: DiagnosticKind::BareIdentifier,
				span: SourceSpan {
					start: pos,
					end: name_end
				},
				message: format!(
					"bare identifier `{}` is not valid here; \
					 {}s must be wrapped in `{{}}`",
					canonical_name(bare_name),
					what
				),
				related: vec![],
				suggestions
			},
			end
		))
	}

	/// Detect an incomplete parameter definition, where formal parameters lack
	/// their `:`, and offer to complete it. Only two or more formal parameters
	/// can lack their `:`, since a lone braced name without one begins the
	/// body instead.
	///
	/// # Parameters
	/// - `pos`: The position of the failure.
	///
	/// # Returns
	/// An `IncompleteParameterDefinition` analysis, if the pattern matches.
	fn incomplete_parameters(&self, pos: usize) -> Option<Analysis>
	{
		let source = self.source;
		// The formal parameters lie before `pos`, past any delimiters that
		// earlier fixes discarded. Earlier fixes may have edited them, e.g.,
		// by bracing bare names, so edit them likewise.
		let start = self.skipped.map_or(0, |(_, end)| end);
		let mut edits = self
			.fixes
			.iter()
			.filter(|edit| start <= edit.span.start && edit.span.end <= pos)
			.map(|edit| {
				replacement(
					edit.span.start - start,
					edit.span.end - start,
					&edit.replacement
				)
			})
			.collect::<Vec<_>>();
		edits.sort_by_key(|edit| (edit.span.start, edit.span.end));
		let params = apply(&source[start..pos], &edits);
		if params.trim_matches(is_token_space).is_empty()
		{
			return None;
		}
		// The formal parameters end before any whitespace before `pos`, where
		// the `:` belongs.
		let end =
			start + source[start..pos].trim_end_matches(is_token_space).len();

		let trailing = source[pos..].trim_start_matches(is_token_space);
		// The start of the trailing content, past any whitespace after `pos`.
		let trailing_start = source.len() - trailing.len();

		// Try incorporating trailing content as the body. Only use it if the
		// result actually parses; otherwise fall back to a placeholder, which
		// supplants the trailing content. This parses the source once more, but
		// only once, since the formal parameters fail only once.
		let trailing_works = !trailing.is_empty() && {
			let mut candidate = params;
			candidate.push_str(": ");
			candidate.push_str(trailing);
			Parser::parse(&candidate).is_ok()
		};
		let suggestion = if trailing_works
		{
			Suggestion {
				description: "complete parameter definition".into(),
				edits: vec![replacement(end, trailing_start, ": ")]
			}
		}
		else
		{
			Suggestion {
				description: "complete parameter definition".into(),
				edits: vec![Edit {
					span: SourceSpan {
						start: end,
						end: source.len()
					},
					replacement: ": 0".into(),
					placeholders: vec![Placeholder {
						span: SourceSpan { start: 2, end: 3 },
						description: "expression",
						valid_kinds: OPERAND_KINDS
					}]
				}]
			}
		};
		let diagnostic = Diagnostic {
			kind: DiagnosticKind::IncompleteParameterDefinition,
			span: SourceSpan { start, end },
			message: "expected `:` and expression body after parameters".into(),
			related: vec![],
			suggestions: vec![suggestion]
		};
		Some(
			if trailing_works
			{
				Analysis::Diagnosed(diagnostic, Repair::Fix)
			}
			else
			{
				Analysis::Supplanting(diagnostic)
			}
		)
	}

	/// Create a diagnostic for trailing input after a complete function. The
	/// fix removes the trailing input and the whitespace before it, but not
	/// text that an earlier fix inserted.
	///
	/// # Parameters
	/// - `pos`: The byte offset of the trailing input.
	///
	/// # Returns
	/// A diagnostic with a `TrailingInput` kind.
	fn trailing_input(&self, pos: usize) -> Diagnostic
	{
		let source = self.source;
		let kept = self.fixes.iter().map(|edit| edit.span.end).fold(
			source[..pos].trim_end_matches(is_token_space).len(),
			usize::max
		);
		Diagnostic {
			kind: DiagnosticKind::TrailingInput,
			span: SourceSpan {
				start: pos,
				end: source.len()
			},
			message: format!(
				"unexpected `{}` after expression",
				unexpected_token(source, pos).2
			),
			related: vec![],
			suggestions: vec![Suggestion {
				description: "remove trailing input".into(),
				edits: vec![replacement(kept, source.len(), "")]
			}]
		}
	}
}

impl<'src> Recovery<'src> for Doctor<'src>
{
	fn repair(&mut self, site: &FailureSite<'src>) -> Repair
	{
		if self.state == State::Supplanted
		{
			return Repair::Stop;
		}
		if let Some(compound) = &mut self.compound
		{
			if compound.absorbs(site)
			{
				return Repair::Fix;
			}
			self.finish_compound();
		}
		// The faces of the dice expression that a fix supplied before a
		// misplaced `drop` fail at once, and the fix already accounts for them.
		if let Some(drop_pos) = self.reread.take()
			&& site.site == Site::Faces
			&& self
				.preceding(site.position())
				.is_some_and(|(_, operator)| operator == drop_pos)
		{
			return Repair::Reread;
		}
		match self.analyze(site)
		{
			Analysis::Diagnosed(diagnostic, repair) =>
			{
				if repair == Repair::Stop
				{
					self.state = State::Unfixable;
				}
				self.record(diagnostic);
				repair
			},
			Analysis::Supplanting(diagnostic) =>
			{
				self.state = State::Supplanted;
				self.record(diagnostic);
				Repair::Fix
			},
			Analysis::Compound(compound) =>
			{
				self.compound = Some(compound);
				Repair::Fix
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
//                              Fix generation.                               //
////////////////////////////////////////////////////////////////////////////////

/// Apply edits to a source.
///
/// # Parameters
/// - `source`: The source.
/// - `edits`: The edits, in ascending order of position. They must not overlap,
///   though insertions may abut.
///
/// # Returns
/// The source with the edits applied.
///
/// # Panics
/// If an edit's [span](Edit::span) is out of bounds or does not fall on
/// character boundaries of `source`.
fn apply(source: &str, edits: &[Edit]) -> String
{
	let mut corrected = String::with_capacity(source.len());
	let mut copied = 0;
	for edit in edits
	{
		corrected.push_str(&source[copied..edit.span.start]);
		corrected.push_str(&edit.replacement);
		copied = edit.span.end;
	}
	corrected.push_str(&source[copied..]);
	corrected
}

/// Create an [edit](Edit) that inserts text, with no placeholders.
///
/// # Parameters
/// - `pos`: The byte offset at which to insert.
/// - `text`: The text to insert.
///
/// # Returns
/// The edit.
fn insertion(pos: usize, text: &str) -> Edit { replacement(pos, pos, text) }

/// Create an [edit](Edit) that replaces a region with text, with no
/// placeholders.
///
/// # Parameters
/// - `start`: The byte offset of the start of the region.
/// - `end`: The byte offset of the end of the region.
/// - `text`: The replacement text, or `""` to delete the region.
///
/// # Returns
/// The edit.
fn replacement(start: usize, end: usize, text: &str) -> Edit
{
	Edit {
		span: SourceSpan { start, end },
		replacement: text.to_string(),
		placeholders: vec![]
	}
}

/// Answer the unexpected token at or after a position, past any [whitespace
/// between tokens](is_token_space), to show in a message. The token is the
/// text up to the next whitespace, but at least one character. Whitespace
/// that may not separate tokens, e.g., U+00A0 NO-BREAK SPACE, is a token by
/// itself, and is shown by its code point, e.g., `U+00A0`, since it would be
/// invisible.
///
/// # Parameters
/// - `source`: The source.
/// - `pos`: The byte offset of the position.
///
/// # Returns
/// The byte offsets of the start and end of the token, and the token as
/// shown, which is empty at the end of the source.
fn unexpected_token(source: &str, pos: usize) -> (usize, usize, Cow<'_, str>)
{
	let rest = source[pos..].trim_start_matches(is_token_space);
	let start = source.len() - rest.len();
	match rest.chars().next()
	{
		None => (start, start, Cow::Borrowed("")),
		Some(c) if c.is_whitespace() =>
		{
			let token = format!("U+{:04X}", u32::from(c));
			(start, start + c.len_utf8(), Cow::Owned(token))
		},
		Some(c) =>
		{
			let len = rest[c.len_utf8()..]
				.find(char::is_whitespace)
				.map_or(rest.len(), |i| c.len_utf8() + i);
			(start, start + len, Cow::Borrowed(&rest[..len]))
		}
	}
}

/// Find the stray whitespace of a source: each character of whitespace outside
/// of braces that may not [separate tokens](is_token_space), e.g., U+00A0
/// NO-BREAK SPACE, which text pasted from a word processor often carries.
/// Inside braces, any whitespace belongs to the name. The name of a variable
/// whose `}` is missing ends at the next brace or the end of the source, or
/// where the doctor [breaks](unclosed_name_ends) it, if it does, since the
/// text after the break lies between tokens. The scan takes time linear in the
/// length of the source.
///
/// # Parameters
/// - `source`: The source.
///
/// # Returns
/// The byte offsets of the start and end of each character of stray
/// whitespace, in order.
fn stray_whitespace(source: &str) -> Vec<(usize, usize)>
{
	let mut strays = Vec::new();
	let mut at = 0;
	while let Some(c) = source[at..].chars().next()
	{
		if c == '{'
		{
			let opened = at + 1;
			let end = source[opened..]
				.find(['{', '}'])
				.map_or(source.len(), |i| opened + i);
			at = if source[end..].starts_with('}')
			{
				end + 1
			}
			else
			{
				let name = source[opened..end].trim_start();
				match unclosed_name_ends(name)
				{
					[Some(length), _] => end - name.len() + length,
					_ => end
				}
			};
			continue;
		}
		if c.is_whitespace() && !is_token_space(c)
		{
			strays.push((at, at + c.len_utf8()));
		}
		at += c.len_utf8();
	}
	strays
}

/// The shape of a [compound fix](Compound): the construct whose contents it
/// supplies.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Shape
{
	/// A group or the expression of a binding, after its `(`: `0)`.
	Group,

	/// A range, after its `[`: `0:0]`.
	Range,

	/// Custom faces, after their `[`: `0]`.
	CustomFaces,

	/// A variable, after its `{`: `x}`.
	Variable,

	/// The end of a range, after its `:`: `0]`.
	RangeEnd
}

/// A [token](Token) that a [compound fix](Compound) may supply, and the
/// [site](Site) of the failure at which the parser needs it.
#[derive(Debug)]
struct Token
{
	/// The text of the token.
	text: &'static str,

	/// Whether the parser needs the token at the specified site.
	needed_at: fn(Site) -> bool,

	/// The description and valid kinds of the placeholder that the token is,
	/// if any.
	placeholder: Option<(&'static str, &'static [&'static str])>
}

impl Token
{
	/// An operand, needed at a [goal](Site::Goal).
	///
	/// # Parameters
	/// - `description`: The description of the placeholder.
	///
	/// # Returns
	/// The token.
	const fn operand(description: &'static str) -> Self
	{
		Self {
			text: "0",
			needed_at: |site| matches!(site, Site::Goal { .. }),
			placeholder: Some((description, OPERAND_KINDS))
		}
	}

	/// A closing delimiter, needed at a [closer](Site::Closer).
	///
	/// # Parameters
	/// - `text`: The delimiter.
	///
	/// # Returns
	/// The token.
	const fn closer(text: &'static str) -> Self
	{
		Self {
			text,
			needed_at: |site| matches!(site, Site::Closer { .. }),
			placeholder: None
		}
	}
}

impl Shape
{
	/// Answer the tokens that a compound fix of this shape supplies, in order.
	///
	/// # Returns
	/// The tokens.
	fn tokens(self) -> &'static [Token]
	{
		const GROUP: &[Token] =
			&[Token::operand("expression"), Token::closer(")")];
		const RANGE: &[Token] = &[
			Token::operand("range start"),
			Token {
				text: ":",
				needed_at: |site| matches!(site, Site::Colon { .. }),
				placeholder: None
			},
			Token::operand("range end"),
			Token::closer("]")
		];
		const CUSTOM_FACES: &[Token] = &[
			Token {
				text: "0",
				needed_at: |site| matches!(site, Site::FaceValue { .. }),
				placeholder: Some(("face value", &["integer"]))
			},
			Token::closer("]")
		];
		const VARIABLE: &[Token] = &[
			Token {
				text: "x",
				needed_at: |site| matches!(site, Site::VariableName { .. }),
				placeholder: Some(("identifier", &["identifier"]))
			},
			Token::closer("}")
		];
		const RANGE_END: &[Token] =
			&[Token::operand("range end"), Token::closer("]")];
		match self
		{
			Shape::Group => GROUP,
			Shape::Range => RANGE,
			Shape::CustomFaces => CUSTOM_FACES,
			Shape::Variable => VARIABLE,
			Shape::RangeEnd => RANGE_END
		}
	}

	/// Answer what a compound fix of this shape supplies, when it supplies
	/// only the specified number of its tokens, because the source supplies
	/// the rest.
	///
	/// # Parameters
	/// - `supplied`: The number of tokens supplied.
	///
	/// # Returns
	/// A description of the supplied tokens.
	fn what(self, supplied: usize) -> &'static str
	{
		match (self, supplied)
		{
			(Shape::Group, _) => "expression",
			(Shape::Range, 1) => "range start",
			(Shape::Range, _) => "range start and end",
			(Shape::CustomFaces, _) => "face value",
			(Shape::Variable, _) => "identifier",
			(Shape::RangeEnd, _) => "range end"
		}
	}
}

/// A fix that supplies the missing contents of a delimited construct, and
/// perhaps its closing delimiter, e.g., `0)` after `(`. The parser needs the
/// tokens of the fix one at a time, each at a failure at the same position;
/// the fix supplies only those tokens that the parser needs, since the source
/// may supply the rest, e.g., the `)` of `( )`.
#[derive(Debug)]
struct Compound
{
	/// The shape of the fix.
	shape: Shape,

	/// The byte offset of the opening delimiter of the construct.
	opener_pos: usize,

	/// The byte offset at which the fix inserts its tokens.
	at: usize,

	/// The number of tokens supplied so far.
	supplied: usize
}

impl Compound
{
	/// Begin a compound fix, supplying its first token.
	///
	/// # Parameters
	/// - `shape`: The shape of the fix.
	/// - `opener_pos`: The byte offset of the opening delimiter.
	/// - `at`: The byte offset of the failure, where the fix inserts its
	///   tokens.
	///
	/// # Returns
	/// The fix.
	fn new(shape: Shape, opener_pos: usize, at: usize) -> Self
	{
		Self {
			shape,
			opener_pos,
			at,
			supplied: 1
		}
	}

	/// Supply the next token, if the parser needs it at the specified
	/// failure.
	///
	/// # Parameters
	/// - `site`: The failure.
	///
	/// # Returns
	/// `true` if the fix supplied the token, `false` otherwise.
	fn absorbs(&mut self, site: &FailureSite<'_>) -> bool
	{
		let absorbs = site.position() == self.at
			&& self
				.shape
				.tokens()
				.get(self.supplied)
				.is_some_and(|token| (token.needed_at)(site.site));
		if absorbs
		{
			self.supplied += 1;
		}
		absorbs
	}

	/// Answer the diagnostic of the fix, as supplied so far. If the fix
	/// supplied every token, it closes an unclosed delimiter; otherwise, it
	/// supplies only the missing contents of a closed one.
	///
	/// # Parameters
	/// - `source`: The source text.
	///
	/// # Returns
	/// The diagnostic.
	fn diagnostic(&self, source: &str) -> Diagnostic
	{
		let tokens = self.shape.tokens();
		let mut replacement = String::new();
		let mut placeholders = Vec::new();
		for token in &tokens[..self.supplied]
		{
			if let Some((description, valid_kinds)) = token.placeholder
			{
				placeholders.push(Placeholder {
					span: SourceSpan {
						start: replacement.len(),
						end: replacement.len() + token.text.len()
					},
					description,
					valid_kinds
				});
			}
			replacement.push_str(token.text);
		}
		let edits = vec![Edit {
			span: SourceSpan {
				start: self.at,
				end: self.at
			},
			replacement,
			placeholders
		}];
		let opener = source[self.opener_pos..].chars().next().unwrap_or('(');
		let span = SourceSpan {
			start: self.opener_pos,
			end: self.opener_pos + opener.len_utf8()
		};
		if self.supplied == tokens.len()
		{
			let closer = tokens[tokens.len() - 1].text;
			let description = match self.shape
			{
				Shape::RangeEnd => "insert range end and `]`".to_string(),
				_ => format!("insert expression and `{}`", closer)
			};
			Diagnostic {
				kind: DiagnosticKind::UnclosedDelimiter {
					opener,
					expected_closer: closer.chars().next().unwrap_or(')')
				},
				span,
				message: format!("expected `{}` to close `{}`", closer, opener),
				related: vec![],
				suggestions: vec![Suggestion { description, edits }]
			}
		}
		else
		{
			let what = self.shape.what(self.supplied);
			let next = source[self.at..].chars().next().unwrap_or(' ');
			Diagnostic {
				kind: DiagnosticKind::MissingExpression { opener },
				span,
				message: format!("expected {} before `{}`", what, next),
				related: vec![],
				suggestions: vec![Suggestion {
					description: format!("insert {}", what),
					edits
				}]
			}
		}
	}
}

/// Create a diagnostic for an unclosed delimiter whose contents are complete.
/// The fix inserts the closing delimiter.
///
/// # Parameters
/// - `opener`: The opening delimiter character (e.g., `(`).
/// - `closer`: The expected closing delimiter character (e.g., `)`).
/// - `opener_pos`: The byte offset of the opening delimiter.
/// - `error_pos`: The byte offset where the error was detected.
///
/// # Returns
/// A diagnostic with an `UnclosedDelimiter` kind and a suggestion that
/// inserts the closing delimiter.
fn make_unclosed_delimiter(
	opener: char,
	closer: char,
	opener_pos: usize,
	error_pos: usize
) -> Diagnostic
{
	// The parser only raises a closer expectation after it has already
	// consumed the contents of the delimited construct. A construct without
	// contents is a [`Compound`] fix instead.
	Diagnostic {
		kind: DiagnosticKind::UnclosedDelimiter {
			opener,
			expected_closer: closer
		},
		span: SourceSpan {
			start: opener_pos,
			end: opener_pos + opener.len_utf8()
		},
		message: format!("expected `{}` to close `{}`", closer, opener),
		related: vec![],
		suggestions: vec![Suggestion {
			description: format!("insert `{}`", closer),
			edits: vec![insertion(error_pos, &closer.to_string())]
		}]
	}
}

/// Create a diagnostic for a variable whose `}` is missing. The fixes insert
/// the `}` at each place where the name may [break](unclosed_name_ends), the
/// more likely first, and then at the failure, which keeps the whole name as
/// written. A less likely break is offered only if no bare word follows its
/// break character, since the fix would not parse, e.g., `{hit}-points + 2`
/// for `{hit-points + 2`. Unless it is the only fix, the last is offered only
/// at the end of the input, since a brace follows the failure otherwise, and
/// only where the name holds no `)` or `]` that nothing in the name opened,
/// since the whole name would swallow the closing delimiter of a construct
/// around the variable, e.g., the `)` of `({x + 2)`.
///
/// # Parameters
/// - `source`: The source text.
/// - `opener_pos`: The byte offset of the `{`.
/// - `ends`: The byte offsets at which the name may break, the more likely
///   first.
/// - `error_pos`: The byte offset where the `}` was expected.
/// - `at_eof`: Whether `error_pos` is at the end of the input.
///
/// # Returns
/// A diagnostic with an `UnclosedDelimiter` kind and a suggestion for each
/// fix.
fn make_unclosed_brace(
	source: &str,
	opener_pos: usize,
	ends: &[usize],
	error_pos: usize,
	at_eof: bool
) -> Diagnostic
{
	let mut diagnostic =
		make_unclosed_delimiter('{', '}', opener_pos, error_pos);
	let whole = diagnostic.suggestions.pop();
	diagnostic.suggestions = ends
		.iter()
		.enumerate()
		.filter_map(|(i, &end)| {
			let mut after =
				source[end..].trim_start_matches(is_token_space).chars();
			let before = after.next().unwrap_or(' ');
			// A less likely break must not leave a bare word after the break
			// character, e.g., `points` in `{hit}-points + 2`.
			let bare = after
				.as_str()
				.trim_start_matches(is_token_space)
				.starts_with(is_bare_word_start);
			(i == 0 || !bare).then(|| Suggestion {
				description: format!("insert `}}` before `{}`", before),
				edits: vec![insertion(end, "}")]
			})
		})
		.collect();
	if ends.is_empty()
		|| at_eof && !swallows_closer(&source[opener_pos + 1..error_pos])
	{
		diagnostic.suggestions.extend(whole);
	}
	diagnostic
}

/// Answer whether a name holds a `)` or `]` that nothing in the name opened.
///
/// # Parameters
/// - `name`: The name, as written.
///
/// # Returns
/// `true` if the name holds an unopened closing delimiter, `false` otherwise.
fn swallows_closer(name: &str) -> bool
{
	let (mut parentheses, mut brackets) = (0usize, 0usize);
	for c in name.chars()
	{
		let depth = match c
		{
			'(' | ')' => &mut parentheses,
			'[' | ']' => &mut brackets,
			_ => continue
		};
		if matches!(c, '(' | '[')
		{
			*depth += 1;
		}
		else if *depth == 0
		{
			return true;
		}
		else
		{
			*depth -= 1;
		}
	}
	false
}

/// Create a diagnostic for a missing right operand. Offers two suggestions:
/// inserting a placeholder operand, or dropping the operator.
///
/// # Parameters
/// - `source`: The source text.
/// - `operator`: The binary operator character (e.g., `+`).
/// - `op_pos`: The byte offset of the operator.
/// - `error_pos`: The byte offset where the right operand was expected.
///
/// # Returns
/// A diagnostic with a `MissingRightOperand` kind and two suggestions.
fn make_missing_right_operand(
	source: &str,
	operator: char,
	op_pos: usize,
	error_pos: usize
) -> Diagnostic
{
	// Suggestion 1: insert `0` at the error position, separated by a space
	// from the operator before it and from the token after it, but not from
	// a closing delimiter or separator, e.g., `(1 + 0)` for `(1 + )`.
	let before = &source[..error_pos];
	let after = &source[error_pos..];
	let mut operand = String::new();
	if !before.is_empty() && !before.ends_with(is_token_space)
	{
		operand.push(' ');
	}
	let placeholder_start = operand.len();
	operand.push('0');
	let placeholder_end = operand.len();
	if !after.is_empty()
		&& !after.starts_with(is_token_space)
		&& !after.starts_with([')', ']', '}', ',', ':'])
	{
		operand.push(' ');
	}

	// Suggestion 2: drop the operator, and the whitespace around it, keeping
	// a single space between the surviving neighbors, but none before a
	// closing delimiter or separator, e.g., `(1)` for `(1 + )`.
	let before_op = source[..op_pos].trim_end_matches(is_token_space);
	let rest = after.trim_start_matches(is_token_space);
	let separator = if !after.is_empty()
		&& !before_op.is_empty()
		&& !rest.starts_with([')', ']', '}', ',', ':'])
	{
		" "
	}
	else
	{
		""
	};

	Diagnostic {
		kind: DiagnosticKind::MissingRightOperand { operator },
		span: SourceSpan {
			start: op_pos,
			end: error_pos
		},
		message: format!("expected operand after `{}`", operator),
		related: vec![],
		suggestions: vec![
			Suggestion {
				description: format!("insert operand after `{}`", operator),
				edits: vec![Edit {
					span: SourceSpan {
						start: error_pos,
						end: error_pos
					},
					replacement: operand,
					placeholders: vec![Placeholder {
						span: SourceSpan {
							start: placeholder_start,
							end: placeholder_end
						},
						description: "operand",
						valid_kinds: OPERAND_KINDS
					}]
				}]
			},
			Suggestion {
				description: format!("remove `{}`", operator),
				edits: vec![replacement(
					before_op.len(),
					source.len() - rest.len(),
					separator
				)]
			},
		]
	}
}

/// Create a diagnostic for a missing left operand. Offers two suggestions:
/// inserting a placeholder operand, or dropping the operator.
///
/// # Parameters
/// - `source`: The source text.
/// - `operator`: The binary operator character at the start of the input.
/// - `op_pos`: The byte offset of the operator.
///
/// # Returns
/// A diagnostic with a `MissingLeftOperand` kind and two suggestions.
fn make_missing_left_operand(
	source: &str,
	operator: char,
	op_pos: usize
) -> Diagnostic
{
	// Suggestion 2 drops the operator and the whitespace after it.
	let op_end = op_pos + operator.len_utf8();
	let after_op = source[op_end..].trim_start_matches(is_token_space);

	Diagnostic {
		kind: DiagnosticKind::MissingLeftOperand { operator },
		span: SourceSpan {
			start: op_pos,
			end: op_end
		},
		message: format!("expected operand before `{}`", operator),
		related: vec![],
		suggestions: vec![
			Suggestion {
				description: format!("insert operand before `{}`", operator),
				edits: vec![Edit {
					span: SourceSpan {
						start: op_pos,
						end: op_pos
					},
					replacement: "0 ".into(),
					placeholders: vec![Placeholder {
						span: SourceSpan { start: 0, end: 1 },
						description: "operand",
						valid_kinds: OPERAND_KINDS
					}]
				}]
			},
			Suggestion {
				description: format!("remove `{}`", operator),
				edits: vec![replacement(
					op_pos,
					source.len() - after_op.len(),
					""
				)]
			},
		]
	}
}

/// Create a diagnostic for a comma in the formal parameters that another
/// comma or the `:` follows. The fix removes the comma, and the whitespace
/// after it.
///
/// # Parameters
/// - `comma`: The byte offset of the comma.
/// - `next`: The byte offset of the `,` or `:` that follows the comma.
///
/// # Returns
/// A diagnostic with a `MissingParameter` kind and a suggestion that removes
/// the comma.
fn make_missing_parameter(comma: usize, next: usize) -> Diagnostic
{
	Diagnostic {
		kind: DiagnosticKind::MissingParameter,
		span: SourceSpan {
			start: comma,
			end: comma + 1
		},
		message: "expected a formal parameter after `,`".into(),
		related: vec![],
		suggestions: vec![Suggestion {
			description: "remove `,`".into(),
			edits: vec![replacement(comma, next, "")]
		}]
	}
}

/// Create a diagnostic for missing dice faces after `d`/`D`. The fix inserts
/// `6` as a placeholder face count immediately after the dice operator.
///
/// # Parameters
/// - `source`: The source text.
/// - `d_pos`: The byte offset of the `d`/`D` operator.
///
/// # Returns
/// A diagnostic with a `MissingDiceFaces` kind and a suggestion with a
/// placeholder for the face count.
fn make_missing_dice_faces(source: &str, d_pos: usize) -> Diagnostic
{
	// Insert `6` as a default face count right after the `d`/`D`, preserving
	// any whitespace between the operator and the error.
	let insert_pos = d_pos + 1;
	Diagnostic {
		kind: DiagnosticKind::MissingDiceFaces,
		span: SourceSpan {
			start: d_pos,
			end: d_pos + 1
		},
		message: format!(
			"expected face count after `{}`",
			&source[d_pos..d_pos + 1]
		),
		related: vec![],
		suggestions: vec![Suggestion {
			description: "insert face count".into(),
			edits: vec![Edit {
				span: SourceSpan {
					start: insert_pos,
					end: insert_pos
				},
				replacement: "6".into(),
				placeholders: vec![Placeholder {
					span: SourceSpan { start: 0, end: 1 },
					description: "face count",
					valid_kinds: FACE_COUNT_KINDS
				}]
			}]
		}]
	}
}

/// Answer whether text begins with the keyword `drop`, as a drop clause would,
/// rather than a name that begins with `drop`, e.g., `dropout`: `drop` must
/// be followed by a direction, or by something that cannot continue a [bare
/// word](crate::parser::bare_word), e.g., whitespace or the end of the text.
///
/// # Parameters
/// - `text`: The text.
///
/// # Returns
/// `true` if `text` begins with the keyword `drop`, `false` otherwise.
fn begins_drop(text: &str) -> bool
{
	text.strip_prefix("drop").is_some_and(|after| {
		after.starts_with("lowest")
			|| after.starts_with("highest")
			|| after
				.chars()
				.next()
				.is_none_or(|c| c.is_whitespace() || !is_bare_word_continue(c))
	})
}

/// Create a diagnostic for a drop clause that follows something other than a
/// dice expression. After an operand, which the parser read as the count of a
/// dice expression whose operator is the `d` of `drop`, the fix inserts
/// placeholder faces after the operand, e.g., `3D6 drop lowest` for
/// `3 drop lowest`. Otherwise, it inserts a placeholder dice expression
/// before `drop`, e.g., `1 + 1D6 drop lowest` for `1 + drop lowest`.
///
/// # Parameters
/// - `source`: The source.
/// - `drop_pos`: The byte offset of `drop`.
/// - `after_operand`: Whether an operand precedes `drop`.
///
/// # Returns
/// A diagnostic with a `MisplacedDropClause` kind and a suggestion with a
/// placeholder for the faces or the dice expression.
fn make_misplaced_drop(
	source: &str,
	drop_pos: usize,
	after_operand: bool
) -> Diagnostic
{
	let edit = if after_operand
	{
		// Separate the faces from `drop`, unless whitespace already does.
		let operand_end =
			source[..drop_pos].trim_end_matches(is_token_space).len();
		Edit {
			span: SourceSpan {
				start: operand_end,
				end: operand_end
			},
			replacement: if operand_end == drop_pos { "D6 " } else { "D6" }
				.into(),
			placeholders: vec![Placeholder {
				span: SourceSpan { start: 1, end: 2 },
				description: "face count",
				valid_kinds: FACE_COUNT_KINDS
			}]
		}
	}
	else
	{
		// Separate the dice expression from a `-` just before it, which
		// would otherwise make its count a negative constant.
		let separator = if source[..drop_pos].ends_with('-')
		{
			" "
		}
		else
		{
			""
		};
		Edit {
			span: SourceSpan {
				start: drop_pos,
				end: drop_pos
			},
			replacement: format!("{}1D6 ", separator),
			placeholders: vec![Placeholder {
				span: SourceSpan {
					start: separator.len(),
					end: separator.len() + 3
				},
				description: "dice expression",
				valid_kinds: DICE_KINDS
			}]
		}
	};
	Diagnostic {
		kind: DiagnosticKind::MisplacedDropClause,
		span: SourceSpan {
			start: drop_pos,
			end: drop_pos + "drop".len()
		},
		message: "`drop` must follow a dice expression".into(),
		related: vec![],
		suggestions: vec![Suggestion {
			description: if after_operand
			{
				"insert dice faces before `drop`"
			}
			else
			{
				"insert a dice expression before `drop`"
			}
			.into(),
			edits: vec![edit]
		}]
	}
}

/// Create a diagnostic for an incomplete drop clause. The fix inserts
/// `lowest` as a placeholder direction after the `drop` keyword.
///
/// # Parameters
/// - `drop_start`: The byte offset of `drop`.
/// - `drop_end`: The byte offset of the end of `drop`.
///
/// # Returns
/// A diagnostic with an `IncompleteDropClause` kind and a suggestion with a
/// placeholder for the drop direction.
fn make_incomplete_drop(drop_start: usize, drop_end: usize) -> Diagnostic
{
	// Insert ` lowest` after `drop`.
	Diagnostic {
		kind: DiagnosticKind::IncompleteDropClause,
		span: SourceSpan {
			start: drop_start,
			end: drop_end
		},
		message: "expected `lowest` or `highest` after `drop`".into(),
		related: vec![],
		suggestions: vec![Suggestion {
			description: "insert drop direction".into(),
			edits: vec![Edit {
				span: SourceSpan {
					start: drop_end,
					end: drop_end
				},
				replacement: " lowest".into(),
				placeholders: vec![Placeholder {
					span: SourceSpan { start: 1, end: 7 },
					description: "direction",
					valid_kinds: DROP_DIRECTION_KINDS
				}]
			}]
		}]
	}
}

/// Create a diagnostic for a bare closing delimiter with no matching opener.
/// The fix removes the spurious closer. Emitted from
/// [`analyze`](Doctor::analyze) when an expression was expected at the error
/// position but the source has `)`, `]`, or `}` there instead.
///
/// # Parameters
/// - `closer`: The unmatched closing delimiter character.
/// - `pos`: The byte offset of the closer.
///
/// # Returns
/// A diagnostic with an
/// [`UnopenedDelimiter`](DiagnosticKind::UnopenedDelimiter) kind and a single
/// suggestion that removes the offending character.
fn make_unopened_delimiter(closer: char, pos: usize) -> Diagnostic
{
	Diagnostic {
		kind: DiagnosticKind::UnopenedDelimiter { closer },
		span: SourceSpan {
			start: pos,
			end: pos + closer.len_utf8()
		},
		message: format!("unexpected `{}` with no matching opener", closer),
		related: vec![],
		suggestions: vec![Suggestion {
			description: format!("remove the unopened `{}`", closer),
			edits: vec![replacement(pos, pos + closer.len_utf8(), "")]
		}]
	}
}

/// Create a diagnostic for a character of [stray whitespace](stray_whitespace).
/// The fix replaces it with a space.
///
/// # Parameters
/// - `source`: The original source.
/// - `start`: The byte offset of the character.
/// - `end`: The byte offset of the end of the character.
///
/// # Returns
/// A diagnostic with an `UnexpectedToken` kind that shows the character by its
/// code point, e.g., `U+00A0`, since it would be invisible.
fn make_stray_whitespace(source: &str, start: usize, end: usize) -> Diagnostic
{
	let (_, _, token) = unexpected_token(source, start);
	Diagnostic {
		kind: DiagnosticKind::UnexpectedToken,
		span: SourceSpan { start, end },
		message: format!(
			"unexpected `{}`; only spaces, tabs, and line breaks may separate \
			 tokens",
			token
		),
		related: vec![],
		suggestions: vec![Suggestion {
			description: format!("replace `{}` with a space", token),
			edits: vec![replacement(start, end, " ")]
		}]
	}
}

/// Create a diagnostic for an empty or whitespace-only expression. The fix
/// inserts `0` as a placeholder.
///
/// # Parameters
/// - `start`: The byte offset of the whitespace that the expression supplants.
/// - `end`: The byte offset of the end of the whitespace, i.e., of the source.
///
/// # Returns
/// A diagnostic with an `EmptyExpression` kind and a suggestion with a
/// placeholder for the expression.
fn make_empty_expression(start: usize, end: usize) -> Diagnostic
{
	Diagnostic {
		kind: DiagnosticKind::EmptyExpression,
		span: SourceSpan { start: 0, end: 0 },
		message: "expected expression".into(),
		related: vec![],
		suggestions: vec![Suggestion {
			description: "insert expression".into(),
			edits: vec![Edit {
				span: SourceSpan { start, end },
				replacement: "0".into(),
				placeholders: vec![Placeholder {
					span: SourceSpan { start: 0, end: 1 },
					description: "expression",
					valid_kinds: OPERAND_KINDS
				}]
			}]
		}]
	}
}

////////////////////////////////////////////////////////////////////////////////
//                          Semantic error analysis.                          //
////////////////////////////////////////////////////////////////////////////////

/// Run the [validator](Validator) over a parsed [function](Function) and
/// translate any [semantic error](CompilationError) into a [`Diagnostic`].
///
/// Invoked from [`diagnose`] only after the original source parses cleanly —
/// that is, when no fixes have been applied — so that semantic
/// diagnostics reference the user's actual source text rather than a
/// fix-synthesized variant. Returns an empty vector when the validator
/// accepts the function, or a single-element vector when it rejects it.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function to validate.
///
/// # Returns
/// The semantic diagnostics produced by the validator, in source order.
pub(crate) fn run_validator<'src>(ast: &Function<'src>) -> Vec<Diagnostic>
{
	match Validator::validate(ast)
	{
		Ok(()) => Vec::new(),
		Err(error) => vec![analyze_semantic_error(ast, error)]
	}
}

/// Translate a [semantic error](CompilationError) into a [`Diagnostic`].
///
/// Exhaustively matches the variants [`Validator::validate`] is permitted to
/// produce today. The `ParseError` and `OptimizationFailed` arms are statically
/// unreachable — the parser has already succeeded by the time this helper runs,
/// and the optimizer is not invoked from the diagnostic pipeline — but listing
/// them explicitly (rather than using a wildcard) forces a compile-time
/// consideration whenever a new [`CompilationError`] variant is introduced.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function that produced the error.
/// - `error`: The semantic error returned by [`Validator::validate`].
///
/// # Returns
/// A [`Diagnostic`] describing the semantic error, with a rename suggestion
/// whose corrected source parses and validates cleanly.
fn analyze_semantic_error<'src>(
	ast: &Function<'src>,
	error: CompilationError<'src>
) -> Diagnostic
{
	match error
	{
		CompilationError::DuplicateParameter {
			name,
			first,
			duplicate
		} => make_duplicate_parameter(ast, &name, first, duplicate),
		CompilationError::BindingCollidesWithParameter {
			name,
			parameter,
			binding
		} =>
		{
			make_binding_collides_with_parameter(ast, &name, parameter, binding)
		},
		CompilationError::DuplicateBinding {
			name,
			first,
			duplicate
		} => make_duplicate_binding(ast, &name, first, duplicate),
		CompilationError::UseBeforeBind {
			name,
			reference,
			binding
		} => make_use_before_bind(ast, &name, reference, binding),
		CompilationError::ParseError(_)
		| CompilationError::OptimizationFailed =>
		{
			unreachable!(
				"Validator::validate produces only semantic errors; \
				 ParseError is produced by the parser and OptimizationFailed \
				 by the optimizer, neither of which is reached from \
				 diagnose() at this point"
			)
		}
	}
}

/// Build a
/// [`BindingCollidesWithParameter`](DiagnosticKind::BindingCollidesWithParameter)
/// diagnostic with a rename [`Suggestion`] whose corrected source is guaranteed
/// to parse and validate cleanly.
///
/// The primary [`span`](Diagnostic::span) points at the binding-site name; a
/// single [`RelatedLabel`] points at the colliding parameter declaration. The
/// suggestion splices a fresh name into the binding span — references in the
/// body are deliberately left alone because there is no safe way to pick which
/// of them (if any) was intended to refer to the parameter rather than the
/// binding.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function (used to enumerate in-use names).
/// - `name`: The colliding name.
/// - `parameter`: The span of the colliding parameter declaration.
/// - `binding`: The span of the binding-site name.
///
/// # Returns
/// A fully-formed [`Diagnostic`] with one rename [`Suggestion`].
fn make_binding_collides_with_parameter<'src>(
	ast: &Function<'src>,
	name: &str,
	parameter: SourceSpan,
	binding: SourceSpan
) -> Diagnostic
{
	let used = collect_in_use_names(ast);
	let fresh = suggest_rename_with_pool(name, &used);
	Diagnostic {
		kind: DiagnosticKind::BindingCollidesWithParameter {
			name: name.to_string()
		},
		span: binding,
		message: format!(
			"local binding `{}` collides with a formal parameter of the same \
			 name; bindings, parameters, and environment variables share one \
			 flat namespace per function",
			name
		),
		related: vec![RelatedLabel {
			span: parameter,
			message: "declared as a parameter here".into()
		}],
		suggestions: vec![Suggestion {
			description: format!("rename local binding to `{}`", fresh),
			edits: vec![replacement(binding.start, binding.end, &fresh)]
		}]
	}
}

/// Build a [`DuplicateBinding`](DiagnosticKind::DuplicateBinding) diagnostic
/// with a rename [`Suggestion`] whose corrected source is guaranteed to parse
/// and validate cleanly.
///
/// The primary [`span`](Diagnostic::span) points at the duplicate binding-site
/// name; a single [`RelatedLabel`] points at the first binding. The suggestion
/// splices a fresh name into the duplicate binding span — references in the
/// body are deliberately left alone because there is no safe way to pick which
/// one (if any) was intended to refer to the rebinding rather than the
/// original.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function (used to enumerate in-use names).
/// - `name`: The rebound name.
/// - `first`: The span of the first binding-site name.
/// - `duplicate`: The span of the duplicate binding-site name.
///
/// # Returns
/// A fully-formed [`Diagnostic`] with one rename [`Suggestion`].
fn make_duplicate_binding<'src>(
	ast: &Function<'src>,
	name: &str,
	first: SourceSpan,
	duplicate: SourceSpan
) -> Diagnostic
{
	let used = collect_in_use_names(ast);
	let fresh = suggest_rename_with_pool(name, &used);
	Diagnostic {
		kind: DiagnosticKind::DuplicateBinding {
			name: name.to_string()
		},
		span: duplicate,
		message: format!(
			"local binding `{}` is bound more than once; a function body \
			 provides a single flat namespace, so rebinding is not permitted",
			name
		),
		related: vec![RelatedLabel {
			span: first,
			message: "first bound here".into()
		}],
		suggestions: vec![Suggestion {
			description: format!("rename duplicate binding to `{}`", fresh),
			edits: vec![replacement(duplicate.start, duplicate.end, &fresh)]
		}]
	}
}

/// Build a [`UseBeforeBind`](DiagnosticKind::UseBeforeBind) diagnostic with a
/// rename [`Suggestion`] whose corrected source is guaranteed to parse and
/// validate cleanly.
///
/// The primary [`span`](Diagnostic::span) points at the offending reference; a
/// single [`RelatedLabel`] points at the later binding site. The suggestion
/// splices a fresh name into the binding site — the forward reference is then
/// reinterpreted as an external variable. Renaming the binding rather than the
/// reference is consistent with [`make_binding_collides_with_parameter`] and
/// [`make_duplicate_binding`]: always rewrite the declaration, never the
/// references.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function (used to enumerate in-use names).
/// - `name`: The name referenced before its binding.
/// - `reference`: The span of the offending reference.
/// - `binding`: The span of the binding-site name that appears later in the
///   source.
///
/// # Returns
/// A fully-formed [`Diagnostic`] with one rename [`Suggestion`].
fn make_use_before_bind<'src>(
	ast: &Function<'src>,
	name: &str,
	reference: SourceSpan,
	binding: SourceSpan
) -> Diagnostic
{
	let used = collect_in_use_names(ast);
	let fresh = suggest_rename_with_pool(name, &used);
	Diagnostic {
		kind: DiagnosticKind::UseBeforeBind {
			name: name.to_string()
		},
		span: reference,
		message: format!(
			"reference to `{}` precedes its binding; references to a local \
			 binding must lexically follow the binding site, including \
			 references inside the bound expression itself (self-reference \
			 is not permitted)",
			name
		),
		related: vec![RelatedLabel {
			span: binding,
			message: "bound here".into()
		}],
		suggestions: vec![Suggestion {
			description: format!("rename local binding to `{}`", fresh),
			edits: vec![replacement(binding.start, binding.end, &fresh)]
		}]
	}
}

/// Collect every name that is in use anywhere in `ast`: formal parameters,
/// [local-binding](crate::ast::Binding) names, and
/// [variable references](crate::ast::Variable). A fresh name suggested against
/// this pool is guaranteed to collide with nothing currently in the function.
///
/// # Type parameters
/// - `'a`: The lifetime of the borrow of the function.
///
/// # Parameters
/// - `ast`: The parsed function.
///
/// # Returns
/// A set containing every parameter, binding, and variable-reference name.
pub(crate) fn collect_in_use_names<'a>(
	ast: &'a Function<'_>
) -> HashSet<&'a str>
{
	let mut names: HashSet<&'a str> = HashSet::new();
	if let Some(ref parameters) = ast.parameters
	{
		for param in parameters
		{
			names.insert(&param.name);
		}
	}
	for event in Walk::new(Node::Expression(&ast.body))
	{
		match event
		{
			Event::Enter(Node::Expression(Expression::Variable(v))) =>
			{
				names.insert(&v.name);
			},
			Event::Enter(Node::Expression(Expression::Binding(b))) =>
			{
				names.insert(&b.name);
			},
			_ =>
			{}
		}
	}
	names
}

/// Build a [`DuplicateParameter`](DiagnosticKind::DuplicateParameter)
/// diagnostic with a rename suggestion whose corrected source is guaranteed to
/// parse and validate cleanly.
///
/// The primary [`span`](Diagnostic::span) points at the duplicate
/// occurrence; a single [`RelatedLabel`] on [`related`](Diagnostic::related)
/// points at the first occurrence. The suggestion splices a fresh name
/// produced by [`suggest_rename`] into the duplicate span — references in the
/// body are deliberately left alone because there is no safe way to pick
/// which one of them (if any) was intended to refer to a different binding.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text from which `ast` was parsed.
///
/// # Parameters
/// - `ast`: The parsed function (used to enumerate in-use parameter names).
/// - `name`: The duplicated parameter name.
/// - `first`: The span of the first occurrence of `name`.
/// - `duplicate`: The span of the duplicate occurrence that triggered the
///   error.
///
/// # Returns
/// A fully-formed [`Diagnostic`] with one rename [`Suggestion`].
fn make_duplicate_parameter<'src>(
	ast: &Function<'src>,
	name: &str,
	first: SourceSpan,
	duplicate: SourceSpan
) -> Diagnostic
{
	let parameters = ast.parameters.as_deref().unwrap_or(&[]);
	let fresh = suggest_rename(name, parameters);
	Diagnostic {
		kind: DiagnosticKind::DuplicateParameter {
			name: name.to_string()
		},
		span: duplicate,
		message: format!(
			"parameter `{}` is declared more than once; review references \
			 to `{}` in the body — one may have meant a different parameter \
			 or an external variable",
			name, name
		),
		related: vec![RelatedLabel {
			span: first,
			message: "first declared here".into()
		}],
		suggestions: vec![Suggestion {
			description: format!("rename duplicate parameter to `{}`", fresh),
			edits: vec![replacement(duplicate.start, duplicate.end, &fresh)]
		}]
	}
}

/// Suggest a fresh parameter name that does not collide with any existing name
/// in `parameters`.
///
/// When the duplicate is a single ASCII letter — the common case, given dice
/// expressions tend to use terse names like `x`, `y`, `a`, `b` — the
/// replacement is the first unused letter encountered by **bumping from the
/// highest attested same-case single-letter parameter** and wrapping through
/// the alphabet. So `x, x` becomes `x, y` (bump `x` → `y`), `a, b, b, c`
/// becomes `a, b, d, c` (bump highest `c` → `d`), `a, x, x` becomes `a, x, y`
/// (bump highest `x` → `y`), and `a, z, z` becomes `a, z, b` (bump `z` wraps
/// past unused `a` which is taken, lands on `b`). Continuing from the user's
/// apparent "trajectory" is usually more natural than filling the earliest
/// alphabetic gap, which would produce `a, x, b` in the third example.
///
/// When the duplicate is a multi-character name, or all 26 letters in the
/// matching case are already in use, the replacement falls through to a `newN`
/// form (`new0`, `new1`, …), where `N` is the first non-negative integer for
/// which the resulting name is unused. The `newN` loop likewise skips over
/// names the user has already adopted, so `abc, new0, abc` becomes `abc, new0,
/// new1` and `abc, new0, new1, abc` becomes `abc, new0, new1, new2`.
///
/// # Parameters
/// - `duplicate`: The duplicated parameter name.
/// - `parameters`: The function's parameter list, used to determine which names
///   are already in use.
///
/// # Returns
/// A fresh parameter name that does not appear in `parameters`.
fn suggest_rename(duplicate: &str, parameters: &[Parameter<'_>]) -> String
{
	let used: HashSet<&str> = parameters.iter().map(|p| &*p.name).collect();
	suggest_rename_with_pool(duplicate, &used)
}

/// Suggest a fresh name that does not collide with any name in `used`. Shares
/// the core algorithm with [`suggest_rename`] so that the rename suggestions
/// for [local-binding](crate::ast::Binding) errors use the same heuristics as
/// the original duplicate-parameter flow: single-letter inputs bump forward
/// through the same-case alphabet, wrapping past the highest attested letter
/// in the pool, and everything else falls back to `newN`.
///
/// # Parameters
/// - `duplicate`: The name to rename.
/// - `used`: The set of names already in use, against which the candidate must
///   not collide.
///
/// # Returns
/// A fresh name that does not appear in `used`.
fn suggest_rename_with_pool(duplicate: &str, used: &HashSet<&str>) -> String
{
	let mut chars = duplicate.chars();
	if let (Some(c), None) = (chars.next(), chars.next())
		&& c.is_ascii_alphabetic()
	{
		let start = if c.is_ascii_uppercase() { b'A' } else { b'a' };
		// The highest attested same-case single-letter name. The duplicate
		// itself is always attested, so `highest` is at least `c`.
		let highest = used
			.iter()
			.filter_map(|n| {
				let mut cs = n.chars();
				match (cs.next(), cs.next())
				{
					(Some(ch), None)
						if ch.is_ascii_alphabetic()
							&& ch.is_ascii_uppercase()
								== c.is_ascii_uppercase() =>
					{
						Some(ch as u8)
					},
					_ => None
				}
			})
			.max()
			.unwrap_or(c as u8);
		// Iterate the 25 other letters by bumping forward from `highest` and
		// wrapping through the alphabet. `offset == 26` would loop back to
		// `highest`, which is attested, so 1..26 is sufficient.
		for offset in 1u8..26
		{
			let code = start + ((highest - start + offset) % 26);
			let candidate = (code as char).to_string();
			if !used.contains(candidate.as_str())
			{
				return candidate;
			}
		}
	}
	for i in 0usize..
	{
		let candidate = format!("new{}", i);
		if !used.contains(candidate.as_str())
		{
			return candidate;
		}
	}
	unreachable!("cannot exhaust usize worth of `newN` candidates")
}

////////////////////////////////////////////////////////////////////////////////
//                                The doctor.                                 //
////////////////////////////////////////////////////////////////////////////////

/// Diagnose a source string, collecting all parse errors, semantic errors, and
/// suggested fixes.
///
/// The doctor runs two alternative passes. If the source parses cleanly, the
/// doctor runs the semantic [`Validator`] against the AST and translates any
/// error into a corresponding [`Diagnostic`]. Otherwise, it parses the source
/// once more, in the parser's recovery mode: wherever the parse cannot
/// continue, the parser consults the doctor, which diagnoses the failure and,
/// if it can, answers a repair that corresponds to the first suggestion of the
/// diagnostic, so that the parse continues as though the source had been
/// edited. This continues until the parse succeeds (all errors collected) or
/// an unfixable error is encountered. The semantic pass is skipped after any
/// fix, because attaching semantic diagnostics to fix-synthesized characters
/// the user never typed would mislead them about what their source actually
/// says.
///
/// ```mermaid
/// sequenceDiagram
///     participant D as diagnose
///     participant P as Parser
///     participant R as Doctor
///     participant V as Validator
///     D->>P: parse(source)
///     alt parses cleanly
///         P-->>D: AST
///         D->>V: validate(AST)
///         V-->>D: semantic error, if any
///     else fails
///         P-->>D: error
///         D->>P: parse_recovering(source, doctor)
///         loop each failure
///             P->>R: repair(failure)
///             R->>R: diagnose, record the fix
///             R-->>P: repair, or stop if unfixable
///         end
///         P-->>D: recovered AST, or error
///         D->>R: finish
///         R-->>D: diagnostics, corrected source
///     end
/// ```
///
/// # Parameters
/// - `source`: The source string to diagnose.
///
/// # Returns
/// A [`DiagnoseResult`] containing all diagnostics and, if all parse errors
/// were fixable, the corrected source string. Semantic diagnostics never
/// prevent the corrected-source return: a duplicate-parameter program
/// produces a diagnostic *and* a `corrected_source` equal to the original
/// input, because the source already parses.
///
/// # Performance
/// Designed for keystroke-speed execution. The doctor parses the source at most
/// three times, whatever the number of errors, and takes time linear in the
/// length of the source: the clean parse, the recovering parse, and, for
/// formal parameters without a `:`, a parse of the completed definition. The
/// semantic pass runs at most once per call.
///
/// # Examples
/// Single error with an unambiguous fix:
///
/// ```rust
/// use xdy::diagnostics::{DiagnosticKind, diagnose};
///
/// let result = diagnose("{x");
/// assert_eq!(result.diagnostics.len(), 1);
/// assert!(matches!(
///     result.diagnostics[0].kind,
///     DiagnosticKind::UnclosedDelimiter { opener: '{', .. }
/// ));
/// assert_eq!(
///     result.corrected_source.as_deref(),
///     Some("{x}")
/// );
/// ```
///
/// An error collected by the recovering parse:
///
/// ```rust
/// use xdy::diagnostics::{DiagnosticKind, diagnose};
///
/// let result = diagnose("3D6 + + 1D3");
/// assert_eq!(result.diagnostics.len(), 1);
/// assert!(matches!(
///     result.diagnostics[0].kind,
///     DiagnosticKind::MissingRightOperand { operator: '+' }
/// ));
/// // The corrected source inserts `0` as a placeholder operand.
/// assert!(result.corrected_source.is_some());
/// // The corrected source parses cleanly.
/// assert!(
///     xdy::Parser::parse(
///         result.corrected_source.as_ref().unwrap()
///     ).is_ok()
/// );
/// ```
///
/// A semantic diagnostic on a program that otherwise parses cleanly:
///
/// ```rust
/// use xdy::diagnostics::{DiagnosticKind, diagnose};
///
/// let result = diagnose("{x}, {x}: {x}");
/// assert_eq!(result.diagnostics.len(), 1);
/// assert!(matches!(
///     result.diagnostics[0].kind,
///     DiagnosticKind::DuplicateParameter { .. }
/// ));
/// // Semantic diagnostics don't prevent `corrected_source`; the original
/// // source already parses, so it is returned verbatim.
/// assert_eq!(result.corrected_source.as_deref(), Some("{x}, {x}: {x}"));
/// ```
#[cfg_attr(doc, aquamarine::aquamarine)]
pub fn diagnose(source: &str) -> DiagnoseResult
{
	// Fast path: if the source parses cleanly, skip the recovering parse and
	// run the semantic pass directly on the AST. Fixing up a syntactically
	// invalid source first and then reporting a semantic error against the
	// fix-synthesized text would attach diagnostics to characters the user
	// never typed, which is confusing — so semantic checks run only on the
	// unmodified original.
	if let Ok(ast) = Parser::parse(source)
	{
		return DiagnoseResult {
			diagnostics: run_validator(&ast),
			corrected_source: Some(source.to_string())
		};
	}

	// The parser reads the stray whitespace as spaces, and the doctor
	// diagnoses it instead.
	let strays = stray_whitespace(source);
	let mut text = source.as_bytes().to_vec();
	for &(start, end) in &strays
	{
		text[start..end].fill(b' ');
	}
	let text = String::from_utf8(text)
		.expect("replacing whole characters with spaces preserves UTF-8");
	let mut doctor = Doctor::new(&text, source, strays);
	let recovered = Parser::parse_recovering(&text, &mut doctor).is_ok();
	doctor.finish(recovered)
}