asciidoc-parser 0.19.0

Parser for AsciiDoc format
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
use std::{collections::VecDeque, sync::Arc};

use self_cell::self_cell;

use crate::{
    HasSpan, Parser, Span,
    attributes::Attrlist,
    blocks::{
        Block, ContentModel, IsBlock, caption::assign_block_caption, metadata::BlockMetadata,
        parse_utils::parse_blocks_until,
    },
    content::{Content, SubstitutionGroup},
    document::{InterpretedValue, TocConfig, TocMode},
    parser::{
        InlineSubstitutionRenderer, ModificationContext, ReferenceResolver, ReferenceWarning,
        preprocessor::preprocess,
    },
    span::MatchedItem,
    strings::CowStr,
    warnings::{MatchAndWarnings, Warning, WarningType},
};

/// Attributes that an AsciiDoc table cell may modify even when they are set in
/// the parent document.
///
/// An AsciiDoc cell inherits the parent's attributes and cannot modify them,
/// but the AsciiDoc specification carves out a handful of exceptions:
/// `doctype`, `toc`, `notitle` (and its complement, `showtitle`), and
/// `compat-mode`.
const ASCIIDOC_CELL_MODIFIABLE_ATTRIBUTES: &[&str] =
    &["doctype", "toc", "notitle", "showtitle", "compat-mode"];

/// A table is a delimited block that arranges content into a grid of rows and
/// columns.
///
/// A table is introduced by a table delimiter (`|===`, or `!===` for a nested
/// table) and closed by a matching delimiter. By default cells are separated
/// using prefix-separated value (PSV) syntax: the table's cell separator — a
/// vertical bar (`|`) by default — at the start of a line or preceded by
/// whitespace begins a new cell. Cells flow, in document order, into rows whose
/// length is fixed by the number of columns. (The separator defaults to `!`
/// inside a nested table and can be overridden with the `separator` attribute;
/// see below.)
///
/// The number of columns is determined either by the `cols` attribute or,
/// implicitly, by the number of cells found in the first non-empty line after
/// the opening delimiter.
///
/// # Data formats
///
/// In addition to the default PSV format, a table can be populated from
/// delimiter-separated data with the [`format`](Self::data_format) attribute:
/// `csv` (comma-separated values), `tsv` (tab-separated values), or `dsv`
/// (delimited values, colon-separated by default). The `,===` and `:===`
/// shorthand delimiters select the CSV and DSV formats respectively without an
/// explicit `format` attribute. In a data format the separator is placed
/// *between* values (not in front of each cell) and a cell carries no
/// formatting spec; cell formatting is instead applied per column with the
/// `cols` attribute. See [`DataFormat`] for the parsing rules.
///
/// Column specifier style operators (the `a`, `d`, `e`, `h`, `l`, `m`, and `s`
/// operators) are supported, along with proportional width and the horizontal
/// and vertical alignment operators. Per-cell horizontal and vertical alignment
/// operators are supported and override the column's alignment, and a per-cell
/// style operator (in the last position of the cell specifier) is supported and
/// overrides the column's style. The per-cell span (`+`) operator is supported:
/// a cell can span multiple columns (`<n>+`), multiple rows (`.<n>+`), or a
/// block of both (`<n>.<n>+`). The per-cell duplication (`*`) operator is
/// supported: a cell with a duplication factor (`<n>*`) clones its content and
/// properties into `<n>` consecutive cells.
///
/// Table sizing is supported: the [`width`](Self::width) attribute sets a fixed
/// table width, the `autowidth` option ([`is_autowidth`](Self::is_autowidth))
/// sizes the table and its columns to their content, and an individual column
/// can be made [autowidth](TableColumn::is_autowidth) with the `~` width value.
///
/// Table borders are supported: the [`frame`](Self::frame) attribute controls
/// the border around the table and the [`grid`](Self::grid) attribute controls
/// the borders between cells. Each falls back to a document-level default
/// (`table-frame` / `table-grid`) and then to `all`.
///
/// Zebra striping is supported via the [`stripes`](Self::stripes) attribute,
/// which falls back to the `table-stripes` document attribute and then to
/// `none`.
///
/// Nested tables are supported: an [`AsciiDoc`](ColumnStyle::AsciiDoc) cell may
/// contain its own table. The cell separator defaults to the vertical bar (`|`)
/// but switches to the exclamation mark (`!`) inside an AsciiDoc cell, so a
/// nested table is opened with `!===` and separates its cells with `!`. The
/// `separator` attribute overrides the default separator with an explicit
/// character at any level.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TableBlock<'src> {
    columns: Vec<TableColumn>,
    data_format: DataFormat,
    header_row: Option<TableRow<'src>>,
    body_rows: Vec<TableRow<'src>>,
    footer_row: Option<TableRow<'src>>,
    source: Span<'src>,
    title_source: Option<Span<'src>>,
    title: Option<String>,
    caption: Option<String>,
    number: Option<usize>,
    frame: Frame,
    grid: Grid,
    stripes: Stripes,
    anchor: Option<Span<'src>>,
    anchor_reftext: Option<Span<'src>>,
    attrlist: Option<Attrlist<'src>>,
}

impl<'src> TableBlock<'src> {
    /// Returns `true` if `line` is a table delimiter.
    ///
    /// A table delimiter is one of the lead characters `|`, `!`, `,`, or `:`
    /// followed by three or more equals signs (`===`). The lead character also
    /// selects the table's data format and default cell separator:
    ///
    /// * `|===` is the ordinary (PSV) table delimiter.
    /// * `!===` opens a table whose default cell separator is the exclamation
    ///   mark, which lets a nested table be distinguished from the
    ///   `|`-separated table that encloses it.
    /// * `,===` is the shorthand for a CSV table.
    /// * `:===` is the shorthand for a DSV table.
    pub(crate) fn is_table_delimiter(line: &Span<'src>) -> bool {
        let data = line.data();
        // `len() >= 4` plus the leading delimiter character guarantees `rest`
        // holds at least three bytes, so the closure only needs to confirm they
        // are all `=`.
        data.len() >= 4
            && matches!(data.as_bytes().first(), Some(b'|' | b'!' | b',' | b':'))
            && data
                .get(1..)
                .is_some_and(|rest| rest.bytes().all(|b| b == b'='))
    }

    pub(crate) fn parse(
        metadata: &BlockMetadata<'src>,
        parser: &mut Parser,
    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
        let delimiter = metadata.block_start.take_normalized_line();

        if !Self::is_table_delimiter(&delimiter.item) {
            return None;
        }

        let delimiter_text = delimiter.item.data();

        // Find the matching closing delimiter.
        let mut next = delimiter.after;
        let (closing_delimiter, after) = loop {
            if next.is_empty() {
                break (next, next);
            }

            let line = next.take_normalized_line();
            if line.item.data() == delimiter_text {
                break (line.item, line.after);
            }
            next = line.after;
        };

        let inside = delimiter.after.trim_remainder(closing_delimiter);

        // The data format governs how the table body is split into cells. It
        // defaults to PSV, but the `format` attribute selects CSV, TSV, or DSV,
        // and the `,===` / `:===` shorthand delimiters select CSV / DSV. The
        // lead character of the delimiter (`delimiter_text`) is passed so the
        // shorthand can be honored.
        let data_format = resolve_data_format(metadata, delimiter_text);

        // The cell separator partitions each row into cells. In PSV it defaults
        // to the vertical bar (`|`), except inside an AsciiDoc table cell — a
        // nested, standalone document — where it defaults to the exclamation
        // mark (`!`) so a nested table is distinguished from the `|`-separated
        // table that encloses it. Each data format has its own default (CSV =
        // comma, TSV = tab, DSV = colon). The `separator` attribute overrides
        // the default; an empty `separator` falls back to the default, and the
        // two-character sequence `\t` is interpreted as a tab.
        let separator = resolve_separator(metadata, parser, data_format);

        // The `cols` attribute, when present, fixes the number of columns and
        // carries the per-column formatting. When it is absent the column count
        // is implicit (resolved per format below).
        let cols_attr: Vec<TableColumn> = metadata
            .attrlist
            .as_ref()
            .and_then(|a| a.named_attribute("cols"))
            .map(|attr| parse_cols(attr.value()))
            .unwrap_or_default();

        // The `autowidth` option sizes the table to its content; the columns
        // inherit the setting, so every column becomes autowidth regardless of
        // any proportional width set on its specifier.
        let autowidth = metadata
            .attrlist
            .as_ref()
            .is_some_and(|a| a.has_option("autowidth"));

        // The first row is an (implicit) header row when the line directly after
        // the opening delimiter is non-empty and is itself followed by an empty
        // line. The `header` option forces the same interpretation; the
        // `noheader` option suppresses only the implicit detection, so an
        // explicit `header` still wins when both are present.
        let opts_header = metadata
            .attrlist
            .as_ref()
            .is_some_and(|a| a.has_option("header"));
        let opts_noheader = metadata
            .attrlist
            .as_ref()
            .is_some_and(|a| a.has_option("noheader"));

        // The last row is promoted to a footer row when the `footer` option is
        // set. Unlike the header row, a footer cell is processed with its
        // column's style (it is simply the last body row, relabeled).
        let opts_footer = metadata
            .attrlist
            .as_ref()
            .is_some_and(|a| a.has_option("footer"));

        // The blank line must genuinely exist after the first row; the end of the
        // table (an empty remainder) does not count, so a single-row table is not
        // mistaken for an all-header table.
        let line1 = inside.take_line();
        let line1_blank = line1.item.data().trim().is_empty();
        let line2_blank =
            !line1.after.is_empty() && line1.after.take_line().item.data().trim().is_empty();

        // An implicit header additionally requires that the first row be complete
        // on the first line. If the first cell spans multiple lines — for PSV,
        // the first non-blank line after the blank gap continues the cell instead
        // of starting a new one; for CSV/TSV, the first line opens a quoted value
        // that is not closed on that line — there is no implicit header (matching
        // Asciidoctor, which cancels the implicit header in these cases).
        let first_row_complete = match data_format {
            DataFormat::Psv => first_nonblank_line(line1.after)
                .is_none_or(|line| psv_line_starts_cell(line.data(), separator.as_str())),
            DataFormat::Csv | DataFormat::Tsv => !line_has_unclosed_quote(line1.item.data()),
            DataFormat::Dsv => true,
        };

        let has_header =
            opts_header || (!opts_noheader && !line1_blank && line2_blank && first_row_complete);

        // A titled table is given a caption (e.g. "Table 1. ") that a processor
        // prepends to the title, drawn from the `table-caption` attribute (which
        // defaults to "Table"); each such captioned table consumes the next
        // value of a document-wide table counter. An explicit `caption`
        // attribute sets the label verbatim with no number; an explicitly empty
        // `caption` (e.g. `[caption=]`) removes the label entirely. When
        // `table-caption` is unset and no explicit `caption` is given, no caption
        // (and no number) is assigned. See [`assign_block_caption`] for the full,
        // shared rules.
        //
        // Computed before the cell iterator below borrows `parser` immutably, so
        // that the mutable counter update does not conflict with that borrow.
        let caption = assign_block_caption(
            parser,
            "table",
            metadata.attrlist.as_ref(),
            metadata.title.is_some(),
        );
        let number = caption.as_ref().and_then(|caption| caption.number);
        let caption = caption.map(|caption| caption.prefix);

        // The `frame` and `grid` attributes control the table's borders, and the
        // `stripes` attribute controls zebra striping. The borders each default
        // to `all` and stripes defaults to `none`; the default can be changed for
        // the whole document with the `table-frame` / `table-grid` /
        // `table-stripes` attribute, and an explicit attribute on the table
        // overrides both. Each value is resolved here (while `parser` is borrowed
        // only immutably) and stored on the block so the accessors need no further
        // document lookup.
        let frame = resolve_table_attribute::<Frame>(metadata, parser, "frame", "table-frame");
        let grid = resolve_table_attribute::<Grid>(metadata, parser, "grid", "table-grid");
        let stripes =
            resolve_table_attribute::<Stripes>(metadata, parser, "stripes", "table-stripes");

        // Split the body into columns and rows according to the data format.
        // PSV walks a grid that honors cell spans and duplication; the data
        // formats (CSV/TSV/DSV) split on a separator with no per-cell spec and
        // flow the values into fixed-width rows.
        let mut warnings: Vec<Warning<'src>> = vec![];
        let body = TableBody {
            inside,
            separator,
            cols_attr,
            autowidth,
            has_header,
        };
        let (columns, rows) = match data_format {
            DataFormat::Psv => build_psv_table(body, parser, &mut warnings),
            DataFormat::Csv | DataFormat::Tsv | DataFormat::Dsv => {
                build_data_table(body, data_format, parser, &mut warnings)
            }
        };

        let mut rows = rows.into_iter();
        let header_row = if has_header { rows.next() } else { None };
        let mut body_rows: Vec<TableRow<'src>> = rows.collect();

        // The footer row, when requested, is the last row of the table. It is
        // moved out of the body so the caller sees it as a distinct footer. When
        // the table has no rows to spare, no footer is produced.
        let footer_row = if opts_footer { body_rows.pop() } else { None };

        let source = metadata
            .source
            .trim_remainder(closing_delimiter.discard_all())
            .trim_trailing_whitespace();

        if closing_delimiter.is_empty() {
            warnings.push(Warning {
                source: delimiter.item,
                warning: WarningType::UnterminatedDelimitedBlock,
            });
        }

        Some(MatchAndWarnings {
            item: Some(MatchedItem {
                item: Self {
                    columns,
                    data_format,
                    header_row,
                    body_rows,
                    footer_row,
                    source,
                    title_source: metadata.title_source,
                    title: metadata.title.clone(),
                    caption,
                    number,
                    frame,
                    grid,
                    stripes,
                    anchor: metadata.anchor,
                    anchor_reftext: metadata.anchor_reftext,
                    attrlist: metadata.attrlist.clone(),
                },
                after,
            }),
            warnings,
        })
    }

