cortexkit-rich-ui-syntax 0.2.0

Bounded rich UI language parser and syntax-tree-derived tool output reference scanner
Documentation
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
//! Incremental reply parsing.
//!
//! [`StreamParser`] keeps every received byte (up to the reply limit) for one
//! whole-source parse at [`StreamParser::finish`], so the final result never
//! depends on how the source was chunked. While a reply streams in, it also
//! runs a separate *provisional* machine that turns each byte, once, into
//! bounded block and inline state, and reports a display-only [`Revision`]
//! whenever the drawable picture of the received prefix changes.
//!
//! The provisional machine never reparses the received prefix. Text whose
//! classification is still open (a code span waiting for its closing
//! backticks, a link waiting for its destination, a tag waiting for its
//! closing tag, a possible reference, the start of a line that may still
//! become a block marker) is held in a bounded buffer and is re-examined at
//! most a small constant number of times, when it resolves. Completed tags
//! and links are classified by the same parser [`crate::parse`] uses, run once
//! over just their buffered text.

use crate::parser::{parse_bytes, source_cap, Engine};
use crate::{parse, Document, Kind, Markdown, Mode, Node, Span, EMPHASIS_DEPTH, MARKDOWN_DEPTH};
use std::collections::VecDeque;
use std::sync::Arc;

/// The most source bytes one unresolved provisional context may hold: a code
/// span, a link, a whole tag, a table header line, or a line start that may
/// still become a block marker. At this size the held text is drawn as a
/// provisional literal and dropped. Final parsing has no such threshold.
pub const STREAM_CONTEXT_BYTES: usize = 32 * 1024;
/// The most source bytes a tag's opening (`<Name …>`) may hold before it is
/// drawn as a provisional literal.
pub const TAG_OPENING_BYTES: usize = 4 * 1024;
/// The most characters a possible `$$tc(N)` reference may hold before it is
/// drawn as a provisional literal. This is a streaming threshold, not a limit
/// on reference numbers: the final parse accepts any length.
pub const PENDING_REFERENCE_CHARS: usize = 24;

/// Text nodes are split at this many bytes so that drawing a revision copies
/// a bounded amount of text from the run still being written.
const RUN_BYTES: usize = 1024;
/// Link labels nest brackets at most this deep before the held link is drawn
/// as a provisional literal. This bounds how often a failed link's text is
/// re-examined.
const LABEL_DEPTH: usize = 4;
/// Quote and list containers open at once, counting a list and its item as
/// two.
const CONTAINERS: usize = 2 * MARKDOWN_DEPTH;

/// The work one [`StreamParser::feed`] call did, as plain counts. The caller
/// does its own fuel accounting: it reserves part of a reply's budget for the
/// final parse and charges provisional work separately.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Work {
    /// Received bytes consumed by the provisional machine. Each byte is
    /// scanned once; bytes past the reply limit are not scanned.
    pub scanned: usize,
    /// Bytes of held, unresolved text examined again: when a held context
    /// resolves, fails, or reaches its cap, or when a completed tag or link
    /// is classified.
    pub reexamined: usize,
}

/// A provisional picture of a streaming reply.
///
/// A revision is a pure function of the received prefix: the same bytes give
/// an equal revision however they were chunked. Revisions are **not**
/// monotone; a later byte may reclassify text drawn by an earlier revision
/// (a closing backtick, a link destination, a table delimiter row, the rest
/// of a line), so a caller drawing revisions replaces the whole drawn
/// revision with the next one. Text still held unresolved is not drawn at all.
///
/// A revision is inert. It carries display structure only: no reference
/// numbers (a reference is a [`Display::PendingReference`] placeholder), no
/// link destinations, no tag props, and nothing that names data to fetch or
/// an action to take. It is never a storage-time reference decision; use
/// [`StreamParser::finish`] or [`crate::scan_references`] for that.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Revision {
    pub nodes: Vec<Arc<DisplayNode>>,
}

/// One node of a provisional display tree. Spans are byte offsets into the
/// received text after the same UTF-8 repair [`crate::parse_bytes`] applies:
/// each maximal invalid byte subsequence becomes one U+FFFD.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DisplayNode {
    pub span: Span,
    pub kind: Display,
    pub children: Vec<Arc<DisplayNode>>,
}

/// What a provisional display node draws.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Display {
    Paragraph,
    Heading(u8),
    ThematicBreak,
    /// Fenced code; its text is in `Text` children.
    CodeBlock {
        language: String,
    },
    Quote,
    List {
        /// The number of an ordered list's first item; `None` for a bullet
        /// list.
        start: Option<u64>,
    },
    Item,
    /// A task list marker. Only a completed tag's body, which the final
    /// parser classifies, produces one; while streaming, a list item's
    /// `[ ]` or `[x]` is drawn as text.
    Task(bool),
    Table,
    TableHead,
    TableRow,
    TableCell,
    Emphasis,
    Strong,
    Strikethrough,
    Code(String),
    /// A link's or image's label, drawn as text. The destination is withheld.
    Link {
        image: bool,
    },
    Text(String),
    /// Opaque or provisional literal source text, control characters
    /// replaced.
    Literal(String),
    SoftBreak,
    HardBreak,
    /// A complete tag, by name only: props are withheld.
    Tag {
        name: String,
        block: bool,
    },
    /// Where a tool-output reference stands. Its number is withheld.
    PendingReference,
}

/// Parses a source that arrives in chunks.
///
/// [`finish`](Self::finish) returns exactly what [`parse_bytes`] returns for
/// the whole received input, for every chunking: tree, references,
/// diagnostics and spans. Bytes beyond the mode's size limit, plus a
/// three-byte margin for a split UTF-8 character, are counted but not kept;
/// `finish` then marks the document shortened, and in program mode refuses
/// it (`E_SOURCE_TOO_LARGE`, no nodes), so a program never runs from a
/// cut-off prefix.
///
/// In reply mode, [`feed`](Self::feed) also returns a provisional
/// [`Revision`] whenever the drawable picture changes. Programs are not drawn
/// while streaming: in program mode `feed` always returns `None`.
pub struct StreamParser {
    mode: Mode,
    bytes: Vec<u8>,
    machine: Option<Box<Machine>>,
    last: Revision,
    work: Work,
}

impl StreamParser {
    pub fn new(mode: Mode) -> Self {
        Self {
            mode,
            bytes: vec![],
            machine: (mode == Mode::Reply).then(|| Box::new(Machine::new())),
            last: Revision::default(),
            work: Work::default(),
        }
    }

    /// Accepts the next chunk. Returns the new revision when the provisional
    /// picture differs from the last one returned, and `None` otherwise.
    pub fn feed(&mut self, chunk: &[u8]) -> Option<Revision> {
        let keep = chunk
            .len()
            .min((source_cap(self.mode) + 3).saturating_sub(self.bytes.len()));
        self.bytes.extend_from_slice(&chunk[..keep]);
        self.work = Work::default();
        let machine = self.machine.as_mut()?;
        for &byte in chunk {
            if machine.stopped {
                break;
            }
            machine.byte(byte);
        }
        self.work = std::mem::take(&mut machine.work);
        if !std::mem::take(&mut machine.out.dirty) {
            return None;
        }
        let revision = machine.out.snapshot();
        if revision == self.last {
            return None;
        }
        self.last = revision.clone();
        Some(revision)
    }

    /// The current provisional picture: the last revision `feed` returned, or
    /// an empty one before any.
    pub fn revision(&self) -> &Revision {
        &self.last
    }

    /// The work the last `feed` call did.
    pub fn work(&self) -> Work {
        self.work
    }

    /// Parses everything received, exactly as [`parse_bytes`] parses it.
    pub fn finish(self) -> Document {
        parse_bytes(&self.bytes, self.mode)
    }
}

/// Replaces control characters other than line feed and tab, so provisional
/// text never carries terminal or layout controls. A carriage return becomes
/// a line feed; the builder drops the line feed of a CRLF pair.
fn sanitize(c: char) -> char {
    match c {
        '\r' => '\n',
        '\n' | '\t' => c,
        c if c.is_control() => '\u{fffd}',
        c => c,
    }
}

fn sanitize_str(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut cr = false;
    for c in text.chars() {
        if !(cr && c == '\n') {
            out.push(sanitize(c));
        }
        cr = c == '\r';
    }
    out
}

/// A text run still being written into a frame.
struct Run {
    literal: bool,
    span: Span,
    text: String,
    cr: bool,
}

impl Run {
    fn node(&self) -> DisplayNode {
        DisplayNode {
            span: self.span,
            kind: if self.literal {
                Display::Literal(self.text.clone())
            } else {
                Display::Text(self.text.clone())
            },
            children: vec![],
        }
    }
}

/// An open display container.
struct Frame {
    kind: Display,
    span: Span,
    children: Vec<Arc<DisplayNode>>,
    run: Option<Run>,
}

/// The provisional display tree, built append-only except where a later
/// byte reclassifies the content of the innermost open frame. Finished nodes
/// are shared, so a snapshot copies only the open frames' child lists and
/// the current text run.
struct Builder {
    frames: Vec<Frame>,
    dirty: bool,
}

