einvoice-xpath 0.1.0

Parser and syntax tree for the XPath 2.0 subset used by the official e-invoicing validation stylesheets
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
//! Path expressions, steps, node tests and primary expressions (\[25\]–\[48\]).

use super::{NameRole, Parser};
use crate::ast::{
    Axis, AxisStep, Expr, KindTest, Literal, NameOrWildcard, NameTest, NodeTest, PathExpr,
    PathStart, Step,
};
use crate::error::{ErrorKind, Result};
use crate::lexer::Tok;

/// The names that cannot be function names (XPath 2.0 appendix A.3), because a name
/// followed by `(` is a kind test or an expression keyword.
const RESERVED_FUNCTION_NAMES: [&str; 13] = [
    "attribute",
    "comment",
    "document-node",
    "element",
    "empty-sequence",
    "if",
    "item",
    "node",
    "processing-instruction",
    "schema-attribute",
    "schema-element",
    "text",
    "typeswitch",
];

/// The names that start a kind test (\[54\] `KindTest`) when followed by `(`.
const KIND_TEST_NAMES: [&str; 9] = [
    "attribute",
    "comment",
    "document-node",
    "element",
    "node",
    "processing-instruction",
    "schema-attribute",
    "schema-element",
    "text",
];

/// `descendant-or-self::node()`, the step `//` stands for.
pub(crate) fn descendant_or_self_step() -> Step {
    Step::Axis(AxisStep {
        axis: Axis::DescendantOrSelf,
        test: NodeTest::Kind(KindTest::AnyKind),
        predicates: Vec::new(),
    })
}