    /// Returns the caption assigned to this table, if any.
    ///
    /// A titled table is captioned with a label that a processor prepends to
    /// the [`title`](IsBlock::title). By default the label combines the
    /// `table-caption` attribute and an automatically incremented number (e.g.
    /// `"Table 1. "`). An explicit `caption` attribute on the table overrides
    /// this with a verbatim label and no number; an explicitly empty `caption`
    /// (e.g. `[caption=]`) removes the label entirely. The caption is absent
    /// when the table has no title, when `table-caption` has been unset and no
    /// explicit `caption` is given, or when an empty `caption` was supplied.
    pub fn caption(&self) -> Option<&str> {
        self.caption.as_deref()
    }

    /// Returns the number assigned to this table, if any.
    ///
    /// A titled table for which the `table-caption` attribute is set is
    /// numbered with an automatically incremented, document-wide table counter
    /// (the same number that appears in its [`caption`](Self::caption), e.g.
    /// the `1` in `"Table 1. "`). The number is absent when the table is
    /// not captioned, or when its caption comes from an explicit
    /// (unnumbered) `caption` attribute.
    pub fn number(&self) -> Option<usize> {
        self.number
    }

    /// Returns the columns of this table.
    pub fn columns(&self) -> &[TableColumn] {
        &self.columns
    }

    /// Returns the [`DataFormat`] used to populate this table.
    ///
    /// The format comes from the `format` attribute on the table (`psv`, `csv`,
    /// `tsv`, or `dsv`) or from a shorthand delimiter (`,===` selects CSV,
    /// `:===` selects DSV). When neither is present the format defaults to
    /// [`DataFormat::Psv`].
    pub fn data_format(&self) -> DataFormat {
        self.data_format
    }

    /// Returns the fixed width of this table, as a percentage of the content
    /// area, when the `width` attribute is set.
    ///
    /// The `width` attribute is an integer percentage from 1 to 100; the
    /// trailing `%` sign is optional (`[width=75%]` and `[width=75]` are
    /// equivalent). A value outside that range, or one that is not an integer,
    /// is ignored and reported as `None`. When the attribute is absent the
    /// table spans the width of the content area and this returns `None`.
    pub fn width(&self) -> Option<usize> {
        let raw = self
            .attrlist
            .as_ref()
            .and_then(|a| a.named_attribute("width"))?
            .value();

        let raw = raw.strip_suffix('%').unwrap_or(raw);
        match raw.parse::<usize>() {
            Ok(width) if (1..=100).contains(&width) => Some(width),
            _ => None,
        }
    }

    /// Returns `true` if this table carries the `autowidth` option.
    ///
    /// An autowidth table is sized to fit its content rather than spanning the
    /// width of the content area, and each of its [columns](TableColumn) is
    /// likewise [autowidth](TableColumn::is_autowidth).
    pub fn is_autowidth(&self) -> bool {
        self.attrlist
            .as_ref()
            .is_some_and(|a| a.has_option("autowidth"))
    }

    /// Returns the [`Frame`] that controls the border drawn around this table.
    ///
    /// The frame comes from the `frame` attribute on the table, which accepts
    /// `all`, `ends`, `sides`, or `none`. When the attribute is absent the
    /// value is taken from the `table-frame` document attribute, and when
    /// that too is absent it defaults to [`Frame::All`].
    pub fn frame(&self) -> Frame {
        self.frame
    }

    /// Returns the [`Grid`] that controls the borders drawn between this
    /// table's cells.
    ///
    /// The grid comes from the `grid` attribute on the table, which accepts
    /// `all`, `rows`, `cols`, or `none`. When the attribute is absent the value
    /// is taken from the `table-grid` document attribute, and when that too is
    /// absent it defaults to [`Grid::All`].
    pub fn grid(&self) -> Grid {
        self.grid
    }

    /// Returns the [`Stripes`] that control which rows of this table are shaded
    /// to create a zebra-striping effect.
    ///
    /// The stripes come from the `stripes` attribute on the table, which
    /// accepts `none`, `even`, `odd`, `all`, or `hover`. When the attribute
    /// is absent the value is taken from the `table-stripes` document
    /// attribute, and when that too is absent it defaults to
    /// [`Stripes::None`].
    ///
    /// As a shorthand, a `stripes-<value>` role on the table (e.g.
    /// `[.stripes-even]`) applies the same CSS class directly without setting
    /// the `stripes` attribute. That shorthand does not affect this value
    /// (which remains [`Stripes::None`]); the role is instead reported
    /// among the table's [roles](crate::attributes::Attrlist::roles).
    pub fn stripes(&self) -> Stripes {
        self.stripes
    }

    /// Returns the header row of this table, if one was declared.
    pub fn header_row(&self) -> Option<&TableRow<'src>> {
        self.header_row.as_ref()
    }

    /// Returns the body rows of this table.
    pub fn body_rows(&self) -> &[TableRow<'src>] {
        &self.body_rows
    }

    /// Returns the footer row of this table, if one was declared.
    pub fn footer_row(&self) -> Option<&TableRow<'src>> {
        self.footer_row.as_ref()
    }

    /// Resolves any deferred cross-references in this table's cells.
    pub(crate) fn resolve_references(
        &mut self,
        resolver: &dyn ReferenceResolver,
        renderer: &dyn InlineSubstitutionRenderer,
        warnings: &mut Vec<ReferenceWarning>,
    ) {
        let rows = self
            .header_row
            .iter_mut()
            .chain(self.body_rows.iter_mut())
            .chain(self.footer_row.iter_mut());

        for row in rows {
            for cell in row.cells.iter_mut() {
                cell.resolve_references(resolver, renderer, warnings);
            }
        }
    }
}

impl<'src> IsBlock<'src> for TableBlock<'src> {
    fn content_model(&self) -> ContentModel {
        ContentModel::Table
    }

    fn raw_context(&self) -> CowStr<'src> {
        "table".into()
    }

    fn title_source(&'src self) -> Option<Span<'src>> {
        self.title_source
    }

    fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    // These forward to the inherent `caption()`/`number()` (the documented
    // public accessors) so that the captioned table is reported correctly
    // through the trait interface too — `dyn IsBlock` / generic `T: IsBlock`
    // consumers resolve to these rather than the inherent methods.
    fn caption(&self) -> Option<&str> {
        self.caption.as_deref()
    }

    fn number(&self) -> Option<usize> {
        self.number
    }

    fn anchor(&'src self) -> Option<Span<'src>> {
        self.anchor
    }

    fn anchor_reftext(&'src self) -> Option<Span<'src>> {
        self.anchor_reftext
    }

    fn attrlist(&'src self) -> Option<&'src Attrlist<'src>> {
        self.attrlist.as_ref()
    }
}

impl<'src> HasSpan<'src> for TableBlock<'src> {
    fn span(&self) -> Span<'src> {
        self.source
    }
}

/// A column in a [`TableBlock`].
///
/// A column carries its proportional width, the horizontal and vertical
/// alignment applied to its cells' content, and the [style](ColumnStyle) used
/// to process and render that content.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TableColumn {
    width: usize,
    autowidth: bool,
    h_align: HorizontalAlignment,
    v_align: VerticalAlignment,
    style: ColumnStyle,
}

impl TableColumn {
    /// Returns the width of this column relative to the other columns in the
    /// table. The default width is `1`.
    ///
    /// This value carries two different meanings depending on the table, and a
    /// caller that resolves columns to final sizes must check which applies:
    ///
    /// * In an ordinary table (no column is [autowidth](Self::is_autowidth)),
    ///   the width is a *proportional* ratio. Each column's share of the table
    ///   is its width divided by the sum of all the column widths, so
    ///   `[cols="1,2,3"]` yields shares of 1/6, 2/6, and 3/6.
    /// * When at least one column in the table is autowidth (its specifier uses
    ///   the special width value `~`), the AsciiDoc specification instead reads
    ///   these widths as literal *percentages* (100-based): in
    ///   `[cols="25,~,~"]` the first column is 25% wide and the `~` columns are
    ///   sized to their content.
    ///
    /// The two cases are distinguished by whether any column in the table is
    /// autowidth, which the caller can test with
    /// `table.columns().iter().any(TableColumn::is_autowidth)`.
    ///
    /// When this column itself is autowidth, this width is not used to size the
    /// column (the column is sized to its content instead). A column made
    /// autowidth by the `~` specifier reports the default width of `1`, but one
    /// that inherits autowidth from the table's `autowidth` option retains
    /// whatever width its specifier set (e.g. `2` for the first column of
    /// `[%autowidth,cols="2,1"]`).
    pub fn width(&self) -> usize {
        self.width
    }

    /// Returns `true` if this column is sized to fit its content rather than to
    /// a proportional width.
    ///
    /// A column is autowidth when its column specifier uses the special width
    /// value `~`, or when the table as a whole carries the `autowidth` option
    /// (in which case every column inherits the setting).
    pub fn is_autowidth(&self) -> bool {
        self.autowidth
    }

    /// Returns the horizontal alignment applied to this column's content.
    ///
    /// The alignment comes from a horizontal alignment operator (`<`, `>`, or
    /// `^`) on the column's specifier and defaults to
    /// [`HorizontalAlignment::Left`].
    pub fn h_align(&self) -> HorizontalAlignment {
        self.h_align
    }

    /// Returns the vertical alignment applied to this column's content.
    ///
    /// The alignment comes from a vertical alignment operator (`.<`, `.>`, or
    /// `.^`) on the column's specifier and defaults to
    /// [`VerticalAlignment::Top`].
    pub fn v_align(&self) -> VerticalAlignment {
        self.v_align
    }

    /// Returns the [style](ColumnStyle) applied to this column's content.
    ///
    /// The style comes from a style operator in the last position of the
    /// column's specifier (`a`, `d`, `e`, `h`, `l`, `m`, or `s`) and defaults
    /// to [`ColumnStyle::Default`].
    pub fn style(&self) -> ColumnStyle {
        self.style
    }
}

impl Default for TableColumn {
    fn default() -> Self {
        Self {
            width: 1,
            autowidth: false,
            h_align: HorizontalAlignment::Left,
            v_align: VerticalAlignment::Top,
            style: ColumnStyle::Default,
        }
    }
}

/// The data format that governs how a [`TableBlock`]'s body is split into
/// cells.
///
/// The format is selected by the `format` attribute (`psv`, `csv`, `tsv`, or
/// `dsv`) or by a shorthand delimiter (`,===` for CSV, `:===` for DSV). The
/// default is [`Psv`](Self::Psv).
///
/// In the PSV format the separator is placed in front of each cell and a cell
/// may carry a formatting spec. In the delimiter-separated formats (CSV, TSV,
/// and DSV) the separator is placed *between* values and a cell carries no
/// spec; cell formatting is applied per column with the `cols` attribute
/// instead. In every delimiter-separated format empty lines are skipped,
/// whitespace surrounding each value is stripped, and a "ragged" table (whose
/// rows do not all have the same number of cells) has its cells flowed into
/// fixed-width rows, dropping any cells left over at the end.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum DataFormat {
    /// Prefix-separated values: the default format. The separator (a vertical
    /// bar, `|`, by default) is placed in front of each cell.
    #[default]
    Psv,

    /// Comma-separated values (the `csv` format). The default separator is a
    /// comma (`,`). Values may be enclosed in double quotes (`"`), within which
    /// the separator and newlines are literal and a double quote is written by
    /// doubling it (`""`); a newline that is not inside a quoted value begins a
    /// new row. Loosely based on RFC 4180.
    Csv,

    /// Tab-separated values (the `tsv` format). Parsed by the same rules as
    /// [`Csv`](Self::Csv), but the default separator is a tab.
    Tsv,

    /// Delimited values (the `dsv` format). The default separator is a colon
    /// (`:`). Unlike CSV and TSV, an enclosing character is not recognized;
    /// instead the separator can be included in a value by escaping it with a
    /// single backslash (`\:`).
    Dsv,
}