impl Builder {
    fn new() -> Self {
        Self {
            frames: vec![Frame {
                kind: Display::Paragraph,
                span: Span::default(),
                children: vec![],
                run: None,
            }],
            dirty: false,
        }
    }
    fn top(&mut self) -> &mut Frame {
        self.frames.last_mut().expect("the root frame stays open")
    }
    fn top_kind(&self) -> &Display {
        &self.frames.last().expect("the root frame stays open").kind
    }
    fn flush_run(&mut self) {
        let top = self.top();
        if let Some(run) = top.run.take() {
            top.children.push(Arc::new(run.node()));
        }
    }
    fn extend(&mut self, end: usize) {
        let top = self.top();
        top.span.end = top.span.end.max(end);
    }
    fn open(&mut self, kind: Display, at: usize) {
        self.flush_run();
        self.frames.push(Frame {
            kind,
            span: Span::new(at, at),
            children: vec![],
            run: None,
        });
        self.dirty = true;
    }
    fn close(&mut self) {
        if self.frames.len() < 2 {
            return;
        }
        self.flush_run();
        let frame = self.frames.pop().expect("checked above");
        let node = DisplayNode {
            span: frame.span,
            kind: frame.kind,
            children: frame.children,
        };
        self.extend(node.span.end);
        self.top().children.push(Arc::new(node));
        self.dirty = true;
    }
    /// Drops the innermost frame and everything drawn in it.
    fn discard(&mut self) {
        if self.frames.len() > 1 {
            self.frames.pop();
            self.dirty = true;
        }
    }
    fn push(&mut self, node: DisplayNode) {
        self.flush_run();
        self.extend(node.span.end);
        self.top().children.push(Arc::new(node));
        self.dirty = true;
    }
    fn char(&mut self, c: char, start: usize, end: usize, literal: bool) {
        let top = self.top();
        let same = top
            .run
            .as_ref()
            .is_some_and(|run| run.literal == literal && run.text.len() < RUN_BYTES);
        if !same {
            if let Some(run) = top.run.take() {
                top.children.push(Arc::new(run.node()));
            }
            top.run = Some(Run {
                literal,
                span: Span::new(start, start),
                text: String::new(),
                cr: false,
            });
        }
        let run = top.run.as_mut().expect("opened above");
        if !(run.cr && c == '\n') {
            run.text.push(sanitize(c));
        }
        run.cr = c == '\r';
        run.span.end = end;
        top.span.end = top.span.end.max(end);
        self.dirty = true;
    }
    /// Removes trailing spaces and tabs from the text run being written.
    fn trim_run(&mut self) {
        let top = self.top();
        let Some(run) = &mut top.run else { return };
        let kept = run.text.trim_end_matches([' ', '\t']).len();
        if run.literal || kept == run.text.len() {
            return;
        }
        run.span.end -= run.text.len() - kept;
        run.text.truncate(kept);
        if run.text.is_empty() {
            top.run = None;
        }
        self.dirty = true;
    }
    fn snapshot(&self) -> Revision {
        let mut nested: Option<DisplayNode> = None;
        for (i, frame) in self.frames.iter().enumerate().rev() {
            let mut children = frame.children.clone();
            let mut end = frame.span.end;
            if let Some(run) = &frame.run {
                children.push(Arc::new(run.node()));
            }
            if let Some(node) = nested.take() {
                end = end.max(node.span.end);
                children.push(Arc::new(node));
            }
            if i == 0 {
                return Revision { nodes: children };
            }
            nested = Some(DisplayNode {
                span: Span::new(frame.span.start, end),
                kind: frame.kind.clone(),
                children,
            });
        }
        Revision::default()
    }
}

/// Converts a node from the final parser into inert display, mapping its
/// offsets (relative to `source`) through `map`.
fn convert(node: &Node, source: &str, map: &dyn Fn(usize) -> usize) -> DisplayNode {
    let span = Span::new(map(node.span.start), map(node.span.end));
    let mut children: Vec<Arc<DisplayNode>> = node
        .children
        .iter()
        .map(|child| Arc::new(convert(child, source, map)))
        .collect();
    let kind = match &node.kind {
        Kind::Markdown(markdown) => match markdown {
            Markdown::Paragraph => Display::Paragraph,
            Markdown::Heading(level) => Display::Heading(*level),
            Markdown::ThematicBreak => Display::ThematicBreak,
            Markdown::CodeBlock { language, text } => {
                children = vec![Arc::new(DisplayNode {
                    span,
                    kind: Display::Text(sanitize_str(text)),
                    children: vec![],
                })];
                Display::CodeBlock {
                    language: sanitize_str(language),
                }
            }
            Markdown::Code(text) => Display::Code(sanitize_str(text)),
            Markdown::Quote => Display::Quote,
            Markdown::List { start } => Display::List { start: *start },
            Markdown::Item => Display::Item,
            Markdown::Task(checked) => Display::Task(*checked),
            Markdown::Table => Display::Table,
            Markdown::TableHead => Display::TableHead,
            Markdown::TableRow => Display::TableRow,
            Markdown::TableCell => Display::TableCell,
            Markdown::Emphasis => Display::Emphasis,
            Markdown::Strong => Display::Strong,
            Markdown::Strikethrough => Display::Strikethrough,
            Markdown::Link { image, .. } => Display::Link { image: *image },
            Markdown::SoftBreak => Display::SoftBreak,
            Markdown::HardBreak => Display::HardBreak,
        },
        Kind::Text(text) => Display::Text(sanitize_str(text)),
        Kind::Literal(text) => Display::Literal(sanitize_str(text)),
        Kind::Reference { .. } => Display::PendingReference,
        Kind::Tag { name, block, .. } => Display::Tag {
            name: name.clone(),
            block: *block,
        },
        // Reply children never hold these; if one appears, draw its source.
        Kind::Handle { .. }
        | Kind::Interpolation { .. }
        | Kind::Declaration { .. }
        | Kind::Expression(_) => {
            children.clear();
            Display::Literal(sanitize_str(
                source
                    .get(node.span.start..node.span.end)
                    .unwrap_or_default(),
            ))
        }
    };
    DisplayNode {
        span,
        kind,
        children,
    }
}

/// Decodes UTF-8 one byte at a time, replacing each maximal invalid
/// subsequence with U+FFFD as `String::from_utf8_lossy` does.
#[derive(Default)]
struct Decoder {
    buf: [u8; 4],
    len: usize,
    need: usize,
}

impl Decoder {
    /// Feeds one byte; returns up to two decoded characters, each with the
    /// count of fed bytes (this one included) it does not cover.
    fn push(&mut self, byte: u8, out: &mut Vec<(char, usize)>) {
        if self.len > 0 {
            let valid = match (self.buf[0], self.len) {
                (0xE0, 1) => (0xA0..=0xBF).contains(&byte),
                (0xED, 1) => (0x80..=0x9F).contains(&byte),
                (0xF0, 1) => (0x90..=0xBF).contains(&byte),
                (0xF4, 1) => (0x80..=0x8F).contains(&byte),
                _ => (0x80..=0xBF).contains(&byte),
            };
            if valid {
                self.buf[self.len] = byte;
                self.len += 1;
                if self.len == self.need {
                    let c = std::str::from_utf8(&self.buf[..self.len])
                        .ok()
                        .and_then(|s| s.chars().next())
                        .unwrap_or('\u{fffd}');
                    self.len = 0;
                    out.push((c, 0));
                }
                return;
            }
            self.len = 0;
            out.push(('\u{fffd}', 1));
        }
        let need = match byte {
            0x00..=0x7F => {
                out.push((byte as char, 0));
                return;
            }
            0xC2..=0xDF => 2,
            0xE0..=0xEF => 3,
            0xF0..=0xF4 => 4,
            _ => {
                out.push(('\u{fffd}', 0));
                return;
            }
        };
        self.buf[0] = byte;
        self.len = 1;
        self.need = need;
    }
}

/// One decoded character, at its byte offset in the received text. `fresh`
/// characters have not yet been through line classification; the others are
/// held inline text being examined again.
#[derive(Clone, Copy, Debug)]
struct Item {
    at: usize,
    c: char,
    fresh: bool,
}

impl Item {
    fn end(self) -> usize {
        self.at + self.c.len_utf8()
    }
    fn inline(self) -> Self {
        Self {
            fresh: false,
            ..self
        }
    }
}

fn eol(c: char) -> bool {
    c == '\n' || c == '\r'
}

fn bytes(items: &[Item]) -> usize {
    items.iter().map(|item| item.c.len_utf8()).sum()
}

enum Container {
    Quote,
    List { ordered: bool },
    Item { width: usize },
}

/// The block whose content the current lines feed.
#[derive(Default)]
enum Leaf {
    #[default]
    None,
    Paragraph {
        /// The paragraph's first line, kept while it may still become a table
        /// header row.
        header: Option<Header>,
        lines: usize,
    },
    Heading,
    Fence {
        ch: char,
        len: usize,
        indent: usize,
    },
    Table,
}

#[derive(Default)]
struct Header {
    items: Vec<Item>,
    bytes: usize,
    pipe: bool,
}

enum Line {
    /// The line's start is still being classified.
    Start(Box<LineStart>),
    /// The rest of the line is inline content of the current leaf.
    Content { skip_ws: bool },
    /// The rest of the line is fenced code.
    Code,
}

impl Line {
    fn start() -> Self {
        Self::Start(Box::default())
    }
}

#[derive(Default)]
struct LineStart {
    /// Open containers this line continues so far.
    matched: usize,
    /// Columns of indentation since the last container marker.
    indent: usize,
    /// One space after `>` belongs to the quote marker.
    swallow: bool,
    /// Containers this line opens.
    opened: Vec<Opening>,
    phase: Phase,
    /// Characters this line has consumed so far.
    consumed: usize,
    /// The line was already tried as a table delimiter row.
    no_delim: bool,
    /// The line's content starts a reply declaration.
    declaration: bool,
}

enum Opening {
    Quote {
        at: usize,
    },
    Item {
        at: usize,
        ordered: bool,
        start: Option<u64>,
        width: usize,
    },
}