impl Parser<'_> {
    /// \[25\] `PathExpr ::= ("/" RelativePathExpr?) | ("//" RelativePathExpr) | RelativePathExpr`.
    /// A lone `/` is a path only when no relative path can follow it (the
    /// "leading-lone-slash" constraint of appendix A.2.1.1).
    pub(super) fn path_expr(&mut self) -> Result<Expr> {
        match self.peek()? {
            Tok::Slash => {
                self.bump()?;
                let steps = if self.starts_step()? {
                    self.relative_path()?
                } else {
                    Vec::new()
                };
                Ok(Expr::Path(PathExpr {
                    start: PathStart::Root,
                    steps,
                }))
            }
            Tok::DoubleSlash => {
                self.bump()?;
                let mut steps = vec![descendant_or_self_step()];
                steps.extend(self.relative_path()?);
                Ok(Expr::Path(PathExpr {
                    start: PathStart::Root,
                    steps,
                }))
            }
            _ => {
                let mut steps = self.relative_path()?;
                if steps.len() == 1
                    && let Some(Step::Expr(_)) = steps.first()
                    && let Some(Step::Expr(expr)) = steps.pop()
                {
                    return Ok(expr);
                }
                Ok(Expr::Path(PathExpr {
                    start: PathStart::Relative,
                    steps,
                }))
            }
        }
    }

    /// Whether the current token can start a step (\[27\] `StepExpr`).
    pub(super) fn starts_step(&mut self) -> Result<bool> {
        Ok(matches!(
            self.peek()?,
            Tok::Name { .. }
                | Tok::PrefixWildcard(_)
                | Tok::LocalWildcard(_)
                | Tok::Star
                | Tok::At
                | Tok::Dot
                | Tok::DotDot
                | Tok::Dollar
                | Tok::LParen
                | Tok::String(_)
                | Tok::Integer(_)
                | Tok::Decimal(_)
                | Tok::Double(_)
        ))
    }

    /// \[26\] `RelativePathExpr ::= StepExpr (("/" | "//") StepExpr)*`
    fn relative_path(&mut self) -> Result<Vec<Step>> {
        let mut steps = vec![self.step()?];
        loop {
            match self.peek()? {
                Tok::Slash => {
                    self.bump()?;
                }
                Tok::DoubleSlash => {
                    self.bump()?;
                    steps.push(descendant_or_self_step());
                }
                _ => return Ok(steps),
            }
            steps.push(self.step()?);
        }
    }

    /// \[27\] `StepExpr ::= FilterExpr | AxisStep`
    fn step(&mut self) -> Result<Step> {
        let offset = self.offset()?;
        let tok = self.peek()?.clone();
        let axis_step = |parser: &mut Self, axis: Axis| -> Result<Step> {
            let test = parser.node_test(axis)?;
            let predicates = parser.predicates()?;
            Ok(Step::Axis(AxisStep {
                axis,
                test,
                predicates,
            }))
        };
        match tok {
            Tok::DotDot => {
                self.bump()?;
                let predicates = self.predicates()?;
                Ok(Step::Axis(AxisStep {
                    axis: Axis::Parent,
                    test: NodeTest::Kind(KindTest::AnyKind),
                    predicates,
                }))
            }
            Tok::At => {
                self.bump()?;
                axis_step(self, Axis::Attribute)
            }
            Tok::Name {
                prefix: None,
                local,
            } if *self.peek2()? == Tok::ColonColon => {
                let axis = self.axis(&local, offset)?;
                self.bump()?;
                self.bump()?;
                axis_step(self, axis)
            }
            Tok::Name {
                prefix: None,
                local,
            } if *self.peek2()? == Tok::LParen && KIND_TEST_NAMES.contains(&local.as_str()) => {
                // \[31\]: the default axis is `attribute` for an attribute test, else `child`.
                let axis = if local == "attribute" {
                    Axis::Attribute
                } else {
                    Axis::Child
                };
                axis_step(self, axis)
            }
            Tok::Name { .. } if *self.peek2()? == Tok::LParen => {
                Ok(Step::Expr(self.filter_expr()?))
            }
            Tok::Name { .. } | Tok::Star | Tok::PrefixWildcard(_) | Tok::LocalWildcard(_) => {
                axis_step(self, Axis::Child)
            }
            Tok::Dollar
            | Tok::LParen
            | Tok::Dot
            | Tok::String(_)
            | Tok::Integer(_)
            | Tok::Decimal(_)
            | Tok::Double(_) => Ok(Step::Expr(self.filter_expr()?)),
            _ => Err(self.unexpected("an expression")),
        }
    }

    /// \[30\] `ForwardAxis`, \[33\] `ReverseAxis`, limited to the supported axes.
    fn axis(&self, name: &str, offset: usize) -> Result<Axis> {
        Ok(match name {
            "child" => Axis::Child,
            "attribute" => Axis::Attribute,
            "self" => Axis::SelfAxis,
            "descendant-or-self" => Axis::DescendantOrSelf,
            "following-sibling" => Axis::FollowingSibling,
            "parent" => Axis::Parent,
            "ancestor" => Axis::Ancestor,
            "ancestor-or-self" => Axis::AncestorOrSelf,
            "preceding-sibling" => Axis::PrecedingSibling,
            "preceding" => Axis::Preceding,
            "descendant" | "following" | "namespace" => {
                return Err(self.error_at(
                    ErrorKind::Unsupported,
                    offset,
                    format!("axis `{name}::`"),
                ));
            }
            _ => {
                return Err(self.error_at(
                    ErrorKind::Syntax,
                    offset,
                    format!("unknown axis `{name}::`"),
                ));
            }
        })
    }

    /// \[35\] `NodeTest ::= KindTest | NameTest`. Unprefixed names take the default element
    /// namespace, except on the attribute axis.
    pub(super) fn node_test(&mut self, axis: Axis) -> Result<NodeTest> {
        let offset = self.offset()?;
        let tok = self.peek()?.clone();
        let role = if axis == Axis::Attribute {
            NameRole::NoNamespace
        } else {
            NameRole::Element
        };
        match tok {
            Tok::Name {
                prefix: None,
                local,
            } if *self.peek2()? == Tok::LParen && KIND_TEST_NAMES.contains(&local.as_str()) => {
                Ok(NodeTest::Kind(self.kind_test()?))
            }
            Tok::Name { prefix, local } => {
                self.bump()?;
                Ok(NodeTest::Name(NameTest::Name(
                    self.resolve(prefix, local, role, offset)?,
                )))
            }
            Tok::Star => {
                self.bump()?;
                Ok(NodeTest::Name(NameTest::Any))
            }
            Tok::PrefixWildcard(prefix) => {
                self.bump()?;
                let namespace = self.resolve_prefix(&prefix, offset)?;
                Ok(NodeTest::Name(NameTest::AnyLocal { prefix, namespace }))
            }
            Tok::LocalWildcard(local) => {
                self.bump()?;
                Ok(NodeTest::Name(NameTest::AnyNamespace { local }))
            }
            _ => Err(self.unexpected("a node test")),
        }
    }

    /// \[54\] `KindTest`, limited to `node()`, `text()`, `comment()`,
    /// `processing-instruction()`, `element(name?)` and `attribute(name?)`.
    pub(super) fn kind_test(&mut self) -> Result<KindTest> {
        let offset = self.offset()?;
        let Tok::Name { local, .. } = self.bump()?.tok else {
            return Err(self.error_at(ErrorKind::Syntax, offset, "expected a kind test"));
        };
        self.expect(&Tok::LParen, "`(`")?;
        let test = match local.as_str() {
            "node" => KindTest::AnyKind,
            "text" => KindTest::Text,
            "comment" => KindTest::Comment,
            "processing-instruction" => {
                if *self.peek()? != Tok::RParen {
                    return Err(self.error_here(
                        ErrorKind::Unsupported,
                        "`processing-instruction()` with a name argument",
                    ));
                }
                KindTest::ProcessingInstruction
            }
            "element" => KindTest::Element(self.kind_test_name(NameRole::Element, "element")?),
            "attribute" => {
                KindTest::Attribute(self.kind_test_name(NameRole::NoNamespace, "attribute")?)
            }
            other => {
                return Err(self.error_at(
                    ErrorKind::Unsupported,
                    offset,
                    format!("kind test `{other}()`"),
                ));
            }
        };
        self.expect(&Tok::RParen, "`)`")?;
        Ok(test)
    }

    /// The optional name or `*` of `element(...)` (\[64\]) or `attribute(...)` (\[60\]); a
    /// type name after it is refused.
    fn kind_test_name(&mut self, role: NameRole, kind: &str) -> Result<Option<NameOrWildcard>> {
        let offset = self.offset()?;
        let name = match self.peek()?.clone() {
            Tok::RParen => return Ok(None),
            Tok::Star => {
                self.bump()?;
                NameOrWildcard::Wildcard
            }
            Tok::Name { prefix, local } => {
                self.bump()?;
                NameOrWildcard::Name(self.resolve(prefix, local, role, offset)?)
            }
            _ => return Err(self.unexpected("a name, `*` or `)`")),
        };
        if *self.peek()? == Tok::Comma {
            return Err(self.error_here(ErrorKind::Unsupported, format!("type name in `{kind}()`")));
        }
        Ok(Some(name))
    }

    /// \[39\] `PredicateList ::= Predicate*`, \[40\] `Predicate ::= "[" Expr "]"`
    pub(super) fn predicates(&mut self) -> Result<Vec<Expr>> {
        let mut predicates = Vec::new();
        while *self.peek()? == Tok::LBracket {
            self.bump()?;
            predicates.push(self.expr()?);
            self.expect(&Tok::RBracket, "`]`")?;
        }
        Ok(predicates)
    }

    /// \[38\] `FilterExpr ::= PrimaryExpr PredicateList`
    fn filter_expr(&mut self) -> Result<Expr> {
        let base = self.primary_expr()?;
        let predicates = self.predicates()?;
        if predicates.is_empty() {
            Ok(base)
        } else {
            Ok(Expr::Filter {
                base: Box::new(base),
                predicates,
            })
        }
    }

    /// \[41\] `PrimaryExpr ::= Literal | VarRef | ParenthesizedExpr | ContextItemExpr | FunctionCall`
    fn primary_expr(&mut self) -> Result<Expr> {
        let token = self.bump()?;
        Ok(match token.tok {
            Tok::String(s) => Expr::Literal(Literal::String(s)),
            Tok::Integer(n) => Expr::Literal(Literal::Integer(n)),
            Tok::Decimal(n) => Expr::Literal(Literal::Decimal(n)),
            Tok::Double(n) => Expr::Literal(Literal::Double(n)),
            Tok::Dot => Expr::ContextItem,
            Tok::Dollar => Expr::VarRef(self.qname(NameRole::NoNamespace, "a variable name")?),
            Tok::LParen => {
                if *self.peek()? == Tok::RParen {
                    self.bump()?;
                    return Ok(Expr::Sequence(Vec::new()));
                }
                let inner = self.expr()?;
                self.expect(&Tok::RParen, "`)`")?;
                inner
            }
            Tok::Name { prefix, local } => self.function_call(prefix, local, token.start)?,
            other => {
                return Err(self.error_at(
                    ErrorKind::Syntax,
                    token.start,
                    format!("expected an expression, found {}", other.describe()),
                ));
            }
        })
    }

    /// \[48\] `FunctionCall ::= QName "(" (ExprSingle ("," ExprSingle)*)? ")"`; the name has
    /// been read.
    fn function_call(
        &mut self,
        prefix: Option<String>,
        local: String,
        offset: usize,
    ) -> Result<Expr> {
        if prefix.is_none() {
            if local == "function" {
                return Err(self.error_at(
                    ErrorKind::Unsupported,
                    offset,
                    "inline function (XPath 3.0)",
                ));
            }
            if RESERVED_FUNCTION_NAMES.contains(&local.as_str()) {
                return Err(self.error_at(
                    ErrorKind::Syntax,
                    offset,
                    format!("`{local}(` is not allowed here (reserved function name)"),
                ));
            }
        }
        let name = self.resolve(prefix, local, NameRole::Function, offset)?;
        self.expect(&Tok::LParen, "`(`")?;
        let mut args = Vec::new();
        if *self.peek()? == Tok::RParen {
            self.bump()?;
        } else {
            loop {
                args.push(self.expr_single()?);
                if self.bump_if_comma()? {
                    continue;
                }
                self.expect(&Tok::RParen, "`,` or `)`")?;
                break;
            }
        }
        Ok(Expr::FunctionCall { name, args })
    }

    fn bump_if_comma(&mut self) -> Result<bool> {
        if *self.peek()? == Tok::Comma {
            self.bump()?;
            Ok(true)
        } else {
            Ok(false)
        }
    }
}