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
//! Parser and syntax tree for the XPath 2.0 subset that the official e-invoicing validation
//! stylesheets use, and for the XSLT pieces built on it: match patterns, sequence types
//! (`as` attributes) and attribute value templates.
//!
//! Only syntax is handled here: function names are resolved to namespaces but not to
//! functions, and nothing is evaluated. The subset is closed: a construct outside it is
//! refused with an [`ErrorKind::Unsupported`] error naming it, never parsed as something
//! else. Supported:
//!
//! - path expressions on the axes `child`, `attribute`, `self`, `parent`, `ancestor`,
//! `ancestor-or-self`, `descendant`, `descendant-or-self` (and `//`), `preceding-sibling`,
//! `preceding`, `following-sibling`; name tests, wildcards and the kind tests `node()`, `text()`,
//! `comment()`, `processing-instruction()`, `element(name?)`, `attribute(name?)`;
//! predicates; filter expressions as steps;
//! - `for`, `some`, `every` (one or more bindings), `if then else`, `to`, `instance of`,
//! `cast as`, `castable as`, general and value comparisons, `or`, `and`, `+ - * div idiv mod`,
//! unary `-`, `|`, sequences, literals (numeric literals keep their kind and text),
//! variable references, `.`, function calls;
//! - patterns of XSLT 2.0 without `id()`/`key()`, plus the XSLT 3.0 forms `root()` and
//! parenthesized patterns as Saxon-HE 12.10 reads them (see [`parse_pattern`]).
//!
//! Refused: `intersect`, `except`, the `union` keyword, `treat as`, node
//! comparisons, unary `+`, XPath comments, the `following` and `namespace` axes (in
//! patterns, the axes other than `child` and `attribute`), `document-node()`,
//! `schema-element()`, `schema-attribute()`, typed `element()` and `attribute()` tests, `item()`, `empty-sequence()`, atomic types other than `xs:string`,
//! `xs:anyURI`, `xs:boolean`, `xs:integer`, `xs:decimal` and `xs:date`, and every XPath 3.0
//! addition except the zero-argument `document-uri()`, which is an ordinary function call
//! here.
//!
//! Refused as well, with an [`ErrorKind::TooDeep`] error: an expression or pattern nested
//! deeper than [`MAX_DEPTH`], which would otherwise need a call stack in proportion to
//! its depth to be read, compiled and evaluated.
//!
//! ```
//! use einvoice_xpath::{parse_expr, NamespaceMap, XS_NAMESPACE};
//!
//! let ns = NamespaceMap::new().bind("xs", XS_NAMESPACE);
//! let expr = parse_expr("xs:decimal(Amount) * 2 = 10", &ns)?;
//! assert_eq!(expr.to_string(), "xs:decimal(Amount) * 2 = 10");
//! assert!(parse_expr("a intersect b", &ns).is_err());
//! # Ok::<(), einvoice_xpath::Error>(())
//! ```
pub use ;
pub use ;
pub use ;
use Parser;
/// The maximum depth of an expression or pattern: deeper, it is refused with an
/// [`ErrorKind::TooDeep`] error.
///
/// Reading, printing, compiling and evaluating an expression recurse over its tree, so
/// the call stack they need grows with its depth: without a bound, a short text nested
/// deeply enough (`((((...))))`, `1+1+...+1`) would overflow the stack of its thread,
/// which aborts the whole process. The depth is 1 for a literal, a variable reference,
/// `.` and a call without argument; every other expression is one level deeper than its
/// deepest operand. A chain of left-associative operators nests too (`a + b + c` is
/// `(a + b) + c`, depth 3), and so do the steps of a path (`a/b/c` is `(a/b)/c`), the
/// predicates of a step (each filters what the previous ones kept), the bindings of a
/// `for`, `some` or `every` (each binds inside the previous one) and the steps of a
/// pattern; parentheses add no level, but the nesting of parentheses is bounded by the
/// same maximum. The steps of a path written with `//` count the
/// `descendant-or-self::node()` step `//` stands for.
///
/// 128: the official stylesheets nest their expressions 32 deep at most (CEN, Peppol,
/// Factur-X, XRechnung and the French rules), most often through a chain of `or` or
/// `and`.
pub const MAX_DEPTH: u32 = 128;
/// Parses an XPath expression (XPath 2.0 production \[1\] `XPath ::= Expr`).
///
/// # Errors
///
/// A syntax error, a construct outside the supported subset, or an undeclared prefix.
/// Parses an XSLT match pattern.
///
/// XSLT 3.0 parenthesized patterns are read as Saxon-HE 12.10 reads them, normalised:
///
/// - `(P)` without predicate, as the whole of an alternative, is `P`: its alternatives
/// become alternatives of the pattern (`(a | b)` is `a | b`, two template rules with
/// their own default priorities when the template has no `priority`);
/// - a path ending with a parenthesized step without predicate is distributed over the
/// alternatives of that step: `r/(a | b/c)` is `r/a | r/b/c` (Saxon makes it a union
/// pattern, split like one);
/// - kept as a [`PathStep::Parenthesized`]: the first step of a path, with or without
/// predicates (`(a | b)[p]`, `(a/b | c)[p]/d`), and a step between two others
/// without predicate whose alternatives are single steps (`(a | b)/(c | d)/e`).
///
/// Refused as unsupported: a predicate on a parenthesized step after the first (Saxon
/// refuses most of them, XTSE0340), a parenthesized step nested inside another one that
/// is kept, and, inside a parenthesized step that is not the first, or after `/` or
/// `//`, alternatives other than single relative steps.
///
/// # Errors
///
/// A syntax error, a construct outside the supported subset, or an undeclared prefix.
/// Parses a sequence type, as in an XSLT `as` attribute.
///
/// # Errors
///
/// A syntax error, an unsupported or unknown type, or an undeclared prefix.
/// Parses an attribute value template.
///
/// # Errors
///
/// A misplaced `}`, an empty or unterminated `{...}`, or an error in an expression.