#[derive(Default)]
enum Phase {
    #[default]
    Match,
    Open,
    Bullet {
        ch: char,
        buf: Vec<Item>,
        indent: usize,
    },
    /// A thematic break so far. `item` is the list marker width when the
    /// line is a list item if it turns out not to be a break.
    Thematic {
        ch: char,
        count: usize,
        buf: Vec<Item>,
        item: Option<usize>,
    },
    Ordered {
        buf: Vec<Item>,
        value: u64,
        digits: usize,
        indent: usize,
    },
    OrderedDelim {
        buf: Vec<Item>,
        value: u64,
        width: usize,
    },
    ItemSpaces {
        at: usize,
        ordered: bool,
        start: Option<u64>,
        width: usize,
        spaces: usize,
    },
    Heading {
        buf: Vec<Item>,
        level: u8,
    },
    FenceRun {
        buf: Vec<Item>,
        ch: char,
        count: usize,
    },
    FenceInfo {
        buf: Vec<Item>,
        ch: char,
        count: usize,
        info: String,
    },
    Declaration {
        buf: Vec<Item>,
    },
    DelimRow {
        buf: Vec<Item>,
    },
    FenceIndent {
        stripped: usize,
    },
    FenceClose {
        buf: Vec<Item>,
        count: usize,
        lead: usize,
        trailing: bool,
    },
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Target {
    Paragraph,
    Heading,
    Cell,
}

/// The inline run in progress: a paragraph, a heading or a table cell.
#[derive(Default)]
struct Inline {
    target: Option<Target>,
    /// The run's frame is open in the builder.
    opened: bool,
    pending: Pending,
    delims: Vec<Delim>,
    /// The source character before the next one, for emphasis flanking.
    prev: char,
    /// Spaces at the end of the run's current line, for hard breaks.
    spaces: usize,
    /// Inside a bare `http(s)://` address, drawn as plain text.
    url: bool,
    recent: String,
    /// The next character is the first of a line, at a block position.
    block_start: bool,
    /// The next character starts a reply declaration.
    declaration: bool,
    /// In a table row: no cell content yet, so a `|` is the leading pipe.
    leading: bool,
}

/// Inline text held until its classification is known.
#[derive(Default)]
enum Pending {
    #[default]
    None,
    Escape(usize),
    Bang(usize),
    Angle {
        at: usize,
        block: bool,
    },
    Delim {
        ch: char,
        at: usize,
        len: usize,
    },
    Reference(Vec<Item>),
    Ticks {
        at: usize,
        len: usize,
    },
    Code(Code),
    Link(Link),
}

struct Code {
    at: usize,
    run: usize,
    buf: Vec<Item>,
    closing: usize,
    bytes: usize,
}

struct Link {
    image: bool,
    buf: Vec<Item>,
    bytes: usize,
    stage: Stage,
    escaped: bool,
}

enum Stage {
    Label { depth: usize },
    Paren,
    Destination { nesting: usize, quote: Option<char> },
}

/// An emphasis delimiter run that may still open emphasis. `index` is its
/// text node among the run frame's children.
struct Delim {
    ch: char,
    count: usize,
    orig: usize,
    close: bool,
    index: usize,
    start: usize,
    end: usize,
}

/// Text taken verbatim, bypassing line classification.
enum Raw {
    /// Reply prose braces: opaque literal through the matching `}`.
    Braces(Braces),
    /// A reply declaration: opaque literal through its last line.
    Declaration(Declaration),
    /// A tag, held whole until it is complete.
    Capture(Box<Capture>),
}

struct Braces {
    depth: usize,
    escaped: bool,
    cr: bool,
    /// Whitespace at the start of a new line, held until the line proves
    /// blank (which ends the braces) or not.
    held: Option<Vec<Item>>,
}

struct Declaration {
    scan: DeclarationScan,
    cr: bool,
    /// The line break and the start of the next line, held until it shows
    /// whether the declaration continues.
    held: Option<(Vec<Item>, String)>,
}

struct Machine {
    decoder: Decoder,
    decoded: Vec<(char, usize)>,
    raw: usize,
    offset: usize,
    stopped: bool,
    queue: VecDeque<Item>,
    out: Builder,
    containers: Vec<Container>,
    leaf: Leaf,
    line: Line,
    /// The end of the paragraph's last line, until the next line shows
    /// whether the paragraph continues.
    eol: Option<usize>,
    cr: bool,
    inline: Inline,
    raw_mode: Option<Raw>,
    work: Work,
}

impl Machine {
    fn new() -> Self {
        Self {
            decoder: Decoder::default(),
            decoded: vec![],
            raw: 0,
            offset: 0,
            stopped: false,
            queue: VecDeque::new(),
            out: Builder::new(),
            containers: vec![],
            leaf: Leaf::None,
            line: Line::start(),
            eol: None,
            cr: false,
            inline: Inline::default(),
            raw_mode: None,
            work: Work::default(),
        }
    }

    fn byte(&mut self, byte: u8) {
        self.work.scanned += 1;
        self.raw += 1;
        let mut decoded = std::mem::take(&mut self.decoded);
        self.decoder.push(byte, &mut decoded);
        for (c, uncovered) in decoded.drain(..) {
            // The final parse keeps no character that ends past the limit.
            if self.raw - uncovered > crate::REPLY_BYTES {
                self.stopped = true;
                break;
            }
            self.char(c);
        }
        self.decoded = decoded;
        if self.raw >= crate::REPLY_BYTES + 3 {
            self.stopped = true;
        }
    }

    fn char(&mut self, c: char) {
        let at = self.offset;
        self.offset += c.len_utf8();
        if at == 0 && c == '\u{feff}' {
            return;
        }
        let mut queue = std::mem::take(&mut self.queue);
        queue.push_back(Item { at, c, fresh: true });
        while let Some(item) = queue.pop_front() {
            self.step(item, &mut queue);
        }
        self.queue = queue;
    }

    fn step(&mut self, item: Item, q: &mut VecDeque<Item>) {
        if self.raw_mode.is_some() {
            self.raw_step(item, q);
        } else if item.fresh {
            self.block_step(item, q);
        } else {
            self.inline_step(item, q);
        }
    }

    /// Queues held characters to be examined again, ahead of anything else.
    fn rerun(&mut self, q: &mut VecDeque<Item>, items: &[Item]) {
        for item in items.iter().rev() {
            self.work.reexamined += item.c.len_utf8();
            q.push_front(*item);
        }
    }

    /// Examines held inline characters again now, to completion, outside the
    /// line machine. Used when a block ends and its held text must resolve.
    fn run_now(&mut self, items: &[Item]) {
        let mut q = VecDeque::new();
        self.rerun(&mut q, items);
        while let Some(item) = q.pop_front() {
            if self.raw_mode.is_some() {
                self.raw_step(item, &mut q);
            } else {
                self.inline_step(item.inline(), &mut q);
            }
        }
    }
}

/// What a line start resolved to, when it was not inline content.
enum Block {
    Heading {
        at: usize,
        level: u8,
    },
    Thematic {
        span: Span,
    },
    Fence {
        at: usize,
        ch: char,
        len: usize,
        indent: usize,
        language: String,
    },
}

impl Machine {
    fn block_step(&mut self, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        let crlf = c == '\n' && self.cr;
        self.cr = c == '\r';
        if crlf {
            return;
        }
        match &mut self.line {
            Line::Content { skip_ws } => {
                if eol(c) {
                    self.content_eol(item.at);
                    return;
                }
                if *skip_ws && (c == ' ' || c == '\t') {
                    return;
                }
                *skip_ws = false;
                if let Leaf::Paragraph {
                    header: Some(header),
                    lines: 1,
                } = &mut self.leaf
                {
                    header.items.push(item);
                    header.bytes += c.len_utf8();
                    header.pipe |= c == '|';
                    if header.bytes > STREAM_CONTEXT_BYTES {
                        self.drop_header();
                    }
                }
                self.inline_step(item.inline(), q);
            }
            Line::Code => {
                self.out.char(c, item.at, item.end(), false);
                if eol(c) {
                    self.out.flush_run();
                    self.line = Line::start();
                }
            }
            Line::Start(_) => self.line_char(item, q),
        }
    }

    fn drop_header(&mut self) {
        if let Leaf::Paragraph { header, .. } = &mut self.leaf {
            *header = None;
        }
    }

    /// A line of inline content ends.
    fn content_eol(&mut self, at: usize) {
        match &mut self.leaf {
            Leaf::Paragraph { header, .. } => {
                if header.as_ref().is_some_and(|header| !header.pipe) {
                    *header = None;
                }
                self.eol = Some(at);
            }
            Leaf::Heading => {
                self.end_run();
                self.leaf = Leaf::None;
            }
            Leaf::Table => {
                self.end_run();
                self.out.close();
            }
            Leaf::None | Leaf::Fence { .. } => {}
        }
        self.line = Line::start();
    }

    /// Classifies the start of a line, one character at a time.
    fn line_char(&mut self, item: Item, q: &mut VecDeque<Item>) {
        let Line::Start(mut ls) = std::mem::replace(&mut self.line, Line::Code) else {
            return;
        };
        let decided = self.classify(&mut ls, item, q);
        if !decided {
            ls.consumed += 1;
            self.line = Line::Start(ls);
        }
    }

