einvoice-xslt-exec 0.3.0

Executor of the XSLT 2.0 subset used by the official e-invoicing validation stylesheets: runs the compiled Schematron and writes SVRL, with the behaviour of Saxon-HE
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
//! Running an [`Executable`] on a document.
//!
//! Template rules are chosen as Saxon-HE chooses them: highest priority first, the last
//! declared on a tie (an ambiguity is reported as a warning, as Saxon does), then the
//! built-in rules of XSLT 2.0 section 6.6. A dynamic error while matching a pattern is
//! recovered as Saxon recovers it: a warning, and the rule does not match. Variables live in frames, one per template,
//! function or global variable invocation; the XPath evaluator reads them through the
//! [`Host`] interface, and calls stylesheet functions through it too.
//!
//! Evaluation order follows Saxon-HE 12.10 where it is observable (which dynamic errors
//! are raised): global variables are evaluated when first referenced; a local variable
//! that nothing refers to is never evaluated; one referred to once, not in a loop, is
//! evaluated when the reference is (Saxon inlines it); a value that depends on the
//! position, size or current item of its focus is evaluated where it is bound, and so are
//! the parameters `xsl:apply-templates` and `xsl:next-match` pass; any other local
//! variable, template parameter default or parameter `xsl:call-template` passes is
//! evaluated as Saxon's `LearningEvaluator` evaluates it: when first referenced, until
//! it has been evaluated 20 times with its value computed each time (in a function body,
//! 20 times at all), then where it is bound (see [`Eval`]). Saxon keeps these counts in
//! the compiled stylesheet, over every document it transforms with it; they are kept
//! here for one transformation, as Saxon on a stylesheet compiled for one document.
//! Arguments of stylesheet functions are evaluated before the call (Saxon evaluates them
//! lazily too).

use std::borrow::Cow;
use std::cell::{Cell, OnceCell, RefCell};
use std::collections::{HashMap, HashSet};
use std::rc::{Rc, Weak};
use std::sync::Arc;

use einvoice_xdm::{AtomicType, AtomicValue, DateTime};
use einvoice_xpath::{AtomicKind, ItemType, Occurrence, QName, SequenceType};
use einvoice_xpath_eval::{
    Argument, Clock, CompiledExpr, Context, DEFAULT_MAX_DEPTH, Document, DocumentError,
    DynamicContext, ErrorCode, FunctionId, Host, Item, LimitExceeded, Node, NodeAxis, NodeKind,
    NodeSelection, SelectionStorage, StackLimit, VariableId,
};
use einvoice_xslt::{NamespaceBinding, NumberLevel};

use crate::error::{LIMIT, Result, TransformError, err};
use crate::output::{CompactBuilder, CompactTransformation, ResultObserver};
use crate::pattern::{self, Matcher};
use crate::prepare::{
    AvtItem, Binding, CompiledAvt, CompiledFunction, CompiledTemplate, CountPattern, Eval,
    Executable, Inst, ModeRef, Plan, Value, When, WithParam, kind_index, rule_name_number,
};
use crate::sink::{
    Compact, SequenceSink, Serialization, Sink, Target, TextSink, TreeSink, WholeText, XmlWriter,
    attribute_name, copy_node, element_name, serialize,
};

/// Fetches the documents that `document()` asks for, by absolute URI. The engine never
/// reads files or the network itself.
pub trait DocumentResolver {
    /// The text of the document at `uri`, or `None` when it cannot be retrieved.
    fn resolve(&self, uri: &str) -> Option<String>;
}

/// The maximum nesting of invocations in a transformation, unless the caller sets another
/// one ([`TransformOptions::max_nesting`]): template rules, built-in template rules, named
/// templates and stylesheet functions invoked one inside another. It is
/// [`max_nesting_for_depth`] of [`DEFAULT_MAX_DEPTH`]: 576.
pub const DEFAULT_MAX_NESTING: u32 = max_nesting_for_depth(DEFAULT_MAX_DEPTH);

/// The most tail calls in a row in a transformation, unless the caller sets another
/// maximum ([`TransformOptions::max_tail_calls`]): 65 536. A template that ends with an
/// `xsl:call-template` runs the template it calls in its place, as Saxon does, without
/// stack in proportion to the recursion; the ISO Schematron skeleton recurses so over
/// each character of every XPath expression of a schema (8519 for the longest of the CEN
/// rules).
pub const DEFAULT_MAX_TAIL_CALLS: u32 = 1 << 16;

/// The nesting of invocations that the official stylesheets need on a document
/// `max_depth` deep, with room to spare: twice the depth, plus 64.
///
/// A compiled Schematron applies the template rules of each pattern to every element, one
/// element level inside another, so the nesting reaches the depth of the document; where
/// a rule fires, the ISO Schematron skeleton writes the location of the node with a
/// template that recurses over its ancestors (mode `schematron-get-full-path`), which
/// nests as deep again. The 64 levels above are for the few invocations around them
/// (the root template, the next rule in a chain) and for stylesheets that recurse
/// otherwise. On a document [`DEFAULT_MAX_DEPTH`] deep, this nesting fits in the 2 MiB
/// stack a thread is given by default, in a debug build too.
#[must_use]
pub const fn max_nesting_for_depth(max_depth: u32) -> u32 {
    max_depth.saturating_mul(2).saturating_add(64)
}

/// Options of a transformation.
pub struct TransformOptions<'r> {
    /// The implicit timezone in minutes east of UTC (Saxon takes the JVM's). Default: UTC.
    pub implicit_timezone: Option<i16>,
    /// The value of `fn:current-dateTime()`, with a timezone. Default: the system clock,
    /// read once per transformation; required on targets without one
    /// (`wasm32-unknown-unknown`).
    pub current_date_time: Option<DateTime>,
    /// Values of the global parameters (`xsl:param`), by name.
    pub parameters: Vec<(QName, Vec<Item>)>,
    /// Where `document()` gets documents other than the source document.
    pub resolver: Option<&'r dyn DocumentResolver>,
    /// The maximum element depth of the source document and of the documents `document()`
    /// loads (the document element is at depth 1): a document with an element nested
    /// deeper is refused with a `LIMIT` error ([`TransformError::limit`]). Default:
    /// [`DEFAULT_MAX_DEPTH`].
    pub max_depth: u32,
    /// The maximum nesting of template rules, built-in template rules, named templates
    /// and stylesheet functions invoked one inside another: beyond it, the transformation
    /// ends with a `LIMIT` error ([`TransformError::limit`]) instead of overflowing the
    /// stack. Default: [`DEFAULT_MAX_NESTING`]. A larger value needs a larger stack.
    pub max_nesting: u32,
    /// The most stack, in bytes, the invocations in progress and the expressions they
    /// evaluate may use, measured from the start of the transformation: beyond it, the
    /// transformation ends with a `LIMIT` error ([`LimitExceeded::Stack`]) instead of
    /// overflowing the stack. It bounds what the nesting alone cannot, the stack of each
    /// level, which depends on the stylesheet: a function calling itself from a nested
    /// expression needs much more than a template rule applied to a child. The thread
    /// must have this much stack and some more, for its caller and for the deepest level.
    /// Default: [`DEFAULT_MAX_STACK`].
    pub max_stack: u32,
    /// The most tail calls in a row (`xsl:call-template` at the end of a template, run in
    /// its place): beyond it, the transformation ends with a `LIMIT` error
    /// ([`LimitExceeded::TailCalls`]) rather than run for ever. Default:
    /// [`DEFAULT_MAX_TAIL_CALLS`].
    pub max_tail_calls: u32,
}

/// The default of [`TransformOptions::max_stack`]: 1.5 MiB, for a thread with the
/// default 2 MiB stack of a spawned thread; 256 KiB on WebAssembly. With it, the official
/// stylesheets run on a document [`DEFAULT_MAX_DEPTH`] deep, in a debug build too, and
/// the 512 KiB left are more than the caller and one level of invocation need.
///
/// On WebAssembly, the stack measured is the program's stack in linear memory (1 MiB),
/// but what runs out first is the stack of the engine running the code, which the
/// program cannot measure: 512 KiB by default in wasmtime. A recursion stopped at 256 KiB
/// of the former uses at most 380 KB of the latter (measured on the `wasm-validate`
/// example of `einvoice-validator`, a function calling itself without end), and the
/// official stylesheets need less than half of it on a document [`DEFAULT_MAX_DEPTH`]
/// deep.
#[cfg(not(target_family = "wasm"))]
pub const DEFAULT_MAX_STACK: u32 = 3 << 19;

/// The default of [`TransformOptions::max_stack`] (see the other targets).
#[cfg(target_family = "wasm")]
pub const DEFAULT_MAX_STACK: u32 = 256 << 10;

impl Default for TransformOptions<'_> {
    fn default() -> Self {
        Self {
            implicit_timezone: None,
            current_date_time: None,
            parameters: Vec::new(),
            resolver: None,
            max_depth: DEFAULT_MAX_DEPTH,
            max_nesting: DEFAULT_MAX_NESTING,
            max_stack: DEFAULT_MAX_STACK,
            max_tail_calls: DEFAULT_MAX_TAIL_CALLS,
        }
    }
}

impl std::fmt::Debug for TransformOptions<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TransformOptions")
            .field("implicit_timezone", &self.implicit_timezone)
            .field("current_date_time", &self.current_date_time)
            .field("parameters", &self.parameters)
            .field("resolver", &self.resolver.map(|_| "..."))
            .field("max_depth", &self.max_depth)
            .field("max_nesting", &self.max_nesting)
            .field("max_stack", &self.max_stack)
            .field("max_tail_calls", &self.max_tail_calls)
            .finish()
    }
}

/// The result of a transformation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transformation {
    /// The result document, serialized as `xsl:output` asks.
    pub output: String,
    /// Recoverable conditions reported as warnings, as Saxon reports them, each
    /// distinct message once in the order first reported, as Saxon's standard error
    /// reporter prints them: ambiguous template rule matches (`XTDE0540: ...`), and
    /// dynamic errors raised while matching a pattern, after which the node is taken
    /// not to match (`line <L>: <CODE>: An error occurred matching pattern {...}:
    /// <message>`, `L` the line of the `xsl:template` or `xsl:number`).
    pub warnings: Vec<String>,
    /// The messages of `xsl:message`, in the order they were made, each its content
    /// written as Saxon-HE 12.10's message handler writes it: as XML without
    /// declaration nor indentation (text escaped, elements as tags). A message without
    /// content is not kept, as Saxon prints nothing for it. The message of an
    /// `xsl:message` that terminates is the error's ([`TransformError::message`], code
    /// `XTMM9000`).
    pub messages: Vec<String>,
}

/// The result of [`Executable::transform_tree`]: the result tree.
#[derive(Debug, Clone)]
pub struct TreeTransformation {
    /// The result document (its nodes without base URI).
    pub document: Rc<Document>,
    /// The warnings, as [`Transformation::warnings`].
    pub warnings: Vec<String>,
    /// The messages, as [`Transformation::messages`].
    pub messages: Vec<String>,
}