/// The style applied to the content of a [column](TableColumn) (and, by
/// extension, to each body cell in that column).
///
/// A style is specified by a style operator in the last position of a column
/// specifier. When no style operator is present, [`Default`](Self::Default) is
/// assigned and the column is processed as paragraph text.
///
/// The style governs both how a cell's content is parsed and how it is
/// rendered: most styles leave the content as inline markup (changing only the
/// surrounding formatting), [`Literal`](Self::Literal) processes the content
/// verbatim, and [`AsciiDoc`](Self::AsciiDoc) parses the content as a nested,
/// standalone AsciiDoc document.
///
/// The verse operator (`v`) recognized by older versions of AsciiDoc has been
/// deprecated and is not modeled here.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ColumnStyle {
    /// Block elements (lists, delimited blocks, and block macros) are
    /// supported; the content is parsed as a nested, standalone AsciiDoc
    /// document (the `a` operator).
    AsciiDoc,

    /// All of the markup permitted in a paragraph (inline formatting and inline
    /// macros) is supported (the `d` operator). This is the default style,
    /// assigned automatically when no style operator is present.
    #[default]
    Default,

    /// Text is italicized (the `e` operator).
    Emphasis,

    /// The header semantics and styles are applied to the text and cell borders
    /// (the `h` operator).
    Header,

    /// Content is treated as if it were inside a literal block (the `l`
    /// operator).
    Literal,

    /// Text is rendered using a monospace font (the `m` operator).
    Monospace,

    /// Text is bold (the `s` operator).
    Strong,
}

/// The horizontal alignment of a column's content.
///
/// Specified by a horizontal alignment operator at the start of a
/// [column specifier](TableColumn): the less-than sign (`<`) for
/// [`Left`](Self::Left), the greater-than sign (`>`) for
/// [`Right`](Self::Right), and the caret (`^`) for [`Center`](Self::Center).
/// The default is [`Left`](Self::Left).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HorizontalAlignment {
    /// Content is aligned to the left side of the column (the `<` operator).
    /// This is the default horizontal alignment.
    Left,

    /// Content is centered horizontally in the column (the `^` operator).
    Center,

    /// Content is aligned to the right side of the column (the `>` operator).
    Right,
}

/// The vertical alignment of a column's content.
///
/// Specified by a vertical alignment operator on a
/// [column specifier](TableColumn), always introduced by a dot (`.`): `.<` for
/// [`Top`](Self::Top), `.>` for [`Bottom`](Self::Bottom), and `.^` for
/// [`Middle`](Self::Middle). The default is [`Top`](Self::Top).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VerticalAlignment {
    /// Content is aligned to the top of the column's cells (the `.<` operator).
    /// This is the default vertical alignment.
    Top,

    /// Content is centered vertically in the column's cells (the `.^`
    /// operator).
    Middle,

    /// Content is aligned to the bottom of the column's cells (the `.>`
    /// operator).
    Bottom,
}

/// The border drawn around a [`TableBlock`].
///
/// The frame is set with the `frame` attribute on the table (or, document-wide,
/// the `table-frame` attribute). The default is [`All`](Self::All).
///
/// An unrecognized value falls back to [`All`](Self::All). (Asciidoctor instead
/// passes an unrecognized value straight through to a CSS class, which the
/// stylesheet ignores; this parser models only the four documented values.)
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Frame {
    /// A border is drawn on every side of the table (the `all` value). This is
    /// the default frame.
    #[default]
    All,

    /// A border is drawn on the top and bottom of the table (the `ends` value).
    ///
    /// The `topbot` value recognized by older versions of AsciiDoc is accepted
    /// as a synonym.
    Ends,

    /// A border is drawn on the left and right sides of the table (the `sides`
    /// value).
    Sides,

    /// No border is drawn around the table (the `none` value).
    None,
}

/// The borders drawn between the cells of a [`TableBlock`].
///
/// The grid is set with the `grid` attribute on the table (or, document-wide,
/// the `table-grid` attribute). The default is [`All`](Self::All).
///
/// An unrecognized value falls back to [`All`](Self::All). (Asciidoctor instead
/// passes an unrecognized value straight through to a CSS class, which the
/// stylesheet ignores; this parser models only the four documented values.)
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Grid {
    /// A border is drawn between all cells (the `all` value). This is the
    /// default grid.
    #[default]
    All,

    /// A border is drawn between the rows of the table (the `rows` value).
    Rows,

    /// A border is drawn between the columns of the table (the `cols` value).
    Cols,

    /// No border is drawn between the cells (the `none` value).
    None,
}

/// The zebra striping applied to the rows of a [`TableBlock`].
///
/// Striping shades the specified rows with a background color to create a zebra
/// effect. It is set with the `stripes` attribute on the table (or,
/// document-wide, the `table-stripes` attribute). The default is
/// [`None`](Self::None).
///
/// Under the covers, a converter applies the CSS class `stripes-<value>` to the
/// table; the actual shading depends on the stylesheet. As a shorthand, the
/// same class can be applied directly with a role (e.g. `[.stripes-even]`)
/// rather than the `stripes` attribute. A role does not set this value (see
/// [`TableBlock::stripes`]).
///
/// An unrecognized value falls back to [`None`](Self::None). (Asciidoctor
/// instead passes an unrecognized value straight through to a CSS class, which
/// the stylesheet ignores; this parser models only the five documented values.)
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Stripes {
    /// No rows are shaded (the `none` value). This is the default.
    #[default]
    None,

    /// Even rows are shaded (the `even` value).
    Even,

    /// Odd rows are shaded (the `odd` value).
    Odd,

    /// All rows are shaded (the `all` value).
    All,

    /// The row under the mouse cursor is shaded (the `hover` value). This has
    /// an effect only in HTML output.
    Hover,
}

/// A table-level attribute value ([`Frame`], [`Grid`], or [`Stripes`]) that can
/// be parsed from an attribute value and has a default.
trait TableAttributeValue: Copy + Default {
    /// Parse the value of the table attribute (or its document-level
    /// `table-<name>` counterpart). An unrecognized value yields the default.
    fn from_attr_value(value: &str) -> Self;
}

impl TableAttributeValue for Frame {
    fn from_attr_value(value: &str) -> Self {
        match value.trim() {
            // `topbot` is the older synonym for `ends`.
            "ends" | "topbot" => Frame::Ends,
            "sides" => Frame::Sides,
            "none" => Frame::None,
            // `all` and any unrecognized value.
            _ => Frame::All,
        }
    }
}

impl TableAttributeValue for Grid {
    fn from_attr_value(value: &str) -> Self {
        match value.trim() {
            "rows" => Grid::Rows,
            "cols" => Grid::Cols,
            "none" => Grid::None,
            // `all` and any unrecognized value.
            _ => Grid::All,
        }
    }
}

impl TableAttributeValue for Stripes {
    fn from_attr_value(value: &str) -> Self {
        match value.trim() {
            "even" => Stripes::Even,
            "odd" => Stripes::Odd,
            "all" => Stripes::All,
            "hover" => Stripes::Hover,
            // `none` and any unrecognized value.
            _ => Stripes::None,
        }
    }
}

/// Resolve a table-level attribute ([`Frame`], [`Grid`], or [`Stripes`]).
///
/// An explicit attribute on the table (`attr_name`) wins; otherwise the
/// document-level default (`doc_attr_name`) is consulted; otherwise the value
/// falls back to the type's default.
fn resolve_table_attribute<B: TableAttributeValue>(
    metadata: &BlockMetadata<'_>,
    parser: &Parser,
    attr_name: &str,
    doc_attr_name: &str,
) -> B {
    if let Some(attr) = metadata
        .attrlist
        .as_ref()
        .and_then(|a| a.named_attribute(attr_name))
    {
        B::from_attr_value(attr.value())
    } else if let InterpretedValue::Value(value) = parser.attribute_value(doc_attr_name) {
        B::from_attr_value(&value)
    } else {
        B::default()
    }
}

/// Resolve the [`DataFormat`] of a table.
///
/// An explicit, recognized `format` attribute (`psv`, `csv`, `tsv`, or `dsv`)
/// always wins. Otherwise the lead character of the delimiter selects the
/// format via its shorthand — `,===` is CSV and `:===` is DSV — and any other
/// delimiter (`|===`, `!===`) is PSV.
fn resolve_data_format(metadata: &BlockMetadata<'_>, delimiter_text: &str) -> DataFormat {
    if let Some(attr) = metadata
        .attrlist
        .as_ref()
        .and_then(|a| a.named_attribute("format"))
    {
        match attr.value().trim() {
            "psv" => return DataFormat::Psv,
            "csv" => return DataFormat::Csv,
            "tsv" => return DataFormat::Tsv,
            "dsv" => return DataFormat::Dsv,
            // An unrecognized format value falls through to the shorthand (or
            // the PSV default).
            _ => {}
        }
    }

    match delimiter_text.as_bytes().first() {
        Some(b',') => DataFormat::Csv,
        Some(b':') => DataFormat::Dsv,
        _ => DataFormat::Psv,
    }
}

/// Resolve the cell separator for a table.
///
/// Each [`DataFormat`] supplies a default separator: PSV uses the vertical bar
/// (`|`), except inside an AsciiDoc table cell — a nested, standalone document
/// — where it defaults to the exclamation mark (`!`) so a nested table is
/// distinguished from the `|`-separated table that encloses it; CSV defaults to
/// a comma (`,`), TSV to a tab, and DSV to a colon (`:`). An explicit
/// `separator` attribute on the table overrides the default; an empty
/// `separator` value (e.g. `[separator=]`) falls back to the default. The
/// two-character sequence `\t` in the attribute value is interpreted as a tab,
/// so a tab-separated table can be written `[format=csv,separator=\t]`.
fn resolve_separator(
    metadata: &BlockMetadata<'_>,
    parser: &Parser,
    data_format: DataFormat,
) -> String {
    let default = match data_format {
        DataFormat::Psv => {
            if parser.nested_document_depth > 0 {
                "!"
            } else {
                "|"
            }
        }
        DataFormat::Csv => ",",
        DataFormat::Tsv => "\t",
        DataFormat::Dsv => ":",
    };

    metadata
        .attrlist
        .as_ref()
        .and_then(|a| a.named_attribute("separator"))
        .map(|attr| attr.value())
        .filter(|value| !value.is_empty())
        // The author writes a literal tab as the escape sequence `\t`.
        .map(|value| value.replace("\\t", "\t"))
        .unwrap_or_else(|| default.to_string())
}

/// Finalize a table's columns once the column count is known.
///
/// When the `cols` attribute supplied columns (`cols_attr` is non-empty) they
/// are used as-is; otherwise `ncols` default columns are created. When the
/// table carries the `autowidth` option, every column is made autowidth
/// regardless of the proportional width set on its specifier.
fn finalize_columns(
    cols_attr: Vec<TableColumn>,
    ncols: usize,
    autowidth: bool,
) -> Vec<TableColumn> {
    let mut columns = if cols_attr.is_empty() {
        (0..ncols).map(|_| TableColumn::default()).collect()
    } else {
        cols_attr
    };

    if autowidth {
        for column in columns.iter_mut() {
            column.autowidth = true;
        }
    }

    columns
}

/// The inputs shared by the PSV and data-format table-body builders.
struct TableBody<'src> {
    /// The region between the opening and closing delimiters.
    inside: Span<'src>,

    /// The resolved cell separator.
    separator: String,

    /// Columns parsed from the `cols` attribute (empty when the attribute is
    /// absent, in which case the column count is implicit).
    cols_attr: Vec<TableColumn>,

    /// Whether the table carries the `autowidth` option.
    autowidth: bool,

    /// Whether the first row is a header row.
    has_header: bool,
}