    /// Returns true when the line start is decided and `self.line` is set.
    fn classify(&mut self, ls: &mut LineStart, item: Item, q: &mut VecDeque<Item>) -> bool {
        let c = item.c;
        loop {
            if ls.swallow {
                ls.swallow = false;
                if c == ' ' {
                    return false;
                }
            }
            match &mut ls.phase {
                Phase::Match => {
                    while matches!(
                        self.containers.get(ls.matched),
                        Some(Container::List { .. })
                    ) {
                        ls.matched += 1;
                    }
                    if ls.matched == self.containers.len() {
                        ls.phase = if matches!(self.leaf, Leaf::Fence { .. }) {
                            Phase::FenceIndent { stripped: 0 }
                        } else {
                            Phase::Open
                        };
                        continue;
                    }
                    if eol(c) {
                        return self.blank_line(ls, item);
                    }
                    let fits = match self.containers[ls.matched] {
                        Container::Quote => {
                            if c == '>' && ls.indent <= 3 {
                                ls.matched += 1;
                                ls.indent = 0;
                                ls.swallow = true;
                                return false;
                            }
                            (c == ' ' || c == '\t') && ls.indent < 3
                        }
                        Container::Item { width } => {
                            if c == ' ' || c == '\t' {
                                ls.indent += if c == '\t' { 4 - ls.indent % 4 } else { 1 };
                                if ls.indent >= width {
                                    ls.indent -= width;
                                    ls.matched += 1;
                                }
                                return false;
                            }
                            false
                        }
                        Container::List { .. } => unreachable!("lists match through their items"),
                    };
                    if fits {
                        ls.indent += if c == '\t' { 4 - ls.indent % 4 } else { 1 };
                        return false;
                    }
                    // The line leaves an open container. Fenced code has no
                    // lazy continuation, so it ends here.
                    if matches!(self.leaf, Leaf::Fence { .. }) {
                        self.close_containers(ls.matched);
                    }
                    ls.phase = Phase::Open;
                }
                Phase::Open => {
                    if c == ' ' || c == '\t' {
                        ls.indent += if c == '\t' { 4 - ls.indent % 4 } else { 1 };
                        return false;
                    }
                    if eol(c) {
                        return self.blank_line(ls, item);
                    }
                    let header = matches!(
                        &self.leaf,
                        Leaf::Paragraph { header: Some(h), lines: 1 } if h.pipe
                    );
                    if header
                        && !ls.no_delim
                        && ls.opened.is_empty()
                        && ls.matched == self.containers.len()
                        && matches!(c, '|' | '-' | ':')
                    {
                        ls.phase = Phase::DelimRow { buf: vec![item] };
                        return false;
                    }
                    if ls.indent >= 4 {
                        return self.content(ls, &[item], q);
                    }
                    let buf = vec![item];
                    let indent = ls.indent;
                    ls.phase = match c {
                        '>' => {
                            ls.opened.push(Opening::Quote { at: item.at });
                            ls.indent = 0;
                            ls.swallow = true;
                            return false;
                        }
                        '-' | '+' | '*' => Phase::Bullet { ch: c, buf, indent },
                        '_' => Phase::Thematic {
                            ch: c,
                            count: 1,
                            buf,
                            item: None,
                        },
                        '0'..='9' => Phase::Ordered {
                            buf,
                            value: u64::from(c as u8 - b'0'),
                            digits: 1,
                            indent,
                        },
                        '#' => Phase::Heading { buf, level: 1 },
                        '`' | '~' => Phase::FenceRun {
                            buf,
                            ch: c,
                            count: 1,
                        },
                        'e' if ls.consumed == 0 => Phase::Declaration { buf },
                        _ => return self.content(ls, &buf, q),
                    };
                    return false;
                }
                Phase::Bullet { ch, buf, indent } => {
                    let (ch, indent) = (*ch, *indent);
                    if c == ' ' || c == '\t' || eol(c) {
                        if ch == '+' {
                            ls.phase = Phase::ItemSpaces {
                                at: buf[0].at,
                                ordered: false,
                                start: None,
                                width: indent + 1,
                                spaces: 0,
                            };
                        } else {
                            ls.phase = Phase::Thematic {
                                ch,
                                count: 1,
                                buf: std::mem::take(buf),
                                item: Some(indent + 1),
                            };
                        }
                        continue;
                    }
                    if c == ch && ch != '+' {
                        buf.push(item);
                        ls.phase = Phase::Thematic {
                            ch,
                            count: 2,
                            buf: std::mem::take(buf),
                            item: None,
                        };
                        return false;
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    return self.content(ls, &buf, q);
                }
                Phase::Thematic {
                    ch,
                    count,
                    buf,
                    item: marker,
                } => {
                    if c == *ch || c == ' ' || c == '\t' {
                        // Marks and blanks are ASCII, so the length is in bytes.
                        if buf.len() >= STREAM_CONTEXT_BYTES {
                            let mut buf = std::mem::take(buf);
                            buf.push(item);
                            return self.content_with(ls, &buf, q, true);
                        }
                        *count += usize::from(c == *ch);
                        buf.push(item);
                        return false;
                    }
                    if eol(c) && *count >= 3 {
                        let span = Span::new(buf[0].at, item.at);
                        return self.block(ls, Block::Thematic { span }, Some(item), q);
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    if let Some(width) = *marker {
                        // Not a break: a list item whose content follows the
                        // marker.
                        ls.phase = Phase::ItemSpaces {
                            at: buf[0].at,
                            ordered: false,
                            start: None,
                            width,
                            spaces: 0,
                        };
                        self.rerun(q, &buf[1..]);
                        return false;
                    }
                    return self.content(ls, &buf, q);
                }
                Phase::Ordered {
                    buf,
                    value,
                    digits,
                    indent,
                } => {
                    if c.is_ascii_digit() && *digits < 9 {
                        *value = *value * 10 + u64::from(c as u8 - b'0');
                        *digits += 1;
                        buf.push(item);
                        return false;
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    if c == '.' || c == ')' {
                        ls.phase = Phase::OrderedDelim {
                            width: *indent + *digits + 1,
                            value: *value,
                            buf,
                        };
                        return false;
                    }
                    return self.content(ls, &buf, q);
                }
                Phase::OrderedDelim { buf, value, width } => {
                    if c == ' ' || c == '\t' || eol(c) {
                        ls.phase = Phase::ItemSpaces {
                            at: buf[0].at,
                            ordered: true,
                            start: Some(*value),
                            width: *width,
                            spaces: 0,
                        };
                        continue;
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    return self.content(ls, &buf, q);
                }
                Phase::ItemSpaces {
                    at,
                    ordered,
                    start,
                    width,
                    spaces,
                } => {
                    if c == ' ' || c == '\t' {
                        *spaces += if c == '\t' {
                            4 - (*width + *spaces) % 4
                        } else {
                            1
                        };
                        return false;
                    }
                    let content_spaces = if *spaces == 0 || *spaces > 4 {
                        1
                    } else {
                        *spaces
                    };
                    ls.indent = spaces.saturating_sub(content_spaces);
                    ls.opened.push(Opening::Item {
                        at: *at,
                        ordered: *ordered,
                        start: *start,
                        width: *width + content_spaces,
                    });
                    ls.phase = Phase::Open;
                }
                Phase::Heading { buf, level } => {
                    if c == '#' && *level < 6 {
                        *level += 1;
                        buf.push(item);
                        return false;
                    }
                    if c == ' ' || c == '\t' || eol(c) {
                        let block = Block::Heading {
                            at: buf[0].at,
                            level: *level,
                        };
                        return self.block(ls, block, eol(c).then_some(item), q);
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    return self.content(ls, &buf, q);
                }
                Phase::FenceRun { buf, ch, count } => {
                    if c == *ch {
                        *count += 1;
                        buf.push(item);
                        return false;
                    }
                    if *count < 3 {
                        let mut buf = std::mem::take(buf);
                        buf.push(item);
                        return self.content(ls, &buf, q);
                    }
                    ls.phase = Phase::FenceInfo {
                        buf: std::mem::take(buf),
                        ch: *ch,
                        count: *count,
                        info: String::new(),
                    };
                }
                Phase::FenceInfo {
                    buf,
                    ch,
                    count,
                    info,
                } => {
                    if eol(c) {
                        let block = Block::Fence {
                            at: buf[0].at,
                            ch: *ch,
                            len: *count,
                            indent: ls.indent,
                            language: info.split_whitespace().next().unwrap_or("").to_owned(),
                        };
                        return self.block(ls, block, None, q);
                    }
                    buf.push(item);
                    // A backtick fence's info string has no backticks; a held
                    // line stops at the context cap.
                    if *ch == '`' && c == '`' {
                        let buf = std::mem::take(buf);
                        return self.content(ls, &buf, q);
                    }
                    if info.len() >= STREAM_CONTEXT_BYTES {
                        let buf = std::mem::take(buf);
                        return self.content_with(ls, &buf, q, true);
                    }
                    info.push(c);
                    return false;
                }
                Phase::Declaration { buf } => {
                    let mut text: String = buf.iter().map(|item| item.c).collect();
                    text.push(c);
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    match declaration_prefix(&text) {
                        Some(true) => {
                            ls.declaration = true;
                            return self.content(ls, &buf, q);
                        }
                        Some(false) => {
                            ls.phase = Phase::Declaration { buf };
                            return false;
                        }
                        None => return self.content(ls, &buf, q),
                    }
                }
                Phase::DelimRow { buf } => {
                    if matches!(c, '|' | '-' | ':' | ' ' | '\t') && buf.len() < STREAM_CONTEXT_BYTES
                    {
                        buf.push(item);
                        return false;
                    }
                    let mut buf = std::mem::take(buf);
                    if eol(c) && self.delimiter_row(&buf) {
                        self.make_table(&buf);
                        self.line = Line::start();
                        return true;
                    }
                    // Not a delimiter row: classify the line again as usual.
                    ls.no_delim = true;
                    ls.phase = Phase::Open;
                    buf.push(item);
                    self.rerun(q, &buf);
                    return false;
                }
                Phase::FenceIndent { stripped } => {
                    let indent = match self.leaf {
                        Leaf::Fence { indent, .. } => indent,
                        _ => 0,
                    };
                    if c == ' ' && *stripped < indent {
                        *stripped += 1;
                        return false;
                    }
                    ls.phase = Phase::FenceClose {
                        buf: vec![],
                        count: 0,
                        lead: 0,
                        trailing: false,
                    };
                }
                Phase::FenceClose {
                    buf,
                    count,
                    lead,
                    trailing,
                } => {
                    let (ch, len) = match self.leaf {
                        Leaf::Fence { ch, len, .. } => (ch, len),
                        _ => ('`', usize::MAX),
                    };
                    if eol(c) && *count >= len {
                        self.close_leaf();
                        self.line = Line::start();
                        return true;
                    }
                    let held = if c == ' ' && *count == 0 && *lead < 3 {
                        *lead += 1;
                        true
                    } else if c == ch && !*trailing {
                        *count += 1;
                        true
                    } else if (c == ' ' || c == '\t') && *count > 0 {
                        *trailing = true;
                        true
                    } else {
                        false
                    };
                    if held && !eol(c) {
                        buf.push(item);
                        return false;
                    }
                    let mut buf = std::mem::take(buf);
                    buf.push(item);
                    self.line = Line::Code;
                    self.rerun(q, &buf);
                    return true;
                }
            }
        }
    }

    /// The line is a blank line (only whitespace and container markers).
    fn blank_line(&mut self, ls: &mut LineStart, item: Item) -> bool {
        // A blank line continues open list items but not quotes.
        let mut keep = ls.matched;
        while keep < self.containers.len() && !matches!(self.containers[keep], Container::Quote) {
            keep += 1;
        }
        if ls.opened.is_empty()
            && keep == self.containers.len()
            && matches!(self.leaf, Leaf::Fence { .. })
        {
            self.out.char('\n', item.at, item.end(), false);
            self.out.flush_run();
        } else if ls.opened.is_empty() {
            self.close_containers(keep);
        } else {
            self.settle(ls, false);
        }
        self.line = Line::start();
        true
    }

    /// Closes and opens containers for a line that is not a paragraph
    /// continuation. Returns true when the line continues the open paragraph.
    fn settle(&mut self, ls: &mut LineStart, content: bool) -> bool {
        if content && ls.opened.is_empty() && matches!(self.leaf, Leaf::Paragraph { .. }) {
            return true;
        }
        let mut keep = ls.matched;
        if keep > 0 {
            if let Container::List { ordered } = self.containers[keep - 1] {
                let same = matches!(ls.opened.first(), Some(Opening::Item { ordered: o, .. }) if *o == ordered);
                if !same {
                    keep -= 1;
                }
            }
        }
        self.close_containers(keep);
        for opening in std::mem::take(&mut ls.opened) {
            if self.containers.len() + 2 > CONTAINERS {
                break;
            }
            match opening {
                Opening::Quote { at } => {
                    self.containers.push(Container::Quote);
                    self.out.open(Display::Quote, at);
                }
                Opening::Item {
                    at,
                    ordered,
                    start,
                    width,
                } => {
                    let reuse = matches!(self.containers.last(), Some(Container::List { ordered: o }) if *o == ordered);
                    if !reuse {
                        if matches!(self.containers.last(), Some(Container::List { .. })) {
                            self.containers.pop();
                            self.out.close();
                        }
                        self.containers.push(Container::List { ordered });
                        self.out.open(Display::List { start }, at);
                    }
                    self.containers.push(Container::Item { width });
                    self.out.open(Display::Item, at);
                }
            }
        }
        false
    }

    fn close_leaf(&mut self) {
        match std::mem::take(&mut self.leaf) {
            Leaf::Paragraph { .. } | Leaf::Heading => self.end_run(),
            Leaf::Fence { .. } => self.out.close(),
            Leaf::Table => {
                self.end_run();
                while matches!(
                    self.out.top_kind(),
                    Display::TableRow | Display::TableHead | Display::TableCell
                ) {
                    self.out.close();
                }
                self.out.close();
            }
            Leaf::None => {}
        }
        self.eol = None;
    }

    fn close_containers(&mut self, keep: usize) {
        self.close_leaf();
        while self.containers.len() > keep {
            self.containers.pop();
            self.out.close();
        }
    }

    /// The line is inline content; `items` are its characters so far.
    fn content(&mut self, ls: &mut LineStart, items: &[Item], q: &mut VecDeque<Item>) -> bool {
        self.content_with(ls, items, q, false)
    }

    /// Like [`Self::content`]; when `capped`, the held line start reached its
    /// cap and is drawn as a provisional literal instead of examined again.
    fn content_with(
        &mut self,
        ls: &mut LineStart,
        items: &[Item],
        q: &mut VecDeque<Item>,
        capped: bool,
    ) -> bool {
        let block_start = ls.indent <= 3 && !capped;
        let row = ls.opened.is_empty()
            && ls.matched == self.containers.len()
            && matches!(self.leaf, Leaf::Table);
        self.line = Line::Content { skip_ws: false };
        let mut newline = None;
        if row {
            self.out.open(Display::TableRow, items[0].at);
            self.start_run(Target::Cell);
        } else if self.settle(ls, true) {
            if let Leaf::Paragraph { header, lines } = &mut self.leaf {
                *lines += 1;
                *header = None;
            }
            newline = self.eol.take().map(|at| Item {
                at,
                c: '\n',
                fresh: false,
            });
        } else {
            self.leaf = Leaf::Paragraph {
                header: Some(Header::default()),
                lines: 1,
            };
            self.start_run(Target::Paragraph);
        }
        self.inline.block_start = block_start && !row;
        self.inline.declaration = ls.declaration;
        if capped {
            if let Some(newline) = newline {
                self.run_now(&[newline]);
            }
            self.drop_header();
            self.cap_flush(items, "", items[0].at);
        } else {
            // Both go to the front of the queue: the line's characters first,
            // then the paragraph's line break ahead of them, so the break is
            // examined before the characters that follow it.
            self.rerun(q, items);
            if let Some(newline) = newline {
                q.push_front(newline);
            }
        }
        true
    }

    /// The line starts a block that is not a paragraph.
    fn block(
        &mut self,
        ls: &mut LineStart,
        block: Block,
        eol_item: Option<Item>,
        q: &mut VecDeque<Item>,
    ) -> bool {
        self.settle(ls, false);
        match block {
            Block::Thematic { span } => {
                self.out.push(DisplayNode {
                    span,
                    kind: Display::ThematicBreak,
                    children: vec![],
                });
                self.line = Line::start();
            }
            Block::Heading { at, level } => {
                self.out.open(Display::Heading(level), at);
                self.leaf = Leaf::Heading;
                self.start_run(Target::Heading);
                self.inline.opened = true;
                self.line = Line::Content { skip_ws: true };
                if let Some(item) = eol_item {
                    self.rerun(q, &[item]);
                }
            }
            Block::Fence {
                at,
                ch,
                len,
                indent,
                language,
            } => {
                self.out.open(
                    Display::CodeBlock {
                        language: sanitize_str(&language),
                    },
                    at,
                );
                self.leaf = Leaf::Fence { ch, len, indent };
                self.line = Line::start();
            }
        }
        true
    }

    /// Whether a held line is a GFM delimiter row matching the held header.
    fn delimiter_row(&self, buf: &[Item]) -> bool {
        let Leaf::Paragraph {
            header: Some(header),
            ..
        } = &self.leaf
        else {
            return false;
        };
        let row: String = buf.iter().map(|item| item.c).collect();
        let head: String = header.items.iter().map(|item| item.c).collect();
        let cells = row_cells(&row);
        !cells.is_empty()
            && cells.len() == row_cells(&head).len()
            && cells.iter().all(|cell| {
                let cell = cell.trim();
                let dashes = cell.trim_start_matches(':').trim_end_matches(':');
                !dashes.is_empty()
                    && dashes.bytes().all(|b| b == b'-')
                    && cell.len() - dashes.len() <= 2
            })
    }

    /// Redraws the held paragraph line as a table header.
    fn make_table(&mut self, delimiter: &[Item]) {
        let header = match &mut self.leaf {
            Leaf::Paragraph { header, .. } => header.take().unwrap_or_default(),
            _ => Header::default(),
        };
        self.discard_run();
        self.leaf = Leaf::Table;
        let at = header.items.first().map_or(delimiter[0].at, |item| item.at);
        self.out.open(Display::Table, at);
        self.out.open(Display::TableHead, at);
        self.start_run(Target::Cell);
        self.run_now(&header.items);
        self.finish_raw();
        self.end_run();
        self.out.close();
    }
}

/// Splits a table row into cells at unescaped pipes, ignoring one leading
/// and one trailing pipe.
fn row_cells(row: &str) -> Vec<String> {
    let row = row.trim();
    let row = row.strip_prefix('|').unwrap_or(row);
    let row = match row.strip_suffix('|') {
        Some(rest) if !rest.ends_with('\\') => rest,
        _ => row,
    };
    let mut cells = vec![String::new()];
    let mut escaped = false;
    for c in row.chars() {
        if c == '|' && !escaped {
            cells.push(String::new());
        } else {
            cells.last_mut().expect("starts non-empty").push(c);
        }
        escaped = c == '\\' && !escaped;
    }
    cells
}

/// Whether `text` is a reply declaration's start (`export`, whitespace,
/// `let` or `const`, whitespace): `Some(true)` when it is, `Some(false)` when
/// it still may be, `None` when it cannot be.
fn declaration_prefix(text: &str) -> Option<bool> {
    if text.len() > 64 {
        return None;
    }
    let Some(rest) = text.strip_prefix("export") else {
        return "export".starts_with(text).then_some(false);
    };
    if rest.is_empty() {
        return Some(false);
    }
    if !rest.starts_with([' ', '\t']) {
        return None;
    }
    let rest = rest.trim_start_matches([' ', '\t']);
    for keyword in ["let", "const"] {
        if let Some(tail) = rest.strip_prefix(keyword) {
            return match tail.chars().next() {
                None => Some(false),
                Some(' ' | '\t') => Some(true),
                Some(_) => None,
            };
        }
        if keyword.starts_with(rest) {
            return Some(false);
        }
    }
    None
}

fn is_punctuation(c: char) -> bool {
    c.is_ascii_punctuation() || (!c.is_ascii() && !c.is_alphanumeric() && !c.is_whitespace())
}

/// A provisional literal for held text, control characters replaced.
fn literal_node(items: &[Item], prefix: &str, at: usize) -> DisplayNode {
    let mut text = String::from(prefix);
    text.extend(items.iter().map(|item| item.c));
    DisplayNode {
        span: Span::new(
            at,
            items.last().map_or(at + prefix.len(), |item| item.end()),
        ),
        kind: Display::Literal(sanitize_str(&text)),
        children: vec![],
    }
}

impl Machine {
    fn start_run(&mut self, target: Target) {
        self.inline = Inline {
            target: Some(target),
            prev: ' ',
            leading: target == Target::Cell,
            ..Inline::default()
        };
    }

    /// Opens the run's frame before its first drawn content.
    fn ensure_frame(&mut self, at: usize) {
        if self.inline.opened {
            return;
        }
        let kind = match self.inline.target {
            Some(Target::Cell) => Display::TableCell,
            Some(Target::Heading) => Display::Heading(1),
            Some(Target::Paragraph) => Display::Paragraph,
            None => {
                // The inline run already ended, yet inline text arrives on
                // the same line: the rest of a line after a block tag ended
                // the paragraph, or held text the final parser left
                // unconsumed after a tag. That text starts a new paragraph.
                self.leaf = Leaf::Paragraph {
                    header: None,
                    lines: 1,
                };
                self.inline.target = Some(Target::Paragraph);
                Display::Paragraph
            }
        };
        self.out.open(kind, at);
        self.inline.opened = true;
    }

    fn emit_char(&mut self, c: char, start: usize, end: usize) {
        self.ensure_frame(start);
        self.out.char(c, start, end, false);
        let inline = &mut self.inline;
        inline.prev = c;
        inline.spaces = if c == ' ' { inline.spaces + 1 } else { 0 };
        inline.recent.push(c);
        if inline.recent.len() > 8 {
            inline.recent.remove(0);
        }
        if inline.recent.ends_with("http://") || inline.recent.ends_with("https://") {
            inline.url = true;
        }
    }

    fn emit_items(&mut self, items: &[Item]) {
        for item in items {
            self.emit_char(item.c, item.at, item.end());
        }
    }

    fn literal_char(&mut self, item: Item) {
        self.ensure_frame(item.at);
        self.out.char(item.c, item.at, item.end(), true);
        self.inline.prev = item.c;
        self.inline.spaces = 0;
        self.inline.recent.clear();
    }

    fn emit_node(&mut self, node: DisplayNode, prev: char) {
        self.ensure_frame(node.span.start);
        self.out.push(node);
        self.inline.prev = prev;
        self.inline.spaces = 0;
        self.inline.recent.clear();
    }

    /// Draws held text as a provisional literal because its context reached a
    /// buffering cap, and forgets the context.
    fn cap_flush(&mut self, items: &[Item], prefix: &str, at: usize) {
        self.work.reexamined += bytes(items) + prefix.len();
        let prev = items.last().map_or(' ', |item| item.c);
        self.emit_node(literal_node(items, prefix, at), prev);
    }

    /// Ends the inline run: held text resolves as the block ends.
    fn end_run(&mut self) {
        let Some(target) = self.inline.target else {
            return;
        };
        let pending = std::mem::take(&mut self.inline.pending);
        let again = self.resolve_end(pending);
        if !again.is_empty() {
            self.run_now(&again);
        }
        // Anything held again by that pass is drawn as text, without another.
        let pending = std::mem::take(&mut self.inline.pending);
        self.flush_pending(pending);
        self.finish_raw();
        if self.inline.opened {
            self.out.trim_run();
            if target == Target::Heading {
                self.trim_closing_hashes();
            }
            self.out.close();
        }
        self.inline = Inline::default();
    }

    /// Drops the inline run and everything it drew.
    fn discard_run(&mut self) {
        if self.inline.opened {
            self.out.discard();
        }
        self.inline = Inline::default();
        self.eol = None;
    }

    /// A heading's optional closing sequence of `#` is not drawn.
    fn trim_closing_hashes(&mut self) {
        let top = self.out.top();
        let Some(run) = &mut top.run else { return };
        let kept = run.text.trim_end_matches('#');
        if kept.len() < run.text.len() && (kept.is_empty() || kept.ends_with([' ', '\t'])) {
            let kept = kept.trim_end_matches([' ', '\t']).len();
            run.span.end -= run.text.len() - kept;
            run.text.truncate(kept);
            if run.text.is_empty() {
                top.run = None;
            }
            self.out.dirty = true;
        }
    }

    /// Resolves a held context at the end of its block. Returns held
    /// characters to examine once more.
    fn resolve_end(&mut self, pending: Pending) -> Vec<Item> {
        match pending {
            Pending::Code(code) if code.closing == code.run => {
                self.resolve_code(code);
                vec![]
            }
            Pending::Code(code) => {
                for i in 0..code.run {
                    self.emit_char('`', code.at + i, code.at + i + 1);
                }
                code.buf
            }
            Pending::Link(link) => {
                let opener = if link.image { 2 } else { 1 };
                self.emit_items(&link.buf[..opener]);
                link.buf[opener..].to_vec()
            }
            other => {
                self.flush_pending(other);
                vec![]
            }
        }
    }

    /// Draws a held context as plain text.
    fn flush_pending(&mut self, pending: Pending) {
        match pending {
            Pending::None => {}
            Pending::Escape(at) => self.emit_char('\\', at, at + 1),
            Pending::Bang(at) => self.emit_char('!', at, at + 1),
            Pending::Angle { at, .. } => self.emit_char('<', at, at + 1),
            Pending::Delim { ch, at, len } => self.resolve_delim(ch, at, len, ' '),
            Pending::Ticks { at, len } => {
                for i in 0..len {
                    self.emit_char('`', at + i, at + i + 1);
                }
            }
            Pending::Reference(buf) => {
                self.work.reexamined += bytes(&buf);
                self.emit_items(&buf);
            }
            Pending::Code(code) => {
                self.work.reexamined += code.bytes;
                for i in 0..code.run {
                    self.emit_char('`', code.at + i, code.at + i + 1);
                }
                self.emit_items(&code.buf);
            }
            Pending::Link(link) => {
                self.work.reexamined += link.bytes;
                self.emit_items(&link.buf);
            }
        }
    }

    fn inline_step(&mut self, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        // GFM splits table cells at unescaped pipes before inline parsing.
        if self.inline.target == Some(Target::Cell)
            && c == '|'
            && !matches!(self.inline.pending, Pending::Escape(_))
        {
            self.cell_boundary(item);
            return;
        }
        match std::mem::take(&mut self.inline.pending) {
            Pending::None => self.plain(item, q),
            Pending::Escape(at) => {
                if c == '\n' {
                    let node = DisplayNode {
                        span: Span::new(at, item.end()),
                        kind: Display::HardBreak,
                        children: vec![],
                    };
                    self.emit_node(node, '\n');
                } else if c.is_ascii_punctuation() {
                    self.emit_char(c, at, item.end());
                } else {
                    self.emit_char('\\', at, at + 1);
                    q.push_front(item);
                }
            }
            Pending::Bang(at) => {
                if c == '[' {
                    self.inline.pending = Pending::Link(Link {
                        image: true,
                        buf: vec![
                            Item {
                                at,
                                c: '!',
                                fresh: false,
                            },
                            item,
                        ],
                        bytes: 2,
                        stage: Stage::Label { depth: 1 },
                        escaped: false,
                    });
                } else {
                    self.emit_char('!', at, at + 1);
                    q.push_front(item);
                }
            }
            Pending::Angle { at, block } => {
                if c.is_ascii_uppercase() {
                    self.drop_header();
                    self.raw_mode = Some(Raw::Capture(Box::new(Capture::new(at, block))));
                    self.raw_step(item, q);
                } else {
                    self.emit_char('<', at, at + 1);
                    q.push_front(item);
                }
            }
            Pending::Delim { ch, at, len } => {
                if c == ch {
                    self.inline.pending = Pending::Delim {
                        ch,
                        at,
                        len: len + 1,
                    };
                } else {
                    self.resolve_delim(ch, at, len, c);
                    q.push_front(item);
                }
            }
            Pending::Reference(buf) => self.reference(buf, item, q),
            Pending::Ticks { at, len } => {
                if c == '`' {
                    self.inline.pending = Pending::Ticks { at, len: len + 1 };
                } else {
                    self.inline.pending = Pending::Code(Code {
                        at,
                        run: len,
                        buf: vec![],
                        closing: 0,
                        bytes: len,
                    });
                    q.push_front(item);
                }
            }
            Pending::Code(code) => self.code_char(code, item, q),
            Pending::Link(link) => self.link_char(link, item, q),
        }
    }

    fn plain(&mut self, item: Item, q: &mut VecDeque<Item>) {
        let (c, at) = (item.c, item.at);
        if c == '\n' {
            self.newline(at);
            return;
        }
        if std::mem::take(&mut self.inline.declaration) {
            self.drop_header();
            self.raw_mode = Some(Raw::Declaration(Declaration {
                scan: DeclarationScan::default(),
                cr: false,
                held: None,
            }));
            self.raw_step(item, q);
            return;
        }
        if self.inline.url {
            if c.is_whitespace() || c == '<' || c == '>' {
                self.inline.url = false;
            } else {
                self.emit_char(c, at, item.end());
                return;
            }
        }
        let block = std::mem::take(&mut self.inline.block_start);
        if self.inline.target == Some(Target::Cell)
            && !self.inline.opened
            && (c == ' ' || c == '\t')
        {
            return;
        }
        self.inline.leading = false;
        self.inline.pending = match c {
            '\\' => Pending::Escape(at),
            '`' => Pending::Ticks { at, len: 1 },
            '[' => Pending::Link(Link {
                image: false,
                buf: vec![item],
                bytes: 1,
                stage: Stage::Label { depth: 1 },
                escaped: false,
            }),
            '!' => Pending::Bang(at),
            '*' | '_' | '~' => Pending::Delim { ch: c, at, len: 1 },
            '$' => Pending::Reference(vec![item]),
            '<' => Pending::Angle { at, block },
            '{' => {
                // Reply prose braces are opaque literal text.
                self.drop_header();
                self.literal_char(item);
                self.raw_mode = Some(Raw::Braces(Braces {
                    depth: 1,
                    escaped: false,
                    cr: false,
                    held: None,
                }));
                Pending::None
            }
            _ => {
                self.emit_char(c, at, item.end());
                Pending::None
            }
        };
    }

    /// A line break inside a paragraph.
    fn newline(&mut self, at: usize) {
        let hard = self.inline.spaces >= 2;
        if self.inline.opened {
            self.out.trim_run();
            let node = DisplayNode {
                span: Span::new(at, at + 1),
                kind: if hard {
                    Display::HardBreak
                } else {
                    Display::SoftBreak
                },
                children: vec![],
            };
            self.out.push(node);
        }
        self.inline.prev = '\n';
        self.inline.spaces = 0;
        self.inline.url = false;
        self.inline.recent.clear();
    }

    fn cell_boundary(&mut self, item: Item) {
        if std::mem::take(&mut self.inline.leading) {
            return;
        }
        let opened = self.inline.opened;
        self.end_run();
        if !opened {
            self.out.open(Display::TableCell, item.at);
            self.out.close();
        }
        self.start_run(Target::Cell);
        self.inline.leading = false;
    }

    /// Resolves a run of `*`, `_` or `~` once the character after it is
    /// known, following CommonMark's flanking and matching rules.
    fn resolve_delim(&mut self, ch: char, at: usize, len: usize, next: char) {
        let prev = self.inline.prev;
        let left = !next.is_whitespace()
            && (!is_punctuation(next) || prev.is_whitespace() || is_punctuation(prev));
        let right = !prev.is_whitespace()
            && (!is_punctuation(prev) || next.is_whitespace() || is_punctuation(next));
        let (open, close) = if ch == '_' {
            (
                left && (!right || is_punctuation(prev)),
                right && (!left || is_punctuation(next)),
            )
        } else {
            (left, right)
        };
        if ch == '~' && len != 2 {
            for i in 0..len {
                self.emit_char('~', at + i, at + i + 1);
            }
            return;
        }
        let mut count = len;
        let mut pos = at;
        while close && count > 0 {
            let found = self.inline.delims.iter().rposition(|d| {
                d.ch == ch
                    && (ch != '~' || d.count == count)
                    && !((d.close || open)
                        && (d.orig + len).is_multiple_of(3)
                        && !(d.orig.is_multiple_of(3) && len.is_multiple_of(3)))
            });
            let Some(j) = found else { break };
            let used = if ch == '~' || (self.inline.delims[j].count >= 2 && count >= 2) {
                2
            } else {
                1
            };
            let kind = match (ch, used) {
                ('~', _) => Display::Strikethrough,
                (_, 2) => Display::Strong,
                _ => Display::Emphasis,
            };
            self.wrap(j, used, pos, kind);
            count -= used;
            pos += used;
        }
        if count > 0 {
            self.ensure_frame(pos);
            self.out.push(DisplayNode {
                span: Span::new(pos, at + len),
                kind: Display::Text(std::iter::repeat_n(ch, count).collect()),
                children: vec![],
            });
            if open && self.inline.delims.len() < EMPHASIS_DEPTH {
                let index = self.out.top().children.len() - 1;
                self.inline.delims.push(Delim {
                    ch,
                    count,
                    orig: len,
                    close,
                    index,
                    start: pos,
                    end: at + len,
                });
            }
        }
        self.inline.prev = ch;
        self.inline.spaces = 0;
        self.inline.recent.clear();
    }

    /// Wraps everything drawn after opener `j` into an emphasis node closed
    /// by `used` delimiters at `pos`.
    fn wrap(&mut self, j: usize, used: usize, pos: usize, kind: Display) {
        self.out.flush_run();
        let opener = &mut self.inline.delims[j];
        opener.count -= used;
        opener.end -= used;
        let (index, count, start, end, ch) = (
            opener.index,
            opener.count,
            opener.start,
            opener.end,
            opener.ch,
        );
        let span = Span::new(end, pos + used);
        let top = self.out.top();
        let inner = top.children.split_off(index + 1);
        if count == 0 {
            top.children.pop();
        } else {
            top.children[index] = Arc::new(DisplayNode {
                span: Span::new(start, end),
                kind: Display::Text(std::iter::repeat_n(ch, count).collect()),
                children: vec![],
            });
        }
        top.children.push(Arc::new(DisplayNode {
            span,
            kind,
            children: inner,
        }));
        top.span.end = top.span.end.max(span.end);
        self.inline.delims.truncate(j + 1);
        if count == 0 {
            self.inline.delims.pop();
        }
        self.out.dirty = true;
    }

    /// A possible `$$tc(N)` reference, held at most
    /// [`PENDING_REFERENCE_CHARS`] characters.
    fn reference(&mut self, mut buf: Vec<Item>, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        let i = buf.len();
        let fits = match i {
            1 => c == '$',
            2 => c == 't',
            3 => c == 'c',
            4 => c == '(',
            _ => (c.is_ascii_digit() && !(i > 5 && buf[5].c == '0')) || (c == ')' && i > 5),
        };
        if !fits {
            // Not a reference here; one may still start at the next `$`.
            self.emit_char('$', buf[0].at, buf[0].end());
            q.push_front(item);
            self.rerun(q, &buf[1..]);
            return;
        }
        if c == ')' {
            let node = DisplayNode {
                span: Span::new(buf[0].at, item.end()),
                kind: Display::PendingReference,
                children: vec![],
            };
            self.emit_node(node, ')');
            return;
        }
        if buf.len() == PENDING_REFERENCE_CHARS {
            self.cap_flush(&buf, "", buf[0].at);
            q.push_front(item);
            return;
        }
        buf.push(item);
        self.inline.pending = Pending::Reference(buf);
    }

    fn code_char(&mut self, mut code: Code, item: Item, q: &mut VecDeque<Item>) {
        if item.c == '`' {
            code.closing += 1;
        } else if code.closing == code.run {
            self.resolve_code(code);
            q.push_front(item);
            return;
        } else {
            code.closing = 0;
        }
        code.buf.push(item);
        code.bytes += item.c.len_utf8();
        if code.bytes > STREAM_CONTEXT_BYTES {
            let ticks = "`".repeat(code.run);
            self.cap_flush(&code.buf, &ticks, code.at);
            return;
        }
        self.inline.pending = Pending::Code(code);
    }

    /// Draws a closed code span, normalized as CommonMark does.
    fn resolve_code(&mut self, code: Code) {
        self.work.reexamined += code.bytes;
        let content = &code.buf[..code.buf.len() - code.run];
        let mut text: String = content
            .iter()
            .map(|item| if eol(item.c) { ' ' } else { item.c })
            .collect();
        if text.len() >= 2
            && text.starts_with(' ')
            && text.ends_with(' ')
            && text.chars().any(|c| c != ' ')
        {
            text = text[1..text.len() - 1].to_owned();
        }
        let end = code
            .buf
            .last()
            .map_or(code.at + code.run, |item| item.end());
        let node = DisplayNode {
            span: Span::new(code.at, end),
            kind: Display::Code(sanitize_str(&text)),
            children: vec![],
        };
        self.emit_node(node, '`');
    }

    fn link_char(&mut self, mut link: Link, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        link.buf.push(item);
        link.bytes += c.len_utf8();
        if link.bytes > STREAM_CONTEXT_BYTES {
            self.cap_flush(&link.buf, "", link.buf[0].at);
            return;
        }
        if std::mem::take(&mut link.escaped) {
            self.inline.pending = Pending::Link(link);
            return;
        }
        match &mut link.stage {
            Stage::Label { depth } => match c {
                '\\' => link.escaped = true,
                '[' => {
                    *depth += 1;
                    if *depth > LABEL_DEPTH {
                        self.cap_flush(&link.buf, "", link.buf[0].at);
                        return;
                    }
                }
                ']' => {
                    *depth -= 1;
                    if *depth == 0 {
                        link.stage = Stage::Paren;
                    }
                }
                _ => {}
            },
            Stage::Paren => {
                if c != '(' {
                    self.link_failed(link, q);
                    return;
                }
                link.stage = Stage::Destination {
                    nesting: 1,
                    quote: None,
                };
            }
            Stage::Destination { nesting, quote } => match (*quote, c) {
                (_, '\\') => link.escaped = true,
                (Some(open), c) if c == open => *quote = None,
                (Some(_), _) => {}
                (None, '"' | '\'') => *quote = Some(c),
                (None, '(') => *nesting += 1,
                (None, ')') => {
                    *nesting -= 1;
                    if *nesting == 0 {
                        self.link_complete(link, q);
                        return;
                    }
                }
                _ => {}
            },
        }
        self.inline.pending = Pending::Link(link);
    }

    /// Not a link: the brackets are text and what they held is examined
    /// again.
    fn link_failed(&mut self, link: Link, q: &mut VecDeque<Item>) {
        let opener = if link.image { 2 } else { 1 };
        self.emit_items(&link.buf[..opener]);
        self.rerun(q, &link.buf[opener..]);
    }

    /// Classifies a complete `[label](destination)` with the final parser.
    fn link_complete(&mut self, link: Link, q: &mut VecDeque<Item>) {
        let text: String = link.buf.iter().map(|item| item.c).collect();
        self.work.reexamined += text.len();
        let doc = parse(&text, Mode::Reply);
        let found = match doc.nodes.as_slice() {
            [Node {
                kind: Kind::Markdown(Markdown::Paragraph),
                children,
                ..
            }] => match children.as_slice() {
                [node @ Node {
                    kind: Kind::Markdown(Markdown::Link { .. }),
                    ..
                }] if node.span == Span::new(0, text.len()) => Some(node.clone()),
                _ => None,
            },
            _ => None,
        };
        let Some(node) = found else {
            self.link_failed(link, q);
            return;
        };
        // Offsets in the link's text map back to where each character arrived.
        let mut offsets = Vec::with_capacity(text.len() + 1);
        for item in &link.buf {
            offsets.extend((0..item.c.len_utf8()).map(|i| item.at + i));
        }
        offsets.push(link.buf.last().map_or(0, |item| item.end()));
        let display = convert(&node, &text, &|o| offsets[o.min(offsets.len() - 1)]);
        self.emit_node(display, ')');
    }
}

/// Tracks a reply declaration's text to tell, at each line end, whether the
/// next line continues it. Mirrors the final parser's rule one character at
/// a time: an open bracket, tag or string, or a trailing operator, continues.
struct DeclarationScan {
    depth: i32,
    tags: i32,
    header: bool,
    closing: bool,
    header_braces: i32,
    quote: Option<char>,
    escaped: bool,
    operand: bool,
    previous: char,
    /// A `<` waiting for the next character to show whether it opens a tag.
    angle: bool,
}

impl Default for DeclarationScan {
    fn default() -> Self {
        Self {
            depth: 0,
            tags: 0,
            header: false,
            closing: false,
            header_braces: 0,
            quote: None,
            escaped: false,
            operand: true,
            previous: '\0',
            angle: false,
        }
    }
}

impl DeclarationScan {
    fn feed(&mut self, c: char) {
        if std::mem::take(&mut self.angle) {
            if c.is_ascii_uppercase() || c == '/' {
                self.closing = c == '/';
                self.tags += if self.closing { -1 } else { 1 };
                self.header = true;
                self.header_braces = 0;
            } else if self.tags == 0 {
                self.operand = true;
            }
            self.previous = '<';
        }
        if self.escaped {
            self.escaped = false;
            return;
        }
        if c == '\\' {
            self.escaped = true;
            return;
        }
        if let Some(quote) = self.quote {
            if c == quote {
                self.quote = None;
                self.operand = false;
            }
            return;
        }
        if (self.header || self.tags == 0) && (c == '\'' || c == '"') {
            self.quote = Some(c);
            return;
        }
        if c == '<' && !self.header && (self.tags > 0 || self.operand) {
            self.angle = true;
            return;
        }
        if self.header {
            match c {
                '{' => self.header_braces += 1,
                '}' => self.header_braces -= 1,
                '>' if self.header_braces == 0 => {
                    if !self.closing && self.previous == '/' {
                        self.tags -= 1;
                    }
                    self.header = false;
                    self.operand = false;
                }
                _ => {}
            }
        } else if self.tags == 0 {
            match c {
                '(' | '[' | '{' => {
                    self.depth += 1;
                    self.operand = true;
                }
                ')' | ']' | '}' => {
                    self.depth -= 1;
                    self.operand = false;
                }
                '+' | '-' | '*' | '/' | '%' | '?' | ':' | ',' | '=' | '!' | '<' | '>' | '&'
                | '|' => self.operand = true,
                c if !c.is_whitespace() => self.operand = false,
                _ => {}
            }
        }
        if !c.is_whitespace() {
            self.previous = c;
        }
    }
    fn continues(&self) -> bool {
        self.depth > 0 || self.tags > 0 || self.header || self.operand || self.angle
    }
}

/// Scans a tag opening as the final parser bounds a failed one: through the
/// first `>` outside strings and braces.
#[derive(Default)]
struct TagScan {
    quote: Option<char>,
    braces: usize,
    escaped: bool,
    prev: char,
}

impl TagScan {
    /// Returns whether the opening self-closes once its `>` arrives.
    fn feed(&mut self, c: char) -> Option<bool> {
        let prev = std::mem::replace(&mut self.prev, c);
        if self.escaped {
            self.escaped = false;
        } else if c == '\\' {
            self.escaped = true;
        } else if let Some(quote) = self.quote {
            if c == quote {
                self.quote = None;
            }
        } else if c == '\'' || c == '"' {
            self.quote = Some(c);
        } else if c == '{' {
            self.braces += 1;
        } else if c == '}' {
            self.braces = self.braces.saturating_sub(1);
        } else if c == '>' && self.braces == 0 {
            return Some(prev == '/');
        }
        None
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum CapturePhase {
    /// The outer `<Name …>`.
    Opening,
    /// After the outer opening: the rest of its line decides between a block
    /// and an inline tag.
    Suffix {
        self_closing: bool,
    },
    Body,
    /// A block tag's close was seen; its line must end before the tag is
    /// complete.
    CloseLine,
}

/// Where the body scan stands inside one tag-like construct.
enum Inner {
    Plain,
    Angle,
    Open(TagScan),
    Close { named: bool, spaced: bool },
}

/// A tag held whole until complete, then classified by the final parser.
/// The scan only decides where the tag ends; the final parser decides what
/// it is.
struct Capture {
    base: usize,
    text: String,
    /// Leading bytes of `text` that already passed line classification.
    inline_bytes: usize,
    block_candidate: bool,
    phase: CapturePhase,
    opener: TagScan,
    opener_end: usize,
    block: bool,
    depth: usize,
    braces: usize,
    escaped: bool,
    inner: Inner,
    line_start: usize,
    line_blank: bool,
    first_line: bool,
    cr: bool,
}

impl Capture {
    fn new(base: usize, block_candidate: bool) -> Self {
        Self {
            base,
            text: "<".to_owned(),
            inline_bytes: 1,
            block_candidate,
            phase: CapturePhase::Opening,
            opener: TagScan {
                prev: '<',
                ..TagScan::default()
            },
            opener_end: 0,
            block: false,
            depth: 0,
            braces: 0,
            escaped: false,
            inner: Inner::Plain,
            line_start: 0,
            line_blank: false,
            first_line: true,
            cr: false,
        }
    }

    /// Feeds one body character; returns true at the outer tag's close.
    fn body(&mut self, c: char) -> bool {
        if std::mem::take(&mut self.escaped) {
            return false;
        }
        match &mut self.inner {
            Inner::Plain => match c {
                '\\' => self.escaped = true,
                '<' if self.braces == 0 => self.inner = Inner::Angle,
                '{' => self.braces += 1,
                '}' => self.braces = self.braces.saturating_sub(1),
                _ => {}
            },
            Inner::Angle => {
                if c.is_ascii_uppercase() {
                    self.inner = Inner::Open(TagScan::default());
                } else if c == '/' {
                    self.inner = Inner::Close {
                        named: false,
                        spaced: false,
                    };
                } else {
                    self.inner = Inner::Plain;
                    return self.body(c);
                }
            }
            Inner::Open(scan) => {
                if let Some(self_closing) = scan.feed(c) {
                    self.depth += usize::from(!self_closing);
                    self.inner = Inner::Plain;
                }
            }
            Inner::Close { named, spaced } => {
                if c.is_ascii_alphanumeric() && !*spaced && (*named || c.is_ascii_uppercase()) {
                    *named = true;
                } else if c.is_whitespace() && *named {
                    *spaced = true;
                } else if c == '>' && *named {
                    self.inner = Inner::Plain;
                    if self.depth == 0 {
                        return true;
                    }
                    self.depth -= 1;
                } else {
                    self.inner = Inner::Plain;
                }
            }
        }
        false
    }
}

impl Machine {
    fn raw_step(&mut self, item: Item, q: &mut VecDeque<Item>) {
        match self.raw_mode.take() {
            Some(Raw::Braces(braces)) => self.braces_step(braces, item, q),
            Some(Raw::Declaration(declaration)) => self.declaration_step(declaration, item, q),
            Some(Raw::Capture(capture)) => self.capture_step(capture, item, q),
            None => {}
        }
    }

    /// The paragraph's current line ended inside verbatim text; the next
    /// character starts a line.
    fn resume_line_start(&mut self) {
        self.line = Line::start();
        self.eol = None;
    }

    fn braces_step(&mut self, mut b: Braces, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        if let Some(held) = &mut b.held {
            if c == '\n' && b.cr {
                b.cr = false;
                self.literal_char(item);
                self.raw_mode = Some(Raw::Braces(b));
                return;
            }
            if c == ' ' || c == '\t' {
                held.push(item);
                self.raw_mode = Some(Raw::Braces(b));
                return;
            }
            let mut held = std::mem::take(held);
            b.held = None;
            if eol(c) {
                // A blank line ends unclosed braces before it.
                held.push(item);
                self.resume_line_start();
                self.rerun(q, &held);
                return;
            }
            for item in held {
                self.literal_char(item);
            }
        }
        self.literal_char(item);
        b.cr = c == '\r';
        if eol(c) {
            b.escaped = false;
            b.held = Some(vec![]);
        } else if b.escaped {
            b.escaped = false;
        } else if c == '\\' {
            b.escaped = true;
        } else if c == '{' {
            b.depth += 1;
        } else if c == '}' {
            b.depth -= 1;
            if b.depth == 0 {
                return;
            }
        }
        self.raw_mode = Some(Raw::Braces(b));
    }

    fn declaration_step(&mut self, mut d: Declaration, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        if let Some((held, prefix)) = &mut d.held {
            if c == '\n' && d.cr && held.len() == 1 {
                d.cr = false;
                held.push(item);
                self.raw_mode = Some(Raw::Declaration(d));
                return;
            }
            if prefix.is_empty() && (c == ' ' || c == '\t') {
                held.push(item);
                self.raw_mode = Some(Raw::Declaration(d));
                return;
            }
            let continues = if prefix.is_empty() && eol(c) {
                false
            } else if prefix.is_empty() && d.scan.continues() {
                true
            } else {
                prefix.push(c);
                match prefix.as_str() {
                    "&" | "|" => {
                        held.push(item);
                        self.raw_mode = Some(Raw::Declaration(d));
                        return;
                    }
                    "&&" | "||" => true,
                    p => p.starts_with(['?', ':', '.']),
                }
            };
            let mut held = std::mem::take(held);
            d.held = None;
            if !continues {
                // The declaration ended at its line break; the held text is
                // ordinary input again.
                held.push(item);
                self.rerun(q, &held);
                return;
            }
            for item in held {
                self.literal_char(item);
                d.scan.feed(item.c);
            }
        }
        if eol(c) {
            d.held = Some((vec![item], String::new()));
            d.cr = c == '\r';
        } else {
            self.literal_char(item);
            d.scan.feed(c);
        }
        self.raw_mode = Some(Raw::Declaration(d));
    }

    fn capture_step(&mut self, mut cap: Box<Capture>, item: Item, q: &mut VecDeque<Item>) {
        let c = item.c;
        let crlf = c == '\n' && cap.cr;
        let line_end = eol(c) && !crlf;
        let blank = |c: char| c == ' ' || c == '\t';
        match cap.phase {
            CapturePhase::Suffix { self_closing } if line_end => {
                if self_closing {
                    let (end, block) = (cap.text.len(), cap.block_candidate);
                    return self.complete_capture(&cap, end, block, item, q);
                }
                cap.block = cap.block_candidate;
                cap.phase = CapturePhase::Body;
            }
            CapturePhase::Suffix { self_closing } if !blank(c) => {
                if self_closing {
                    let end = cap.opener_end;
                    return self.complete_capture(&cap, end, false, item, q);
                }
                cap.block = false;
                cap.phase = CapturePhase::Body;
            }
            CapturePhase::CloseLine if line_end => {
                let (end, block) = (cap.text.len(), cap.block_candidate);
                return self.complete_capture(&cap, end, block, item, q);
            }
            _ => {}
        }
        if line_end && !cap.first_line && cap.line_blank {
            // A blank line ends a failed opening and an inline tag's body,
            // but not a block tag's body.
            let ends = match cap.phase {
                CapturePhase::Opening => true,
                CapturePhase::Body => !cap.block,
                _ => false,
            };
            if ends {
                let (end, block) = (cap.line_start, cap.block_candidate);
                return self.complete_capture(&cap, end, block, item, q);
            }
            cap.braces = 0;
            cap.inner = Inner::Plain;
        }
        cap.text.push(c);
        if !item.fresh {
            cap.inline_bytes = cap.text.len();
        }
        if line_end || crlf {
            cap.line_start = cap.text.len();
            cap.line_blank = true;
            cap.first_line = false;
        } else if !blank(c) {
            cap.line_blank = false;
        }
        cap.cr = c == '\r';
        match cap.phase {
            CapturePhase::Opening => {
                if let Some(self_closing) = cap.opener.feed(c) {
                    cap.opener_end = cap.text.len();
                    // Only a tag at a block position waits for the rest of
                    // its line to decide between block and inline.
                    if cap.block_candidate {
                        cap.phase = CapturePhase::Suffix { self_closing };
                    } else if self_closing {
                        let end = cap.text.len();
                        return self.complete_capture_now(&cap, end, false, q);
                    } else {
                        cap.phase = CapturePhase::Body;
                    }
                } else if cap.text.len() > TAG_OPENING_BYTES {
                    return self.capture_cap(&cap);
                }
            }
            CapturePhase::Body => {
                if cap.body(c) {
                    if cap.block {
                        cap.phase = CapturePhase::CloseLine;
                    } else {
                        let (end, block) = (cap.text.len(), cap.block_candidate);
                        return self.complete_capture_now(&cap, end, block, q);
                    }
                }
            }
            CapturePhase::Suffix { .. } | CapturePhase::CloseLine => {}
        }
        if cap.text.len() > STREAM_CONTEXT_BYTES {
            return self.capture_cap(&cap);
        }
        self.raw_mode = Some(Raw::Capture(cap));
    }

    /// A held tag reached its cap: draw it as a provisional literal.
    fn capture_cap(&mut self, cap: &Capture) {
        self.work.reexamined += cap.text.len();
        let node = DisplayNode {
            span: Span::new(cap.base, cap.base + cap.text.len()),
            kind: Display::Literal(sanitize_str(&cap.text)),
            children: vec![],
        };
        let prev = cap.text.chars().last().unwrap_or('<');
        self.emit_node(node, prev);
        if eol(prev) {
            self.resume_line_start();
        }
    }

    fn complete_capture(
        &mut self,
        cap: &Capture,
        end: usize,
        block_candidate: bool,
        next: Item,
        q: &mut VecDeque<Item>,
    ) {
        q.push_front(next);
        self.complete_capture_now(cap, end, block_candidate, q);
    }

    /// Classifies the held tag `text[..end]` with the final parser, draws the
    /// result, and queues whatever the tag did not consume.
    fn complete_capture_now(
        &mut self,
        cap: &Capture,
        end: usize,
        block_candidate: bool,
        q: &mut VecDeque<Item>,
    ) {
        let text = &cap.text[..end];
        self.work.reexamined += end;
        let mut engine = Engine::new(text, end, Mode::Reply);
        let (node, stop) = match engine.tag(0, end, 1, block_candidate) {
            Ok((node, stop, _)) if stop > 0 && stop <= end => (node, stop),
            _ => (
                Node {
                    span: Span::new(0, end),
                    kind: Kind::Literal(text.to_owned()),
                    children: vec![],
                },
                end,
            ),
        };
        let display = convert(&node, text, &|o| cap.base + o);
        let prev = text[..stop].chars().last().unwrap_or('>');
        if matches!(node.kind, Kind::Tag { block: true, .. }) {
            // A block tag ends the paragraph it interrupts.
            if matches!(self.leaf, Leaf::Paragraph { .. }) {
                self.close_leaf();
            }
            self.out.push(display);
        } else {
            self.emit_node(display, prev);
        }
        if eol(prev) {
            self.resume_line_start();
        }
        let rest: Vec<Item> = cap.text[stop..]
            .char_indices()
            .map(|(i, c)| Item {
                at: cap.base + stop + i,
                c,
                fresh: stop + i >= cap.inline_bytes,
            })
            .collect();
        self.rerun(q, &rest);
    }

    /// Ends verbatim text left open when its block ends.
    fn finish_raw(&mut self) {
        match self.raw_mode.take() {
            Some(Raw::Capture(cap)) => self.capture_cap(&cap),
            Some(Raw::Declaration(Declaration {
                held: Some((held, _)),
                ..
            })) => {
                for item in held {
                    self.literal_char(item);
                }
            }
            _ => {}
        }
    }
}