impl Executable {
    /// Runs the stylesheet on a document: `xsl:apply-templates` on its document node in
    /// the default mode, the result serialized. `base_uri` is the URI of the document:
    /// the base URI of its nodes, and the URI under which `document()` loads it again. As
    /// in Saxon-HE 12.10, it is not its document URI: `document-uri(/)` is empty.
    ///
    /// The source text can be borrowed (`&str`, `&String`) or given (`String`), as for
    /// [`Document::parse`]: borrowed, the texts of its nodes are copied into the tree;
    /// given, the tree keeps the text and refers to it, so that the document is in memory
    /// once. A given text is kept as a copy only for a stylesheet that calls
    /// `document()` and a document with a `base_uri`, where `document()` may load the
    /// document again from its text.
    ///
    /// # Errors
    ///
    /// The document is not well-formed (or has a document type declaration), or the
    /// transformation raised a dynamic error.
    pub fn transform<'s>(
        &self,
        source: impl Into<Cow<'s, str>>,
        base_uri: Option<&str>,
    ) -> std::result::Result<Transformation, TransformError> {
        self.transform_with(source, base_uri, &TransformOptions::default())
    }

    /// [`Executable::transform`] with options.
    ///
    /// # Errors
    ///
    /// As [`Executable::transform`], and an invalid timezone or current date and time
    /// (or none given on a target without a system clock).
    pub fn transform_with<'s>(
        &self,
        source: impl Into<Cow<'s, str>>,
        base_uri: Option<&str>,
        options: &TransformOptions<'_>,
    ) -> std::result::Result<Transformation, TransformError> {
        let ((output, WholeText), warnings, messages) =
            self.run(source, base_uri, options, WholeText)?;
        Ok(Transformation {
            output,
            warnings,
            messages,
        })
    }

    /// Runs the stylesheet on a tree (`source`, a document node) and gives the result
    /// tree instead of its serialization, for a transformation whose result is read by
    /// another one. `document()` loads every document through the resolver of the
    /// options, the source's own URI included.
    ///
    /// # Errors
    ///
    /// The source is not a document node, or the transformation raised a dynamic error;
    /// as [`Executable::transform_with`] otherwise.
    pub fn transform_tree(
        &self,
        source: &Node,
        options: &TransformOptions<'_>,
    ) -> std::result::Result<TreeTransformation, TransformError> {
        if source.kind() != NodeKind::Document {
            return err(
                "XTDE0040",
                "the source of a tree transformation is a document node",
            );
        }
        let mut sink = TreeSink::new();
        let (warnings, messages) = self.execute(source, None, None, options, &mut sink)?;
        Ok(TreeTransformation {
            document: sink.finish()?,
            warnings,
            messages,
        })
    }

    /// The serialization parameters of the stylesheet's `xsl:output`.
    #[must_use]
    pub const fn serialization(&self) -> Serialization {
        self.serialization
    }

    /// [`Executable::transform_with`], the result kept as a
    /// [`CompactOutput`](crate::CompactOutput) instead of a string, and read as it is
    /// written by `observer`: a large result in less memory, read without parsing its
    /// text.
    ///
    /// # Errors
    ///
    /// As [`Executable::transform_with`].
    pub fn transform_compact<'s>(
        &self,
        source: impl Into<Cow<'s, str>>,
        base_uri: Option<&str>,
        options: &TransformOptions<'_>,
        observer: &mut dyn ResultObserver,
    ) -> std::result::Result<CompactTransformation, TransformError> {
        let target = Compact {
            builder: CompactBuilder::default(),
            observer,
        };
        // The pieces are all taken: nothing is left in the writer's buffer.
        let ((_, target), warnings, messages) = self.run(source, base_uri, options, target)?;
        let mut output = target.builder.finish();
        // The source tree is dropped: the room left in the buffers is released.
        output.shrink_to_fit();
        Ok(CompactTransformation {
            output,
            warnings,
            messages,
        })
    }

    /// Runs the stylesheet, its result written to `target`: what
    /// [`XmlWriter::finish`] returns, the warnings and the messages.
    #[expect(
        clippy::type_complexity,
        reason = "the writer's result, the warnings and the messages"
    )]
    fn run<'s, T: Target>(
        &self,
        source: impl Into<Cow<'s, str>>,
        base_uri: Option<&str>,
        options: &TransformOptions<'_>,
        target: T,
    ) -> std::result::Result<((String, T), Vec<String>, Vec<String>), TransformError> {
        let source = source.into();
        // The text `document()` parses again when asked for the source document.
        let source_text = match &source {
            Cow::Borrowed(text) => Some(Cow::Borrowed(*text)),
            Cow::Owned(text) if self.calls_document && base_uri.is_some() => {
                Some(Cow::Owned(text.clone()))
            }
            Cow::Owned(_) => None,
        };
        let document = Document::parse_with_max_depth(source, base_uri, options.max_depth)
            .map_err(|e| match e.limit() {
                Some(limit) => TransformError::limit_exceeded(limit),
                None => TransformError::new("SXXP0003", e.message()),
            })?;
        let mut writer = XmlWriter::new(self.serialization, target);
        let (warnings, messages) = self.execute(
            &document.root(),
            source_text,
            base_uri,
            options,
            &mut writer,
        )?;
        let finished = writer.finish()?;
        Ok((finished, warnings, messages))
    }

    /// Runs the stylesheet on the tree of `root`, its result written to `sink`: the
    /// warnings and the messages. `source_text` and `source_uri` are the text of the
    /// source and its URI, for `document()` asked for the source document by its URI.
    fn execute<'a>(
        &self,
        root: &Node,
        source_text: Option<Cow<'a, str>>,
        source_uri: Option<&'a str>,
        options: &'a TransformOptions<'a>,
        sink: &mut dyn Sink,
    ) -> std::result::Result<(Vec<String>, Vec<String>), TransformError> {
        let runtime = Runtime {
            exe: self,
            source_root: root.clone(),
            source_text: RefCell::new(source_text),
            source_uri,
            options,
            dc: Cell::new(None),
            globals: (0..self.globals.len())
                .map(|_| RefCell::new(GlobalState::Pending))
                .collect(),
            frames: RefCell::new(Vec::new()),
            documents: RefCell::new(Vec::new()),
            warnings: RefCell::new(Vec::new()),
            warned: RefCell::new(HashSet::new()),
            messages: RefCell::new(Vec::new()),
            stashed: RefCell::new(None),
            nesting: Cell::new(0),
            stack: StackLimit::from_here(options.max_stack),
            learners: (0..self.learners).map(|_| Rc::default()).collect(),
            argument_learners: RefCell::new(HashMap::new()),
            rule_name_numbers: RefCell::new(Vec::new()),
            selections: RefCell::new(Vec::new()),
        };
        let timezone = options.implicit_timezone.unwrap_or(0);
        let clock = match options.current_date_time {
            Some(now) => Clock::new(timezone, now)?,
            None => system_clock(timezone)?,
        };
        let dc = DynamicContext::new(&runtime, clock)
            .with_stack_limit(runtime.stack)
            .with_regex_cache(&self.regexes);
        runtime.dc.set(Some(&dc));
        runtime.apply_rule(root, 1, &Size::Known(1), self.default_mode, None, &[], sink)?;
        let warnings = runtime.warnings.take();
        let messages = runtime.messages.take();
        Ok((warnings, messages))
    }
}

/// The system clock, in the implicit timezone.
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn system_clock(timezone: i16) -> Result<Clock> {
    Ok(Clock::system(timezone)?)
}

/// No system clock on this target: the caller must give the current date and time.
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
fn system_clock(_timezone: i16) -> Result<Clock> {
    Err(einvoice_xpath_eval::Error::new(
        ErrorCode::Unsupported,
        "no system clock on this target: give TransformOptions::current_date_time",
    )
    .into())
}

#[derive(Debug)]
enum GlobalState {
    Pending,
    Evaluating,
    Done(Rc<[Item]>),
}

/// A value computed when first read.
struct Thunk<'e> {
    value: &'e Value,
    as_type: Option<&'e SequenceType>,
    code: &'static str,
    context: Context,
    /// The mode and the current template rule, for a sequence constructor.
    mode: usize,
    rule: Option<u32>,
    /// The frame the expression's variables are read from.
    frame: Option<Weak<Frame<'e>>>,
    memo: OnceCell<Rc<[Item]>>,
    /// The learner to tell when the value is computed ([`Eval::Learning`]).
    report: Option<Rc<Learner>>,
    /// The frame, kept alive until the value is computed: for a parameter of a tail call
    /// (see [`Runtime::invoke`]), whose frame ends before the template it calls.
    hold: RefCell<Option<Rc<Frame<'e>>>>,
}

/// Dropping a value kept by the parameters of tail calls drops the frames they keep, and
/// the values not computed in those frames, and so on: a chain as long as the tail calls
/// (a parameter passed down a recursion and never read). It is dropped here in a loop,
/// not by a recursion of drops as deep as the chain.
impl Drop for Thunk<'_> {
    fn drop(&mut self) {
        let mut frames: Vec<Rc<Frame<'_>>> = self.hold.get_mut().take().into_iter().collect();
        while let Some(frame) = frames.pop() {
            // A frame still read elsewhere is not dropped here.
            let Ok(frame) = Rc::try_unwrap(frame) else {
                continue;
            };
            for slot in frame.slots.into_inner() {
                if let Slot::Lazy(thunk) = slot
                    && let Ok(mut thunk) = Rc::try_unwrap(thunk)
                {
                    frames.extend(thunk.hold.get_mut().take());
                }
            }
        }
    }
}

/// The argument of a stylesheet function call, computed when first read, in the frame
/// of the caller.
struct ArgumentThunk<'e> {
    argument: Argument,
    param: &'e Binding,
    function: &'e CompiledFunction,
    frame: Option<Weak<Frame<'e>>>,
    memo: OnceCell<Rc<[Item]>>,
    report: Option<Rc<Learner>>,
}

/// The counters of Saxon's `LearningEvaluator` for one binding ([`Eval::Learning`]).
#[derive(Debug, Default)]
struct Learner {
    /// Evaluations so far.
    count: Cell<u32>,
    /// Evaluations whose value was computed.
    completed: Cell<u32>,
    /// Whether the binding is now evaluated where it is bound.
    eager: Cell<bool>,
}

/// Saxon's `LearningEvaluator`: the evaluations whose completion counts, and how many
/// completed ones make the binding eager.
const LEARNING_LIMIT: u32 = 40;
const EAGER_AFTER: u32 = 20;