/// Build the columns and rows of a PSV (prefix-separated values) table.
///
/// The column count comes from the `cols` attribute (`cols_attr`) or, when that
/// is absent, from the number of column slots in the first non-empty line.
/// Cells are then scanned in document order and partitioned into rows by
/// walking the grid: a cell's span (colspan/rowspan) governs how many column
/// slots it occupies, so a column-spanning cell fills its row with fewer cells
/// and a row-spanning cell carries its columns down into the rows below.
///
/// This mirrors Asciidoctor's grid walk. `active_rowspans[k]` records the
/// number of column slots that cells from earlier rows occupy in the row `k`
/// steps ahead of the one being filled; a row closes once its own cells'
/// colspans plus the slots carried into it (`active_rowspans[0]`) reach
/// `ncols`. A cell whose span pushes the row *past* `ncols` overruns the grid:
/// the whole overrunning row is dropped (with a warning), again matching
/// Asciidoctor. A row whose columns are entirely pre-filled by carried slots
/// has no cells of its own to close it, so the next cell overruns and is
/// dropped together with that pre-filled row. A duplicated cell (`<n>*`) is
/// expanded into `<n>` independent cells — each carrying the original's
/// content, alignment, and style — before the grid walk, so each clone occupies
/// its own column slot exactly like an ordinary cell. A duplication factor of
/// zero drops the cell entirely.
fn build_psv_table<'src>(
    body: TableBody<'src>,
    parser: &mut Parser,
    warnings: &mut Vec<Warning<'src>>,
) -> (Vec<TableColumn>, Vec<TableRow<'src>>) {
    let TableBody {
        inside,
        separator,
        cols_attr,
        autowidth,
        has_header,
    } = body;

    let separator = separator.as_str();

    // When the column count is implicit, it is the number of column slots in the
    // first non-empty line: a cell that spans columns (`<n>+`) counts as `<n>`
    // slots, not one, and a cell duplicated `<n>` times (`<n>*`) counts as `<n>`
    // single-column slots (one per clone).
    let first_line_cells: usize =
        scan_cells(inside.discard_empty_lines().take_line().item, separator)
            .0
            .iter()
            .map(|c| c.spec.colspan.max(1) * c.spec.repeat.min(MAX_DUPLICATION_FACTOR))
            .sum();

    let ncols = if cols_attr.is_empty() {
        first_line_cells
    } else {
        cols_attr.len()
    };

    let columns = finalize_columns(cols_attr, ncols, autowidth);

    let (raw_cells, recovered_first_cell) = scan_cells(inside, separator);
    if let Some(source) = recovered_first_cell {
        warnings.push(Warning {
            source,
            warning: WarningType::TableMissingLeadingSeparator,
        });
    }

    let raw_cells = expand_duplicates(raw_cells);

    // A table can never have more rows than it has cells, so a row span is
    // clamped to the cell count for the `active_rowspans` bookkeeping below: a
    // larger span carries into rows that can't exist and so has no additional
    // layout effect. The clamp also bounds the `active_rowspans` allocation, so a
    // hostile specifier such as `.1000000000+` can't trigger a multi-gigabyte
    // allocation. (The cell's reported [`rowspan`] keeps the literal parsed
    // value, matching Asciidoctor.)
    //
    // [`rowspan`]: TableCell::rowspan
    let max_rowspan = raw_cells.len().saturating_add(1);

    let mut raw_rows: Vec<Vec<RawCell<'src>>> = vec![];

    if ncols > 0 {
        // A queue: each completed row consumes the slots carried into it from the
        // front (`pop_front`), while a multi-row cell reserves slots in the rows it
        // extends into via the back. `VecDeque` keeps both ends O(1); a `Vec` would
        // pay an O(n) shift on every `remove(0)`.
        let mut active_rowspans: VecDeque<usize> = VecDeque::from([0]);
        let mut column_visits = 0usize;
        let mut current_row: Vec<RawCell<'src>> = vec![];

        for raw in raw_cells {
            let colspan = raw.spec.colspan.max(1);
            let rowspan = raw.spec.rowspan.max(1).min(max_rowspan);

            // A cell that spans more than one row reserves `colspan` slots in
            // each of the rows it extends into (but not its own row).
            if rowspan > 1 {
                if active_rowspans.len() < rowspan {
                    active_rowspans.resize(rowspan, 0);
                }
                for slot in active_rowspans.iter_mut().take(rowspan).skip(1) {
                    *slot += colspan;
                }
            }

            column_visits += colspan;
            let cell_source = raw.content;
            current_row.push(raw);

            // The slots carried into the current row are `active_rowspans[0]`; the
            // deque is never empty here, so the fallback is unreachable.
            let carried = active_rowspans.front().copied().unwrap_or(0);
            let effective = column_visits + carried;
            if effective >= ncols {
                if effective == ncols {
                    raw_rows.push(std::mem::take(&mut current_row));
                } else {
                    // Overrun: this cell's span pushes the row past `ncols`.
                    // Discard the whole row so the remaining cells stay aligned to
                    // the grid.
                    current_row.clear();
                    warnings.push(Warning {
                        source: cell_source,
                        warning: WarningType::TableCellExceedsColumnCount,
                    });
                }
                column_visits = 0;
                active_rowspans.pop_front();
                if active_rowspans.is_empty() {
                    active_rowspans.push_back(0);
                }
            }
        }

        // If the table ends mid-row, the cells accumulated since the last
        // complete row never filled `ncols`. Matching Asciidoctor's
        // `close_table`, that incomplete row is dropped and an error is logged
        // against its last cell.
        if let Some(last) = current_row.last() {
            warnings.push(Warning {
                source: last.content,
                warning: WarningType::TableDroppingIncompleteRowAtEndOfTable,
            });
        }
    }

    // Each cell is processed according to the style of the column it falls in. A
    // cell's column is its ordinal position within its row (matching Asciidoctor,
    // which assigns the column by cell count, not grid slot). The header row
    // (when present) is the first row and is always processed as plain header
    // content, regardless of the column styles, so that a style operator doesn't
    // affect the header row.
    let mut rows: Vec<TableRow<'src>> = Vec::with_capacity(raw_rows.len());
    for (row_idx, raw_row) in raw_rows.into_iter().enumerate() {
        let is_header = has_header && row_idx == 0;
        let mut cells = Vec::with_capacity(raw_row.len());
        for (col_idx, raw) in raw_row.into_iter().enumerate() {
            let column = columns.get(col_idx).cloned().unwrap_or_default();
            cells.push(TableCell::parse(
                raw, &column, is_header, separator, parser, warnings,
            ));
        }
        rows.push(TableRow { cells });
    }

    (columns, rows)
}

/// Build the columns and rows of a delimiter-separated table (CSV, TSV, or
/// DSV).
///
/// The body is split into a flat list of [fields](DataField) by the format's
/// parser, then flowed into fixed-width rows. The column count comes from the
/// `cols` attribute (`cols_attr`) or, when that is absent, from the number of
/// fields in the first row. Because a data cell carries no span, the fields are
/// simply chunked `ncols` at a time; any fields left over after the last
/// complete row are dropped ("extra cells at the end of the last row get
/// dropped"). The first row is the header when `has_header` is set.
fn build_data_table<'src>(
    body: TableBody<'src>,
    data_format: DataFormat,
    parser: &mut Parser,
    warnings: &mut Vec<Warning<'src>>,
) -> (Vec<TableColumn>, Vec<TableRow<'src>>) {
    let TableBody {
        inside,
        separator,
        cols_attr,
        autowidth,
        has_header,
    } = body;

    let separator = separator.as_str();

    // DSV is parsed by its own, simpler rules; CSV and TSV share their rules and
    // differ only in the default separator (resolved by the caller). PSV never
    // reaches this builder.
    let (fields, first_row_len) = if data_format == DataFormat::Dsv {
        parse_dsv_fields(inside, separator)
    } else {
        parse_csv_fields(inside, separator, warnings)
    };

    let ncols = if cols_attr.is_empty() {
        first_row_len
    } else {
        cols_attr.len()
    };

    let columns = finalize_columns(cols_attr, ncols, autowidth);

    // Integer division drops any partial trailing row; `checked_div` yields zero
    // rows when there are no columns.
    let nrows = fields.len().checked_div(ncols).unwrap_or(0);
    let mut rows: Vec<TableRow<'src>> = Vec::with_capacity(nrows);
    let mut fields = fields.into_iter();
    for row_idx in 0..nrows {
        let is_header = has_header && row_idx == 0;
        let mut cells = Vec::with_capacity(ncols);
        for col_idx in 0..ncols {
            // `nrows * ncols <= fields.len()`, so the iterator always yields.
            let Some(field) = fields.next() else { break };
            let column = columns.get(col_idx).cloned().unwrap_or_default();
            cells.push(TableCell::parse_data(
                field, &column, is_header, parser, warnings,
            ));
        }
        rows.push(TableRow { cells });
    }

    (columns, rows)
}

/// A single field of a delimiter-separated (CSV, TSV, or DSV) table, as located
/// by [`parse_csv_fields`] or [`parse_dsv_fields`].
///
/// `content` is the field's value span with surrounding whitespace already
/// stripped. `replacement` holds the value after quote or escape processing
/// when it differs from `content` (a CSV value with a doubled-quote escape, or
/// a DSV value with a backslash-escaped separator); it is `None` when the span
/// is the verbatim value.
struct DataField<'src> {
    content: Span<'src>,
    replacement: Option<String>,
}

/// Parse a CSV/TSV region into its [fields](DataField), returning them in
/// document order together with the number of fields in the first row.
///
/// The rules, loosely based on RFC 4180: empty lines are skipped; whitespace
/// surrounding each value is stripped; a value may be enclosed in double
/// quotes, within which the separator and newlines are literal and a double
/// quote is written by doubling it (`""`). A newline that is not inside a
/// quoted value ends the row. The fields are returned flat; the caller flows
/// them into rows.
///
/// This mirrors Asciidoctor's `Table::ParserContext`: a separator or newline is
/// a cell boundary only when the text accumulated since the previous boundary
/// has no [unclosed quote](has_unclosed_quotes); otherwise it is part of the
/// value. As a result a value whose opening quote is never properly closed (or
/// that has trailing characters after its closing quote) keeps its quotes and
/// absorbs the following separators, rather than being treated as enclosed.
fn parse_csv_fields<'src>(
    region: Span<'src>,
    separator: &str,
    warnings: &mut Vec<Warning<'src>>,
) -> (Vec<DataField<'src>>, usize) {
    let data = region.data();
    let n = data.len();
    let sep_len = separator.len().max(1);
    let at = |k: usize| data.as_bytes().get(k).copied();
    let starts_with_sep = |pos: usize| data.get(pos..).is_some_and(|s| s.starts_with(separator));

    let mut fields: Vec<DataField<'src>> = vec![];
    let mut first_row_len = 0usize;
    let mut first_row_done = false;
    let mut fields_in_row = 0usize;

    // The raw text of the cell currently being accumulated runs from `cell_start`
    // to the next boundary.
    let mut cell_start = 0usize;
    let mut i = 0usize;

    while i <= n {
        let at_eof = i == n;
        let at_sep = !at_eof && starts_with_sep(i);
        let at_nl = !at_eof && at(i) == Some(b'\n');

        if !(at_eof || at_sep || at_nl) {
            i += 1;
            continue;
        }

        let raw = data.get(cell_start..i).unwrap_or_default();

        // A separator or newline that falls inside an unclosed quoted value is
        // part of the value, not a boundary; absorb it and keep scanning.
        if !at_eof && has_unclosed_quotes(raw) {
            i += if at_sep { sep_len } else { 1 };
            continue;
        }

        // A wholly blank physical line (or trailing blank text at the end of the
        // region) between rows is skipped rather than emitted as an empty cell. A
        // blank cell that follows a separator on a populated line is kept.
        let blank_skip = (at_nl || at_eof) && fields_in_row == 0 && raw.trim().is_empty();
        if !blank_skip {
            fields.push(make_csv_field(region, cell_start, i, warnings));
            fields_in_row += 1;
            if !first_row_done {
                first_row_len = fields_in_row;
            }
        }

        if at_eof {
            break;
        }

        if at_nl {
            // The newline ends the row. The first populated row fixes the implicit
            // column count.
            if fields_in_row > 0 {
                first_row_done = true;
            }
            fields_in_row = 0;
            cell_start = i + 1;
            i += 1;
        } else {
            cell_start = i + sep_len;
            i += sep_len;
        }
    }

    (fields, first_row_len)
}

/// Build a CSV/TSV [field](DataField) from the byte range `start..end`,
/// applying Asciidoctor's `close_cell` value processing.
///
/// The value is stripped of surrounding whitespace; then, if it is enclosed in
/// double quotes, the quotes are removed and the inner value is stripped again,
/// so the field's [content](DataField::content) span points at the actual value
/// (this matters for an AsciiDoc cell, which parses that span). Finally any run
/// of consecutive double quotes is collapsed to one (so an escaped `""` becomes
/// a single `"`). A value that is not enclosed (no leading quote, or trailing
/// characters after the closing quote) keeps its quotes and is only collapsed.
///
/// A lone double quote is an unclosed quoted value: it logs an error and the
/// cell is set to empty (matching Asciidoctor).
fn make_csv_field<'src>(
    region: Span<'src>,
    start: usize,
    end: usize,
    warnings: &mut Vec<Warning<'src>>,
) -> DataField<'src> {
    let trimmed = trim_surrounding_whitespace(region.slice(start..end));
    let data = trimmed.data();

    let content = if data == "\"" {
        warnings.push(Warning {
            source: trimmed,
            warning: WarningType::TableCsvDataHasUnclosedQuote,
        });
        trimmed.slice(0..0)
    } else if data.len() >= 2 && data.starts_with('"') && data.ends_with('"') {
        trim_surrounding_whitespace(trimmed.slice(1..data.len() - 1))
    } else {
        trimmed
    };

    let value = squeeze_quotes(content.data());
    let replacement = (value != content.data()).then_some(value);

    DataField {
        content,
        replacement,
    }
}

