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
//! XPath grammar driver β recursive-descent over the token stream
//! produced by [`super::lexer`].
//!
//! ## UTF-8 slicing discipline
//!
//! Several diagnostic paths in this module slice the source string by
//! byte offset to quote the failing token in error messages. Byte
//! offsets that land inside a multi-byte UTF-8 code point panic via
//! `core::str::slice_error_fail` β a real bug the fuzzer found in
//! the error-snippet truncation logic (`(e - s).min(40)`).
//!
//! To prevent regressions, this file opts in to the `restriction`
//! lint [`clippy::string_slice`], which flags every `&str[a..b]`
//! operation. Each safe site below carries an `#[allow(...)]` with
//! a comment explaining why the indices are guaranteed to lie on
//! char boundaries β typically because they came from
//! [`str::char_indices`] or the lexer's span table (whose endpoints
//! are always set just after a complete codepoint).
#![warn(clippy::string_slice)]
use super::ast::*;
use super::lexer::Token;
use crate::error::{ErrorDomain, ErrorLevel, XmlError};
type Result<T> = std::result::Result<T, XmlError>;
fn parse_err(msg: impl Into<String>) -> XmlError {
XmlError::new(ErrorDomain::XPath, ErrorLevel::Error, msg)
}
const NODE_TYPES: &[&str] = &[
"node", "text", "comment", "processing-instruction",
// XPath 2.0 Β§2.5.4 KindTest forms. These also need to be
// recognised at step-start so the parser sees `element()` /
// `attribute()` / `document-node()` as kind tests rather than
// function calls. schema-element / schema-attribute too.
"element", "attribute", "document-node",
"schema-element", "schema-attribute",
];
fn is_node_type(name: &str) -> bool {
NODE_TYPES.contains(&name)
}
fn is_name_tok(tok: &Token, name: &str) -> bool {
matches!(tok, Token::Name(n) if n == name)
}
/// True when `tok` is one of the XPath 2.0 word-form comparison
/// operators (`eq` / `ne` / `lt` / `gt` / `le` / `ge` for value
/// comparison, and `is` for node-identity comparison) named in
/// `name`. The parser uses this in operator positions only β
/// lookup-by-position rules let the same names appear as element /
/// function identifiers in other contexts.
fn is_value_op_kw(tok: &Token, name: &str) -> bool {
debug_assert!(matches!(name, "eq" | "ne" | "lt" | "gt" | "le" | "ge" | "is"));
matches!(tok, Token::Name(n) if n == name)
}
/// Cap on the kind of nesting that actually grows the stack: each
/// `(β¦)`, `[β¦]`, and function-call argument list re-enters the
/// recursive-descent precedence chain (`parse_or` β `parse_and` β
/// β¦ β `parse_primary`). The depth counter is bumped at exactly
/// those re-entry points so `MAX_PARSE_DEPTH` corresponds to the
/// number of such nestings, not to any one parser helper's
/// re-entry count.
const MAX_PARSE_DEPTH: u32 = 64;
/// Parse a `SequenceType` from its lexical form (`"xs:long"`,
/// `"element(e)"`, `"function(*)?"`, β¦). Used to reconstruct a user
/// function's declared signature for function-subtyping checks. Returns
/// `None` on a lex/parse error or trailing input.
pub fn parse_sequence_type_str(src: &str)
-> Option<crate::xpath::ast::SequenceType>
{
let tokens = crate::xpath::lexer::tokenize_only(src).ok()?;
let mut p = Parser::new(tokens);
p.xpath_2_0 = true;
let st = p.parse_sequence_type().ok()?;
(p.peek() == &Token::Eof).then_some(st)
}
pub struct Parser {
tokens: Vec<Token>,
/// Source byte ranges for each `tokens[i]`. Same length as
/// `tokens` when populated by [`Parser::new_with_spans`];
/// otherwise empty (older callers that hand-build token lists
/// for tests skip span tracking β error messages still work,
/// just without column info). Used by [`Parser::error`] to
/// point messages at the offending substring.
spans: Vec<crate::xpath::lexer::Span>,
/// Original expression source. Needed alongside `spans` to
/// reconstruct the lexeme for diagnostic messages. Empty
/// when no source is available (test-only path).
src: String,
pos: usize,
/// Tracks how many `parse_expr` frames are currently on the
/// call stack β checked against [`MAX_PARSE_DEPTH`] on each
/// recursive re-entry to prevent stack overflow.
depth: u32,
/// XPath 2.0 grammar extensions enabled (`if-then-else`,
/// `for-return`). Off in XPath 1.0; the XSLT compiler sets it
/// when the stylesheet declares `version="2.0"` or higher.
xpath_2_0: bool,
}
impl Parser {
#[allow(dead_code)]
pub fn new(tokens: Vec<Token>) -> Self {
Self { tokens, spans: Vec::new(), src: String::new(), pos: 0, depth: 0, xpath_2_0: false }
}
/// Build a parser with full source-location context. Both the
/// original `src` string and the lexer's per-token `spans` are
/// retained so any error this parser surfaces can quote the
/// failing substring and point at its byte offset.
pub fn new_with_spans(
tokens: Vec<Token>,
spans: Vec<crate::xpath::lexer::Span>,
src: impl Into<String>,
) -> Self {
Self { tokens, spans, src: src.into(), pos: 0, depth: 0, xpath_2_0: false }
}
/// Opt into XPath 2.0 grammar (`if-then-else`, `for-return`).
/// Set by [`crate::xpath::parse_xpath_with`] from
/// [`crate::xpath::XPathOptions::xpath_2_0`].
pub fn set_xpath_2_0(&mut self, enabled: bool) { self.xpath_2_0 = enabled; }
/// Construct a parse-time error tagged with the current token's
/// source position. Falls back to a bare message when this
/// parser wasn't seeded with span info.
//
// The two byte-index slices below are guaranteed safe:
// * `self.src[start..end]` β `(start, end)` comes from the
// lexer's span table, whose endpoints are always set just
// after a complete UTF-8 codepoint.
// * `self.src[start..cut]` β `cut` is produced by walking
// `char_indices()` from `start`, so it is always a char
// boundary by construction.
#[allow(clippy::string_slice)]
fn error(&self, msg: impl Into<String>) -> XmlError {
let base = msg.into();
if let Some(&(start, end)) = self.spans.get(self.pos) {
let snippet = if !self.src.is_empty() && end > start && end <= self.src.len() {
let cut = self.src[start..end]
.char_indices()
.nth(40)
.map_or(end, |(i, _)| start + i);
Some(&self.src[start..cut])
} else {
None
};
let context = match snippet {
Some(s) if !s.is_empty() => format!(" at byte {start}: '{s}'"),
_ => format!(" at byte {start}"),
};
parse_err(format!("{base}{context}"))
} else {
parse_err(base)
}
}
fn peek(&self) -> &Token {
self.tokens.get(self.pos).unwrap_or(&Token::Eof)
}
fn peek2(&self) -> &Token {
self.tokens.get(self.pos + 1).unwrap_or(&Token::Eof)
}
fn consume(&mut self) -> Token {
let tok = self.tokens.get(self.pos).cloned().unwrap_or(Token::Eof);
if self.pos < self.tokens.len() {
self.pos += 1;
}
tok
}
// The two byte-index slices below are guaranteed safe β see the
// discipline note on [`Self::error`] for the full argument.
#[allow(clippy::string_slice)]
fn expect(&mut self, expected: &Token) -> Result<()> {
// Capture position BEFORE consuming so the error span points
// at the unexpected token rather than the one past it.
let saved_pos = self.pos;
let tok = self.consume();
if &tok == expected {
Ok(())
} else {
let pos = saved_pos;
let snippet = self.spans.get(pos).and_then(|&(s, e)| {
if !self.src.is_empty() && e > s && e <= self.src.len() {
let end = self.src[s..e]
.char_indices()
.nth(40)
.map_or(e, |(i, _)| s + i);
Some(&self.src[s..end])
} else { None }
});
let context = match (self.spans.get(pos), snippet) {
(Some(&(s, _)), Some(snip)) if !snip.is_empty() =>
format!(" at byte {s}: '{snip}'"),
(Some(&(s, _)), _) => format!(" at byte {s}"),
_ => String::new(),
};
Err(parse_err(format!(
"expected {expected:?}, got {tok:?}{context}"
)))
}
}
pub fn expect_eof(&self) -> Result<()> {
if self.peek() == &Token::Eof {
Ok(())
} else {
Err(self.error(format!(
"unexpected token after expression: {:?}", self.peek()
)))
}
}
pub fn parse_expr(&mut self) -> Result<Expr> {
// XPath 2.0 Β§ 3.5 `Expr ::= ExprSingle ("," ExprSingle)*` β
// a top-level comma-separated sequence of expressions.
// 1.0 doesn't have this form so the loop body never executes
// there.
let first = self.parse_expr_single()?;
if !self.xpath_2_0 || self.peek() != &Token::Comma {
return Ok(first);
}
let mut items = vec![first];
while self.peek() == &Token::Comma {
self.consume();
items.push(self.parse_expr_single()?);
}
Ok(Expr::Sequence(items))
}
fn parse_expr_single(&mut self) -> Result<Expr> {
// XPath 2.0 Β§ 3.5 `ExprSingle ::= ForExpr | QuantifiedExpr |
// IfExpr | OrExpr` β the additions are recognized by their
// leading contextual keyword + a deterministic lookahead.
// No depth bump here: this helper itself doesn't introduce
// recursion; the precedence chain it kicks off bottoms out
// at `parse_primary_expr`, which is where the recursive
// re-entry (`(β¦)`, `[β¦]`, `f(β¦)`) is gated.
if self.xpath_2_0 && self.peek_if_expr_2_0() {
self.parse_if_expr_2_0()
} else if self.xpath_2_0 && self.peek_for_expr_2_0() {
self.parse_for_expr_2_0()
} else if self.xpath_2_0 && self.peek_let_expr_2_0() {
self.parse_let_expr_2_0()
} else if self.xpath_2_0 && self.peek_quantified_2_0() {
self.parse_quantified_2_0()
} else if self.xpath_2_0 && self.peek_try_catch() {
self.parse_try_catch()
} else {
self.parse_or_expr()
}
}
/// Look-ahead for `try {` β the XPath 3.1 try/catch opener.
/// Falls through when `try` is a bare name (no `{`) so older
/// stylesheets using `try` as an element / variable name still
/// parse.
fn peek_try_catch(&self) -> bool {
is_name_tok(self.peek(), "try") && self.peek2() == &Token::LBrace
}
/// `TryCatchExpr ::= TryClause CatchClause+`
/// `TryClause ::= "try" "{" Expr "}"`
/// `CatchClause ::= "catch" NameTest ("|" NameTest)* "{" Expr "}"`
fn parse_try_catch(&mut self) -> Result<Expr> {
use crate::xpath::ast::{XPathCatch, CatchNameTest};
self.consume(); // "try"
self.expect(&Token::LBrace)?;
self.enter()?;
let body = self.parse_expr()?;
self.expect(&Token::RBrace)?;
self.leave();
let mut catches: Vec<XPathCatch> = Vec::new();
while is_name_tok(self.peek(), "catch") {
self.consume(); // "catch"
let mut matchers: Vec<CatchNameTest> = Vec::new();
matchers.push(self.parse_catch_name_test()?);
while self.peek() == &Token::Pipe {
self.consume();
matchers.push(self.parse_catch_name_test()?);
}
self.expect(&Token::LBrace)?;
self.enter()?;
let body = self.parse_expr()?;
self.expect(&Token::RBrace)?;
self.leave();
catches.push(XPathCatch { matchers, body });
}
if catches.is_empty() {
return Err(self.error(
"try/catch requires at least one catch clause"));
}
Ok(Expr::TryCatch { body: Box::new(body), catches })
}
/// One name-test inside an XPath 3.1 `catch` clause name list:
/// `*`, `prefix:*`, `*:NCName`, or `prefix:local` / `local`.
fn parse_catch_name_test(&mut self) -> Result<crate::xpath::ast::CatchNameTest> {
use crate::xpath::ast::CatchNameTest;
match self.peek().clone() {
Token::Star => {
self.consume();
if matches!(self.peek(), Token::Colon) {
if let Token::Name(local) = self.peek2().clone() {
self.consume(); // ':'
self.consume(); // local
return Ok(CatchNameTest::LocalNameOnly(local));
}
}
Ok(CatchNameTest::Any)
}
Token::Name(n) => {
self.consume();
if let Some((prefix, local)) = n.split_once(':') {
if local == "*" {
Ok(CatchNameTest::PrefixWildcard(prefix.to_string()))
} else {
Ok(CatchNameTest::QName {
prefix: Some(prefix.to_string()),
local: local.to_string(),
})
}
} else {
Ok(CatchNameTest::QName { prefix: None, local: n })
}
}
other => Err(self.error(format!(
"expected name-test in catch clause, got {other:?}"
))),
}
}
/// Increment the parser's recursion-depth counter and bail out
/// cleanly if it exceeds [`MAX_PARSE_DEPTH`]. Call this just
/// before re-entering the precedence chain (inside `(β¦)`, `[β¦]`,
/// function-call argument lists, etc.) β each such re-entry
/// pushes ~20 frames worth of precedence helpers before the
/// next gate can fire.
fn enter(&mut self) -> Result<()> {
self.depth += 1;
if self.depth > MAX_PARSE_DEPTH {
// Don't decrement: this Parser instance is consumed on
// error and not reused. Returning early keeps the
// remaining tokens unscanned, which the caller surfaces
// through `expect_eof` if it ever recovers.
return Err(parse_err(format!(
"XPath expression nesting depth exceeds limit ({MAX_PARSE_DEPTH})"
)));
}
Ok(())
}
fn leave(&mut self) {
self.depth -= 1;
}
/// Look-ahead for `if (` β the only spelling that XPath 2.0
/// `IfExpr` can start with. Won't fire on `if` used as a bare
/// name (no `(` after) so XPath 1.0 stylesheets that happen to
/// contain an element/attribute literally named `if` still parse.
fn peek_if_expr_2_0(&self) -> bool {
is_name_tok(self.peek(), "if") && self.peek2() == &Token::LParen
}
/// Look-ahead for `for $` β `ForExpr`'s leading
/// `SimpleForClause` always opens with `for "$" VarName`.
fn peek_for_expr_2_0(&self) -> bool {
is_name_tok(self.peek(), "for") && self.peek2() == &Token::Dollar
}
/// Look-ahead for `let $` β `LetExpr`'s `SimpleLetClause` always
/// opens with `let "$" VarName`. The `$` disambiguates from a
/// `let` element / function name.
fn peek_let_expr_2_0(&self) -> bool {
is_name_tok(self.peek(), "let") && self.peek2() == &Token::Dollar
}
/// `IfExpr ::= "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle`
/// (XPath 2.0 Β§ 3.8). Both branches are full expressions, so
/// `if(a)then if(b)then x else y else z` nests left-to-right.
fn parse_if_expr_2_0(&mut self) -> Result<Expr> {
self.consume(); // "if"
self.expect(&Token::LParen)?;
let cond = self.parse_expr()?;
self.expect(&Token::RParen)?;
if !is_name_tok(self.peek(), "then") {
return Err(self.error("expected `then` after `if (...)`"));
}
self.consume(); // "then"
let then_branch = self.parse_expr_single()?;
if !is_name_tok(self.peek(), "else") {
return Err(self.error("expected `else` after `if (...) then ...`"));
}
self.consume(); // "else"
let else_branch = self.parse_expr_single()?;
Ok(Expr::IfThenElse {
cond: Box::new(cond),
then_branch: Box::new(then_branch),
else_branch: Box::new(else_branch),
})
}
/// Look-ahead for `some $` or `every $` β `QuantifiedExpr`'s
/// only lexical openings. Same disambiguation rationale as
/// `peek_for_expr_2_0`: the `$` after the keyword is required.
fn peek_quantified_2_0(&self) -> bool {
(is_name_tok(self.peek(), "some") || is_name_tok(self.peek(), "every"))
&& self.peek2() == &Token::Dollar
}
/// `QuantifiedExpr ::= ("some" | "every") "$" VarName "in"
/// ExprSingle ("," "$" VarName "in" ExprSingle)*
/// "satisfies" ExprSingle`. Shares its binding-chain shape
/// with `ForExpr`; only the trailing keyword and the boolean
/// folding semantics differ.
fn parse_quantified_2_0(&mut self) -> Result<Expr> {
let kind = match self.consume() {
Token::Name(n) if n == "some" => crate::xpath::ast::QuantifierKind::Some,
Token::Name(n) if n == "every" => crate::xpath::ast::QuantifierKind::Every,
other => return Err(parse_err(format!(
"internal: peek_quantified_2_0 matched but consumed {other:?}"
))),
};
let mut bindings = Vec::new();
loop {
self.expect(&Token::Dollar)?;
let name = match self.consume() {
Token::Name(s) => s,
other => return Err(parse_err(format!(
"expected variable name after `$`, got {other:?}"
))),
};
if !is_name_tok(self.peek(), "in") {
return Err(self.error("expected `in` after quantified-binding name"));
}
self.consume(); // "in"
let in_expr = self.parse_expr_single()?;
bindings.push((name, in_expr));
if self.peek() == &Token::Comma {
self.consume();
continue;
}
break;
}
if !is_name_tok(self.peek(), "satisfies") {
return Err(self.error("expected `satisfies` after quantified bindings"));
}
self.consume(); // "satisfies"
let test = self.parse_expr_single()?;
Ok(Expr::Quantified { kind, bindings, test: Box::new(test) })
}
/// `ForExpr ::= SimpleForClause "return" ExprSingle`
/// where `SimpleForClause ::= "for" "$" VarName "in" ExprSingle
/// ("," "$" VarName "in" ExprSingle)*`. Captures every binding
/// in source order; evaluation iterates them as nested loops
/// (rightmost varies fastest).
fn parse_for_expr_2_0(&mut self) -> Result<Expr> {
self.consume(); // "for"
let mut bindings = Vec::new();
loop {
self.expect(&Token::Dollar)?;
let name = match self.consume() {
Token::Name(s) => s,
other => return Err(parse_err(format!(
"expected variable name after `$`, got {other:?}"
))),
};
if !is_name_tok(self.peek(), "in") {
return Err(self.error("expected `in` after `for $name`"));
}
self.consume(); // "in"
let in_expr = self.parse_expr_single()?;
bindings.push((name, in_expr));
if self.peek() == &Token::Comma {
self.consume(); // ","
continue;
}
break;
}
if !is_name_tok(self.peek(), "return") {
return Err(self.error("expected `return` after `for $v in ...`"));
}
self.consume(); // "return"
let body = self.parse_expr_single()?;
Ok(Expr::For { bindings, body: Box::new(body) })
}
/// `LetExpr ::= SimpleLetClause "return" ExprSingle`
/// where `SimpleLetClause ::= "let" "$" VarName ":=" ExprSingle
/// ("," "$" VarName ":=" ExprSingle)*` (XPath 3.0 Β§ 3.10). Each
/// binding is evaluated once and is in scope for later bindings
/// and the body.
fn parse_let_expr_2_0(&mut self) -> Result<Expr> {
self.consume(); // "let"
let mut bindings = Vec::new();
loop {
self.expect(&Token::Dollar)?;
let name = match self.consume() {
Token::Name(s) => s,
other => return Err(parse_err(format!(
"expected variable name after `$`, got {other:?}"
))),
};
self.expect(&Token::ColonEq)?;
let bound = self.parse_expr_single()?;
bindings.push((name, bound));
if self.peek() == &Token::Comma {
self.consume(); // ","
continue;
}
break;
}
if !is_name_tok(self.peek(), "return") {
return Err(self.error("expected `return` after `let $v := ...`"));
}
self.consume(); // "return"
let body = self.parse_expr_single()?;
Ok(Expr::Let { bindings, body: Box::new(body) })
}
fn parse_or_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_and_expr()?;
while is_name_tok(self.peek(), "or") {
self.consume();
let right = self.parse_and_expr()?;
left = Expr::Or(Box::new(left), Box::new(right));
}
Ok(left)
}
fn parse_and_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_equality_expr()?;
while is_name_tok(self.peek(), "and") {
self.consume();
let right = self.parse_equality_expr()?;
left = Expr::And(Box::new(left), Box::new(right));
}
Ok(left)
}
fn parse_equality_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_relational_expr()?;
loop {
// XPath 2.0 value-comparison keywords (`eq` / `ne`) read
// as `Token::Name`. We accept them as aliases of `=` /
// `!=` here so XSLT 2.0+ stylesheets that only differ in
// operator spelling parse cleanly. Strict spec 2.0
// says these error on node-set operands; we don't add
// that restriction β the runtime general-comparison
// result is equivalent for the atomic cases that
// dominate real stylesheets.
//
// Disambiguation: a bare `Name("eq")` immediately
// followed by `LParen` is the rare function-call
// `eq(...)` (e.g. EXSLT?), not an operator. Otherwise
// it's the keyword.
match self.peek() {
Token::Eq => {
self.consume();
let right = self.parse_relational_expr()?;
left = Expr::Eq(Box::new(left), Box::new(right));
}
Token::Ne => {
self.consume();
let right = self.parse_relational_expr()?;
left = Expr::Ne(Box::new(left), Box::new(right));
}
_ if is_value_op_kw(self.peek(), "eq") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_relational_expr()?;
left = Expr::ValueEq(Box::new(left), Box::new(right));
}
_ if is_value_op_kw(self.peek(), "ne") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_relational_expr()?;
left = Expr::ValueNe(Box::new(left), Box::new(right));
}
_ => break,
}
}
Ok(left)
}
fn parse_relational_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_string_concat_or_range()?;
loop {
match self.peek() {
Token::Lt => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::Lt(Box::new(left), Box::new(right));
}
Token::Gt => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::Gt(Box::new(left), Box::new(right));
}
Token::Le => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::Le(Box::new(left), Box::new(right));
}
Token::Ge => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::Ge(Box::new(left), Box::new(right));
}
// XPath 2.0 `lt` / `gt` / `le` / `ge` β same alias
// treatment as `eq` / `ne` above.
_ if is_value_op_kw(self.peek(), "lt") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::ValueLt(Box::new(left), Box::new(right));
}
_ if is_value_op_kw(self.peek(), "gt") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::ValueGt(Box::new(left), Box::new(right));
}
_ if is_value_op_kw(self.peek(), "le") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::ValueLe(Box::new(left), Box::new(right));
}
_ if is_value_op_kw(self.peek(), "ge") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::ValueGe(Box::new(left), Box::new(right));
}
// XPath 2.0 Β§3.5.3 node-comparison operator `is`.
// Returns true iff the two operands identify the same
// node. We approximate via generated-id equality β
// each node in the index has a stable id, so this is
// exact for source-tree nodes and consistent for
// constructed nodes within one evaluation. Same name-
// tok / function-call disambiguation as the other
// word operators.
_ if is_value_op_kw(self.peek(), "is") && self.peek2() != &Token::LParen => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::NodeIs(Box::new(left), Box::new(right));
}
// XPath 2.0 node-comparison `<<` / `>>` β
// document-order operators. Same precedence as `is`.
Token::LtLt if self.xpath_2_0 => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::NodeBefore(Box::new(left), Box::new(right));
}
Token::GtGt if self.xpath_2_0 => {
self.consume();
let right = self.parse_string_concat_or_range()?;
left = Expr::NodeAfter(Box::new(left), Box::new(right));
}
_ => break,
}
}
Ok(left)
}
/// XPath 2.0 `RangeExpr ::= AdditiveExpr ("to" AdditiveExpr)?`.
/// In 1.0 mode the `to` branch is never considered, so `to` stays
/// available as a plain Name in patterns / element tests.
fn parse_range_or_additive(&mut self) -> Result<Expr> {
let left = self.parse_additive_expr()?;
// XPath 2.0 Β§3.3.1 β `to` is a reserved keyword (never a
// function name in 2.0), so a trailing LParen here always
// belongs to the parenthesised right operand of the range
// (`E to (M)` or `E to (M+1)`), not a function call.
if self.xpath_2_0 && is_name_tok(self.peek(), "to") {
self.consume(); // "to"
let right = self.parse_additive_expr()?;
return Ok(Expr::Range(Box::new(left), Box::new(right)));
}
Ok(left)
}
/// XPath 3.0 `StringConcatExpr ::= RangeExpr ("||" RangeExpr)*`.
/// Each operand atomises to a single xs:string; the result is
/// the concatenation. In 2.0 (no `||`) and 1.0 mode this layer
/// is transparent β it just delegates to the range layer.
fn parse_string_concat_or_range(&mut self) -> Result<Expr> {
let mut left = self.parse_range_or_additive()?;
while self.peek() == &Token::PipePipe {
self.consume();
let right = self.parse_range_or_additive()?;
// Represent `a || b` as `concat(a, b)`. Multi-segment
// chains fold into a single concat call.
left = match left {
Expr::FunctionCall(name, mut args) if name == "concat" => {
args.push(right);
Expr::FunctionCall(name, args)
}
other => Expr::FunctionCall("concat".to_string(), vec![other, right]),
};
}
Ok(left)
}
fn parse_additive_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_multiplicative_expr()?;
loop {
match self.peek() {
Token::Plus => {
self.consume();
let right = self.parse_multiplicative_expr()?;
left = Expr::Add(Box::new(left), Box::new(right));
}
Token::Minus => {
self.consume();
let right = self.parse_multiplicative_expr()?;
left = Expr::Sub(Box::new(left), Box::new(right));
}
_ => break,
}
}
Ok(left)
}
fn parse_multiplicative_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_unary_expr()?;
loop {
match self.peek() {
Token::Star => {
self.consume();
let right = self.parse_unary_expr()?;
left = Expr::Mul(Box::new(left), Box::new(right));
}
tok if is_name_tok(tok, "div") => {
self.consume();
let right = self.parse_unary_expr()?;
left = Expr::Div(Box::new(left), Box::new(right));
}
// XPath 2.0 `idiv` β integer division, truncating
// towards zero. Same precedence as `div` / `mod`.
tok if self.xpath_2_0 && is_name_tok(tok, "idiv")
&& self.peek2() != &Token::LParen =>
{
self.consume();
let right = self.parse_unary_expr()?;
left = Expr::IDiv(Box::new(left), Box::new(right));
}
tok if is_name_tok(tok, "mod") => {
self.consume();
let right = self.parse_unary_expr()?;
left = Expr::Mod(Box::new(left), Box::new(right));
}
_ => break,
}
}
Ok(left)
}
fn parse_unary_expr(&mut self) -> Result<Expr> {
// XPath 2.0 Β§3.4 `UnaryExpr ::= ("-" | "+")* UnionExpr` β
// chains of leading `+` / `-` allowed; even count is a no-op,
// odd count of `-` negates. Unary `+` was XPath 1.0
// forbidden but accepted by libxslt and by XPath 2.0.
let mut neg = false;
loop {
match self.peek() {
Token::Minus => { self.consume(); neg = !neg; }
Token::Plus => { self.consume(); /* identity */ }
_ => break,
}
}
let inner = self.parse_union_expr()?;
let mut expr = if neg { Expr::Neg(Box::new(inner)) } else { inner };
// XPath 3.1 `ArrowExpr ::= UnaryExpr ('=>' ArrowFunctionSpecifier
// ArgumentList)*` β `e => f(args)` is sugar for
// `f(e, args)`. We desugar by emitting an `Expr::FunctionCall`
// with `expr` prepended to the argument list. Multi-segment
// chains nest naturally. Only the simple `name(...)`
// arrow-function-specifier form is accepted here; the parser
// doesn't carry the function-reference / variable-binding
// forms that XPath 3.1 also allows.
while self.xpath_2_0 && self.peek() == &Token::Arrow {
self.consume();
let Token::Name(name) = self.peek().clone() else {
return Err(parse_err("=> must be followed by a function name"));
};
self.consume();
self.expect(&Token::LParen)?;
let mut args = vec![expr];
if !matches!(self.peek(), Token::RParen) {
self.enter()?;
args.push(self.parse_expr_single()?);
self.leave();
while matches!(self.peek(), Token::Comma) {
self.consume();
self.enter()?;
args.push(self.parse_expr_single()?);
self.leave();
}
}
self.expect(&Token::RParen)?;
expr = Expr::FunctionCall(name, args);
}
Ok(expr)
}
fn parse_union_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_intersect_except_expr()?;
loop {
match self.peek() {
Token::Pipe => {
self.consume();
let right = self.parse_intersect_except_expr()?;
left = Expr::Union(Box::new(left), Box::new(right));
}
// XPath 2.0 word-form alias for `|`.
tok if self.xpath_2_0 && is_name_tok(tok, "union")
&& self.peek2() != &Token::LParen =>
{
self.consume();
let right = self.parse_intersect_except_expr()?;
left = Expr::Union(Box::new(left), Box::new(right));
}
_ => break,
}
}
Ok(left)
}
/// XPath 2.0 `IntersectExceptExpr ::= InstanceofExpr (("intersect"
/// | "except") InstanceofExpr)*` β set operations on node-set
/// operands. In 1.0 mode this layer is transparent (passes
/// straight through to the instance-of layer, which itself is
/// transparent without the keyword).
fn parse_intersect_except_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_instanceof_expr()?;
loop {
if !self.xpath_2_0 { break; }
if is_name_tok(self.peek(), "intersect") && self.peek2() != &Token::LParen {
self.consume();
let right = self.parse_instanceof_expr()?;
left = Expr::Intersect(Box::new(left), Box::new(right));
} else if is_name_tok(self.peek(), "except") && self.peek2() != &Token::LParen {
self.consume();
let right = self.parse_instanceof_expr()?;
left = Expr::Except(Box::new(left), Box::new(right));
} else {
break;
}
}
Ok(left)
}
/// XPath 2.0 `InstanceofExpr ::= TreatExpr ("instance" "of"
/// SequenceType)?`. Treat / cast / castable are folded into the
/// same descent β they all read a SequenceType / SingleType on
/// the right. All four are gated on `xpath_2_0`.
fn parse_instanceof_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_treat_expr()?;
if self.xpath_2_0
&& is_name_tok(self.peek(), "instance")
&& is_name_tok(&self.tokens.get(self.pos + 1).cloned().unwrap_or(Token::Eof), "of")
{
self.consume(); // instance
self.consume(); // of
let st = self.parse_sequence_type()?;
left = Expr::InstanceOf(Box::new(left), st);
}
Ok(left)
}
fn parse_treat_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_castable_expr()?;
if self.xpath_2_0
&& is_name_tok(self.peek(), "treat")
&& is_name_tok(&self.tokens.get(self.pos + 1).cloned().unwrap_or(Token::Eof), "as")
{
self.consume(); self.consume();
let st = self.parse_sequence_type()?;
left = Expr::TreatAs(Box::new(left), st);
}
Ok(left)
}
fn parse_castable_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_cast_expr()?;
if self.xpath_2_0
&& is_name_tok(self.peek(), "castable")
&& is_name_tok(&self.tokens.get(self.pos + 1).cloned().unwrap_or(Token::Eof), "as")
{
self.consume(); self.consume();
let st = self.parse_single_type()?;
left = Expr::CastableAs(Box::new(left), st);
}
Ok(left)
}
fn parse_cast_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_path_expr()?;
if self.xpath_2_0
&& is_name_tok(self.peek(), "cast")
&& is_name_tok(&self.tokens.get(self.pos + 1).cloned().unwrap_or(Token::Eof), "as")
{
self.consume(); self.consume();
let st = self.parse_single_type()?;
left = Expr::CastAs(Box::new(left), st);
}
Ok(left)
}
/// `SequenceType ::= ("empty-sequence" "(" ")" | ItemType
/// OccurrenceIndicator?)`. We accept the small atomic + kind
/// vocabulary detailed on [`crate::xpath::ast::ItemType`].
fn parse_sequence_type(&mut self) -> Result<crate::xpath::ast::SequenceType> {
use crate::xpath::ast::{SequenceType, Occurrence};
// `empty-sequence()` short-circuits β no occurrence indicator.
if is_name_tok(self.peek(), "empty-sequence")
&& self.peek2() == &Token::LParen
{
self.consume(); self.consume();
self.expect(&Token::RParen)?;
// `empty-sequence()` matches only the empty sequence: the
// item test matches nothing, so a non-empty value fails the
// per-item check while an empty value passes on cardinality
// alone (ZeroOrMore admits zero items).
return Ok(SequenceType {
item: crate::xpath::ast::ItemType::EmptySequence,
occurrence: Occurrence::ZeroOrMore,
});
}
let item = self.parse_item_type()?;
let occurrence = match self.peek() {
Token::Star => { self.consume(); Occurrence::ZeroOrMore }
Token::Plus => { self.consume(); Occurrence::OneOrMore }
Token::Question => { self.consume(); Occurrence::Optional }
_ => Occurrence::One,
};
Ok(SequenceType { item, occurrence })
}
/// `SingleType ::= AtomicType "?"?`. We accept the same atomic
/// vocabulary as ItemType plus the trailing `?` (parsed as
/// occurrence here, even though SingleType's `?` is permitted
/// but our occurrence handling treats it the same).
fn parse_single_type(&mut self) -> Result<crate::xpath::ast::SequenceType> {
self.parse_sequence_type()
}
fn parse_item_type(&mut self) -> Result<crate::xpath::ast::ItemType> {
use crate::xpath::ast::{ItemType, FunctionSig, SequenceType, Occurrence};
// XPath 3.1 Β§2.5.4.3 function test: `function(*)` or
// `function(SeqType, β¦) as SeqType`. The specific form captures
// the parameter and return types for function subtyping.
if matches!(self.peek(), Token::Name(n) if n == "function")
&& self.peek2() == &Token::LParen
{
self.consume(); // function
self.consume(); // (
if self.peek() == &Token::Star {
self.consume();
self.expect(&Token::RParen)?;
return Ok(ItemType::Function(None));
}
let mut params = Vec::new();
if self.peek() != &Token::RParen {
loop {
params.push(self.parse_sequence_type()?);
if self.peek() == &Token::Comma { self.consume(); continue; }
break;
}
}
self.expect(&Token::RParen)?;
let ret = if matches!(self.peek(), Token::Name(a) if a == "as") {
self.consume();
self.parse_sequence_type()?
} else {
SequenceType { item: ItemType::Any, occurrence: Occurrence::ZeroOrMore }
};
return Ok(ItemType::Function(Some(Box::new(FunctionSig { params, ret }))));
}
// XPath 3.1 Β§2.5.4.4/5 map and array tests: `map(*)` / `map(K, V)`
// and `array(*)` / `array(T)`. Matched as "any map" / "any array";
// the key/value/member types are parsed and discarded.
if self.xpath_2_0
&& matches!(self.peek(), Token::Name(n) if n == "map" || n == "array")
&& self.peek2() == &Token::LParen
{
let is_map = matches!(self.peek(), Token::Name(n) if n == "map");
self.consume(); // map / array
self.consume(); // (
if self.peek() == &Token::Star {
self.consume();
} else {
self.parse_sequence_type()?;
while self.peek() == &Token::Comma {
self.consume();
self.parse_sequence_type()?;
}
}
self.expect(&Token::RParen)?;
return Ok(if is_map { ItemType::Map } else { ItemType::Array });
}
// KindTest forms β `node()`, `element()`, etc. β recognise via
// the lookahead `name (` pattern.
if let Token::Name(name) = self.peek().clone() {
if self.peek2() == &Token::LParen {
let kind = match name.as_str() {
"item" => Some(ItemType::Any),
"node" => Some(ItemType::AnyNode),
"text" => Some(ItemType::Text),
"comment" => Some(ItemType::Comment),
"document-node" => Some(ItemType::Document),
"processing-instruction" => Some(ItemType::PI(None)),
"element" => Some(ItemType::Element(None)),
"attribute" => Some(ItemType::Attribute(None)),
_ => None,
};
if let Some(k) = kind {
self.consume(); // name
self.consume(); // (
// Most kind tests accept an optional name arg.
// For `element(*)` / `attribute(*)` we keep `None`.
let k = match k {
ItemType::Document => {
// `document-node(element(...))` / `document-node(schema-element(...))`
// β drill into the inner kind test so the
// parser consumes both `)`s. We discard
// the inner name (schema validation isn't
// implemented); the outer test still
// matches any document node.
if matches!(self.peek(), Token::Name(n)
if n == "element" || n == "schema-element")
{
self.consume(); // element / schema-element
self.expect(&Token::LParen)?;
while self.peek() != &Token::RParen
&& self.peek() != &Token::Eof
{
self.consume();
}
self.expect(&Token::RParen)?;
}
ItemType::Document
}
ItemType::PI(_) => {
let arg = match self.peek() {
Token::Literal(s) | Token::Name(s) => {
let s = s.clone(); self.consume(); Some(s)
}
_ => None,
};
// Accept `, type` (schema-aware) by
// skipping rest until `)` β we don't
// honour the type but compile shouldn't
// fail on it.
while self.peek() != &Token::RParen && self.peek() != &Token::Eof {
self.consume();
}
ItemType::PI(arg)
}
ItemType::Element(_) => {
let arg = match self.peek() {
Token::Name(s) => { let s = s.clone(); self.consume(); Some(s) }
Token::Star => { self.consume(); None }
_ => None,
};
while self.peek() != &Token::RParen && self.peek() != &Token::Eof {
self.consume();
}
ItemType::Element(arg)
}
ItemType::Attribute(_) => {
let arg = match self.peek() {
Token::Name(s) => { let s = s.clone(); self.consume(); Some(s) }
Token::At => { self.consume();
match self.peek() {
Token::Name(s) => { let s = s.clone(); self.consume(); Some(s) }
_ => None,
} }
Token::Star => { self.consume(); None }
_ => None,
};
while self.peek() != &Token::RParen && self.peek() != &Token::Eof {
self.consume();
}
ItemType::Attribute(arg)
}
other => other,
};
self.expect(&Token::RParen)?;
return Ok(k);
}
}
// Atomic-type test: `xs:integer`, `xs:string`, β¦ Accept
// any prefix:local form; eval handles the type matching.
// XPath 2.0 Β§2.5.4 says the type must be a QName, but
// an unprefixed name may resolve through the surrounding
// xpath-default-namespace; defer the strictness to eval.
self.consume();
// Built-in types are stored by local name (eval matches them by
// the XSD lattice); a user-defined (schema) type keeps its
// `prefix:local` so eval can resolve the namespace against the
// imported schema.
let local = name.rsplit(':').next().unwrap_or(&name).to_string();
let stored = if crate::xpath::eval::atomic_kind_static(&local).is_some() {
local
} else {
name
};
return Ok(ItemType::Atomic(stored));
}
Err(self.error(format!(
"expected SequenceType / ItemType, got {:?}", self.peek()
)))
}
/// Determine if the current position starts a location path step (not a primary expr).
fn is_location_path_start(&self) -> bool {
match self.peek() {
Token::Slash | Token::DoubleSlash | Token::Dot | Token::DotDot | Token::At | Token::Star => true,
Token::Name(name) => {
// Axis name followed by :: β location path
if self.peek2() == &Token::ColonColon {
return true;
}
// XPath 3.1 `map { β¦ }` / `array { β¦ }` constructors are
// primary expressions, not name-test steps.
if self.xpath_2_0 && self.peek2() == &Token::LBrace
&& (name == "map" || name == "array")
{
return false;
}
// XPath 3.1 named function reference `name#arity` is a
// primary expression, not a name-test step.
if self.xpath_2_0 && self.peek2() == &Token::Hash {
return false;
}
// Node-type name followed by ( β location path (step with node type test)
if self.peek2() == &Token::LParen && is_node_type(name) {
return true;
}
// Any other Name β element name test β location path
// UNLESS it's a function call (Name followed by '(' that is NOT a node type)
if self.peek2() == &Token::LParen {
return false; // function call β primary expr
}
true
}
_ => false,
}
}
fn parse_path_expr(&mut self) -> Result<Expr> {
let mut left = self.parse_path_expr_inner()?;
// XPath 3.0 `SimpleMapExpr ::= PathExpr ("!" PathExpr)*`.
// We fold the chain into a left-associative tree of
// `Expr::SimpleMap` nodes. Each `!` evaluates the RHS
// once per item of the LHS, with that item as context.
while self.xpath_2_0 && self.peek() == &Token::Bang {
self.consume();
let right = self.parse_path_expr_inner()?;
left = Expr::SimpleMap(Box::new(left), Box::new(right));
}
Ok(left)
}
fn parse_path_expr_inner(&mut self) -> Result<Expr> {
// `.` carrying a postfix β `.(args)` (dynamic call on the context
// item) or `.?key` (lookup) β is a context-item *primary*, not a
// self step, so route it through the postfix parser.
if self.xpath_2_0 && self.peek() == &Token::Dot
&& matches!(self.peek2(), Token::LParen | Token::Question)
{
return self.parse_filter_path_expr();
}
if self.is_location_path_start() {
let path = self.parse_location_path()?;
return Ok(Expr::Path(path));
}
// Primary expression, optionally followed by predicates and steps
self.parse_filter_path_expr()
}
fn parse_filter_path_expr(&mut self) -> Result<Expr> {
let mut expr = self.parse_primary_expr()?;
// XPath 3.1 PostfixExpr: a primary followed by any mix of dynamic
// function calls `E(args)`, filter predicates `E[β¦]`, postfix
// lookups `E?K`, and relative-path steps `E/β¦`, applied left to
// right (Β§3.2.2 / Β§3.11.3). A single chained loop is what lets a
// call follow a predicate (`$f[3](2)[1]`) and vice versa.
loop {
match self.peek() {
Token::LParen if self.xpath_2_0 => {
expr = self.parse_dynamic_call(expr)?;
}
Token::LBracket => {
// Fold consecutive predicates into one FilterPath; an
// interleaved call/lookup/step starts a fresh wrapper.
let mut predicates = Vec::new();
while self.peek() == &Token::LBracket {
self.consume();
self.enter()?;
predicates.push(self.parse_expr_single()?);
self.leave();
self.expect(&Token::RBracket)?;
}
expr = Expr::FilterPath {
primary: Box::new(expr),
predicates,
steps: Vec::new(),
};
}
Token::Question if self.xpath_2_0 => {
self.consume();
let key = self.parse_lookup_key()?;
expr = Expr::Lookup(Box::new(expr), key);
}
Token::Slash | Token::DoubleSlash => {
let steps = self.parse_path_steps()?;
expr = Expr::FilterPath {
primary: Box::new(expr),
predicates: Vec::new(),
steps,
};
}
_ => break,
}
}
Ok(expr)
}
/// Parse the argument list of a dynamic function call, starting at
/// the opening `(`. A bare `?` argument (followed by `,` or `)`) is
/// an [`Expr::Placeholder`] for partial application.
fn parse_dynamic_call(&mut self, func: Expr) -> Result<Expr> {
self.consume(); // (
self.enter()?;
let mut args = Vec::new();
if self.peek() != &Token::RParen {
loop {
if self.peek() == &Token::Question
&& matches!(self.peek2(), Token::Comma | Token::RParen)
{
self.consume();
args.push(Expr::Placeholder);
} else {
args.push(self.parse_expr_single()?);
}
if self.peek() == &Token::Comma { self.consume(); continue; }
break;
}
}
self.expect(&Token::RParen)?;
self.leave();
Ok(Expr::DynamicCall { func: Box::new(func), args })
}
fn parse_location_path(&mut self) -> Result<LocationPath> {
match self.peek() {
Token::Slash => {
self.consume();
if self.is_step_start() {
let steps = self.parse_steps()?;
Ok(LocationPath::Absolute(steps))
} else {
Ok(LocationPath::Absolute(vec![]))
}
}
Token::DoubleSlash => {
self.consume();
let mut steps = vec![desc_or_self_step()];
steps.extend(self.parse_steps()?);
Ok(LocationPath::Absolute(steps))
}
_ => {
let steps = self.parse_steps()?;
Ok(LocationPath::Relative(steps))
}
}
}
fn is_step_start(&self) -> bool {
match self.peek() {
Token::Dot | Token::DotDot | Token::At | Token::Star => true,
// Parenthesised expression as a step (XPath 2.0 Β§3.2 β
// FilterExpr alternative).
Token::LParen => true,
Token::Name(_) => true,
_ => false,
}
}
fn parse_steps(&mut self) -> Result<Vec<Step>> {
let mut steps = Vec::new();
steps.push(self.parse_step()?);
loop {
match self.peek() {
Token::DoubleSlash => {
self.consume();
steps.push(desc_or_self_step());
steps.push(self.parse_step()?);
}
Token::Slash => {
self.consume();
steps.push(self.parse_step()?);
}
_ => break,
}
}
Ok(steps)
}
/// Parse path steps that continue from a filter expression (after '/' or '//').
fn parse_path_steps(&mut self) -> Result<Vec<Step>> {
let mut steps = Vec::new();
loop {
match self.peek() {
Token::DoubleSlash => {
self.consume();
steps.push(desc_or_self_step());
if self.is_step_start() {
steps.push(self.parse_step()?);
}
}
Token::Slash => {
self.consume();
if self.is_step_start() {
steps.push(self.parse_step()?);
}
}
_ => break,
}
}
Ok(steps)
}
fn parse_step(&mut self) -> Result<Step> {
if self.peek() == &Token::Dot {
self.consume();
let mut predicates = Vec::new();
// XPath 2.0 Β§3.2.1 β `.[predicate]` filters the context
// item. Consume any trailing predicate list.
while self.peek() == &Token::LBracket {
self.consume();
self.enter()?;
let pred = self.parse_expr_single()?;
self.leave();
predicates.push(pred);
self.expect(&Token::RBracket)?;
}
return Ok(Step { axis: Axis::Self_, node_test: NodeTest::AnyNode, predicates, filter: None });
}
if self.peek() == &Token::DotDot {
self.consume();
return Ok(Step { axis: Axis::Parent, node_test: NodeTest::AnyNode, predicates: vec![], filter: None });
}
// XPath 2.0 Β§3.2 β a step can be a FilterExpr (function
// call or parenthesised expression) in addition to the
// axis-step shape. Detect these shapes first so
// `path/key('x', 'y')` and `path/(expr)` parse correctly.
if let Some(filter) = self.try_parse_filter_step()? {
let mut predicates = Vec::new();
while self.peek() == &Token::LBracket {
self.consume();
self.enter()?;
let pred = self.parse_expr_single()?;
self.leave();
predicates.push(pred);
self.expect(&Token::RBracket)?;
}
return Ok(Step {
axis: Axis::Self_,
node_test: NodeTest::AnyNode,
predicates,
filter: Some(Box::new(filter)),
});
}
let (axis, node_test) = self.parse_axis_and_node_test()?;
let mut predicates = Vec::new();
while self.peek() == &Token::LBracket {
self.consume();
self.enter()?;
let pred = self.parse_expr_single()?;
self.leave();
predicates.push(pred);
self.expect(&Token::RBracket)?;
}
Ok(Step { axis, node_test, predicates, filter: None })
}
/// Attempt to parse a step as a FilterExpr. Returns
/// `Some(expr)` when the next tokens shape up as a
/// parenthesised expression `(β¦)` or a function call
/// `name(β¦)` whose name isn't a recognised kind test.
/// Returns `None` to let the caller fall back to axis-step
/// parsing.
fn try_parse_filter_step(&mut self) -> Result<Option<Expr>> {
match self.peek() {
// Parenthesised expression step β XPath 2.0 Β§3.2 allows
// a comma-separated sequence inside the parens (so
// `./(a, b)` sorts (a,b) into document order). Use the
// top-level Expr grammar so the comma is recognised.
Token::LParen => {
self.consume();
if matches!(self.peek(), Token::RParen) {
self.consume();
return Ok(Some(Expr::Sequence(Vec::new())));
}
self.enter()?;
let e = self.parse_expr()?;
self.leave();
self.expect(&Token::RParen)?;
Ok(Some(e))
}
// Function call step β name followed by `(`, where the
// name isn't a recognised KindTest keyword.
Token::Name(n) if self.peek2() == &Token::LParen
&& !is_kind_test_name(n) =>
{
let name = n.clone();
self.consume(); // name
self.consume(); // (
let mut args = Vec::new();
if !matches!(self.peek(), Token::RParen) {
self.enter()?;
args.push(self.parse_expr_single()?);
self.leave();
while matches!(self.peek(), Token::Comma) {
self.consume();
self.enter()?;
args.push(self.parse_expr_single()?);
self.leave();
}
}
self.expect(&Token::RParen)?;
Ok(Some(Expr::FunctionCall(name, args)))
}
// XPath 2.0 Β§3.2 β a step may be a primary VariableRef
// (`$var` or `path/$var`). The variable's value is the
// step's input items and any trailing predicates filter
// it the same way they'd filter a node-step's output.
Token::Dollar if self.xpath_2_0
&& matches!(self.peek2(), Token::Name(_)) =>
{
self.consume(); // $
let name = match self.consume() {
Token::Name(n) => n,
_ => unreachable!("guarded by peek2"),
};
Ok(Some(Expr::Variable(name)))
}
_ => Ok(None),
}
}
fn parse_axis_and_node_test(&mut self) -> Result<(Axis, NodeTest)> {
if self.peek() == &Token::At {
self.consume();
let nt = self.parse_node_test(true)?;
return Ok((Axis::Attribute, nt));
}
if let Token::Name(name) = self.peek() {
if self.peek2() == &Token::ColonColon {
let axis = parse_axis_name(&name.clone())?;
self.consume(); // axis name
self.consume(); // ::
let is_attr = axis == Axis::Attribute;
let nt = self.parse_node_test(is_attr)?;
return Ok((axis, nt));
}
}
// XPath 2.0 Β§2.5.5 β when the axis is implicit, the
// default depends on the kind test that follows.
// `attribute()` / `attribute(name)` defaults to the
// attribute axis; everything else (including `element()`,
// `text()`, `*`) defaults to child. `namespace::` only
// appears with an explicit prefix.
let implicit_axis = match (self.peek(), self.peek2()) {
(Token::Name(n), Token::LParen)
if n == "attribute" || n == "schema-attribute" => Axis::Attribute,
_ => Axis::Child,
};
let is_attr = implicit_axis == Axis::Attribute;
let nt = self.parse_node_test(is_attr)?;
Ok((implicit_axis, nt))
}
/// Parse the name/type argument of an XPath 2.0 KindTest
/// (`element(name)`, `attribute(*, xs:T)`). Accepts `*` (any
/// name), a prefixed QName, or a bare NCName. Returns the
/// closest equivalent 1.0 NodeTest variant β the kind keyword
/// at the call site already encodes whether we're matching
/// elements or attributes, and the engine relies on axis context
/// to disambiguate.
fn parse_kind_name_or_wildcard_nt(&mut self) -> Result<NodeTest> {
match self.peek().clone() {
Token::Star => {
self.consume();
Ok(NodeTest::Wildcard)
}
Token::Name(name) => {
self.consume();
if let Some((prefix, local)) = name.split_once(':') {
if local == "*" {
Ok(NodeTest::PrefixWildcard(prefix.to_string()))
} else {
Ok(NodeTest::QName(prefix.to_string(), local.to_string()))
}
} else {
Ok(NodeTest::LocalName(name))
}
}
other => Err(parse_err(format!(
"kind test expected name or '*', got {other:?}"
))),
}
}
fn parse_node_test(&mut self, _is_attr_axis: bool) -> Result<NodeTest> {
match self.peek().clone() {
Token::Star => {
self.consume();
// XPath 2.0 wildcard `*:NCName` β any namespace,
// matching local name. Lexer emits Star then Colon
// then Name; recognise that sequence here.
if matches!(self.peek(), Token::Colon) {
if let Token::Name(n) = self.peek2().clone() {
self.consume(); // ':'
self.consume(); // local name
return Ok(NodeTest::LocalNameOnly(n));
}
}
Ok(NodeTest::Wildcard)
}
Token::Name(name) if self.peek2() == &Token::LParen => {
let name = name.clone();
self.consume(); // name
self.consume(); // (
let nt = match name.as_str() {
"node" => {
self.expect(&Token::RParen)?;
NodeTest::AnyNode
}
"text" => {
self.expect(&Token::RParen)?;
NodeTest::Text
}
"comment" => {
self.expect(&Token::RParen)?;
NodeTest::Comment
}
"processing-instruction" => {
// XPath 1.0 Β§2.3 spells the target as a string
// Literal (`processing-instruction('thing')`).
// Saxon, libxslt, and Xalan all also accept a
// bare NCName form (`processing-instruction(thing)`)
// for compatibility with informal stylesheets;
// honour the same extension so the W3C suite's
// `match="processing-instruction(thing)"` cases
// compile.
let target = match self.peek() {
Token::Literal(s) => {
let s = s.clone();
self.consume();
Some(s)
}
Token::Name(s) => {
let s = s.clone();
self.consume();
Some(s)
}
_ => None,
};
// XPath 2.0 Β§2.5.4 / XML Names β a PI target
// is an NCName, so a colon is never valid.
if let Some(t) = &target {
if t.contains(':') {
return Err(parse_err(format!(
"processing-instruction target '{t}' \
contains a colon and is not a valid \
NCName"
)));
}
}
self.expect(&Token::RParen)?;
NodeTest::PI(target)
}
// XPath 2.0 Β§2.5.4 schema-aware KindTest variants β
// `element()`, `element(*)`, `element(name)`,
// `element(name, T)`, `element(*, T)`, plus the
// analogous `attribute(...)`, `document-node(...)`,
// and `schema-{element,attribute}(name)` forms.
// We don't carry XPath 2.0 type annotations through
// the index, so the schema portion is ignored: the
// tests collapse to the existing 1.0 name-based
// NodeTest variants, keyed on axis context.
"element" | "attribute" | "schema-element" | "schema-attribute" => {
let nt = if matches!(self.peek(), Token::RParen) {
self.consume();
NodeTest::Wildcard
} else {
let nt = self.parse_kind_name_or_wildcard_nt()?;
// Optional second arg: TypeName β ignored.
// (XPath 2.0 also allows a trailing `?` for
// nillable; the lexer doesn't yet tokenise
// `?`, so callers using that form fall into
// a separate compile error we can address
// once we add the token.)
if matches!(self.peek(), Token::Comma) {
self.consume();
let _ = self.parse_kind_name_or_wildcard_nt()?;
}
self.expect(&Token::RParen)?;
nt
};
nt
}
"document-node" => {
let nt = if matches!(self.peek(), Token::RParen) {
self.consume();
NodeTest::Document(None)
} else if matches!(self.peek(), Token::Name(n) if n == "element" || n == "schema-element") {
self.consume(); // inner kind keyword
self.expect(&Token::LParen)?;
let inner = if matches!(self.peek(), Token::RParen) {
NodeTest::Wildcard
} else {
let n = self.parse_kind_name_or_wildcard_nt()?;
if matches!(self.peek(), Token::Comma) {
self.consume();
let _ = self.parse_kind_name_or_wildcard_nt()?;
}
n
};
self.expect(&Token::RParen)?;
self.expect(&Token::RParen)?;
// The node must be a document node whose
// document element satisfies the inner test
// β keep it wrapped rather than collapsing
// to the bare element test.
NodeTest::Document(Some(Box::new(inner)))
} else {
return Err(parse_err(
"document-node(...) expects element() or schema-element(...)"));
};
nt
}
_ => return Err(parse_err(format!("unknown node type function: {name}"))),
};
Ok(nt)
}
Token::Name(name) => {
let name = name.clone();
self.consume();
if let Some((prefix, local)) = name.split_once(':') {
if local == "*" {
Ok(NodeTest::PrefixWildcard(prefix.to_string()))
} else {
Ok(NodeTest::QName(prefix.to_string(), local.to_string()))
}
} else {
Ok(NodeTest::LocalName(name))
}
}
other => Err(parse_err(format!("expected node test, got {other:?}"))),
}
}
fn parse_primary_expr(&mut self) -> Result<Expr> {
match self.peek().clone() {
// `.` reached here (rather than as a path) is a context-item
// primary carrying a postfix β `.(args)` / `.?key`.
Token::Dot => {
self.consume();
Ok(Expr::ContextItem)
}
Token::Dollar => {
self.consume();
let saved_pos = self.pos;
match self.consume() {
Token::Name(var) => Ok(Expr::Variable(var)),
tok => {
let context = self.spans.get(saved_pos)
.map(|&(s, _)| format!(" at byte {s}"))
.unwrap_or_default();
Err(parse_err(format!(
"expected variable name after '$', got {tok:?}{context}"
)))
}
}
}
Token::LParen => {
self.consume();
// XPath 2.0 Β§ 3.1.2: `(Expr1, Expr2, β¦)` is a sequence
// constructor; a singleton `(Expr)` is just the
// parenthesised expression. In 1.0 mode we accept
// `()` as the empty sequence too (some legacy
// stylesheets use it) but otherwise stay strict.
if self.peek() == &Token::RParen {
self.consume();
return Ok(Expr::Sequence(Vec::new()));
}
self.enter()?;
let first = self.parse_expr()?;
if self.peek() == &Token::Comma {
if !self.xpath_2_0 {
self.leave();
return Err(self.error(
"comma at top of `(...)` is XPath 2.0 sequence-literal syntax"
));
}
let mut items = vec![first];
while self.peek() == &Token::Comma {
self.consume();
items.push(self.parse_expr()?);
}
self.expect(&Token::RParen)?;
self.leave();
return Ok(Expr::Sequence(items));
}
self.expect(&Token::RParen)?;
self.leave();
Ok(first)
}
Token::Literal(s) => {
let s = s.clone();
self.consume();
Ok(Expr::Literal(s))
}
Token::Integer(i) => {
self.consume();
Ok(Expr::Integer(i))
}
Token::Decimal(n) => {
self.consume();
Ok(Expr::Decimal(n))
}
Token::Double(n) => {
self.consume();
Ok(Expr::Double(n))
}
// XPath 3.1 inline function `function($p, β¦) { body }`.
Token::Name(name) if name == "function"
&& self.peek2() == &Token::LParen && self.xpath_2_0 =>
{
self.parse_inline_function()
}
// XPath 3.1 map constructor `map { k: v, β¦ }`.
Token::Name(name) if name == "map"
&& self.peek2() == &Token::LBrace && self.xpath_2_0 =>
{
self.parse_map_constructor()
}
// XPath 3.1 curly array constructor `array { β¦ }`.
Token::Name(name) if name == "array"
&& self.peek2() == &Token::LBrace && self.xpath_2_0 =>
{
self.parse_curly_array()
}
// XPath 3.1 square array constructor `[ a, b, c ]`.
Token::LBracket if self.xpath_2_0 => self.parse_square_array(),
// XPath 3.1 unary lookup `? K` β applies to the context item.
Token::Question if self.xpath_2_0 => {
self.consume();
let key = self.parse_lookup_key()?;
Ok(Expr::UnaryLookup(key))
}
// XPath 3.1 Β§3.1.6 named function reference `name#arity`.
Token::Name(name) if self.xpath_2_0 && self.peek2() == &Token::Hash => {
let name = name.clone();
self.consume(); // name
self.consume(); // #
let arity = match self.consume() {
Token::Integer(i) if i >= 0 => i as usize,
t => return Err(self.error(format!(
"expected non-negative arity after '#', got {t:?}"))),
};
Ok(Expr::NamedFunctionRef { name, arity })
}
Token::Name(name) if self.peek2() == &Token::LParen => {
let name = name.clone();
self.consume(); // function name
self.consume(); // (
// Each argument is an ExprSingle (commas are
// argument separators, not sequence constructors β
// XPath 2.0 Β§ 3.1.5).
self.enter()?;
let mut args = Vec::new();
let mut has_placeholder = false;
if self.peek() != &Token::RParen {
loop {
// XPath 3.1 Β§3.1.6 β a `?` argument is a placeholder
// for partial application.
if self.peek() == &Token::Question
&& matches!(self.peek2(), Token::Comma | Token::RParen)
{
self.consume();
args.push(Expr::Placeholder);
has_placeholder = true;
} else {
args.push(self.parse_expr_single()?);
}
if self.peek() == &Token::Comma { self.consume(); continue; }
break;
}
}
self.expect(&Token::RParen)?;
self.leave();
if has_placeholder {
// Partial application of a named (built-in or user)
// function β desugar to a dynamic call on the named
// function reference so the partial-application path
// handles the bound/unbound slots.
let arity = args.len();
Ok(Expr::DynamicCall {
func: Box::new(Expr::NamedFunctionRef { name, arity }),
args,
})
} else {
Ok(Expr::FunctionCall(name, args))
}
}
other => Err(self.error(format!("unexpected token in expression: {other:?}"))),
}
}
/// XPath 3.1 Β§3.11.1 `map { key : value, β¦ }`. `map` and `{`
/// already peeked by the caller.
fn parse_map_constructor(&mut self) -> Result<Expr> {
self.consume(); // map
self.consume(); // {
self.enter()?;
let mut entries = Vec::new();
if self.peek() != &Token::RBrace {
loop {
let key = self.parse_expr_single()?;
self.expect(&Token::Colon)?;
let val = self.parse_expr_single()?;
entries.push((key, val));
if self.peek() == &Token::Comma { self.consume(); continue; }
break;
}
}
self.expect(&Token::RBrace)?;
self.leave();
Ok(Expr::MapConstructor(entries))
}
/// XPath 3.1 Β§3.1.7 inline function expression
/// `function ( ($p as Type, β¦)? ) (as Type)? { body }`. Parameter
/// and return-type annotations are accepted and discarded β the
/// dynamic evaluator is untyped, so they impose no runtime check.
fn parse_inline_function(&mut self) -> Result<Expr> {
use crate::xpath::ast::{FunctionSig, ItemType, Occurrence, SequenceType};
let item_star = || SequenceType { item: ItemType::Any, occurrence: Occurrence::ZeroOrMore };
self.consume(); // function
self.expect(&Token::LParen)?;
let mut params = Vec::new();
let mut param_types = Vec::new();
if self.peek() != &Token::RParen {
loop {
self.expect(&Token::Dollar)?;
let name = match self.consume() {
Token::Name(n) => n,
t => return Err(self.error(format!(
"expected inline-function parameter name, got {t:?}"))),
};
params.push(name);
// An omitted parameter type defaults to `item()*`.
let pt = if is_name_tok(self.peek(), "as") {
self.consume();
self.parse_sequence_type()?
} else {
item_star()
};
param_types.push(pt);
if self.peek() == &Token::Comma { self.consume(); continue; }
break;
}
}
self.expect(&Token::RParen)?;
let ret = if is_name_tok(self.peek(), "as") {
self.consume();
self.parse_sequence_type()?
} else {
item_star()
};
self.expect(&Token::LBrace)?;
self.enter()?;
let body = if self.peek() == &Token::RBrace {
Expr::Sequence(Vec::new())
} else {
self.parse_expr()?
};
self.expect(&Token::RBrace)?;
self.leave();
Ok(Expr::InlineFunction {
params,
sig: Box::new(FunctionSig { params: param_types, ret }),
body: Box::new(body),
})
}
/// XPath 3.1 Β§3.11.2 `array { Expr }` β curly form; the contained
/// expression's items each become one array member.
fn parse_curly_array(&mut self) -> Result<Expr> {
self.consume(); // array
self.consume(); // {
self.enter()?;
let members = if self.peek() == &Token::RBrace {
Vec::new()
} else {
vec![self.parse_expr()?]
};
self.expect(&Token::RBrace)?;
self.leave();
Ok(Expr::ArrayConstructor { members, square: false })
}
/// XPath 3.1 Β§3.11.2 `[ a, b, c ]` β square form; each
/// comma-separated expression becomes one array member.
fn parse_square_array(&mut self) -> Result<Expr> {
self.consume(); // [
self.enter()?;
let mut members = Vec::new();
if self.peek() != &Token::RBracket {
members.push(self.parse_expr_single()?);
while self.peek() == &Token::Comma {
self.consume();
members.push(self.parse_expr_single()?);
}
}
self.expect(&Token::RBracket)?;
self.leave();
Ok(Expr::ArrayConstructor { members, square: true })
}
/// Parse a lookup key selector following `?` (postfix or unary):
/// `*`, an NCName, an integer, or a parenthesised expression.
fn parse_lookup_key(&mut self) -> Result<crate::xpath::ast::LookupKey> {
use crate::xpath::ast::LookupKey;
match self.peek().clone() {
Token::Star => { self.consume(); Ok(LookupKey::Wildcard) }
Token::Integer(i) => { self.consume(); Ok(LookupKey::Integer(i)) }
Token::Name(n) => { self.consume(); Ok(LookupKey::Name(n)) }
Token::LParen => {
self.consume();
self.enter()?;
let e = self.parse_expr()?;
self.expect(&Token::RParen)?;
self.leave();
Ok(LookupKey::Expr(Box::new(e)))
}
other => Err(self.error(format!(
"expected a lookup key (NCName, integer, '*', or '(expr)') after '?', got {other:?}"
))),
}
}
}
fn parse_axis_name(name: &str) -> Result<Axis> {
Ok(match name {
"ancestor" => Axis::Ancestor,
"ancestor-or-self" => Axis::AncestorOrSelf,
"attribute" => Axis::Attribute,
"child" => Axis::Child,
"descendant" => Axis::Descendant,
"descendant-or-self" => Axis::DescendantOrSelf,
"following" => Axis::Following,
"following-sibling" => Axis::FollowingSibling,
"namespace" => Axis::Namespace,
"parent" => Axis::Parent,
"preceding" => Axis::Preceding,
"preceding-sibling" => Axis::PrecedingSibling,
"self" => Axis::Self_,
_ => return Err(parse_err(format!("unknown axis: {name}"))),
})
}
/// Recognised XPath 2.0 KindTest keywords. A `Name LParen` sequence
/// whose name is one of these is parsed as a kind-test step, not a
/// function-call step.
fn is_kind_test_name(n: &str) -> bool {
matches!(n, "node" | "text" | "comment" | "processing-instruction"
| "element" | "attribute" | "schema-element" | "schema-attribute"
| "document-node")
}
fn desc_or_self_step() -> Step {
Step {
axis: Axis::DescendantOrSelf,
node_test: NodeTest::AnyNode,
predicates: vec![],
filter: None,
}
}
#[cfg(test)]
mod tests {
use super::super::parse_xpath;
/// Security regression: a malicious XPath with deeply nested
/// parentheses must not stack-overflow the recursive-descent
/// parser. The fuzzer found this β input like
/// `((((...((1))...))))` blows through the call chain
/// `parse_or β parse_and β ... β parse_primary β parse_expr`
/// once per `(`, exhausting the stack within a few hundred
/// levels. Post-fix the parser tracks recursion depth and
/// returns a clean parse error instead.
#[test]
fn deep_paren_nesting_errors_cleanly() {
// Rust test threads default to a 2 MB stack; the recursive-
// descent precedence chain pushes roughly twenty frames per
// depth level, so reaching `MAX_PARSE_DEPTH` and triggering
// the depth check needs more headroom than that default
// provides. Run the parse on a thread sized for the real
// production stack (8 MB is the main-thread default on most
// platforms) so we're testing the depth-limit path rather
// than the test-harness's stack ceiling.
std::thread::Builder::new()
.stack_size(8 << 20)
.spawn(|| {
let n = 10_000;
let mut src = String::with_capacity(2 * n + 1);
for _ in 0..n { src.push('('); }
src.push('1');
for _ in 0..n { src.push(')'); }
let result = parse_xpath(&src);
assert!(result.is_err(),
"expected depth-limit error, got Ok β parser accepted {n}-deep nesting");
let msg = result.unwrap_err().to_string().to_lowercase();
assert!(
msg.contains("nesting") || msg.contains("depth") || msg.contains("recursion"),
"expected depth-related error message, got: {msg}"
);
})
.expect("spawn deep-nesting test thread")
.join()
.expect("deep-nesting test thread panicked");
}
/// Same property via predicate nesting (`[β¦]`), since the
/// XPath grammar reaches `parse_expr` recursively from
/// inside step predicates as well as parenthesised
/// sub-expressions.
#[test]
fn deep_predicate_nesting_errors_cleanly() {
// Same stack-headroom rationale as `deep_paren_nesting_errors_cleanly`.
std::thread::Builder::new()
.stack_size(8 << 20)
.spawn(|| {
let n = 10_000;
let mut src = String::with_capacity(2 + 2 * n);
src.push_str("a");
for _ in 0..n { src.push_str("[a"); }
for _ in 0..n { src.push(']'); }
let result = parse_xpath(&src);
assert!(result.is_err(),
"expected depth-limit error, got Ok β parser accepted {n}-deep predicates");
})
.expect("spawn deep-predicate test thread")
.join()
.expect("deep-predicate test thread panicked");
}
/// Security regression for the XPath evaluator's algorithmic
/// DoS where nested predicates that each contain a `//` query
/// have N^k complexity in document size N Γ nesting depth k.
/// At depth 5 against a 30-node doc the fuzzer hit 6+ seconds
/// per evaluation; the slow-unit it produced reached 604s.
#[test]
#[cfg_attr(miri, ignore = "wall-clock deadline test; Miri's interpreter is 50β200Γ \
slower than native and breaches the 5s budget despite the \
parser's step counter aborting correctly")]
fn nested_predicate_dos_returns_budget_error() {
use crate::{parse_str, ParseOptions};
use crate::xpath::XPathContext;
// Small fixture β same shape as the fuzzer's, enough to
// make 30^5 = 24M operations theoretically required, which
// would take seconds without the budget.
let fixture = r#"<catalog>
<book><title>a</title><author>x</author></book>
<book><title>b</title><author>y</author></book>
<book><title>c</title><author>z</author></book>
</catalog>"#;
let doc = parse_str(fixture, &ParseOptions::default()).unwrap();
let ctx = XPathContext::new(&doc);
// Depth-7 nested predicate, each level enumerating
// descendants β easily blows past 30^7 = 22B ops without
// a budget.
let expr = "//*[.=//*[.=//*[.=//*[.=//*[.=//*[.=//*[.=.]]]]]]]";
let t0 = std::time::Instant::now();
let result = ctx.eval(expr);
let dt = t0.elapsed();
// The real invariant is that the budget *bounds* the work and
// returns an error (asserted below). This wall-clock check is a
// secondary guard against the multi-minute hang we regressed
// against; keep it generous, since a debug build under the
// parallel-test load of `cargo test-all` (~Β΅s per charged step Γ
// the [`DEFAULT_MAX_EVAL_STEPS`] cap) can take tens of seconds
// while release aborts sub-second. 60s still catches a true hang.
assert!(
dt < std::time::Duration::from_secs(60),
"expected budget to abort in bounded time, took {dt:?}"
);
let err = result.expect_err("expected budget-exceeded error");
let msg = err.to_string().to_lowercase();
assert!(
msg.contains("budget") || msg.contains("step"),
"expected budget-related error message, got: {msg}"
);
}
/// Counterpart: shallow nested predicates that are well below
/// the budget continue to evaluate normally, returning their
/// natural result rather than a budget error.
#[test]
fn shallow_predicates_evaluate_normally() {
use crate::{parse_str, ParseOptions};
use crate::xpath::XPathContext;
let doc = parse_str("<r><a><b/></a><a><c/></a></r>", &ParseOptions::default()).unwrap();
let ctx = XPathContext::new(&doc);
// Two-deep predicate β easily under the budget.
let _ = ctx.eval("//a[count(./*)=1]").expect("legit query");
}
/// Predicate-nesting depth ceiling (independent of the parser's
/// stack-recursion limit): depth 8 is the threshold above which
/// expressions like `//*[//*[//*[//*[//*[//*[//*[//*[//*[β¦]]]]]]]]]`
/// are rejected at parse time β short-circuiting the evaluator's
/// step budget which would otherwise burn ~500k charges before
/// bailing. See `MAX_PREDICATE_NESTING_DEPTH` in
/// `crates/core/src/xpath/mod.rs`.
#[test]
fn predicate_nesting_depth_limit_rejects_at_parse_time() {
use crate::xpath::ast::max_predicate_nesting;
// Depth 7 β borderline pathological, but legal at parse time.
// (The eval step budget catches it; see
// `nested_predicate_dos_returns_budget_error`.)
let ok = "//*[//*[//*[//*[//*[//*[//*[.='x']]]]]]]";
let parsed = parse_xpath(ok).expect("depth-7 should parse");
assert_eq!(max_predicate_nesting(&parsed), 7);
// Depth 9 β beyond the ceiling; parse must reject.
let bad = "//*[//*[//*[//*[//*[//*[//*[//*[//*[.='x']]]]]]]]]";
let err = parse_xpath(bad).expect_err("depth-9 must be rejected");
let msg = err.message.to_lowercase();
assert!(
msg.contains("predicate") && msg.contains("nesting"),
"expected predicate-nesting error message, got: {msg}",
);
}
/// Sanity: realistic nesting depths still parse fine β the
/// depth limit is high enough not to break normal XPath.
#[test]
fn moderate_nesting_still_parses() {
// 20 levels of parens around a literal β deeper than any
// legitimate XPath I've ever seen in the wild.
let n = 20;
let mut src = String::new();
for _ in 0..n { src.push('('); }
src.push('1');
for _ in 0..n { src.push(')'); }
assert!(parse_xpath(&src).is_ok());
}
/// Error-snippet truncation must respect UTF-8 boundaries. The
/// `expect` path truncates the source span to keep the error
/// message short; the fuzzer found a case where the previous
/// byte-based cut landed inside a `Γ` (a 2-byte code point)
/// and panicked in `core::str::slice_error_fail`.
///
/// We force the cut into a multi-byte char by placing a long
/// run of unexpected name characters where the parser expects
/// a closing token. After parsing `1` as a function argument,
/// the parser calls `expect(&RParen)` and instead encounters
/// the multi-byte name token β whose span is the substring we
/// truncate.
///
/// Sweep 2/3/4-byte code points so the cut lands at a different
/// offset within a glyph for each; a byte-based truncation
/// would panic for at least one.
#[test]
fn parse_error_snippet_truncates_on_char_boundary() {
for ch in ['Γ', 'δΈ', 'πΌ'] {
let mut src = String::from("count(1 ");
for _ in 0..50 { src.push(ch); }
src.push(')');
let err = parse_xpath(&src).expect_err(
"malformed input must error, not parse",
);
// Reaching `expect_err` at all proves the panic is gone
// (slice_error_fail would unwind past this frame). The
// message check confirms the snippet path actually ran.
assert!(
err.message.contains(ch),
"error snippet should quote the offending {ch:?}; \
got: {msg}",
msg = err.message,
);
}
}
/// Direct regression for the libFuzzer crash artifact
/// `crash-4d3745946ae1f278285ab808487532d5f3ed3257`. The
/// input is a malformed `normalize-space(...)` call whose
/// argument contains embedded NULs and `Γ` characters at
/// byte offsets that previously straddled the snippet cut.
#[test]
fn fuzz_crash_4d374594_no_panic() {
let bytes: &[u8] = &[
0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x2d,
0x73, 0x70, 0x61, 0x63, 0x65, 0x28, 0x2a, 0x22, 0x00, 0x00,
0x63, 0x2f, 0x31, 0x2f, 0x74, 0xc3, 0x9f, 0x30, 0x30, 0x71,
0xc3, 0x9f, 0x35, 0x61, 0x61, 0x41, 0x71, 0xc3, 0x9f, 0xc3,
0x9f, 0x30, 0x61, 0x3a, 0x30, 0x41, 0x61, 0x71, 0x61, 0x3d,
0xc3, 0x9f, 0x30, 0x30, 0x6e, 0x61, 0xc3, 0x9f, 0x30, 0x20,
0x32, 0x2c, 0x61, 0x3c, 0x6a, 0x3a, 0x02, 0x00, 0x71, 0xc3,
0x9f, 0x30, 0x6e, 0x61, 0x41, 0x71, 0xc3, 0x9f, 0x21, 0x22,
0x29,
];
let src = std::str::from_utf8(bytes).expect("fuzz input is valid UTF-8");
let _ = parse_xpath(src); // must not panic; error vs. ok both fine
}
// ββ XPath 2.0 grammar gate βββββββββββββββββββββββββββββββββββββ
fn parse_2_0(src: &str) -> super::super::Result<super::super::ast::Expr> {
let mut opts = super::super::XPathOptions::default();
opts.xpath_2_0 = true;
super::super::parse_xpath_with(src, &opts)
}
/// `if (cond) then a else b` parses only when XPath 2.0 mode is
/// on; the same expression must be rejected in default (1.0) mode
/// so 1.0 stylesheets don't silently sprout 2.0 semantics.
#[test]
fn if_then_else_is_2_0_only() {
// 1.0 mode: `if (1) then 2 else 3` should fail because
// unquoted `if` is parsed as an XPath name test and the
// surrounding `(...)` would be a function-call argument list.
let r1 = parse_xpath("if (1) then 2 else 3");
assert!(r1.is_err(), "XPath 1.0 must reject `if (...) then ... else ...`");
// 2.0 mode: same source parses cleanly.
let r2 = parse_2_0("if (1) then 2 else 3");
assert!(r2.is_ok(), "XPath 2.0 must accept if-then-else; got {:?}", r2);
use super::super::ast::Expr;
assert!(matches!(r2.unwrap(), Expr::IfThenElse { .. }));
}
/// `for $v in 1 to 3 return $v * $v` parses only when XPath 2.0
/// mode is on. (`1 to 3` itself isn't supported yet β the grammar
/// just needs `for $v in <expr> return <expr>` to work; the test
/// uses a path expression instead.)
#[test]
fn for_return_is_2_0_only() {
// Use a path so we don't depend on `to` (range) being implemented.
assert!(parse_xpath("for $v in /a return $v").is_err(),
"XPath 1.0 must reject `for $v in ... return ...`");
let r = parse_2_0("for $v in /a return $v");
assert!(r.is_ok(), "XPath 2.0 must accept for-return; got {:?}", r);
use super::super::ast::Expr;
let Expr::For { bindings, .. } = r.unwrap() else { panic!("expected For") };
assert_eq!(bindings.len(), 1);
assert_eq!(bindings[0].0, "v");
}
}