impl Learner {
    /// One more evaluation: `None` when the binding is now evaluated where it is bound,
    /// else whether the completion of this evaluation is to be reported
    /// ([`Learner::completed`]). With `every_counts` (a function body), each evaluation
    /// counts as completed.
    fn evaluation(&self, every_counts: bool) -> Option<bool> {
        if self.eager.get() {
            return None;
        }
        let count = self.count.get().saturating_add(1);
        self.count.set(count);
        if every_counts {
            if count >= EAGER_AFTER {
                self.eager.set(true);
            }
            return Some(false);
        }
        Some(count <= LEARNING_LIMIT)
    }

    /// The value of an evaluation was computed (`LearningEvaluator.reportCompletion`):
    /// eager from now on once 20 evaluations have, and all so far.
    fn completed(&self) {
        let completed = self.completed.get().saturating_add(1);
        self.completed.set(completed);
        if completed >= EAGER_AFTER && completed == self.count.get() {
            self.eager.set(true);
        }
    }
}

#[derive(Clone)]
enum Slot<'e> {
    Unset,
    Value(Rc<[Item]>),
    Lazy(Rc<Thunk<'e>>),
    Argument(Rc<ArgumentThunk<'e>>),
}

/// The variables of one invocation.
struct Frame<'e> {
    slots: RefCell<Vec<Slot<'e>>>,
}

impl Frame<'_> {
    fn new(slots: usize) -> Rc<Self> {
        Rc::new(Self {
            slots: RefCell::new(vec![Slot::Unset; slots]),
        })
    }
}

/// The XSLT part of the dynamic context of an instruction.
struct State<'c> {
    context: &'c Context,
    mode: usize,
    /// The rank of the current template rule in `mode`, if any.
    rule: Option<u32>,
}

type Params<'e> = Vec<(&'e QName, Slot<'e>)>;

/// An `xsl:call-template` in tail position, its parameters bound: the template to run in
/// place of the one whose body it ends.
struct TailCall<'e> {
    template: usize,
    params: Params<'e>,
}

struct Runtime<'e, 'a> {
    exe: &'e Executable,
    source_root: Node,
    /// The text of the source document, until `document()` loads it again (absent when
    /// the stylesheet never calls `document()` and the text was given to the tree).
    source_text: RefCell<Option<Cow<'a, str>>>,
    source_uri: Option<&'a str>,
    options: &'a TransformOptions<'a>,
    dc: Cell<Option<&'a DynamicContext<'a>>>,
    globals: Vec<RefCell<GlobalState>>,
    frames: RefCell<Vec<Rc<Frame<'e>>>>,
    /// Documents loaded by `document()`, by absolute URI.
    documents: RefCell<Vec<(String, Node)>>,
    /// The warnings, each distinct one once, in the order first reported.
    warnings: RefCell<Vec<String>>,
    warned: RefCell<HashSet<String>>,
    /// The messages of `xsl:message`.
    messages: RefCell<Vec<String>>,
    /// The error a host callback raised, given back when the evaluator returns it.
    stashed: RefCell<Option<TransformError>>,
    /// The invocations in progress: template rules, built-in template rules, named
    /// templates and stylesheet functions (at most `options.max_nesting`).
    nesting: Cell<u32>,
    /// The stack the transformation may use, from its start (`options.max_stack`): the
    /// invocations and the evaluations of the expressions check it.
    stack: StackLimit,
    /// The counters of the bindings evaluated as Saxon learns ([`Eval::Learning`]).
    learners: Vec<Rc<Learner>>,
    /// The same for the arguments of stylesheet function calls, by call site
    /// ([`Argument::site`]).
    argument_learners: RefCell<HashMap<usize, Rc<Learner>>>,
    /// The number in [`Executable::rule_names`] of each name of the source document, by
    /// [`Node::name_id`], as looked up.
    rule_name_numbers: RefCell<Vec<NameNumber>>,
    /// The storage of the selections of `xsl:apply-templates` done, for the next ones.
    selections: RefCell<Vec<SelectionStorage>>,
}

/// The size of the sequence of nodes a template rule is applied to, the context size of
/// the rule: known, or that of the children of a node, counted when first needed (the
/// built-in rule applies the rules to the children, and most children of a document
/// walked by a compiled Schematron get a rule with an empty body or the built-in rule).
enum Size<'n> {
    Known(usize),
    ChildrenOf(&'n Node, Cell<Option<usize>>),
}

impl Size<'_> {
    fn get(&self) -> usize {
        match self {
            Self::Known(size) => *size,
            Self::ChildrenOf(parent, counted) => {
                if let Some(size) = counted.get() {
                    return size;
                }
                let size = parent.children().count();
                counted.set(Some(size));
                size
            }
        }
    }
}

/// The number of a name of the source document in [`Executable::rule_names`].
#[derive(Debug, Clone, Copy)]
enum NameNumber {
    /// Not looked up yet.
    Unknown,
    /// No mode has rules for the name.
    NoRules,
    Known(u32),
}

/// An invocation counted in [`Runtime::nesting`], until it is dropped.
struct Nested<'n>(&'n Cell<u32>);

impl Drop for Nested<'_> {
    fn drop(&mut self) {
        self.0.set(self.0.get().saturating_sub(1));
    }
}

/// Sets the line of an error.
trait At<T> {
    fn at(self, line: u32) -> Result<T>;
}

impl<T> At<T> for Result<T> {
    fn at(self, line: u32) -> Result<T> {
        self.map_err(|e| e.at(line))
    }
}

/// XPath type names, for messages.
fn type_name(t: &SequenceType) -> String {
    let item = match &t.item {
        ItemType::Atomic(a) => a.name.to_string(),
        ItemType::Kind(k) => format!("{k:?}"),
    };
    let occurrence = match t.occurrence {
        Occurrence::One => "",
        Occurrence::ZeroOrOne => "?",
        Occurrence::ZeroOrMore => "*",
        Occurrence::OneOrMore => "+",
    };
    format!("{item}{occurrence}")
}

const fn atomic_type(kind: AtomicKind) -> AtomicType {
    match kind {
        AtomicKind::String => AtomicType::String,
        AtomicKind::AnyUri => AtomicType::AnyUri,
        AtomicKind::Boolean => AtomicType::Boolean,
        AtomicKind::Integer => AtomicType::Integer,
        AtomicKind::Decimal => AtomicType::Decimal,
        AtomicKind::Date => AtomicType::Date,
    }
}

/// Converts a value to a sequence type with the function conversion rules (XPath 2.0
/// section 3.1.5): atomization, `xs:untypedAtomic` cast to the expected type, URI
/// promotion to `xs:string`, then the item type and cardinality checks.
fn convert(items: Vec<Item>, t: &SequenceType, code: &str) -> Result<Vec<Item>> {
    let count = items.len();
    let cardinality_ok = match t.occurrence {
        Occurrence::One => count == 1,
        Occurrence::ZeroOrOne => count <= 1,
        Occurrence::ZeroOrMore => true,
        Occurrence::OneOrMore => count >= 1,
    };
    if !cardinality_ok {
        return err(
            code,
            format!(
                "Required cardinality of value is {}; supplied value has {count} items",
                type_name(t)
            ),
        );
    }
    match &t.item {
        ItemType::Atomic(a) => {
            let target = atomic_type(a.kind);
            items
                .into_iter()
                .map(|item| {
                    let value = item.atomize();
                    let value = match value {
                        AtomicValue::UntypedAtomic(_) => value.cast(target)?,
                        AtomicValue::AnyUri(s) if target == AtomicType::String => {
                            AtomicValue::String(s)
                        }
                        v if v.is_instance_of(target) => v,
                        v => {
                            return err(
                                code,
                                format!(
                                    "Required item type of value is {}; supplied value has item type {}",
                                    a.name,
                                    v.atomic_type()
                                ),
                            );
                        }
                    };
                    Ok(Item::Atomic(value))
                })
                .collect()
        }
        ItemType::Kind(kind) => {
            let test = pattern::step_test(
                match kind {
                    einvoice_xpath::KindTest::Attribute(_) => {
                        einvoice_xpath::PatternAxis::Attribute
                    }
                    _ => einvoice_xpath::PatternAxis::Child,
                },
                &einvoice_xpath::NodeTest::Kind(kind.clone()),
            );
            for item in &items {
                let ok = match item {
                    Item::Node(n) => {
                        matches!(kind, einvoice_xpath::KindTest::AnyKind) || test.matches(n)
                    }
                    Item::Atomic(_) => false,
                };
                if !ok {
                    return err(
                        code,
                        format!("Required item type of value is {}", type_name(t)),
                    );
                }
            }
            Ok(items)
        }
    }
}

/// Inserts a space where a comment's text would contain `--` or end with `-` (XSLT 2.0
/// section 11.6).
fn comment_text(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut previous_dash = false;
    for c in text.chars() {
        if c == '-' && previous_dash {
            out.push(' ');
        }
        previous_dash = c == '-';
        out.push(c);
    }
    if previous_dash {
        out.push(' ');
    }
    out
}

/// Resolves a URI reference against a base URI (RFC 3986 section 5.2, without the
/// query and fragment cases the rule sets never produce).
fn resolve_uri(reference: &str, base: Option<&str>) -> String {
    let has_scheme = reference.split_once(':').is_some_and(|(scheme, _)| {
        let mut chars = scheme.chars();
        chars.next().is_some_and(|c| c.is_ascii_alphabetic())
            && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
    });
    let Some(base) = base.filter(|_| !has_scheme) else {
        return reference.to_owned();
    };
    if reference.is_empty() {
        return base.to_owned();
    }
    let joined = if reference.starts_with('/') {
        match base.split_once(':') {
            Some((scheme, _)) => format!("{scheme}:{reference}"),
            None => reference.to_owned(),
        }
    } else {
        let directory = base.rfind('/').map_or("", |i| &base[..=i]);
        format!("{directory}{reference}")
    };
    // Remove `.` and `..` segments.
    let (prefix, path) = joined.split_once(':').unwrap_or(("", &joined));
    let mut segments: Vec<&str> = Vec::new();
    for segment in path.split('/') {
        match segment {
            "." => {}
            ".." => {
                if segments.len() > 1 {
                    segments.pop();
                }
            }
            s => segments.push(s),
        }
    }
    let path = segments.join("/");
    if prefix.is_empty() {
        path
    } else {
        format!("{prefix}:{path}")
    }
}

impl<'e, 'a> Runtime<'e, 'a> {
    fn dc(&self) -> Result<&'a DynamicContext<'a>> {
        self.dc
            .get()
            .ok_or_else(|| TransformError::new("INTERNAL", "no dynamic context"))
    }

    /// Converts an evaluator error, giving back the error a host callback raised.
    fn xerr(&self, e: einvoice_xpath_eval::Error) -> TransformError {
        if e.code() == ErrorCode::Host
            && let Some(stashed) = self.stashed.borrow_mut().take()
        {
            return stashed;
        }
        e.into()
    }