/// Collapse every run of consecutive double quotes to a single double quote,
/// matching Ruby's `String#squeeze('"')`.
///
/// Note: the `continue` intentionally leaves `prev_quote` set, so a run of
/// *N ≥ 2* consecutive `"` collapses to a single `"` (e.g. `""""` -> `"`), not
/// to pairs. This deliberately matches Asciidoctor rather than strict RFC 4180,
/// under which only `""` is a double-quote escape — don't "fix" it to a
/// two-character collapse without also changing Asciidoctor.
fn squeeze_quotes(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut prev_quote = false;
    for c in text.chars() {
        if c == '"' {
            if prev_quote {
                continue;
            }
            prev_quote = true;
        } else {
            prev_quote = false;
        }
        out.push(c);
    }
    out
}

/// Determine whether `buffer` (the cell text accumulated so far) holds an
/// unclosed double quote, a direct port of Asciidoctor's
/// `Table::ParserContext#buffer_has_unclosed_quotes?`.
///
/// Only a value that begins with a double quote can be "quoted"; for any other
/// value embedded quotes are literal and this returns `false`. A leading quote
/// is unclosed until a matching trailing quote appears (accounting for escaped
/// `""` pairs).
///
/// Note: the escaped-pair collapse (`replace("\"\"", "")`) runs before the
/// start/end check, so `"""` collapses to a single `"` and is reported
/// *closed*. Strict RFC 4180 would read `"""` as an unclosed field (open quote
/// plus escaped `""` + missing close); this matches Asciidoctor's
/// `buffer_has_unclosed_quotes?` instead, so the divergence is intentional.
fn has_unclosed_quotes(buffer: &str) -> bool {
    let record = buffer.trim();

    if record == "\"" {
        return true;
    }

    if !record.starts_with('"') {
        return false;
    }

    let trailing_quote = record.ends_with('"');
    if (trailing_quote && record.ends_with("\"\"")) || record.starts_with("\"\"") {
        let collapsed = record.replace("\"\"", "");
        collapsed.starts_with('"') && !collapsed.ends_with('"')
    } else {
        !trailing_quote
    }
}

/// Parse a DSV region into its [fields](DataField), returning them in document
/// order together with the number of fields in the first row.
///
/// Each non-empty line is a row. Whitespace surrounding each value is stripped,
/// and the separator can be included in a value by escaping it with a single
/// backslash (`\:`). An enclosing character is not recognized.
fn parse_dsv_fields<'src>(region: Span<'src>, separator: &str) -> (Vec<DataField<'src>>, usize) {
    let data = region.data();
    let n = data.len();
    let sep_len = separator.len().max(1);
    let escaped = format!("\\{separator}");
    let at = |k: usize| data.as_bytes().get(k).copied();

    let mut fields: Vec<DataField<'src>> = vec![];
    let mut first_row_len = 0usize;
    let mut row_count = 0usize;
    let mut i = 0usize;

    while i < n {
        let mut line_end = i;
        while line_end < n && at(line_end) != Some(b'\n') {
            line_end += 1;
        }

        if data.get(i..line_end).unwrap_or("").trim().is_empty() {
            i = if line_end < n { line_end + 1 } else { line_end };
            continue;
        }

        let in_line = |pos: usize| {
            data.get(pos..line_end)
                .is_some_and(|s| s.starts_with(separator))
        };

        let mut fields_in_row = 0usize;
        let mut field_start = i;
        let mut p = i;

        while p < line_end {
            // A backslash that escapes the separator (`\:`) is not a boundary;
            // skip past both so the separator stays in the value.
            if at(p) == Some(b'\\')
                && data
                    .get(p + 1..line_end)
                    .is_some_and(|s| s.starts_with(separator))
            {
                p += 1 + sep_len;
                continue;
            }

            if in_line(p) {
                fields.push(make_dsv_field(region, field_start, p, &escaped, separator));
                fields_in_row += 1;
                p += sep_len;
                field_start = p;
                continue;
            }

            p += 1;
        }

        // The final field of the line runs to the line end.
        fields.push(make_dsv_field(
            region,
            field_start,
            line_end,
            &escaped,
            separator,
        ));
        fields_in_row += 1;

        if row_count == 0 {
            first_row_len = fields_in_row;
        }
        row_count += 1;

        i = if line_end < n { line_end + 1 } else { line_end };
    }

    (fields, first_row_len)
}

/// Build a DSV [field](DataField) from the byte range `start..end`, unescaping
/// any backslash-escaped separators (`escaped`, e.g. `\:`) into the bare
/// separator.
fn make_dsv_field<'src>(
    region: Span<'src>,
    start: usize,
    end: usize,
    escaped: &str,
    separator: &str,
) -> DataField<'src> {
    let trimmed = trim_surrounding_whitespace(region.slice(start..end));
    let replacement = if trimmed.data().contains(escaped) {
        Some(trimmed.data().replace(escaped, separator))
    } else {
        None
    };

    DataField {
        content: trimmed,
        replacement,
    }
}

/// Process a cell's content according to its [style](ColumnStyle), shared by
/// the PSV and data-format cell builders.
///
/// `trimmed` is the cell's content span with surrounding whitespace already
/// removed. `replacement` is the pre-filtered value (an escaped separator
/// unescaped, or a CSV/DSV value after quote/escape processing) when it differs
/// from `trimmed`; it is ignored for the [`AsciiDoc`](ColumnStyle::AsciiDoc)
/// style, which parses `trimmed` verbatim as a nested document. Every other
/// style produces inline [`Simple`](TableCellContent::Simple) content with the
/// verbatim substitution group for [`Literal`](ColumnStyle::Literal) and the
/// normal group otherwise.
fn process_content<'src>(
    trimmed: Span<'src>,
    replacement: Option<String>,
    style: ColumnStyle,
    parser: &mut Parser,
    warnings: &mut Vec<Warning<'src>>,
) -> TableCellContent<'src> {
    if style == ColumnStyle::AsciiDoc {
        // The AsciiDoc style effectively creates a nested, standalone AsciiDoc
        // document in the cell. It inherits the parent document's attributes, but
        // any attribute it defines is scoped to the cell and must not leak back
        // into the parent. Snapshot the attribute set before parsing and restore
        // it afterward to enforce that boundary (matching Asciidoctor, where a
        // `:foo:` set inside a cell is not visible after the table).
        let saved_attributes = parser.attribute_values.clone();

        // An attribute that is set in the parent document cannot be modified
        // inside the cell. Lock every inherited attribute that currently holds a
        // value for the duration of the cell (other than the handful of
        // exceptions the spec carves out), so a body assignment to one of them is
        // ignored. An attribute that is unset in the parent is not locked: the
        // cell may assign it (matching Asciidoctor, which here diverges from the
        // spec's "set or explicitly unset" wording). The lock set is saved and
        // restored so it applies only within the cell and nests correctly.
        // An attribute set in the parent is locked, as is one hard set or unset
        // through the API (its modification context is `ApiOnly`) even though it
        // is unset — matching Asciidoctor, where an API-controlled attribute can
        // never be overridden in a cell. An attribute merely unset in the parent
        // document is not locked, so the cell may assign it.
        let saved_locks = parser.locked_attribute_names.clone();
        for (name, value) in saved_attributes.iter() {
            let api_locked = value.modification_context == ModificationContext::ApiOnly;
            if (!matches!(value.value, InterpretedValue::Unset) || api_locked)
                && !ASCIIDOC_CELL_MODIFIABLE_ATTRIBUTES.contains(&name.as_str())
            {
                parser.locked_attribute_names.insert(name.clone());
            }
        }

        // The modifiable attributes may always be changed inside a cell, even
        // when the parent or the API set them with a restrictive modification
        // context. Relax their context for the duration of the cell so a body
        // assignment is honored; the snapshot restore reverts it afterward.
        for name in ASCIIDOC_CELL_MODIFIABLE_ATTRIBUTES {
            if let Some(attr) = Arc::make_mut(&mut parser.attribute_values).get_mut(*name) {
                attr.modification_context = ModificationContext::Anywhere;
            }
        }

        // A cell does not inherit the parent's doctype; it resets to the default
        // (`article`). The cell body may still set its own doctype, and the
        // derived `backend-html5-doctype-*` attribute is refreshed to match.
        parser.force_doctype("article");

        // Likewise, a cell does not inherit the parent's `toc` setting: a nested
        // document starts without a table of contents and may enable its own.
        // Reset the value to unset; the relax loop above already made `toc`
        // modifiable inside the cell, so a cell-body `:toc:` is still honored.
        if let Some(toc) = Arc::make_mut(&mut parser.attribute_values).get_mut("toc") {
            toc.value = InterpretedValue::Unset;
        }

        // A cell whose content holds a preprocessor directive (an `include::`)
        // is parsed from an owned, expanded source the cell carries; every other
        // cell is parsed in place from the parent document's source, which keeps
        // its spans (and line numbers) and avoids a copy.
        let cell = if content_has_directive(trimmed.data()) {
            // The preprocessor may report warnings (e.g. an unresolved include
            // target) located by offset into the expanded source. As with the
            // owned parse warnings below, that source cannot escape this cell, so
            // these warnings are dropped on this rare path.
            let (expanded, _source_map, _preprocessor_warnings) =
                preprocess(trimmed.data(), parser);
            let owned = OwnedCell::new(expanded, |source| {
                // Warnings from the owned parse borrow the owned source and so
                // cannot escape it; the include path is rare and currently
                // warning-free, so they are dropped here. The `debug_assert`
                // turns any future warning added to this path into a loud test
                // failure rather than a silent loss.
                let mut owned_warnings: Vec<Warning<'_>> = vec![];

                // Substitution warnings (e.g. `attribute-missing=warn`) recorded
                // while parsing this owned source carry offsets into it, not the
                // primary document source, so they too must be discarded.
                let substitution_warnings_mark = parser.substitution_warnings_len();

                let (title, inline, toc, blocks) =
                    parse_asciidoc_cell_body(Span::new(source), parser, &mut owned_warnings);

                parser.truncate_substitution_warnings(substitution_warnings_mark);

                debug_assert!(
                    owned_warnings.is_empty(),
                    "warnings from an include-expanded AsciiDoc cell are dropped; \
                     propagate them before adding any to this path"
                );

                OwnedCellInner {
                    title,
                    inline,
                    toc,
                    blocks,
                }
            });
            AsciiDocCell::Owned(Arc::new(owned))
        } else {
            let (title, inline, toc, blocks) = parse_asciidoc_cell_body(trimmed, parser, warnings);
            AsciiDocCell::Borrowed(BorrowedCell {
                title,
                inline,
                toc,
                blocks,
            })
        };

        parser.locked_attribute_names = saved_locks;
        parser.attribute_values = saved_attributes;
        TableCellContent::AsciiDoc(cell)
    } else {
        let mut content = match replacement {
            Some(replacement) => Content::from_filtered(trimmed, replacement),
            None => Content::from(trimmed),
        };

        let substitutions = if style == ColumnStyle::Literal {
            SubstitutionGroup::Verbatim
        } else {
            SubstitutionGroup::Normal
        };
        substitutions.apply(&mut content, parser, None);

        TableCellContent::Simple(content)
    }
}

/// Parses the body of an AsciiDoc table cell — a nested, standalone AsciiDoc
/// document — returning its (shown) title, whether its doctype is `inline`, and
/// its blocks.
///
/// A leading level-0 title line (`= Title`) is the nested document's title
/// rather than a section, so it is split off and rendered here (a level-0
/// heading is otherwise rejected in block parsing). The render-time decisions
/// (`inline`, and whether the title is shown) depend on the cell's now-mutated
/// attribute state, so they are resolved before the caller restores the
/// parent's attribute snapshot.
fn parse_asciidoc_cell_body<'src>(
    content: Span<'src>,
    parser: &mut Parser,
    warnings: &mut Vec<Warning<'src>>,
) -> (Option<String>, bool, TocConfig, Vec<Block<'src>>) {
    let first_line = content.take_line();
    let (title_source, body) = if first_line.item.data().starts_with("= ") {
        (
            Some(first_line.item.discard(2).discard_whitespace()),
            first_line.after,
        )
    } else {
        (None, content)
    };

    // A nested document keeps its own footnote registry: footnotes defined
    // inside this cell must not be shared with (or numbered into the list of)
    // the enclosing document. We swap in a fresh, empty footnote list for the
    // duration of the cell parse and restore the parent's afterward, discarding
    // the cell's footnotes (see issue #544). The `footnote-number` counter is a
    // document-wide attribute and is deliberately *not* reset, so footnote
    // numbering continues across the cell as Asciidoctor does.
    let saved_footnotes = parser.take_footnotes();

    // Mark that we are inside an AsciiDoc cell (a nested document) for the
    // duration of the parse, so a table found within defaults its cell separator
    // to `!` rather than `|` (matching Asciidoctor's `Document#nested?`).
    parser.nested_document_depth += 1;
    let mut maw = parse_blocks_until(body, |_| false, parser);
    parser.nested_document_depth -= 1;
    warnings.append(&mut maw.warnings);

    parser.restore_footnotes(saved_footnotes);

    let inline = matches!(
        parser.attribute_value("doctype"),
        InterpretedValue::Value(ref v) if v == "inline"
    );

    let title = if parser.resolve_show_title(true) {
        title_source.map(|span| {
            let mut content = Content::from(span);
            SubstitutionGroup::Header.apply(&mut content, parser, None);
            content.rendered().to_string()
        })
    } else {
        None
    };

    // The cell is its own standalone document, so its table-of-contents
    // configuration comes from the cell's own `toc` family of attributes (which
    // it does not inherit from the parent). Resolve it here, before the caller
    // restores the parent's attribute snapshot.
    let toc = TocConfig::from_parser(parser);

    (title, inline, toc, maw.item.item)
}

