sup-xml-core 1.3.0

Safe Rust core: error types, character primitives, encoding utilities
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
/// XPath 1.0 abstract syntax tree.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Axis {
    Ancestor,
    AncestorOrSelf,
    Attribute,
    Child,
    Descendant,
    DescendantOrSelf,
    Following,
    FollowingSibling,
    Namespace,
    Parent,
    Preceding,
    PrecedingSibling,
    Self_,
}

impl Axis {
    pub fn is_reverse(&self) -> bool {
        matches!(
            self,
            Axis::Ancestor | Axis::AncestorOrSelf | Axis::Preceding | Axis::PrecedingSibling
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum NodeTest {
    /// node() — any node on the axis
    AnyNode,
    /// text()
    Text,
    /// comment()
    Comment,
    /// processing-instruction() or processing-instruction('target')
    PI(Option<String>),
    /// document-node() — matches document nodes (XPath 2.0 §2.5.4).
    /// Distinct from `AnyNode` so XSLT `match="document-node()"`
    /// patterns don't accidentally cover element / text / etc.
    ///
    /// The optional inner test carries the element name test of a
    /// `document-node(element(N))` / `document-node(element(*))` form
    /// (XPath 2.0 §2.5.4.3): the node matches only when it is a
    /// document node whose document element satisfies that test.
    /// `None` is the bare `document-node()`.
    Document(Option<Box<NodeTest>>),
    /// * — any element/attribute
    Wildcard,
    /// prefix:* — any element/attribute in this namespace prefix
    PrefixWildcard(String),
    /// *:localname — any namespace, specific local name (XPath 2.0
    /// §2.5.5.3 — `WildcardName ::= NCName ":" "*" | "*" ":" NCName`).
    LocalNameOnly(String),
    /// localname (no prefix)
    LocalName(String),
    /// prefix:localname
    QName(String, String),
    /// Unprefixed name that resolves through the XSLT 2.0
    /// `xpath-default-namespace` attribute (XSLT 2.0 §5.1.1).
    /// The XPath parser produces `LocalName`; the XSLT compiler
    /// rewrites those into this variant when an ancestor of the
    /// stylesheet element declares a non-empty default URI for
    /// element name tests.  Stored as expanded URI + local part so
    /// runtime matching is a pair-compare without binding lookup.
    DefaultNamespaceName { uri: String, local: String },
}

#[derive(Debug, Clone)]
pub struct Step {
    pub axis: Axis,
    pub node_test: NodeTest,
    pub predicates: Vec<Expr>,
    /// XPath 2.0 `StepExpr ::= AxisStep | FilterExpr`.  When
    /// `Some`, the step is a FilterExpr (a primary expression
    /// like a function call or parenthesised expression) that
    /// produces its own sequence per input node — `axis` and
    /// `node_test` are unused for the eval but kept at their
    /// default (`Self_` / `AnyNode`) so existing code that
    /// destructures `Step` directly still type-checks.
    /// `path/key('x', 'y')` and `path/(expr)` parse into this
    /// shape.
    pub filter: Option<Box<Expr>>,
}

#[derive(Debug, Clone)]
pub enum LocationPath {
    Absolute(Vec<Step>),
    Relative(Vec<Step>),
}

#[derive(Debug, Clone)]
pub enum Expr {
    Or(Box<Expr>, Box<Expr>),
    And(Box<Expr>, Box<Expr>),
    /// General comparison (`=`, `!=`, `<`, `>`, `<=`, `>=`) — existential
    /// over the cartesian product of the two operand sequences
    /// (XPath 2.0 §3.5.2).
    Eq(Box<Expr>, Box<Expr>),
    Ne(Box<Expr>, Box<Expr>),
    Lt(Box<Expr>, Box<Expr>),
    Gt(Box<Expr>, Box<Expr>),
    Le(Box<Expr>, Box<Expr>),
    Ge(Box<Expr>, Box<Expr>),
    /// Value comparison (`eq`, `ne`, `lt`, `gt`, `le`, `ge`) — single-
    /// item operands, returns the empty sequence when either side is
    /// empty, raises a type error when either side has more than one
    /// item (XPath 2.0 §3.5.1).
    ValueEq(Box<Expr>, Box<Expr>),
    ValueNe(Box<Expr>, Box<Expr>),
    ValueLt(Box<Expr>, Box<Expr>),
    ValueGt(Box<Expr>, Box<Expr>),
    ValueLe(Box<Expr>, Box<Expr>),
    ValueGe(Box<Expr>, Box<Expr>),
    Add(Box<Expr>, Box<Expr>),
    Sub(Box<Expr>, Box<Expr>),
    Mul(Box<Expr>, Box<Expr>),
    Div(Box<Expr>, Box<Expr>),
    Mod(Box<Expr>, Box<Expr>),
    Neg(Box<Expr>),
    Union(Box<Expr>, Box<Expr>),
    Path(LocationPath),
    /// Primary expression with optional predicates and additional steps.
    FilterPath {
        primary: Box<Expr>,
        predicates: Vec<Expr>,
        steps: Vec<Step>,
    },
    FunctionCall(String, Vec<Expr>),
    Variable(String),
    Literal(String),
    /// Integer literal — no `.` and no exponent (`42`), an `xs:integer`.
    /// A literal too large for `i64` is lexed as a [`Expr::Decimal`].
    Integer(i64),
    /// Decimal literal — a `.` but no exponent (`3.14`), an `xs:decimal`.
    /// Carries an exact [`rust_decimal::Decimal`] parsed from the
    /// source lexical form (so `0.1` is 1/10 exactly, not the f64
    /// nearest neighbour).  Stringifies in decimal form, never
    /// scientific.
    Decimal(rust_decimal::Decimal),
    /// Numeric literal with an exponent (`1.5e0`) — an `xs:double`.
    /// In a 2.0 host this evaluates to a typed double so it takes the
    /// F&O scientific string form.
    Double(f64),
    /// XPath 2.0 `if (cond) then a else b`.  Both branches are
    /// `ExprSingle` — already parsed as full expressions.
    IfThenElse {
        cond: Box<Expr>,
        then_branch: Box<Expr>,
        else_branch: Box<Expr>,
    },
    /// XPath 2.0 `for $v in seq return body`, with chained bindings
    /// (`for $a in A, $b in B return ...`) flattened into the
    /// `bindings` list in source order.
    For {
        bindings: Vec<(String, Expr)>,
        body:     Box<Expr>,
    },
    /// XPath 3.0 `let $v := expr return body`, with chained bindings
    /// (`let $a := A, $b := B return ...`) flattened into the
    /// `bindings` list in source order.  Each binding is evaluated
    /// once and is visible to later bindings and the body.
    Let {
        bindings: Vec<(String, Expr)>,
        body:     Box<Expr>,
    },
    /// XPath 2.0 range `m to n` — yields the integer sequence m, m+1,
    /// …, n (inclusive).  Empty when m > n.  Atomic non-integer
    /// operands round to integers before the range materialises.
    Range(Box<Expr>, Box<Expr>),
    /// XPath 3.0 simple-map operator `E1 ! E2` — evaluates `E2` with
    /// each item of `E1` as the context item; concatenates results
    /// in iteration order (no document-order sort).  Distinct from
    /// `/` in that the right-hand side need not be a node-step:
    /// `(1, 2, 3) ! (. * 2)` yields (2, 4, 6).
    SimpleMap(Box<Expr>, Box<Expr>),
    /// XPath 2.0 node-comparison `$a << $b` — true iff `$a`
    /// precedes `$b` in document order.  Empty operands yield
    /// the empty sequence.
    NodeBefore(Box<Expr>, Box<Expr>),
    /// XPath 2.0 node-comparison `$a >> $b` — node-after.
    NodeAfter(Box<Expr>, Box<Expr>),
    /// XPath 2.0 node-comparison `$a is $b` — true iff both operands
    /// are the same node.  Each operand must atomise to at most one
    /// node; an empty operand yields the empty sequence (§3.5.3).
    NodeIs(Box<Expr>, Box<Expr>),
    /// XPath 2.0 parenthesised sequence literal `(a, b, c)` — at least
    /// two elements (a singleton is just a parenthesised expression).
    /// Evaluation concatenates each item; atomics become synthetic
    /// text nodes so the result is uniformly a NodeSet.
    Sequence(Vec<Expr>),
    /// XPath 2.0 quantified expression `some $v in seq satisfies test`
    /// / `every $v in seq satisfies test`.  Boolean result: any /
    /// all items of the sequence satisfy the predicate.
    Quantified {
        kind:     QuantifierKind,
        bindings: Vec<(String, Expr)>,
        test:     Box<Expr>,
    },
    /// XPath 2.0 `lhs idiv rhs` — integer quotient with truncation
    /// towards zero (XPath 2.0 § 3.4).  Distinct from `div`, which
    /// always produces a float.
    IDiv(Box<Expr>, Box<Expr>),
    /// XPath 2.0 `lhs intersect rhs` — node-set intersection in
    /// document order.
    Intersect(Box<Expr>, Box<Expr>),
    /// XPath 2.0 `lhs except rhs` — nodes in `lhs` not present in
    /// `rhs`, document order preserved.
    Except(Box<Expr>, Box<Expr>),
    /// XPath 2.0 `expr instance of SequenceType` — boolean predicate.
    InstanceOf(Box<Expr>, SequenceType),
    /// XPath 2.0 `expr cast as SingleType` — value conversion;
    /// raises a runtime error when the source value can't be cast.
    CastAs(Box<Expr>, SingleType),
    /// XPath 3.1 `try { TryExpr } catch <NameTest>* { CatchExpr } …`
    /// — evaluate `body`; on dynamic error, walk catches and
    /// evaluate the first whose name-tests match the caught
    /// error's QName.  Inside the catch handler, `$err:code` /
    /// `$err:description` / etc. are bound to the error's
    /// metadata.
    TryCatch {
        body:    Box<Expr>,
        catches: Vec<XPathCatch>,
    },
    /// XPath 2.0 `expr castable as SingleType` — boolean predicate
    /// for "can this value cast without error".
    CastableAs(Box<Expr>, SingleType),
    /// XPath 2.0 `expr treat as SequenceType` — assertion that the
    /// runtime value already conforms; raises an error otherwise.
    TreatAs(Box<Expr>, SequenceType),
    /// Synthetic — not produced by the XPath parser.  The XSLT
    /// compiler wraps each top-level expression whose static context
    /// declares a non-codepoint `[xsl:]default-collation` so the
    /// runtime can install that URI on a thread-local before
    /// evaluating `inner`.  Value-comparison operators (`eq`, `ne`,
    /// `lt`, …) consult that thread-local when both operands are
    /// strings/untyped — XPath 2.0 §3.5.2 says they use the static
    /// default collation in that case.
    WithDefaultCollation(String, Box<Expr>),
    /// Synthetic — not produced by the XPath parser.  The XSLT
    /// compiler wraps each top-level expression that sits in an
    /// XPath-1.0 backwards-compatibility scope (a `[xsl:]version="1.0"`
    /// ancestor inside a 2.0 stylesheet, XSLT 2.0 §3.8).  The runtime
    /// installs a thread-local flag before evaluating `inner` so the
    /// XPath-1.0-compat conversion rules (XPath 2.0 §B.1) apply:
    /// arithmetic operands are atomised to xs:double, and `to`-range
    /// bounds use the first item of a sequence.
    BackwardsCompat(Box<Expr>),
    /// XPath 3.1 §3.11.1 map constructor `map { k1: v1, k2: v2, … }`.
    /// Each entry is `(key-expr, value-expr)`; keys evaluate to a
    /// single atomic value, values to an arbitrary sequence.
    MapConstructor(Vec<(Expr, Expr)>),
    /// XPath 3.1 §3.11.2 array constructors — `[ a, b, c ]` (square:
    /// one member per comma-separated expression) or `array { … }`
    /// (curly: one member per item of the contained sequence).
    ArrayConstructor { members: Vec<Expr>, square: bool },
    /// XPath 3.1 §3.11.3 postfix lookup `E ? K` — indexes into the map
    /// or array produced by `E`.
    Lookup(Box<Expr>, LookupKey),
    /// XPath 3.1 unary lookup `? K` — indexes into the context item.
    UnaryLookup(LookupKey),
    /// XPath 3.1 §3.12 inline function `function($p, …) { body }`.
    /// Parameter types and the return type are accepted but not
    /// enforced; only the parameter names matter for binding.
    InlineFunction {
        params: Vec<String>,
        /// Declared signature (parameter and return types, `item()*` where
        /// omitted) — used for function subtyping in `instance of`.
        sig:    Box<FunctionSig>,
        body:   Box<Expr>,
    },
    /// XPath 3.0 §3.1.4 context-item expression `.` used as a primary —
    /// yields the current context item (which may be a non-node such as a
    /// function item).  Distinct from a `self::node()` step: produced only
    /// where `.` carries a postfix (`.(args)`, `.?key`), so the context
    /// item's actual value, not its node projection, drives the postfix.
    ContextItem,
    /// XPath 3.1 §3.1.6 named function reference `name#arity`.
    NamedFunctionRef { name: String, arity: usize },
    /// XPath 3.1 §3.2.2 dynamic function call `F(args)` where `F` is
    /// an expression yielding a function item.  The `?` placeholder
    /// (partial application) appears as [`Expr::Placeholder`] in args.
    DynamicCall { func: Box<Expr>, args: Vec<Expr> },
    /// XPath 3.1 partial-application argument placeholder (`?`).
    Placeholder,
}

/// The key selector of an XPath 3.1 lookup expression (`?K`).
#[derive(Debug, Clone)]
pub enum LookupKey {
    /// `?*` — all values of the map / all members of the array.
    Wildcard,
    /// `?name` — the entry whose key is the string `name`.
    Name(String),
    /// `?123` — integer key (map) or 1-based position (array).
    Integer(i64),
    /// `?(expr)` — the key(s) computed by the parenthesised expression.
    Expr(Box<Expr>),
}

/// XPath 2.0 SequenceType — limited to what we recognise.  Anything
/// outside `xs:string` / `xs:integer` / `xs:decimal` / `xs:double`
/// / `xs:boolean` / `xs:date` / `xs:dateTime` / `xs:time` /
/// `xs:anyAtomicType` / `xs:anyURI` (atomic) or `item()` / `node()`
/// / `element()` / `attribute()` / `text()` / `comment()` /
/// `processing-instruction()` / `document-node()` (kind) is parsed
/// but flagged as unsupported at eval time.
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceType {
    pub item:        ItemType,
    pub occurrence:  Occurrence,
}

/// The signature of a specific function test `function(T1, …, Tn) as R`
/// (XPath 3.1 §2.5.4.3).  Drives the function-subtyping rules applied by
/// `instance of` / `treat as`: the parameter types are contravariant and
/// the return type is covariant.
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionSig {
    pub params: Vec<SequenceType>,
    pub ret:    SequenceType,
}

/// `SingleType` — a `SequenceType` with implicit `?` (one or zero).
/// Used in `cast as` / `castable as`.
pub type SingleType = SequenceType;

#[derive(Debug, Clone, PartialEq)]
pub enum ItemType {
    /// `item()` — any item.
    Any,
    /// `xs:foo` atomic type test.  Stored verbatim as the local name
    /// (with `xs:` stripped); eval uses the name to pick a string-to-
    /// value coercion.
    Atomic(String),
    /// `node()` — any node.
    AnyNode,
    /// Specific kind tests with optional name.  Name is `None` for
    /// the bare-paren form (e.g. `element()`); `Some` for
    /// `element(foo)`.
    Element(Option<String>),
    Attribute(Option<String>),
    Text,
    Comment,
    PI(Option<String>),
    Document,
    /// Function test (XPath 3.1 §2.5.4.3).  `None` is `function(*)` (any
    /// function item); `Some` carries a specific `function(T1, …, Tn) as R`
    /// signature, matched by function subtyping.
    Function(Option<Box<FunctionSig>>),
    /// Map test (XPath 3.1 §2.5.4.4) — `map(*)` or `map(K, V)`.  Matched as
    /// "any map"; the key/value types are parsed but not modelled.
    Map,
    /// Array test (XPath 3.1 §2.5.4.5) — `array(*)` or `array(T)`.  Matched
    /// as "any array"; the member type is parsed but not modelled.
    Array,
    /// `empty-sequence()` — matches only the empty sequence.  As an
    /// item test it matches no individual item; the empty case is
    /// admitted by the cardinality check alone.
    EmptySequence,
}

/// Occurrence indicator (XPath 2.0 § 2.5.3) attached to a SequenceType.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Occurrence {
    /// Exactly one (default — no indicator).
    One,
    /// `?` — zero or one.
    Optional,
    /// `+` — one or more.
    OneOrMore,
    /// `*` — zero or more.
    ZeroOrMore,
}

/// Distinguishes `some` (∃) from `every` (∀) quantified expressions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantifierKind { Some, Every }

/// One catch clause of an [`Expr::TryCatch`].  The grammar is
/// `"catch" NameTest ("|" NameTest)* "{" Expr "}"`, with `"*"`
/// allowed as a catch-all in place of a specific QName.
#[derive(Debug, Clone)]
pub struct XPathCatch {
    /// QName matchers from the `catch` clause's name list.  An
    /// empty list (or a single `Any`) is catch-all.
    pub matchers: Vec<CatchNameTest>,
    /// Body expression to evaluate when this clause matches.
    pub body:     Expr,
}

/// One name-test in an XPath try/catch `catch` clause name list.
#[derive(Debug, Clone)]
pub enum CatchNameTest {
    /// `*` — any error matches.
    Any,
    /// `*:NCName` — matches any-namespace, specific local name.
    LocalNameOnly(String),
    /// `prefix:*` — namespace wildcard.
    PrefixWildcard(String),
    /// `prefix:local` / `local` — fully-qualified.
    QName { prefix: Option<String>, local: String },
}

/// Maximum depth of predicates-within-predicates anywhere in `expr`.
///
/// A single `Step` carrying `[p]` predicates is depth 1; if `p` itself
/// contains a `Path` whose steps carry their own `[q]` predicates,
/// that's depth 2; and so on.  This is the *semantic* nesting that
/// drives the evaluator's N^k blow-up — distinct from the parser's
/// grammar-production recursion depth (which the parser already bounds
/// with `MAX_PARSE_DEPTH`).
///
/// Used to reject pathological inputs like
/// `//*[//*[//*[//*[//*[.='x']]]]]` at parse time, before the
/// evaluator's step budget burns ~500k charges to reach the same
/// conclusion at much higher latency.
pub fn max_predicate_nesting(expr: &Expr) -> u32 {
    fn expr_depth(e: &Expr) -> u32 {
        match e {
            Expr::Or(a, b)  | Expr::And(a, b)
            | Expr::Eq(a, b) | Expr::Ne(a, b)
            | Expr::Lt(a, b) | Expr::Gt(a, b) | Expr::Le(a, b) | Expr::Ge(a, b)
            | Expr::ValueEq(a, b) | Expr::ValueNe(a, b)
            | Expr::ValueLt(a, b) | Expr::ValueGt(a, b)
            | Expr::ValueLe(a, b) | Expr::ValueGe(a, b)
            | Expr::Add(a, b) | Expr::Sub(a, b)
            | Expr::Mul(a, b) | Expr::Div(a, b) | Expr::Mod(a, b)
            | Expr::Union(a, b)
            | Expr::NodeBefore(a, b) | Expr::NodeAfter(a, b) | Expr::NodeIs(a, b) => expr_depth(a).max(expr_depth(b)),
            Expr::Neg(a) => expr_depth(a),
            Expr::Path(p) => path_depth(p),
            Expr::FilterPath { primary, predicates, steps } => {
                let primary_d = expr_depth(primary);
                let pred_d = predicates.iter()
                    .map(|p| 1 + expr_depth(p))
                    .max()
                    .unwrap_or(0);
                let step_d = steps.iter().map(step_depth).max().unwrap_or(0);
                primary_d.max(pred_d).max(step_d)
            }
            Expr::FunctionCall(_, args) =>
                args.iter().map(expr_depth).max().unwrap_or(0),
            Expr::Variable(_) | Expr::Literal(_)
            | Expr::Integer(_) | Expr::Decimal(_) | Expr::Double(_) => 0,
            Expr::IfThenElse { cond, then_branch, else_branch } =>
                expr_depth(cond)
                    .max(expr_depth(then_branch))
                    .max(expr_depth(else_branch)),
            Expr::For { bindings, body } | Expr::Let { bindings, body } => {
                let body_d = expr_depth(body);
                bindings.iter().map(|(_, e)| expr_depth(e)).max().unwrap_or(0).max(body_d)
            }
            Expr::Range(a, b)     => expr_depth(a).max(expr_depth(b)),
            Expr::SimpleMap(a, b) => expr_depth(a).max(expr_depth(b)),
            Expr::Sequence(items) =>
                items.iter().map(expr_depth).max().unwrap_or(0),
            Expr::Quantified { bindings, test, .. } => {
                let t = expr_depth(test);
                bindings.iter().map(|(_, e)| expr_depth(e)).max().unwrap_or(0).max(t)
            }
            Expr::IDiv(a, b) | Expr::Intersect(a, b) | Expr::Except(a, b)
                => expr_depth(a).max(expr_depth(b)),
            Expr::InstanceOf(a, _) | Expr::CastAs(a, _) | Expr::CastableAs(a, _)
            | Expr::TreatAs(a, _) => expr_depth(a),
            Expr::TryCatch { body, catches } => {
                let b = expr_depth(body);
                catches.iter().map(|c| expr_depth(&c.body)).max().unwrap_or(0).max(b)
            }
            Expr::WithDefaultCollation(_, inner) => expr_depth(inner),
            Expr::BackwardsCompat(inner) => expr_depth(inner),
            Expr::MapConstructor(entries) => entries.iter()
                .map(|(k, v)| expr_depth(k).max(expr_depth(v))).max().unwrap_or(0),
            Expr::ArrayConstructor { members, .. } =>
                members.iter().map(expr_depth).max().unwrap_or(0),
            Expr::Lookup(base, key) => expr_depth(base).max(lookup_key_depth(key)),
            Expr::UnaryLookup(key) => lookup_key_depth(key),
            Expr::InlineFunction { body, .. } => expr_depth(body),
            Expr::NamedFunctionRef { .. } | Expr::Placeholder | Expr::ContextItem => 0,
            Expr::DynamicCall { func, args } => expr_depth(func)
                .max(args.iter().map(expr_depth).max().unwrap_or(0)),
        }
    }
    fn lookup_key_depth(k: &LookupKey) -> u32 {
        match k {
            LookupKey::Expr(e) => expr_depth(e),
            _ => 0,
        }
    }
    fn step_depth(s: &Step) -> u32 {
        s.predicates.iter()
            .map(|p| 1 + expr_depth(p))
            .max()
            .unwrap_or(0)
    }
    fn path_depth(p: &LocationPath) -> u32 {
        let steps = match p {
            LocationPath::Absolute(s) | LocationPath::Relative(s) => s,
        };
        steps.iter().map(step_depth).max().unwrap_or(0)
    }
    expr_depth(expr)
}