    /// Counts one more invocation in progress, until the guard returned is dropped: a
    /// `LIMIT` error beyond `options.max_nesting`, or when the invocations in progress
    /// use more than `options.max_stack` bytes of stack. Every recursion of a
    /// transformation goes through an invocation (a template rule applied to a child, a
    /// named template or a function calling itself), so this bounds the depth of the
    /// call stack.
    fn nested(&self) -> Result<Nested<'_>> {
        let depth = self.nesting.get();
        if depth >= self.options.max_nesting {
            return Err(TransformError::limit_exceeded(LimitExceeded::Nesting {
                max: self.options.max_nesting,
            }));
        }
        self.check_stack()?;
        self.nesting.set(depth + 1);
        Ok(Nested(&self.nesting))
    }

    /// A `LIMIT` error when the invocations in progress use more than `options.max_stack`
    /// bytes of stack. Computing a lazy value checks it: a value may refer to another lazy
    /// one, and so on (a parameter passed down a recursion and read at its end), each
    /// computed inside the one that refers to it. The evaluator checks the same bound
    /// ([`DynamicContext::with_stack_limit`]) at each level of an expression, which may be
    /// evaluated at the deepest invocation.
    fn check_stack(&self) -> Result<()> {
        self.stack.check().map_err(TransformError::limit_exceeded)
    }

    /// Keeps an error for [`Runtime::xerr`] and gives the evaluator a placeholder.
    fn stash(&self, e: TransformError) -> einvoice_xpath_eval::Error {
        let message = e.to_string();
        *self.stashed.borrow_mut() = Some(e);
        einvoice_xpath_eval::Error::new(ErrorCode::Host, message)
    }

    fn evaluate(&self, expr: &CompiledExpr, context: &Context) -> Result<Vec<Item>> {
        expr.evaluate(self.dc()?, context).map_err(|e| self.xerr(e))
    }

    fn test(&self, expr: &CompiledExpr, context: &Context) -> Result<bool> {
        expr.effective_boolean_value(self.dc()?, context)
            .map_err(|e| self.xerr(e))
    }

    /// Runs `f` with `frame` as the frame variables are read from.
    fn in_frame<T>(
        &self,
        frame: Option<Rc<Frame<'e>>>,
        f: impl FnOnce() -> Result<T>,
    ) -> Result<T> {
        let Some(frame) = frame else {
            return f();
        };
        self.frames.borrow_mut().push(frame);
        let result = f();
        self.frames.borrow_mut().pop();
        result
    }

    fn top_frame(&self) -> Option<Rc<Frame<'e>>> {
        self.frames.borrow().last().cloned()
    }

    fn set_slot(&self, slot: usize, value: Slot<'e>) -> Result<()> {
        let frames = self.frames.borrow();
        let Some(frame) = frames.last() else {
            return err("INTERNAL", "no frame for a local variable");
        };
        let mut slots = frame.slots.borrow_mut();
        let Some(s) = slots.get_mut(slot) else {
            return err("INTERNAL", "variable slot out of range");
        };
        *s = value;
        Ok(())
    }

    #[inline(never)]
    fn force(&self, thunk: &Thunk<'e>) -> Result<Rc<[Item]>> {
        if let Some(value) = thunk.memo.get() {
            return Ok(Rc::clone(value));
        }
        self.check_stack()?;
        let frame = match &thunk.frame {
            None => None,
            Some(weak) => Some(
                weak.upgrade()
                    .ok_or_else(|| TransformError::new("INTERNAL", "variable frame is gone"))?,
            ),
        };
        let state = State {
            context: &thunk.context,
            mode: thunk.mode,
            rule: thunk.rule,
        };
        let value = self.in_frame(frame, || {
            self.value(thunk.value, thunk.as_type, &state, thunk.code)
        })?;
        let _ = thunk.memo.set(Rc::clone(&value));
        thunk.hold.borrow_mut().take();
        if let Some(learner) = &thunk.report {
            learner.completed();
        }
        Ok(value)
    }

    #[inline(never)]
    fn force_argument(&self, thunk: &ArgumentThunk<'e>) -> Result<Rc<[Item]>> {
        if let Some(value) = thunk.memo.get() {
            return Ok(Rc::clone(value));
        }
        self.check_stack()?;
        let frame = match &thunk.frame {
            None => None,
            Some(weak) => Some(
                weak.upgrade()
                    .ok_or_else(|| TransformError::new("INTERNAL", "caller frame is gone"))?,
            ),
        };
        let items = self.in_frame(frame, || {
            thunk
                .argument
                .evaluate(self.dc()?)
                .map_err(|e| self.xerr(e))
        })?;
        let value: Rc<[Item]> = Self::argument_value(thunk.function, thunk.param, items)?.into();
        let _ = thunk.memo.set(Rc::clone(&value));
        if let Some(learner) = &thunk.report {
            learner.completed();
        }
        Ok(value)
    }

    /// An argument converted to the type of its parameter.
    fn argument_value(
        function: &CompiledFunction,
        param: &Binding,
        items: Vec<Item>,
    ) -> Result<Vec<Item>> {
        match &param.as_type {
            Some(t) => convert(items, t, "XPTY0004").map_err(|e| {
                TransformError::new(
                    e.code(),
                    format!(
                        "{} (argument ${} of {}())",
                        e.message(),
                        param.name,
                        function.name
                    ),
                )
            }),
            None => Ok(items),
        }
    }

    fn slot_value(&self, slot: &Slot<'e>) -> Result<Rc<[Item]>> {
        match slot {
            Slot::Value(v) => Ok(Rc::clone(v)),
            Slot::Lazy(thunk) => self.force(thunk),
            Slot::Argument(thunk) => self.force_argument(thunk),
            Slot::Unset => err("INTERNAL", "variable read before it is bound"),
        }
    }

    fn variable(&self, id: VariableId) -> Result<Rc<[Item]>> {
        let index = id.0 as usize;
        let globals = self.exe.globals.len();
        if index < globals {
            return self.global(index);
        }
        let slot = {
            let frames = self.frames.borrow();
            let Some(frame) = frames.last() else {
                return err("INTERNAL", "no frame for a local variable");
            };
            let slots = frame.slots.borrow();
            slots
                .get(index - globals)
                .cloned()
                .ok_or_else(|| TransformError::new("INTERNAL", "variable slot out of range"))?
        };
        self.slot_value(&slot)
    }

    fn global(&self, index: usize) -> Result<Rc<[Item]>> {
        let g = &self.exe.globals[index];
        {
            let mut state = self.globals[index].borrow_mut();
            match &*state {
                GlobalState::Done(v) => return Ok(Rc::clone(v)),
                GlobalState::Evaluating => {
                    return err(
                        "XTDE0640",
                        format!("Circular definition of global variable ${}", g.name),
                    )
                    .at(g.line);
                }
                GlobalState::Pending => *state = GlobalState::Evaluating,
            }
        }
        let supplied = if g.is_param {
            self.options
                .parameters
                .iter()
                .find(|(n, _)| n.same_expanded_name(&g.name))
        } else {
            None
        };
        let value = if let Some((_, items)) = supplied {
            match &g.as_type {
                Some(t) => convert(items.clone(), t, "XTTE0590"),
                None => Ok(items.clone()),
            }
            .map(Rc::from)
        } else {
            let context = Context::for_item(Item::Node(self.source_root.clone()));
            let state = State {
                context: &context,
                mode: self.exe.default_mode,
                rule: None,
            };
            let frame = (g.slots > 0).then(|| Frame::new(g.slots));
            let code = if g.is_param { "XTTE0590" } else { "XTTE0570" };
            self.in_frame(frame, || {
                self.value(&g.value, g.as_type.as_ref(), &state, code)
            })
        };
        let value = match value {
            Ok(v) => v,
            Err(e) => {
                *self.globals[index].borrow_mut() = GlobalState::Pending;
                return Err(e.at(g.line));
            }
        };
        *self.globals[index].borrow_mut() = GlobalState::Done(Rc::clone(&value));
        Ok(value)
    }

    /// The value of a binding, computed now.
    fn value(
        &self,
        value: &'e Value,
        as_type: Option<&'e SequenceType>,
        state: &State<'_>,
        code: &str,
    ) -> Result<Rc<[Item]>> {
        let items = match value {
            Value::Select(expr) => self.evaluate(expr, state.context)?,
            Value::Content(body) => {
                if as_type.is_some() {
                    let mut sink = SequenceSink::default();
                    self.exec(body, state, &mut sink)?;
                    sink.items
                } else {
                    let mut sink = TreeSink::new();
                    self.exec(body, state, &mut sink)?;
                    vec![Item::Node(sink.finish()?.root())]
                }
            }
            Value::Absent => {
                if as_type.is_some() {
                    Vec::new()
                } else {
                    vec![Item::Atomic(AtomicValue::String(String::new()))]
                }
            }
        };
        let items = match as_type {
            Some(t) => convert(items, t, code)?,
            None => items,
        };
        Ok(items.into())
    }

    /// A binding's slot: computed now, or later (see [`Eval`]).
    fn bind(
        &self,
        value: &'e Value,
        as_type: Option<&'e SequenceType>,
        eval: Eval,
        state: &State<'_>,
        code: &'static str,
    ) -> Result<Slot<'e>> {
        let report = match eval {
            Eval::Eager => None,
            Eval::Lazy => Some(None),
            Eval::Learning {
                learner,
                every_counts,
            } => self
                .learners
                .get(learner)
                .and_then(|l| l.evaluation(every_counts).map(|r| r.then(|| Rc::clone(l)))),
        };
        let Some(report) = report else {
            return Ok(Slot::Value(self.value(value, as_type, state, code)?));
        };
        Ok(Slot::Lazy(Rc::new(Thunk {
            value,
            as_type,
            code,
            context: state.context.clone(),
            mode: state.mode,
            rule: state.rule,
            frame: self.top_frame().map(|f| Rc::downgrade(&f)),
            memo: OnceCell::new(),
            report,
            hold: RefCell::new(None),
        })))
    }

    fn with_params(&self, params: &'e [WithParam], state: &State<'_>) -> Result<Params<'e>> {
        if params.is_empty() {
            return Ok(Vec::new());
        }
        params
            .iter()
            .map(|p| {
                Ok((
                    &p.name,
                    self.bind(&p.value, p.as_type.as_ref(), p.eval, state, "XTTE0590")?,
                ))
            })
            .collect()
    }

    fn exec(&self, body: &'e [Inst], state: &State<'_>, sink: &mut dyn Sink) -> Result<()> {
        for inst in body {
            self.instruction(inst, state, sink)?;
        }
        Ok(())
    }

    /// Runs one instruction. Each instruction has a function of its own: this one is on
    /// the path of every recursion through the templates (`xsl:apply-templates` in a
    /// template rule, ...), whose stack use per level of the document must stay small,
    /// so that it holds none of their locals, and dispatches only the instructions that
    /// hold others or invoke templates; [`Runtime::other_instruction`] the rest.
    fn instruction(&self, inst: &'e Inst, state: &State<'_>, sink: &mut dyn Sink) -> Result<()> {
        match inst {
            Inst::ApplyTemplates {
                line,
                select,
                mode,
                params,
            } => self.apply_templates(*line, select, mode, params, state, sink),
            Inst::NextMatch { line, params } => self.next_match(*line, params, state, sink),
            Inst::CallTemplate {
                line,
                template,
                params,
            } => self.call_template(*line, *template, params, state, sink),
            Inst::Choose { whens, otherwise } => self.choose(whens, otherwise, state, sink),
            Inst::ForEach { line, select, body } => self.for_each(*line, select, body, state, sink),
            Inst::Element {
                line,
                name,
                namespaces,
                attributes,
                body,
            } => self.element(*line, name, namespaces, attributes, body, state, sink),
            _ => self.other_instruction(inst, state, sink),
        }
    }

    /// The instructions [`Runtime::instruction`] does not dispatch itself.
    #[inline(never)]
    fn other_instruction(
        &self,
        inst: &'e Inst,
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        match inst {
            Inst::Variable(binding) => self.variable_instruction(binding, state),
            Inst::Sequence { line, select } => self.sequence(*line, select, state, sink),
            Inst::ValueOf {
                line,
                select,
                separator,
                body,
            } => self.value_of(
                *line,
                select.as_deref(),
                separator.as_ref(),
                body,
                state,
                sink,
            ),
            Inst::Attribute {
                line,
                name,
                namespace,
                namespaces,
                select,
                body,
            } => self.attribute(
                *line,
                (name, namespace.as_ref()),
                namespaces,
                select.as_deref(),
                body,
                state,
                sink,
            ),
            Inst::CopyOf { line, select } => self.copy_of(*line, select, state, sink),
            Inst::Copy { line, body } => self.copy(*line, body, state, sink),
            Inst::ComputedElement {
                line,
                name,
                namespace,
                namespaces,
                body,
            } => self.computed_element(
                *line,
                name,
                namespace.as_ref(),
                namespaces,
                body,
                state,
                sink,
            ),
            Inst::Namespace {
                line,
                name,
                select,
                body,
            } => self.namespace(*line, name, select.as_deref(), body, state, sink),
            Inst::ProcessingInstruction {
                line,
                name,
                select,
                body,
            } => self.processing_instruction(*line, name, select.as_deref(), body, state, sink),
            Inst::Message {
                line,
                terminate,
                body,
            } => self.message(*line, terminate.as_ref(), body, state),
            Inst::Comment { line, body } => self.comment(*line, body, state, sink),
            Inst::Text(text) => sink.text(text),
            Inst::Number { line, level, count } => {
                self.number_instruction(*line, *level, count.as_ref(), state, sink)
            }
            Inst::ApplyTemplates { .. }
            | Inst::NextMatch { .. }
            | Inst::CallTemplate { .. }
            | Inst::Choose { .. }
            | Inst::ForEach { .. }
            | Inst::Element { .. } => self.instruction(inst, state, sink),
        }
    }

    fn apply_templates(
        &self,
        line: u32,
        select: &'e CompiledExpr,
        mode: &ModeRef,
        params: &'e [WithParam],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let mode = match mode {
            ModeRef::Mode(m) => *m,
            ModeRef::Current => state.mode,
        };
        // A selection of nodes read from the tree (`@*|*`), or any value. Each step in a
        // function of its own: the stack of a level of a recursion holds as little as
        // it can (a debug build keeps every local of a function on its stack).
        let Some(nodes) = self.select_in_place(line, select, state)? else {
            return self.apply_to_value(line, select, mode, params, state, sink);
        };
        let params = self.with_params(params, state).at(line)?;
        let size = Size::Known(nodes.len());
        let mut position = 0;
        while let Some(node) = nodes.get(position) {
            position += 1;
            self.apply_rule(&node, position, &size, mode, None, &params, sink)
                .at(line)?;
        }
        self.selections.borrow_mut().push(nodes.into_storage());
        Ok(())
    }

    /// The nodes `select` selects, read in place from the tree, if it can be.
    fn select_in_place(
        &self,
        line: u32,
        select: &CompiledExpr,
        state: &State<'_>,
    ) -> Result<Option<NodeSelection>> {
        let storage = self.selections.borrow_mut().pop().unwrap_or_default();
        select
            .select_in_place(self.dc()?, state.context, storage)
            .map_err(|e| self.xerr(e))
            .at(line)
    }

    /// `xsl:apply-templates` to the value of `select`.
    fn apply_to_value(
        &self,
        line: u32,
        select: &'e CompiledExpr,
        mode: usize,
        params: &'e [WithParam],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let items = self.evaluate(select, state.context).at(line)?;
        let params = self.with_params(params, state).at(line)?;
        let size = items.len();
        for (i, item) in items.iter().enumerate() {
            match item {
                Item::Node(node) => self
                    .apply_rule(node, i + 1, &Size::Known(size), mode, None, &params, sink)
                    .at(line)?,
                // As Saxon, an XSLT 3.0 processor: no pattern of the subset matches an
                // atomic value, whose built-in rule writes it as text.
                Item::Atomic(_) => sink.text(&item.string_value()).at(line)?,
            }
        }
        Ok(())
    }

    fn next_match(
        &self,
        line: u32,
        params: &'e [WithParam],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let Some(rank) = state.rule else {
            return err("XTDE0560", "xsl:next-match without a current template rule").at(line);
        };
        let Some(focus) = &state.context.focus else {
            return err("XTDE0560", "xsl:next-match without a context item").at(line);
        };
        let Item::Node(node) = &focus.item else {
            return err("XTDE0560", "xsl:next-match on an atomic value").at(line);
        };
        let params = self.with_params(params, state).at(line)?;
        self.apply_rule(
            node,
            focus.position,
            &Size::Known(focus.size),
            state.mode,
            Some(rank),
            &params,
            sink,
        )
        .at(line)
    }

    fn call_template(
        &self,
        line: u32,
        template: usize,
        params: &'e [WithParam],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let params = self.with_params(params, state).at(line)?;
        self.invoke(
            template,
            state.context,
            state.mode,
            state.rule,
            &params,
            sink,
        )
        .at(line)
    }

    fn choose(
        &self,
        whens: &'e [When],
        otherwise: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        for when in whens {
            if self.test(&when.test, state.context).at(when.line)? {
                return self.exec(&when.body, state, sink);
            }
        }
        self.exec(otherwise, state, sink)
    }

    fn for_each(
        &self,
        line: u32,
        select: &'e CompiledExpr,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let items = self.evaluate(select, state.context).at(line)?;
        let size = items.len();
        for (i, item) in items.into_iter().enumerate() {
            let context = Context::at(item, i + 1, size);
            let inner = State {
                context: &context,
                mode: state.mode,
                rule: None,
            };
            self.exec(body, &inner, sink)?;
        }
        Ok(())
    }

    #[inline(never)]
    fn sequence(
        &self,
        line: u32,
        select: &'e CompiledExpr,
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        for item in self.evaluate(select, state.context).at(line)? {
            sink.item(&item).at(line)?;
        }
        Ok(())
    }

    #[inline(never)]
    fn value_of(
        &self,
        line: u32,
        select: Option<&'e CompiledExpr>,
        separator: Option<&'e CompiledAvt>,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let separator = match separator {
            Some(avt) => Some(self.avt(avt, state).at(line)?),
            None => None,
        };
        let mut parts = TextSink::new();
        let text = if let Some(expr) = select {
            for item in self.evaluate(expr, state.context).at(line)? {
                parts.item(&item)?;
            }
            parts.finish(separator.as_deref().unwrap_or(" "))
        } else {
            self.exec(body, state, &mut parts)?;
            parts.finish(separator.as_deref().unwrap_or(""))
        };
        sink.text(&text).at(line)
    }

    #[inline(never)]
    #[expect(
        clippy::too_many_arguments,
        reason = "the parts of the instruction, its dynamic context and its sink"
    )]
    fn attribute(
        &self,
        line: u32,
        (name, namespace): (&'e CompiledAvt, Option<&'e CompiledAvt>),
        namespaces: &[NamespaceBinding],
        select: Option<&'e CompiledExpr>,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let name = self.avt(name, state).at(line)?;
        let namespace = match namespace {
            Some(avt) => Some(self.avt(avt, state).at(line)?),
            None => None,
        };
        let name = attribute_name(&name, namespace.as_deref(), namespaces).at(line)?;
        let value = self.simple_content(select, body, state).at(line)?;
        sink.attribute(&name, &value).at(line)
    }

    #[inline(never)]
    fn comment(
        &self,
        line: u32,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let mut parts = TextSink::new();
        self.exec(body, state, &mut parts)?;
        sink.comment(&comment_text(&parts.finish(""))).at(line)
    }

    #[inline(never)]
    fn number_instruction(
        &self,
        line: u32,
        level: NumberLevel,
        count: Option<&'e CountPattern>,
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let text = self.number(line, level, count, state).at(line)?;
        sink.text(&text).at(line)
    }

    #[expect(
        clippy::too_many_arguments,
        reason = "the parts of the instruction, its dynamic context and its sink"
    )]
    fn element(
        &self,
        line: u32,
        name: &QName,
        namespaces: &Arc<[NamespaceBinding]>,
        attributes: &'e [(Arc<QName>, CompiledAvt)],
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        sink.start_element(name, namespaces).at(line)?;
        for (attribute, value) in attributes {
            self.attribute_value(line, attribute, value, state, sink)?;
        }
        self.exec(body, state, sink)?;
        sink.end_element().at(line)
    }

    /// An attribute of a literal result element.
    #[inline(never)]
    fn attribute_value(
        &self,
        line: u32,
        name: &QName,
        value: &'e CompiledAvt,
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let value = self.avt(value, state).at(line)?;
        sink.attribute(name, &value).at(line)
    }

    /// `xsl:copy`: a copy of the context item, the body evaluated for a document node or
    /// an element only, which keeps its namespace nodes.
    #[inline(never)]
    fn copy(
        &self,
        line: u32,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let Some(focus) = &state.context.focus else {
            return err("XTTE0945", "xsl:copy without a context item").at(line);
        };
        let Item::Node(node) = &focus.item else {
            return sink.item(&focus.item).at(line);
        };
        match node.kind() {
            NodeKind::Document => {
                sink.start_document().at(line)?;
                self.exec(body, state, sink)?;
                sink.end_document().at(line)
            }
            NodeKind::Element => {
                let Some(name) = node.name() else {
                    return err("INTERNAL", "element without name");
                };
                let namespaces: Arc<[NamespaceBinding]> = node.namespaces_in_scope().into();
                sink.start_copied_element(name, &namespaces).at(line)?;
                self.exec(body, state, sink)?;
                sink.end_element().at(line)
            }
            // Of the other kinds, a copy of the node alone: the body is not evaluated.
            _ => copy_node(sink, node).at(line),
        }
    }

    /// `xsl:copy-of`: deep copies of the nodes selected, the atomic values as they are.
    #[inline(never)]
    fn copy_of(
        &self,
        line: u32,
        select: &'e CompiledExpr,
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        for item in self.evaluate(select, state.context).at(line)? {
            match &item {
                Item::Node(node) => copy_node(sink, node).at(line)?,
                Item::Atomic(_) => sink.item(&item).at(line)?,
            }
        }
        Ok(())
    }

    /// `xsl:element`: an element named by the computed name, in the namespace it names,
    /// without namespace nodes of its own: the binding its name needs is added when its
    /// start tag is complete (namespace fixup, after those of `xsl:namespace`).
    #[expect(
        clippy::too_many_arguments,
        reason = "the parts of the instruction, its dynamic context and its sink"
    )]
    #[inline(never)]
    fn computed_element(
        &self,
        line: u32,
        name: &'e CompiledAvt,
        namespace: Option<&'e CompiledAvt>,
        namespaces: &[NamespaceBinding],
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let name = self.element_name(line, name, namespace, namespaces, state)?;
        sink.start_element(&name, &Arc::from(Vec::new())).at(line)?;
        self.exec(body, state, sink)?;
        sink.end_element().at(line)
    }

    /// The name of `xsl:element`.
    #[inline(never)]
    fn element_name(
        &self,
        line: u32,
        name: &'e CompiledAvt,
        namespace: Option<&'e CompiledAvt>,
        namespaces: &[NamespaceBinding],
        state: &State<'_>,
    ) -> Result<QName> {
        let text = self.avt(name, state).at(line)?;
        let namespace = match namespace {
            Some(avt) => Some(self.avt(avt, state).at(line)?),
            None => None,
        };
        element_name(&text, namespace.as_deref(), namespaces).at(line)
    }

    /// `xsl:namespace`: a namespace node on the element being constructed (XSLT 2.0
    /// section 11.7).
    #[inline(never)]
    fn namespace(
        &self,
        line: u32,
        name: &'e CompiledAvt,
        select: Option<&'e CompiledExpr>,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let prefix = self.avt(name, state).at(line)?;
        let prefix = prefix.trim_matches(einvoice_xpath::is_xml_whitespace);
        let uri = self.simple_content(select, body, state).at(line)?;
        if !(prefix.is_empty() || einvoice_xpath::is_ncname(prefix)) || prefix == "xmlns" {
            return err(
                "XTDE0920",
                format!("invalid namespace prefix {prefix:?} in xsl:namespace"),
            )
            .at(line);
        }
        let xml = einvoice_xpath::XML_NAMESPACE;
        if (prefix == "xml") != (uri == xml) {
            return err(
                "XTDE0925",
                "the xml prefix and the XML namespace are bound to each other only",
            )
            .at(line);
        }
        if uri.is_empty() {
            return err("XTDE0930", "xsl:namespace with an empty namespace URI").at(line);
        }
        if prefix == "xml" {
            return Ok(());
        }
        sink.namespace((!prefix.is_empty()).then_some(prefix), &uri)
            .at(line)
    }

    /// `xsl:processing-instruction` (XSLT 2.0 section 11.6): the target, an `NCName` other
    /// than `xml` in any case; the value without leading whitespace, `?>` written `? >`.
    #[inline(never)]
    fn processing_instruction(
        &self,
        line: u32,
        name: &'e CompiledAvt,
        select: Option<&'e CompiledExpr>,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let target = self.avt(name, state).at(line)?;
        let target = target.trim_matches(einvoice_xpath::is_xml_whitespace);
        if !einvoice_xpath::is_ncname(target) || target.eq_ignore_ascii_case("xml") {
            return err(
                "XTDE0890",
                format!("invalid processing instruction name {target:?}"),
            )
            .at(line);
        }
        let value = self.simple_content(select, body, state).at(line)?;
        let value = value
            .trim_start_matches(einvoice_xpath::is_xml_whitespace)
            .replace("?>", "? >");
        sink.processing_instruction(target, &value).at(line)
    }

    /// `xsl:message`: its content, a document node, written as Saxon writes messages
    /// (see [`Transformation::messages`]); with `terminate` `yes`, the transformation ends
    /// with the error `XTMM9000`.
    #[inline(never)]
    fn message(
        &self,
        line: u32,
        terminate: Option<&'e CompiledAvt>,
        body: &'e [Inst],
        state: &State<'_>,
    ) -> Result<()> {
        let terminate = match terminate {
            None => false,
            Some(avt) => {
                let value = self.avt(avt, state).at(line)?;
                match value.trim_matches(einvoice_xpath::is_xml_whitespace) {
                    "yes" | "true" | "1" => true,
                    "no" | "false" | "0" => false,
                    other => {
                        return err(
                            "XTDE0030",
                            format!("invalid value {other:?} of xsl:message/@terminate"),
                        )
                        .at(line);
                    }
                }
            }
        };
        let mut content = TreeSink::new();
        self.exec(body, state, &mut content)?;
        let document = content.finish()?;
        let root = document.root();
        let text = if root.children().next().is_some() {
            let settings = Serialization {
                indent: false,
                omit_xml_declaration: true,
                standalone: None,
            };
            let text = serialize(&root, settings, true).at(line)?;
            self.messages.borrow_mut().push(text.clone());
            text
        } else {
            String::new()
        };
        if terminate {
            return err("XTMM9000", text).at(line);
        }
        Ok(())
    }

    fn variable_instruction(&self, binding: &'e Binding, state: &State<'_>) -> Result<()> {
        if !binding.used {
            return Ok(());
        }
        let slot = self
            .bind(
                &binding.value,
                binding.as_type.as_ref(),
                binding.eval,
                state,
                "XTTE0570",
            )
            .at(binding.line)?;
        self.set_slot(binding.slot, slot).at(binding.line)
    }

    /// The value of `xsl:attribute`: `select` joined with spaces, or the content.
    fn simple_content(
        &self,
        select: Option<&'e CompiledExpr>,
        body: &'e [Inst],
        state: &State<'_>,
    ) -> Result<String> {
        let mut parts = TextSink::new();
        if let Some(expr) = select {
            for item in self.evaluate(expr, state.context)? {
                parts.item(&item)?;
            }
            Ok(parts.finish(" "))
        } else {
            self.exec(body, state, &mut parts)?;
            Ok(parts.finish(""))
        }
    }

    fn avt(&self, avt: &'e CompiledAvt, state: &State<'_>) -> Result<String> {
        if let [AvtItem::Text(text)] = avt.0.as_slice() {
            return Ok(text.to_string());
        }
        let mut out = String::new();
        for item in &avt.0 {
            match item {
                AvtItem::Text(text) => out.push_str(text),
                AvtItem::Expr(expr) => {
                    for (i, value) in self.evaluate(expr, state.context)?.iter().enumerate() {
                        if i > 0 {
                            out.push(' ');
                        }
                        out.push_str(&value.atomize().to_string());
                    }
                }
            }
        }
        Ok(out)
    }

    /// Finds the best rule for `node` in `mode` (ranked after `after`, for
    /// `xsl:next-match`) and runs it, or the built-in rule.
    #[expect(
        clippy::too_many_arguments,
        reason = "the focus, the mode, the rule and the parameters of the invocation"
    )]
    fn apply_rule(
        &self,
        node: &Node,
        position: usize,
        size: &Size<'_>,
        mode: usize,
        after: Option<u32>,
        params: &[(&'e QName, Slot<'e>)],
        sink: &mut dyn Sink,
    ) -> Result<()> {
        match self.find_rule(mode, node, after)? {
            Some(rank) => {
                let index = self.exe.modes[mode].rules[rank as usize].template;
                let template = &self.exe.templates[index];
                if template.body.is_empty() && template.params.is_empty() {
                    // Nothing to bind or run: only the invocation counts, as in `invoke`.
                    let _nested = self.nested().at(template.line)?;
                    return Ok(());
                }
                let context = Context::at(Item::Node(node.clone()), position, size.get());
                self.invoke(index, &context, mode, Some(rank), params, sink)
            }
            None => self.builtin(node, mode, params, sink),
        }
    }

    /// The chain of rules of `mode` for the name of `node`, an element or (`attribute`)
    /// an attribute. The names of the source document are looked up once per
    /// transformation ([`Executable::rule_names`]), then by number in each mode.
    fn named_chain(&self, mode: usize, node: &Node, attribute: bool) -> &'e [u32] {
        let exe: &'e Executable = self.exe;
        let compiled = &exe.modes[mode];
        let id = node
            .name_id()
            .filter(|_| Rc::ptr_eq(node.document(), self.source_root.document()));
        let Some(id) = id else {
            return compiled.named_chain(attribute, node.name());
        };
        let number = {
            let mut numbers = self.rule_name_numbers.borrow_mut();
            if numbers.is_empty() {
                numbers.resize(
                    self.source_root.document().name_count(),
                    NameNumber::Unknown,
                );
            }
            match numbers.get_mut(id as usize) {
                Some(NameNumber::Known(number)) => *number,
                Some(NameNumber::NoRules) => return &[],
                Some(entry) => {
                    let found = node
                        .name()
                        .and_then(|name| rule_name_number(&exe.rule_names, name));
                    *entry = found.map_or(NameNumber::NoRules, NameNumber::Known);
                    match found {
                        Some(number) => number,
                        None => return &[],
                    }
                }
                None => return compiled.named_chain(attribute, node.name()),
            }
        };
        compiled.named_by_number(attribute, number)
    }

    /// Whether `node` matches the pattern of a rule. A dynamic error while matching is
    /// recovered as Saxon recovers it (see [`Runtime::pattern_error`]).
    fn rule_matches(&self, rank: u32, mode: usize, node: &Node) -> Result<bool> {
        let rule = &self.exe.modes[mode].rules[rank as usize];
        if rule.certain & (1 << kind_index(node.kind())) != 0 {
            // Every node of its kind the rule is tried on matches it.
            return Ok(true);
        }
        let template = &self.exe.templates[rule.template];
        let matcher = Matcher { dc: self.dc()? };
        // A union pattern with an explicit priority is one rule: its alternatives in
        // order, the first error ending the match.
        let (alternatives, text) = match rule.alternative {
            Some(index) => (
                template.pattern.get(index..=index).unwrap_or_default(),
                template
                    .alternative_texts
                    .get(index)
                    .map_or("", String::as_str),
            ),
            None => (template.pattern.as_slice(), template.pattern_text.as_str()),
        };
        let outcome = alternatives
            .iter()
            .try_fold(false, |found, a| Ok(found || matcher.matches(a, node)?));
        outcome.or_else(|e| self.pattern_error(e, template.line, text))
    }

    /// A dynamic error raised while matching a pattern (of a template rule, or the
    /// `count` pattern of `xsl:number`), as Saxon-HE 12.10 handles it
    /// (`Pattern.matchesItem`): the node does not match, and the error is reported as
    /// a warning, whatever its code, except a circularity (`XTDE0640`), which ends the
    /// transformation. A global variable whose evaluation failed is not kept in error:
    /// the next reference evaluates it again (`GlobalVariable.actuallyEvaluate`), in a
    /// pattern or elsewhere. The refusals, limits and bugs of this crate (`UNSUPPORTED`,
    /// `LIMIT`, `INTERNAL`) are never recovered: Saxon would not have raised them.
    fn pattern_error(
        &self,
        e: einvoice_xpath_eval::Error,
        line: u32,
        pattern: &str,
    ) -> Result<bool> {
        let e = self.xerr(e);
        if matches!(
            e.code(),
            "XTDE0640" | "UNSUPPORTED" | LIMIT | "INTERNAL" | "HOST"
        ) {
            return Err(e.at(line));
        }
        self.warn(format!(
            "line {line}: {}: An error occurred matching pattern {{{pattern}}}: {}",
            e.code(),
            e.message()
        ));
        Ok(false)
    }

    /// Reports a warning, once: Saxon's standard error reporter prints each distinct
    /// warning once.
    fn warn(&self, message: String) {
        if self.warned.borrow_mut().insert(message.clone()) {
            self.warnings.borrow_mut().push(message);
        }
    }

    /// The rule for `node` in `mode`, chosen exactly as Saxon-HE 12.10 chooses it
    /// (`SimpleMode.getRule` and `searchRuleChain`): the chains of the node's name, of
    /// its kind, then the generic chain, each in rank order; a rule of higher priority
    /// replaces the best so far; on a tie the later declared rule wins and the ambiguity
    /// is reported as an XTDE0540 warning. For `xsl:next-match` (`after` is the current
    /// rule) only the rules ranked below the current one are candidates (lower
    /// priority, or same priority and declared before it, or an earlier part of the
    /// same template), and a tie between two parts of one template goes to the part
    /// found last.
    fn find_rule(&self, mode: usize, node: &Node, after: Option<u32>) -> Result<Option<u32>> {
        let compiled = &self.exe.modes[mode];
        let rules = &compiled.rules;
        let kind = node.kind();
        let named = match kind {
            NodeKind::Element => self.named_chain(mode, node, false),
            NodeKind::Attribute => self.named_chain(mode, node, true),
            _ => &[],
        };
        if after.is_none() && named.is_empty() {
            match compiled.plan(kind) {
                Plan::Builtin => return Ok(None),
                Plan::Rule(rank) => return Ok(Some(rank)),
                Plan::Search => {}
            }
        }
        let below_current = |rank: u32| -> bool {
            let Some(current) = after else {
                return true;
            };
            let (r, c) = (&rules[rank as usize], &rules[current as usize]);
            match r.compare_rank(c) {
                std::cmp::Ordering::Less => true,
                std::cmp::Ordering::Greater => false,
                std::cmp::Ordering::Equal => {
                    r.sequence < c.sequence || (r.sequence == c.sequence && r.part < c.part)
                }
            }
        };
        let mut best: Option<u32> = None;
        for chain in compiled.chains_with(kind, named) {
            for &rank in chain {
                if !below_current(rank) {
                    continue;
                }
                let Some(chosen) = best else {
                    if self.rule_matches(rank, mode, node)? {
                        best = Some(rank);
                    }
                    continue;
                };
                let (head, b) = (&rules[rank as usize], &rules[chosen as usize]);
                match head.compare_rank(b) {
                    std::cmp::Ordering::Less => break,
                    std::cmp::Ordering::Greater => {
                        if self.rule_matches(rank, mode, node)? {
                            best = Some(rank);
                        }
                    }
                    std::cmp::Ordering::Equal => {
                        if !self.rule_matches(rank, mode, node)? {
                            continue;
                        }
                        if head.template != b.template {
                            self.ambiguity(node, compiled, b, head);
                        }
                        // The later declared rule; between two parts of one template,
                        // the higher part, or for xsl:next-match the one found last.
                        let keep = b.sequence > head.sequence
                            || (b.sequence == head.sequence
                                && after.is_none()
                                && b.part > head.part);
                        best = Some(if keep { chosen } else { rank });
                        break;
                    }
                }
            }
        }
        Ok(best)
    }

    /// Reports a tie between `a`, the best rule so far, and `b`, as Saxon's
    /// `reportAmbiguity` does, naming them in that order.
    fn ambiguity(
        &self,
        node: &Node,
        mode: &crate::prepare::CompiledMode,
        a: &crate::prepare::Rule,
        b: &crate::prepare::Rule,
    ) {
        let (ta, tb) = (
            &self.exe.templates[a.template],
            &self.exe.templates[b.template],
        );
        let later = if a.sequence > b.sequence { ta } else { tb };
        self.warn(format!(
            "XTDE0540: Ambiguous rule match for {} in mode {}: matches the template rules on line {} and line {}; the one on line {} is used",
            describe(node),
            mode.name,
            ta.line,
            tb.line,
            later.line
        ));
    }

    /// The built-in template rule for `node`. For a document node or an element, it
    /// applies the template rules to the children, one level of recursion counted as an
    /// invocation ([`Runtime::nested`]).
    fn builtin(
        &self,
        node: &Node,
        mode: usize,
        params: &[(&'e QName, Slot<'e>)],
        sink: &mut dyn Sink,
    ) -> Result<()> {
        match node.kind() {
            NodeKind::Document | NodeKind::Element => {
                let _nested = self.nested()?;
                // The children are counted only if a template rule applied to one runs.
                let size = Size::ChildrenOf(node, Cell::new(None));
                for (i, child) in node.children().enumerate() {
                    self.apply_rule(&child, i + 1, &size, mode, None, params, sink)?;
                }
                Ok(())
            }
            NodeKind::Text | NodeKind::Attribute => sink.text(&node.string_value()),
            NodeKind::Comment | NodeKind::ProcessingInstruction => Ok(()),
        }
    }

    /// Runs a template: its parameters bound from `params` or their default values,
    /// then its body. Counted as an invocation ([`Runtime::nested`]).
    ///
    /// An `xsl:call-template` in tail position of the body (its last instruction, or the
    /// last of the branch of an `xsl:choose` or `xsl:if` that is) runs as Saxon runs it,
    /// in the same invocation rather than one more inside it (Saxon's tail calls): a
    /// template that recurses so, over each character of a string for instance, needs no
    /// stack in proportion to its recursion. The frame of an invocation replaced is kept
    /// by the parameters it passes that are not computed yet, until they are
    /// ([`Runtime::exec_tail`]). At most `max_tail_calls` in a row.
    fn invoke(
        &self,
        index: usize,
        context: &Context,
        mode: usize,
        rule: Option<u32>,
        params: &[(&'e QName, Slot<'e>)],
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let template = &self.exe.templates[index];
        if template.tail_calls {
            return self.invoke_with_tail_calls(index, context, mode, rule, params, sink);
        }
        let _nested = self.nested().at(template.line)?;
        let frame = (template.slots > 0).then(|| Frame::new(template.slots));
        let state = State {
            context,
            mode,
            rule,
        };
        self.in_frame(frame, || {
            self.bind_params(template, params, &state)?;
            self.exec(&template.body, &state, sink)
        })
        .at(template.line)
    }

    /// [`Runtime::invoke`] of a template with an `xsl:call-template` in tail position
    /// (see [`CompiledTemplate::tail_calls`](crate::prepare::CompiledTemplate)): the
    /// templates it calls so run in a loop, in place of the one that calls them. Out of
    /// [`Runtime::invoke`], whose stack use is part of every level of recursion through
    /// the templates.
    #[inline(never)]
    fn invoke_with_tail_calls(
        &self,
        index: usize,
        context: &Context,
        mode: usize,
        rule: Option<u32>,
        params: &[(&'e QName, Slot<'e>)],
        sink: &mut dyn Sink,
    ) -> Result<()> {
        let _nested = self.nested().at(self.exe.templates[index].line)?;
        let state = State {
            context,
            mode,
            rule,
        };
        let mut current = index;
        let mut passed: Option<Params<'e>> = None;
        let mut tail_calls = 0_u32;
        loop {
            let template = &self.exe.templates[current];
            let frame = (template.slots > 0).then(|| Frame::new(template.slots));
            let params = passed.as_deref().unwrap_or(params);
            let tail = self
                .in_frame(frame, || {
                    self.bind_params(template, params, &state)?;
                    self.exec_tail(&template.body, &state, sink)
                })
                .at(template.line)?;
            let Some(tail) = tail else {
                return Ok(());
            };
            tail_calls += 1;
            if tail_calls > self.options.max_tail_calls {
                return Err(TransformError::limit_exceeded(LimitExceeded::TailCalls {
                    max: self.options.max_tail_calls,
                })
                .at(template.line));
            }
            current = tail.template;
            passed = Some(tail.params);
        }
    }

    /// Runs a sequence constructor in tail position of a template body: its
    /// `xsl:call-template` in tail position, if any, is not run but returned, its
    /// parameters bound, for [`Runtime::invoke`] to run in place of the template.
    fn exec_tail(
        &self,
        body: &'e [Inst],
        state: &State<'_>,
        sink: &mut dyn Sink,
    ) -> Result<Option<Box<TailCall<'e>>>> {
        let Some((last, rest)) = body.split_last() else {
            return Ok(None);
        };
        self.exec(rest, state, sink)?;
        match last {
            Inst::CallTemplate {
                line,
                template,
                params,
            } => {
                let params = self.with_params(params, state).at(*line)?;
                // The values not computed yet read this frame, which ends now.
                let frame = self.top_frame();
                for (_, slot) in &params {
                    if let Slot::Lazy(thunk) = slot {
                        thunk.hold.borrow_mut().clone_from(&frame);
                    }
                }
                Ok(Some(Box::new(TailCall {
                    template: *template,
                    params,
                })))
            }
            Inst::Choose { whens, otherwise } => {
                for when in whens {
                    if self.test(&when.test, state.context).at(when.line)? {
                        return self.exec_tail(&when.body, state, sink);
                    }
                }
                self.exec_tail(otherwise, state, sink)
            }
            other => {
                self.instruction(other, state, sink)?;
                Ok(None)
            }
        }
    }

    /// Binds the parameters of a template being invoked, in its frame: from `params`, or
    /// their default values. Out of [`Runtime::invoke`], whose stack use is part of every
    /// level of recursion through the templates.
    #[inline(never)]
    fn bind_params(
        &self,
        template: &'e CompiledTemplate,
        params: &[(&'e QName, Slot<'e>)],
        state: &State<'_>,
    ) -> Result<()> {
        for param in &template.params {
            let passed = params
                .iter()
                .find(|(name, _)| name.same_expanded_name(&param.name));
            let slot = match (passed, &param.as_type) {
                (Some((_, slot)), None) => slot.clone(),
                (Some((_, slot)), Some(t)) => {
                    let items = self.slot_value(slot)?.to_vec();
                    Slot::Value(convert(items, t, "XTTE0590").at(param.line)?.into())
                }
                (None, as_type) => self
                    .bind(
                        &param.value,
                        as_type.as_ref(),
                        param.eval,
                        state,
                        "XTTE0590",
                    )
                    .at(param.line)?,
            };
            self.set_slot(param.slot, slot)?;
        }
        Ok(())
    }

    /// Calls a stylesheet function. Counted as an invocation ([`Runtime::nested`]). Its
    /// arguments are bound as Saxon binds them: those evaluated before the call as they
    /// are, the others lazily, with Saxon's learning per call site.
    fn call(&self, id: FunctionId, args: Vec<Argument>) -> Result<Vec<Item>> {
        let function = &self.exe.functions[id.0 as usize];
        let _nested = self.nested().at(function.line)?;
        let frame = self.bind_arguments(function, args)?;
        self.function_body(function, frame)
    }

    /// The frame of a function call, its parameters bound to the arguments. Out of
    /// [`Runtime::call`], whose stack use is part of every level of a recursion through
    /// functions.
    #[inline(never)]
    fn bind_arguments(
        &self,
        function: &'e CompiledFunction,
        args: Vec<Argument>,
    ) -> Result<Rc<Frame<'e>>> {
        let caller = self.top_frame().map(|f| Rc::downgrade(&f));
        let frame = Frame::new(function.slots);
        for (param, argument) in function.params.iter().zip(args) {
            let learner = argument.site().map(|site| {
                Rc::clone(self.argument_learners.borrow_mut().entry(site).or_default())
            });
            let report = match &learner {
                None => None,
                Some(l) => l.evaluation(false),
            };
            let slot = match report {
                None => {
                    let items = argument.evaluate(self.dc()?).map_err(|e| self.xerr(e))?;
                    Slot::Value(Self::argument_value(function, param, items)?.into())
                }
                Some(report) => Slot::Argument(Rc::new(ArgumentThunk {
                    argument,
                    param,
                    function,
                    frame: caller.clone(),
                    memo: OnceCell::new(),
                    report: if report { learner } else { None },
                })),
            };
            frame.slots.borrow_mut()[param.slot] = slot;
        }
        Ok(frame)
    }

    /// Runs the body of a function in its frame.
    #[inline(never)]
    fn function_body(
        &self,
        function: &'e CompiledFunction,
        frame: Rc<Frame<'e>>,
    ) -> Result<Vec<Item>> {
        // A stylesheet function has no focus and no current template rule.
        let context = Context::new();
        let state = State {
            context: &context,
            mode: self.exe.default_mode,
            rule: None,
        };
        let mut sink = SequenceSink::default();
        self.in_frame(Some(frame), || self.exec(&function.body, &state, &mut sink))
            .at(function.line)?;
        match &function.as_type {
            Some(t) => convert(sink.items, t, "XTTE0780").at(function.line),
            None => Ok(sink.items),
        }
    }

    fn number(
        &self,
        line: u32,
        level: NumberLevel,
        count: Option<&'e CountPattern>,
        state: &State<'_>,
    ) -> Result<String> {
        let Some(Item::Node(node)) = state.context.focus.as_ref().map(|f| &f.item) else {
            return err("XTTE0990", "xsl:number needs a node as context item");
        };
        let matcher = Matcher { dc: self.dc()? };
        let counted = |n: &Node| -> Result<bool> {
            match count {
                // As a template rule's pattern, an error is recovered (Saxon's
                // `Navigator` matches the count pattern with `matchesItem`).
                Some(pattern) => pattern
                    .alternatives
                    .iter()
                    .try_fold(false, |found, a| Ok(found || matcher.matches(a, n)?))
                    .or_else(|e| self.pattern_error(e, line, &pattern.text)),
                None => Ok(n.kind() == node.kind()
                    && match (n.name(), node.name()) {
                        (Some(a), Some(b)) => a.same_expanded_name(b),
                        (None, None) => true,
                        _ => false,
                    }),
            }
        };
        let position = |n: &Node| -> Result<usize> {
            let mut p = 1;
            for sibling in n.axis(NodeAxis::PrecedingSibling) {
                if counted(&sibling)? {
                    p += 1;
                }
            }
            Ok(p)
        };
        let mut numbers = Vec::new();
        for ancestor in node.axis(NodeAxis::AncestorOrSelf) {
            if counted(&ancestor)? {
                numbers.push(position(&ancestor)?);
                if level == NumberLevel::Single {
                    break;
                }
            }
        }
        numbers.reverse();
        Ok(numbers
            .iter()
            .map(usize::to_string)
            .collect::<Vec<_>>()
            .join("."))
    }

    fn load_document(&self, uri: &str, base: Option<&str>) -> Result<Node> {
        let absolute = resolve_uri(uri, base.or(self.exe.base_uri.as_deref()));
        if let Some((_, node)) = self.documents.borrow().iter().find(|(u, _)| *u == absolute) {
            return Ok(node.clone());
        }
        // As Saxon: the source document, asked for by its base URI, is parsed again and
        // is a different tree. Loaded once: the tree is kept under its URI above.
        let is_source = self.source_uri == Some(absolute.as_str());
        let text: Cow<'_, str> = if is_source {
            self.source_text.take().ok_or_else(|| {
                TransformError::new(
                    "INTERNAL",
                    "document() loads the source document, whose text was not kept",
                )
            })?
        } else {
            // The resolver's text is given to the tree, not copied.
            Cow::Owned(
                self.options
                    .resolver
                    .and_then(|r| r.resolve(&absolute))
                    .ok_or_else(|| {
                        TransformError::new(
                            "FODC0002",
                            format!("cannot retrieve the document at {absolute}"),
                        )
                    })?,
            )
        };
        // A loaded document has a document URI, as one in Saxon's document pool. A text
        // from the resolver is parsed once for every transformation of the stylesheet.
        let parsed = match text {
            Cow::Owned(text) if !is_source => {
                self.exe
                    .loaded
                    .parse_loaded(text, &absolute, self.options.max_depth)
            }
            text => Document::parse_loaded_with_max_depth(text, &absolute, self.options.max_depth),
        };
        let node = parsed
            .map_err(|e| loaded_document_error(&absolute, &e))?
            .root();
        self.documents.borrow_mut().push((absolute, node.clone()));
        Ok(node)
    }
}