/// Returns `true` when the cell content holds an `include::` preprocessor
/// directive at the start of a line, which must be expanded before the cell is
/// parsed.
fn content_has_directive(content: &str) -> bool {
    content.starts_with("include::") || content.contains("\ninclude::")
}

/// A row of cells in a [`TableBlock`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TableRow<'src> {
    cells: Vec<TableCell<'src>>,
}

impl<'src> TableRow<'src> {
    /// Returns the cells in this row.
    pub fn cells(&self) -> &[TableCell<'src>] {
        &self.cells
    }
}

/// A single cell in a [`TableBlock`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TableCell<'src> {
    h_align: HorizontalAlignment,
    v_align: VerticalAlignment,
    style: ColumnStyle,
    colspan: usize,
    rowspan: usize,
    content: TableCellContent<'src>,
    source: Span<'src>,
}

impl<'src> TableCell<'src> {
    /// Build a cell from the raw (untrimmed) span of its content, processing it
    /// according to the [style](ColumnStyle) of the `column` the cell belongs
    /// to.
    ///
    /// The cell's horizontal and vertical alignment come from the alignment
    /// operators on its [specifier](RawCell::spec) when present; otherwise they
    /// are inherited from the column. Likewise, a style operator on the cell's
    /// specifier overrides the column's [style](ColumnStyle); with no cell
    /// style operator, the cell is processed with the column's style. A
    /// header cell (`is_header`) is always processed as plain header
    /// content, regardless of any style operator on the column or the cell.
    ///
    /// Leading and trailing whitespace is always stripped. For every style but
    /// [`AsciiDoc`](ColumnStyle::AsciiDoc) the cell holds inline
    /// [`Content`](TableCellContent::Simple): escaped cell separators (the
    /// table's `separator` character preceded by a backslash, e.g. `\|`) are
    /// unescaped and substitutions are applied — the verbatim group for
    /// [`Literal`](ColumnStyle::Literal), the normal group otherwise. An
    /// [`AsciiDoc`](ColumnStyle::AsciiDoc) cell instead parses its content as a
    /// nested sequence of [blocks](TableCellContent::AsciiDoc).
    fn parse(
        raw: RawCell<'src>,
        column: &TableColumn,
        is_header: bool,
        separator: &str,
        parser: &mut Parser,
        warnings: &mut Vec<Warning<'src>>,
    ) -> Self {
        // A cell's own alignment operator overrides the column's alignment; with
        // no operator, the cell inherits the column's alignment.
        let h_align = raw.spec.h_align.unwrap_or(column.h_align);
        let v_align = raw.spec.v_align.unwrap_or(column.v_align);

        // A cell's own style operator overrides the column's style; with no
        // operator, the cell is processed with the column's style. The header
        // row is always processed as plain header content, so neither a column
        // nor a cell style operator ever affects a header cell.
        let style = if is_header {
            ColumnStyle::Default
        } else {
            raw.spec.style.unwrap_or(column.style)
        };

        let trimmed = trim_cell_content(raw.content, style);

        // An escaped cell separator (a backslash in front of the table's
        // separator, e.g. `\|` or `\!`) is unescaped to the bare separator. Only
        // the active separator is unescaped, so a `\|` in a `!`-separated table
        // is left untouched. The replacement is computed only for the inline
        // styles; an AsciiDoc cell parses its content verbatim (see
        // [`process_content`]).
        let escaped = format!("\\{separator}");
        let replacement = if style != ColumnStyle::AsciiDoc && trimmed.data().contains(&escaped) {
            Some(trimmed.data().replace(&escaped, separator))
        } else {
            None
        };

        let content = process_content(trimmed, replacement, style, parser, warnings);

        Self {
            h_align,
            v_align,
            style,
            colspan: raw.spec.colspan.max(1),
            rowspan: raw.spec.rowspan.max(1),
            content,
            // The cell's source begins at its content, immediately after the
            // separator (before any trimming), so the cell's reported line is
            // the separator's line.
            source: raw.content,
        }
    }

    /// Build a cell from a [data field](DataField) of a delimiter-separated
    /// table (CSV, TSV, or DSV).
    ///
    /// Unlike a PSV cell, a data cell carries no per-cell specifier: its
    /// alignment and [style](ColumnStyle) come entirely from the `column`, and
    /// it always spans a single row and column. The separator escaping is
    /// handled by the format parser before this point, so the field already
    /// holds the extracted value (its [`replacement`](DataField::replacement),
    /// when present, is the value after quote/escape processing). A header cell
    /// (`is_header`) is processed as plain header content.
    fn parse_data(
        field: DataField<'src>,
        column: &TableColumn,
        is_header: bool,
        parser: &mut Parser,
        warnings: &mut Vec<Warning<'src>>,
    ) -> Self {
        let style = if is_header {
            ColumnStyle::Default
        } else {
            column.style
        };

        let source = field.content;
        let content = process_content(field.content, field.replacement, style, parser, warnings);

        Self {
            h_align: column.h_align,
            v_align: column.v_align,
            style,
            colspan: 1,
            rowspan: 1,
            content,
            source,
        }
    }

    /// Returns the horizontal alignment of this cell's content.
    ///
    /// The alignment comes from a horizontal alignment operator (`<`, `>`, or
    /// `^`) on the cell's specifier, which overrides the column's alignment. A
    /// cell with no horizontal alignment operator inherits its column's
    /// [`h_align`](TableColumn::h_align).
    pub fn h_align(&self) -> HorizontalAlignment {
        self.h_align
    }

    /// Returns the vertical alignment of this cell's content.
    ///
    /// The alignment comes from a vertical alignment operator (`.<`, `.>`, or
    /// `.^`) on the cell's specifier, which overrides the column's alignment. A
    /// cell with no vertical alignment operator inherits its column's
    /// [`v_align`](TableColumn::v_align).
    pub fn v_align(&self) -> VerticalAlignment {
        self.v_align
    }

    /// Returns the [style](ColumnStyle) applied to this cell's content.
    ///
    /// The style comes from a style operator in the last position of the cell's
    /// specifier (`a`, `d`, `e`, `h`, `l`, `m`, or `s`), which overrides the
    /// column's style. A cell with no style operator inherits its column's
    /// [`style`](TableColumn::style). A header cell is always
    /// [`Default`](ColumnStyle::Default), because the header row ignores style
    /// operators on both column and cell specifiers.
    pub fn style(&self) -> ColumnStyle {
        self.style
    }

    /// Returns the number of columns this cell spans.
    ///
    /// The span comes from a column span factor (`<n>`) or block span factor
    /// (`<n>.<n>`) in front of the span operator (`+`) on the cell's specifier.
    /// A cell with no column span factor spans a single column, so the default
    /// is `1`.
    pub fn colspan(&self) -> usize {
        self.colspan
    }

    /// Returns the number of rows this cell spans.
    ///
    /// The span comes from a row span factor (`.<n>`) or block span factor
    /// (`<n>.<n>`) in front of the span operator (`+`) on the cell's specifier.
    /// A cell with no row span factor spans a single row, so the default is
    /// `1`.
    pub fn rowspan(&self) -> usize {
        self.rowspan
    }

    /// Returns the interpreted content of this cell.
    pub fn content(&self) -> &TableCellContent<'src> {
        &self.content
    }

    /// Resolves any deferred cross-references in this cell's content.
    fn resolve_references(
        &mut self,
        resolver: &dyn ReferenceResolver,
        renderer: &dyn InlineSubstitutionRenderer,
        warnings: &mut Vec<ReferenceWarning>,
    ) {
        match &mut self.content {
            TableCellContent::Simple(content) => {
                content.resolve_references(resolver, renderer, warnings);
            }
            TableCellContent::AsciiDoc(cell) => {
                cell.resolve_references(resolver, renderer, warnings);
            }
        }
    }
}

impl<'src> HasSpan<'src> for TableCell<'src> {
    /// Returns the cell's source span, which begins at the cell's content
    /// immediately after its separator. Its [line](Span::line) is therefore the
    /// line on which the cell starts.
    fn span(&self) -> Span<'src> {
        self.source
    }
}

/// The interpreted content of a [`TableCell`].
///
/// The variant is determined by the [style](ColumnStyle) of the cell's column:
/// an [`AsciiDoc`](ColumnStyle::AsciiDoc) column produces
/// [`AsciiDoc`](Self::AsciiDoc) content, and every other style produces
/// [`Simple`](Self::Simple) inline content.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TableCellContent<'src> {
    /// Inline content: the cell's text after its substitutions (normal for most
    /// styles, verbatim for [`Literal`](ColumnStyle::Literal)) have been
    /// applied.
    Simple(Content<'src>),

    /// Block content: the cell's text parsed as a nested, standalone AsciiDoc
    /// document. Produced by the [`AsciiDoc`](ColumnStyle::AsciiDoc) style.
    AsciiDoc(AsciiDocCell<'src>),
}

/// The content of an [`AsciiDoc`](TableCellContent::AsciiDoc) table cell: a
/// nested, standalone AsciiDoc document.
///
/// Because the cell behaves like its own document, a few render-time decisions
/// depend on attribute state that is scoped to the cell and gone by the time
/// the document is rendered. They are therefore resolved while the cell is
/// parsed and captured here: whether the cell's nested document title is shown
/// (and its rendered text), and whether the cell's `doctype` is `inline` (in
/// which case a lone paragraph renders without the usual block wrapper).
///
/// A cell whose content has no preprocessor directives is parsed in place from
/// the parent document's source ([`Borrowed`](Self::Borrowed)). A cell that
/// expands an `include::` directive owns its preprocessed source
/// ([`Owned`](Self::Owned)); the owned store is shared behind an [`Arc`] so the
/// cell stays cheaply cloneable.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AsciiDocCell<'src> {
    /// Parsed in place from the parent document's source.
    Borrowed(BorrowedCell<'src>),

    /// Parsed from an owned, include-expanded source the cell carries.
    Owned(Arc<OwnedCell>),
}

impl<'src> AsciiDocCell<'src> {
    /// Returns the cell's nested-document title, rendered to its display text.
    ///
    /// This is `Some` only when the cell began with a level-0 title line
    /// (`= Title`) *and* the cell's effective `showtitle`/`notitle` state means
    /// that title is shown; otherwise it is `None`.
    pub fn title(&self) -> Option<&str> {
        match self {
            Self::Borrowed(cell) => cell.title.as_deref(),
            Self::Owned(cell) => cell.borrow_dependent().title.as_deref(),
        }
    }

    /// Returns `true` when the cell's `doctype` resolves to `inline`.
    ///
    /// An `inline` document renders a lone paragraph as bare inline content,
    /// without the enclosing block wrapper.
    pub fn is_inline(&self) -> bool {
        match self {
            Self::Borrowed(cell) => cell.inline,
            Self::Owned(cell) => cell.borrow_dependent().inline,
        }
    }

    /// Returns where (and whether) the cell's table of contents is generated.
    ///
    /// The cell is a standalone nested document, so this is resolved from the
    /// cell's own `toc` attribute and is independent of the parent document's
    /// setting.
    pub fn toc_mode(&self) -> TocMode {
        self.toc().mode
    }

    /// Returns the depth of section levels included in the cell's table of
    /// contents, resolved from the cell's own `toclevels` attribute (default
    /// `2`).
    pub fn toc_levels(&self) -> usize {
        self.toc().levels
    }

    /// Returns the title of the cell's table of contents, resolved from the
    /// cell's own `toc-title` attribute (default _Table of Contents_).
    pub fn toc_title(&self) -> &str {
        &self.toc().title
    }

    /// Returns the CSS class applied to the cell's table-of-contents container,
    /// resolved from the cell's own `toc-class` attribute (default `toc`).
    pub fn toc_class(&self) -> &str {
        &self.toc().class
    }

    /// Returns the resolved table-of-contents configuration for the cell.
    pub(crate) fn toc(&self) -> &TocConfig {
        match self {
            Self::Borrowed(cell) => &cell.toc,
            Self::Owned(cell) => &cell.borrow_dependent().toc,
        }
    }

    /// Returns the blocks parsed from the cell's content.
    pub fn blocks(&self) -> &[Block<'_>] {
        match self {
            Self::Borrowed(cell) => &cell.blocks,
            Self::Owned(cell) => &cell.borrow_dependent().blocks,
        }
    }

    /// Resolves any deferred cross-references in the cell's blocks.
    fn resolve_references(
        &mut self,
        resolver: &dyn ReferenceResolver,
        renderer: &dyn InlineSubstitutionRenderer,
        warnings: &mut Vec<ReferenceWarning>,
    ) {
        match self {
            Self::Borrowed(cell) => {
                for block in &mut cell.blocks {
                    block.resolve_references(resolver, renderer, warnings);
                }
            }

            // The owned store is shared behind an `Arc`, but references are
            // resolved immediately after parsing while the cell is still its sole
            // owner, so `get_mut` succeeds.
            Self::Owned(cell) => {
                if let Some(cell) = Arc::get_mut(cell) {
                    cell.with_dependent_mut(|_, dependent| {
                        for block in &mut dependent.blocks {
                            block.resolve_references(resolver, renderer, warnings);
                        }
                    });
                }
            }
        }
    }
}

/// An [`AsciiDoc`](TableCellContent::AsciiDoc) cell parsed in place from the
/// parent document's source.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BorrowedCell<'src> {
    title: Option<String>,
    inline: bool,
    toc: TocConfig,
    blocks: Vec<Block<'src>>,
}

