Skip to main content

einvoice_xpath/
lib.rs

1//! Parser and syntax tree for the XPath 2.0 subset that the official e-invoicing validation
2//! stylesheets use, and for the XSLT pieces built on it: match patterns, sequence types
3//! (`as` attributes) and attribute value templates.
4//!
5//! Only syntax is handled here: function names are resolved to namespaces but not to
6//! functions, and nothing is evaluated. The subset is closed: a construct outside it is
7//! refused with an [`ErrorKind::Unsupported`] error naming it, never parsed as something
8//! else. Supported:
9//!
10//! - path expressions on the axes `child`, `attribute`, `self`, `parent`, `ancestor`,
11//!   `ancestor-or-self`, `descendant-or-self` (and `//`), `preceding-sibling`, `preceding`,
12//!   `following-sibling`; name tests, wildcards and the kind tests `node()`, `text()`,
13//!   `comment()`, `processing-instruction()`, `element(name?)`, `attribute(name?)`;
14//!   predicates; filter expressions as steps;
15//! - `for`, `some`, `every` (one or more bindings), `if then else`, `to`, `instance of`,
16//!   `cast as`, `castable as`, general and value comparisons, `or`, `and`, `+ - * div idiv mod`,
17//!   unary `-`, `|`, sequences, literals (numeric literals keep their kind and text),
18//!   variable references, `.`, function calls;
19//! - patterns of XSLT 2.0 without `id()`/`key()`, plus the XSLT 3.0 forms `root()` and
20//!   `(A | B)[p]`.
21//!
22//! Refused: `intersect`, `except`, the `union` keyword, `treat as`, node
23//! comparisons, unary `+`, XPath comments, the `descendant`, `following` and `namespace`
24//! axes, `document-node()`, `schema-element()`, `schema-attribute()`, typed `element()` and
25//! `attribute()` tests, `item()`, `empty-sequence()`, atomic types other than `xs:string`,
26//! `xs:anyURI`, `xs:boolean`, `xs:integer`, `xs:decimal` and `xs:date`, and every XPath 3.0
27//! addition except the zero-argument `document-uri()`, which is an ordinary function call
28//! here.
29//!
30//! ```
31//! use einvoice_xpath::{parse_expr, NamespaceMap, XS_NAMESPACE};
32//!
33//! let ns = NamespaceMap::new().bind("xs", XS_NAMESPACE);
34//! let expr = parse_expr("xs:decimal(Amount) * 2 = 10", &ns)?;
35//! assert_eq!(expr.to_string(), "xs:decimal(Amount) * 2 = 10");
36//! assert!(parse_expr("a intersect b", &ns).is_err());
37//! # Ok::<(), einvoice_xpath::Error>(())
38//! ```
39
40mod ast;
41mod error;
42mod lexer;
43mod names;
44mod parser;
45mod print;
46
47pub use ast::{
48    ArithmeticOp, AtomicKind, AtomicType, Avt, AvtPart, Axis, AxisStep, Binding, CompareKind,
49    ComparisonOp, Expr, ItemType, KindTest, Literal, NameOrWildcard, NameTest, NodeTest,
50    Occurrence, PathExpr, PathPattern, PathStart, Pattern, PatternAlternative, PatternAnchor,
51    PatternAxis, PatternLink, PatternStep, Quantifier, SequenceType, SingleType, Step,
52};
53pub use error::{Error, ErrorKind, Result};
54pub use names::{
55    FN_NAMESPACE, NamespaceMap, Namespaces, QName, XML_NAMESPACE, XS_NAMESPACE, is_name_char,
56    is_name_start_char, is_ncname, is_xml_whitespace,
57};
58
59use parser::Parser;
60
61/// Parses an XPath expression (XPath 2.0 production \[1\] `XPath ::= Expr`).
62///
63/// # Errors
64///
65/// A syntax error, a construct outside the supported subset, or an undeclared prefix.
66pub fn parse_expr(src: &str, namespaces: &dyn Namespaces) -> Result<Expr> {
67    let mut parser = Parser::new(src, 0, namespaces);
68    let expr = parser.expr()?;
69    parser.expect_end()?;
70    Ok(expr)
71}
72
73/// Parses an XSLT match pattern.
74///
75/// # Errors
76///
77/// A syntax error, a construct outside the supported subset, or an undeclared prefix.
78pub fn parse_pattern(src: &str, namespaces: &dyn Namespaces) -> Result<Pattern> {
79    let mut parser = Parser::new(src, 0, namespaces);
80    let pattern = parser.pattern()?;
81    parser.expect_end()?;
82    Ok(pattern)
83}
84
85/// Parses a sequence type, as in an XSLT `as` attribute.
86///
87/// # Errors
88///
89/// A syntax error, an unsupported or unknown type, or an undeclared prefix.
90pub fn parse_sequence_type(src: &str, namespaces: &dyn Namespaces) -> Result<SequenceType> {
91    let mut parser = Parser::new(src, 0, namespaces);
92    let sequence_type = parser.sequence_type()?;
93    parser.expect_end()?;
94    Ok(sequence_type)
95}
96
97/// Parses an attribute value template.
98///
99/// # Errors
100///
101/// A misplaced `}`, an empty or unterminated `{...}`, or an error in an expression.
102pub fn parse_avt(src: &str, namespaces: &dyn Namespaces) -> Result<Avt> {
103    parser::parse_avt(src, namespaces)
104}