/// A node, for messages: its path from the root, written as Saxon-HE 12.10 writes it
/// (`Navigator.getPath`): `/r/x[2]/@a`, `/r/text()[1]`, the outermost element without
/// position. Saxon's error reporter prints each distinct warning once, so the path tells
/// apart exactly the nodes Saxon's tells apart.
fn describe(node: &Node) -> String {
    let name = |n: &Node| n.name().map(ToString::to_string).unwrap_or_default();
    // The position among the preceding siblings of the same kind and, for a node with a
    // name, the same name (`Navigator.getNumberSimple`).
    let number = |n: &Node| {
        1 + n
            .axis(NodeAxis::PrecedingSibling)
            .filter(|s| {
                s.kind() == n.kind()
                    && match (s.name(), n.name()) {
                        (Some(a), Some(b)) => a.same_expanded_name(b),
                        (None, None) => true,
                        _ => false,
                    }
            })
            .count()
    };
    // The path of each node from the root down, from its parent's: a loop rather than a
    // recursion over the ancestors, whose number only the document bounds.
    let lineage: Vec<Node> = node.axis(NodeAxis::AncestorOrSelf).collect();
    let mut path = String::new();
    let mut parent_kind: Option<NodeKind> = None;
    for n in lineage.iter().rev() {
        let parent_path = std::mem::take(&mut path);
        // A step under the parent, which is "/" for the document node.
        let under_parent = |step: String| {
            format!(
                "{}/{step}",
                if parent_path == "/" {
                    ""
                } else {
                    parent_path.as_str()
                }
            )
        };
        path = match n.kind() {
            NodeKind::Document => "/".to_owned(),
            NodeKind::Element => match parent_kind {
                None => name(n),
                Some(NodeKind::Document) => format!("/{}", name(n)),
                Some(_) => format!("{parent_path}/{}[{}]", name(n), number(n)),
            },
            NodeKind::Attribute => format!("{parent_path}/@{}", name(n)),
            NodeKind::Text => under_parent(format!("text()[{}]", number(n))),
            NodeKind::Comment => under_parent(format!("comment()[{}]", number(n))),
            NodeKind::ProcessingInstruction => {
                under_parent(format!("processing-instruction()[{}]", number(n)))
            }
        };
        parent_kind = Some(n.kind());
    }
    path
}