self_cell! {
    /// An [`AsciiDoc`](TableCellContent::AsciiDoc) cell that owns its
    /// (include-expanded) source, with the parsed blocks borrowing from it.
    pub struct OwnedCell {
        owner: String,

        #[covariant]
        dependent: OwnedCellInner,
    }

    impl {Debug, Eq, PartialEq}
}

/// The parsed contents of an [`OwnedCell`], borrowing its owned source.
#[derive(Debug, Eq, PartialEq)]
struct OwnedCellInner<'src> {
    title: Option<String>,
    inline: bool,
    toc: TocConfig,
    blocks: Vec<Block<'src>>,
}

/// Parse the value of the `cols` attribute into a list of columns, mirroring
/// Asciidoctor's `parse_colspecs`.
///
/// All spaces are first removed from the value. A wholly blank value yields no
/// columns (the caller then takes the column count from the first row), and a
/// lone integer (the deprecated `cols="3"` form) yields that many default
/// columns. Otherwise the value is a list of column specifiers separated by
/// commas, or by semicolons when no comma is present. An empty record (e.g. the
/// trailing field of `cols="1,,1"`) contributes a default column, and a
/// specifier may be preceded by a multiplier (`<n>*`) that repeats the column
/// `n` times. Each specifier's alignment operators, proportional width, and
/// [style operator](parse_col_spec) are interpreted.
fn parse_cols(value: &str) -> Vec<TableColumn> {
    // Asciidoctor strips every space from the cols value before parsing, so
    // `cols=" 1, 1 "` is equivalent to `cols="1,1"`.
    let records: String = value.chars().filter(|c| !c.is_whitespace()).collect();

    // A wholly blank cols value is ignored: the caller falls back to the column
    // count of the first row.
    if records.is_empty() {
        return vec![];
    }

    // Deprecated single-integer form: `cols=3` is equivalent to `cols="3*"` and
    // produces that many equally sized columns.
    if let Ok(count) = records.parse::<usize>() {
        return vec![TableColumn::default(); count];
    }

    // Split on commas when present, otherwise on semicolons (Asciidoctor accepts
    // either as the column-spec separator, but not a mix). Empty records are
    // kept: each one contributes a default column.
    let parts: Vec<&str> = if records.contains(',') {
        records.split(',').collect()
    } else {
        records.split(';').collect()
    };

    let mut columns: Vec<TableColumn> = vec![];
    for part in parts {
        if part.is_empty() {
            columns.push(TableColumn::default());
        } else if let Some((count, spec)) = part.split_once('*') {
            let repeat = count.parse::<usize>().unwrap_or(1).max(1);
            let column = parse_col_spec(spec);
            for _ in 0..repeat {
                columns.push(column.clone());
            }
        } else {
            columns.push(parse_col_spec(part));
        }
    }

    columns
}

/// Parse a single column specifier, extracting its alignment, proportional
/// width, and style.
///
/// A column specifier is positional: an optional horizontal alignment operator
/// (`<`, `>`, or `^`) comes first, followed by an optional vertical alignment
/// operator (`.<`, `.>`, or `.^`), followed by the width, and finally an
/// optional style operator in the last position. When a multiplier (`<n>*`) is
/// present, the operators follow the multiplier, so the `spec` passed here is
/// the portion after the `*`.
///
/// The width is either the special autowidth value `~` (sizing the column to
/// its content) or the first contiguous run of digits after any alignment
/// operators; a spec with neither falls back to the default width. The style
/// operator is the trailing letter (`a`, `d`, `e`, `h`, `l`, `m`, or `s`); an
/// unrecognized trailing letter leaves the style at its default.
fn parse_col_spec(spec: &str) -> TableColumn {
    let mut rest = spec.trim();

    // Horizontal alignment operator (if present) always comes first.
    let mut h_align = HorizontalAlignment::Left;
    match rest.as_bytes().first() {
        Some(b'<') => {
            h_align = HorizontalAlignment::Left;
            rest = &rest[1..];
        }

        Some(b'>') => {
            h_align = HorizontalAlignment::Right;
            rest = &rest[1..];
        }

        Some(b'^') => {
            h_align = HorizontalAlignment::Center;
            rest = &rest[1..];
        }

        _ => {}
    }

    // Vertical alignment operator (if present) follows, introduced by a dot.
    let mut v_align = VerticalAlignment::Top;
    if let Some(after_dot) = rest.strip_prefix('.') {
        match after_dot.as_bytes().first() {
            Some(b'<') => {
                v_align = VerticalAlignment::Top;
                rest = &after_dot[1..];
            }

            Some(b'>') => {
                v_align = VerticalAlignment::Bottom;
                rest = &after_dot[1..];
            }

            Some(b'^') => {
                v_align = VerticalAlignment::Middle;
                rest = &after_dot[1..];
            }

            _ => {}
        }
    }

    // Width comes after the alignment operators. The special value `~` marks
    // the column as autowidth (sized to its content); otherwise the width is
    // the first run of digits. A spec with neither falls back to the default
    // proportional width.
    let mut autowidth = false;
    let mut width = TableColumn::default().width;
    if let Some(after_tilde) = rest.strip_prefix('~') {
        autowidth = true;
        rest = after_tilde;
    } else {
        let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
        if let Ok(parsed) = digits.parse::<usize>()
            && parsed > 0
        {
            width = parsed;
        }
        rest = &rest[digits.len()..];
    }

    // The style operator, if present, occupies the last position on the
    // specifier, so it is the entire remainder after the width. Matching the
    // whole remainder (rather than just its first byte) means a malformed spec
    // with trailing junk — e.g. `1em` — falls back to the default style instead
    // of silently honoring the first letter and discarding the rest.
    let style = match rest.trim() {
        "a" => ColumnStyle::AsciiDoc,
        "d" => ColumnStyle::Default,
        "e" => ColumnStyle::Emphasis,
        "h" => ColumnStyle::Header,
        "l" => ColumnStyle::Literal,
        "m" => ColumnStyle::Monospace,
        "s" => ColumnStyle::Strong,
        _ => ColumnStyle::Default,
    };

    TableColumn {
        width,
        autowidth,
        h_align,
        v_align,
        style,
    }
}

/// The span, alignment, and style overrides parsed from a
/// [cell specifier](RawCell::spec).
///
/// Each alignment and style field is `None` when the corresponding operator is
/// absent from the specifier, in which case the cell inherits that alignment
/// (or style) from its column. `colspan` and `rowspan` are the number of
/// columns and rows the cell spans; they default to `1` (no span). `repeat` is
/// the duplication factor — the number of consecutive cells the content is
/// cloned into — and defaults to `1` (no duplication).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct CellSpec {
    h_align: Option<HorizontalAlignment>,
    v_align: Option<VerticalAlignment>,
    style: Option<ColumnStyle>,
    colspan: usize,
    rowspan: usize,
    repeat: usize,
}

impl Default for CellSpec {
    fn default() -> Self {
        Self {
            h_align: None,
            v_align: None,
            style: None,
            colspan: 1,
            rowspan: 1,
            repeat: 1,
        }
    }
}

/// A single PSV cell as located by [`scan_cells`]: the alignment operators from
/// its specifier together with the raw (untrimmed) span of its content.
#[derive(Clone, Copy)]
struct RawCell<'src> {
    spec: CellSpec,
    content: Span<'src>,
}

/// The largest number of cells a single duplication factor (`<n>*`) is allowed
/// to expand into.
///
/// A duplicated cell is materialized as `<n>` independent cells, so the factor
/// is an amplification: a dozen source bytes such as `1000000000*` would
/// otherwise request a billion `RawCell`s (a multi-gigabyte allocation).
/// Capping the per-specifier factor bounds that amplification while leaving any
/// realistic table — which never duplicates a cell more than a handful of times
/// — untouched. (This is the one point where the implementation diverges from
/// Asciidoctor, which expands the literal factor however large.)
const MAX_DUPLICATION_FACTOR: usize = 1_000;

/// Expand each duplicated cell into the `<n>` independent cells it represents.
///
/// A cell specifier with a duplication factor (`<n>*`) clones the cell's
/// content and properties into `<n>` consecutive cells. Each clone is an
/// ordinary single-slot cell (colspan and rowspan of 1), so expanding here —
/// before the grid is walked — lets the clones flow into rows exactly like
/// cells the author typed out by hand. A duplication factor of zero produces no
/// cells, dropping the original (matching Asciidoctor). A cell with no
/// duplication factor has a `repeat` of 1 and so passes through unchanged. The
/// factor is clamped to [`MAX_DUPLICATION_FACTOR`] so a hostile specifier can't
/// trigger a runaway allocation.
fn expand_duplicates(cells: Vec<RawCell<'_>>) -> Vec<RawCell<'_>> {
    // The common case is no duplication at all, so only the clones beyond the
    // first add to the count.
    let extra: usize = cells
        .iter()
        .map(|c| c.spec.repeat.min(MAX_DUPLICATION_FACTOR).saturating_sub(1))
        .sum();

    let mut expanded = Vec::with_capacity(cells.len() + extra);
    for cell in cells {
        for _ in 0..cell.spec.repeat.min(MAX_DUPLICATION_FACTOR) {
            expanded.push(cell);
        }
    }

    expanded
}

/// Scan a region for PSV cell boundaries, returning the [specifier](CellSpec)
/// and raw (untrimmed) content span of each cell.
///
/// Every unescaped occurrence of the table's `separator` (the vertical bar
/// (`|`) by default, the exclamation mark (`!`) for a nested table, or any
/// string set with the `separator` attribute, e.g. the broken bar `¦`) is a
/// cell boundary, matching Asciidoctor. The token immediately preceding a
/// separator is treated as that cell's [specifier](CellSpec) (e.g. `^`, `2+`,
/// `.>`) only when it parses as one (see [`parse_cell_spec`]) *and* is anchored
/// at the line start or preceded by whitespace; otherwise the token is ordinary
/// content of the preceding cell and the separator is a plain boundary (so the
/// `a` in `|a|b` is content, not a style operator). Content before the first
/// boundary is ignored.
///
/// A separator immediately preceded by a backslash (e.g. `\|`) is escaped: it
/// is literal content rather than a boundary, and the backslash is stripped
/// later in [`TableCell::parse`]. Only the single byte before the separator is
/// inspected, so `\\|` is also read as an escaped separator — matching
/// Asciidoctor, whose check is likewise the single-character
/// `pre_match.end_with? '\'`.
fn scan_cells<'src>(
    region: Span<'src>,
    separator: &str,
) -> (Vec<RawCell<'src>>, Option<Span<'src>>) {
    let data = region.data();
    let bytes = data.as_bytes();
    let len = bytes.len();

    // A zero-length separator would never advance; treat it as a single byte to
    // stay safe. (The resolver never produces an empty separator.)
    let sep_len = separator.len().max(1);

    let mut cells: Vec<RawCell<'src>> = vec![];

    // The content start and specifier of the cell currently being accumulated.
    let mut content_start: Option<usize> = None;

    let mut cur_spec = CellSpec::default();

    // The span of a cell recovered from content that precedes the first
    // separator (see below); `Some` drives a missing-leading-separator warning.
    let mut recovered: Option<Span<'src>> = None;

    let mut i = 0;
    while i < len {
        if data
            .get(i..)
            .is_some_and(|rest| rest.starts_with(separator))
        {
            // A separator immediately preceded by a backslash is escaped: it is
            // literal content, not a cell boundary. The backslash is stripped
            // from the rendered cell later (see `TableCell::parse`).
            if i > 0 && bytes.get(i - 1).copied() == Some(b'\\') {
                i += sep_len;
                continue;
            }

            // Walk back to the start of the token directly preceding this
            // separator. The token (a possible cell specifier) runs back to the
            // previous whitespace, tab, or newline, or to the start of the
            // region.
            let mut tok_start = i;
            while tok_start > 0
                && !matches!(
                    bytes.get(tok_start - 1).copied(),
                    Some(b' ' | b'\t' | b'\n')
                )
            {
                tok_start -= 1;
            }

            let token = data.get(tok_start..i).unwrap_or_default();
            let spec = if token.is_empty() {
                Some(CellSpec::default())
            } else {
                parse_cell_spec(token)
            };

            // Every unescaped separator is a cell boundary (matching
            // Asciidoctor). When the token is empty or a valid specifier it
            // belongs to the *next* cell, so the previous cell's content ends
            // before the token. Otherwise the token is ordinary content of the
            // previous cell (e.g. the `a` in `|a|b`, where `a` is not preceded
            // by whitespace and so is not a specifier), the separator is plain,
            // and the next cell takes the default specifier.
            let (content_end, next_spec) = match spec {
                Some(spec) => (tok_start, spec),
                None => (i, CellSpec::default()),
            };

            match content_start {
                Some(start) => {
                    // The separating whitespace, included in the slice, is
                    // trimmed later in `TableCell::parse`.
                    cells.push(RawCell {
                        spec: cur_spec,
                        content: region.slice(start..content_end),
                    });
                }

                None => {
                    // No cell has been opened yet, so this is the table's first
                    // separator. Non-blank content in front of it means the first
                    // cell is missing its leading separator; recover that content
                    // as the first cell (with the default specifier) and record
                    // its span so the caller can warn, matching Asciidoctor.
                    let leading = region.slice(0..content_end);
                    if !leading.data().trim().is_empty() {
                        cells.push(RawCell {
                            spec: CellSpec::default(),
                            content: leading,
                        });
                        recovered = Some(leading);
                    }
                }
            }

            cur_spec = next_spec;
            content_start = Some(i + sep_len);
            i += sep_len;
            continue;
        }

        i += 1;
    }

    if let Some(start) = content_start {
        cells.push(RawCell {
            spec: cur_spec,
            content: region.slice(start..len),
        });
    }

    (cells, recovered)
}

/// Parse a cell specifier, returning its [span and overrides](CellSpec), or
/// `None` if `token` is not a valid cell specifier.
///
/// A cell specifier is positional and every part is optional, but the whole
/// token must be consumed for it to be valid:
///
/// ```text
/// <factor><span or duplication operator><horizontal><vertical><style>
/// ```
///
/// * The factor and span/duplication operator are an optional count (e.g. `2`,
///   `2.3`, `.3`) that, when present, must be followed by `+` (span) or `*`
///   (duplication). For a span the factor is interpreted as the cell's colspan
///   and rowspan (a missing column or row count defaults to 1). For a
///   duplication the column part of the factor is the duplication count — the
///   number of consecutive cells the content is cloned into — and any row part
///   is ignored; a duplicated cell keeps a colspan and rowspan of 1.
/// * The horizontal alignment operator is `<`, `>`, or `^`.
/// * The vertical alignment operator is a dot followed by `<`, `>`, or `^`.
/// * The style operator is a single lowercase letter in the last position. A
///   recognized operator (`a`, `d`, `e`, `h`, `l`, `m`, or `s`) overrides the
///   column's style on this cell. Any other single lowercase letter still
///   locates the separator but leaves the style at `None`, so the cell inherits
///   its column's style (matching Asciidoctor, which ignores an unrecognized
///   style operator).
fn parse_cell_spec(token: &str) -> Option<CellSpec> {
    let b = token.as_bytes();
    let mut i = 0;

    // Optional span/duplication: an optional span factor followed by `+` (span)
    // or `*` (duplication). The factor is a column count, an optional dot, and an
    // optional row count (`<n>`, `.<n>`, or `<n>.<n>`). The factor is committed
    // only when the operator that must follow it is present; otherwise the
    // leading digits remain and the token fails the full-consumption check below.
    let mut colspan = 1;
    let mut rowspan = 1;
    let mut repeat = 1;
    let col_start = i;

    let mut j = i;
    while matches!(b.get(j).copied(), Some(c) if c.is_ascii_digit()) {
        j += 1;
    }

    let col_end = j;
    let mut has_dot = false;

    let mut row_start = j;
    if b.get(j).copied() == Some(b'.') {
        has_dot = true;
        j += 1;
        row_start = j;
        while matches!(b.get(j).copied(), Some(c) if c.is_ascii_digit()) {
            j += 1;
        }
    }

    let row_end = j;
    match b.get(j).copied() {
        // Span: the factor is interpreted as a colspan and rowspan. A missing
        // column or row count defaults to 1, so `2+` spans two columns, `.3+`
        // spans three rows, and `2.3+` spans a 2x3 block.
        Some(b'+') => {
            // The factor consists only of ASCII digits and dots, so these ranges
            // are always valid `str` slices.
            let col_digits = token.get(col_start..col_end).unwrap_or_default();
            if !col_digits.is_empty() {
                colspan = col_digits.parse().unwrap_or(1);
            }
            if has_dot {
                let row_digits = token.get(row_start..row_end).unwrap_or_default();
                if !row_digits.is_empty() {
                    rowspan = row_digits.parse().unwrap_or(1);
                }
            }
            i = j + 1;
        }

        // Duplication: the factor is interpreted as a duplication count, so the
        // cell's content and properties are cloned into `<n>` consecutive cells.
        // Only the column part of the factor is the count; any row part (`<n>.`)
        // is ignored, matching Asciidoctor. A missing column count defaults to 1.
        // Unlike a span, a duplication leaves `colspan` and `rowspan` at 1: each
        // clone is an ordinary single-slot cell.
        Some(b'*') => {
            let col_digits = token.get(col_start..col_end).unwrap_or_default();
            if !col_digits.is_empty() {
                repeat = col_digits.parse().unwrap_or(1);
            }
            i = j + 1;
        }

        _ => {}
    }

    // Optional horizontal alignment operator.
    let mut h_align = None;
    match b.get(i).copied() {
        Some(b'<') => {
            h_align = Some(HorizontalAlignment::Left);
            i += 1;
        }

        Some(b'>') => {
            h_align = Some(HorizontalAlignment::Right);
            i += 1;
        }

        Some(b'^') => {
            h_align = Some(HorizontalAlignment::Center);
            i += 1;
        }

        _ => {}
    }

    // Optional vertical alignment operator, introduced by a dot.
    let mut v_align = None;
    if b.get(i).copied() == Some(b'.') {
        match b.get(i + 1).copied() {
            Some(b'<') => {
                v_align = Some(VerticalAlignment::Top);
                i += 2;
            }

            Some(b'>') => {
                v_align = Some(VerticalAlignment::Bottom);
                i += 2;
            }

            Some(b'^') => {
                v_align = Some(VerticalAlignment::Middle);
                i += 2;
            }

            _ => {}
        }
    }

    // Optional style operator: a single lowercase letter in the last position.
    // A recognized letter overrides the column's style; any other lowercase
    // letter is consumed (so the separator is still located) but leaves the
    // style at `None`, so the cell inherits its column's style.
    let mut style = None;
    if let Some(c) = b.get(i).copied()
        && c.is_ascii_lowercase()
    {
        style = match c {
            b'a' => Some(ColumnStyle::AsciiDoc),
            b'd' => Some(ColumnStyle::Default),
            b'e' => Some(ColumnStyle::Emphasis),
            b'h' => Some(ColumnStyle::Header),
            b'l' => Some(ColumnStyle::Literal),
            b'm' => Some(ColumnStyle::Monospace),
            b's' => Some(ColumnStyle::Strong),
            _ => None,
        };
        i += 1;
    }

    // The token is a cell specifier only if it was consumed in its entirety.
    if i == b.len() {
        Some(CellSpec {
            h_align,
            v_align,
            style,
            colspan,
            rowspan,
            repeat,
        })
    } else {
        None
    }
}

/// Return the subspan of `s` with surrounding whitespace (including newlines)
/// removed.
fn trim_surrounding_whitespace(s: Span<'_>) -> Span<'_> {
    let data = s.data();
    let start = data.len() - data.trim_start().len();
    let len = data.trim().len();
    s.slice(start..start + len)
}

/// Trim a PSV cell's content according to its [style](ColumnStyle), matching
/// Asciidoctor's `Table::Cell` initializer:
///
/// * A [`Literal`](ColumnStyle::Literal) cell has its trailing whitespace
///   removed and any leading blank lines stripped, but the leading indentation
///   of its first content line is preserved (so an indented literal cell keeps
///   its indentation).
/// * An [`AsciiDoc`](ColumnStyle::AsciiDoc) cell likewise removes trailing
///   whitespace; if the remaining content begins with a newline it strips the
///   leading blank lines (preserving the first content line's indentation, so a
///   leading-indented line is interpreted as a literal block), otherwise it
///   strips the leading whitespace.
/// * Every other style has all surrounding whitespace removed.
fn trim_cell_content(s: Span<'_>, style: ColumnStyle) -> Span<'_> {
    let data = s.data();
    match style {
        ColumnStyle::Literal => {
            let end = data.trim_end().len();
            let mut start = 0;
            while data[start..end].starts_with('\n') {
                start += 1;
            }
            s.slice(start..end)
        }

        ColumnStyle::AsciiDoc => {
            let end = data.trim_end().len();
            if data[..end].starts_with('\n') {
                let mut start = 0;
                while data[start..end].starts_with('\n') {
                    start += 1;
                }
                s.slice(start..end)
            } else {
                let start = end - data[..end].trim_start().len();
                s.slice(start..end)
            }
        }

        _ => trim_surrounding_whitespace(s),
    }
}

/// Returns the first non-blank line in `rest`, or `None` when every remaining
/// line is blank (or `rest` is empty).
fn first_nonblank_line(mut rest: Span<'_>) -> Option<Span<'_>> {
    while !rest.is_empty() {
        let line = rest.take_line();
        if !line.item.data().trim().is_empty() {
            return Some(line.item);
        }
        rest = line.after;
    }
    None
}

/// Returns `true` when `line` begins a new PSV cell, i.e. it contains the
/// separator and the text before the first separator (after any leading
/// whitespace) is either empty or a valid cell specifier. A line that continues
/// the previous cell returns `false`.
fn psv_line_starts_cell(line: &str, separator: &str) -> bool {
    match line.find(separator) {
        Some(pos) => {
            let prefix = line[..pos].trim_start();
            prefix.is_empty() || parse_cell_spec(prefix).is_some()
        }
        None => false,
    }
}

/// Returns `true` when `line` contains an odd number of double quotes, i.e. it
/// opens a quoted CSV/TSV value that is not closed on the same line.
fn line_has_unclosed_quote(line: &str) -> bool {
    line.bytes().filter(|&b| b == b'"').count() % 2 == 1
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{AsciiDocCell, OwnedCell, OwnedCellInner, TocConfig};
    use crate::parser::{
        HtmlSubstitutionRenderer, ReferenceResolver, ResolutionContext, ResolvedReference,
    };

    /// A resolver that resolves nothing; the owned-cell resolution path under
    /// test carries no references, so it is never actually consulted.
    struct NoopResolver;

    impl ReferenceResolver for NoopResolver {
        fn resolve(&self, _context: &ResolutionContext<'_>) -> Option<ResolvedReference> {
            None
        }
    }

    /// When an owned (include-expanded) AsciiDoc cell is shared behind more
    /// than one `Arc` reference, `resolve_references` cannot obtain a
    /// mutable borrow of the store and leaves it untouched rather than
    /// panicking. Production code resolves while the cell is its sole
    /// owner, so this defensive branch is exercised here by deliberately
    /// holding a second reference.
    #[test]
    fn resolve_references_skips_shared_owned_cell() {
        let mut cell = AsciiDocCell::Owned(Arc::new(OwnedCell::new(String::new(), |_source| {
            OwnedCellInner {
                title: None,
                inline: false,
                toc: TocConfig::disabled(),
                blocks: vec![],
            }
        })));

        // Hold a second reference to the same store so `Arc::get_mut` fails.
        let shared = cell.clone();

        let mut warnings = vec![];
        cell.resolve_references(&NoopResolver, &HtmlSubstitutionRenderer {}, &mut warnings);

        // Resolution was skipped silently: no warnings, and the two references
        // still describe the same (unmodified) cell.
        assert!(warnings.is_empty());
        assert_eq!(cell, shared);
    }
}