/// The error for a document that `document()` could not parse: `LIMIT` for one beyond
/// the maximum depth, else `FODC0002`, as Saxon reports a document it cannot parse.
fn loaded_document_error(uri: &str, e: &DocumentError) -> TransformError {
    match e.limit() {
        Some(limit) => TransformError::limit_exceeded(limit).in_document(uri),
        None => TransformError::new("FODC0002", format!("{uri}: {}", e.message())),
    }
}

impl Host for Runtime<'_, '_> {
    fn variable(&self, id: VariableId) -> einvoice_xpath_eval::Result<Rc<[Item]>> {
        Runtime::variable(self, id).map_err(|e| self.stash(e))
    }

    fn call_function(
        &self,
        id: FunctionId,
        args: Vec<Argument>,
        _dc: &DynamicContext<'_>,
    ) -> einvoice_xpath_eval::Result<Vec<Item>> {
        self.call(id, args).map_err(|e| self.stash(e))
    }

    fn document(&self, uri: &str, base: Option<&str>) -> einvoice_xpath_eval::Result<Node> {
        self.load_document(uri, base).map_err(|e| self.stash(e))
    }

    fn system_property(&self, name: &QName) -> String {
        // Saxon-HE 12.10's values.
        einvoice_xslt::system_property(name.namespace.as_deref(), &name.local).to_owned()
    }
}

#[cfg(test)]
mod tests {
    use super::{comment_text, resolve_uri};

    #[test]
    fn comments_never_hold_double_dashes() {
        assert_eq!(comment_text("a--b-"), "a- -b- ");
        assert_eq!(comment_text("-x"), "-x");
        assert_eq!(comment_text("---"), "- - - ");
    }

    #[test]
    fn uris_resolve_against_the_base() {
        assert_eq!(resolve_uri("b.xml", Some("file:/d/a.xml")), "file:/d/b.xml");
        assert_eq!(
            resolve_uri("../c/b.xml", Some("file:/d/e/a.xml")),
            "file:/d/c/b.xml"
        );
        assert_eq!(
            resolve_uri("file:/x.xml", Some("file:/d/a.xml")),
            "file:/x.xml"
        );
        assert_eq!(resolve_uri("/x.xml", Some("file:/d/a.xml")), "file:/x.xml");
        assert_eq!(resolve_uri("x.xml", None), "x.xml");
        assert_eq!(resolve_uri("", Some("file:/d/a.xml")), "file:/d/a.xml");
    }
}