use crate::names::QName;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
Literal(Literal),
VarRef(QName),
ContextItem,
FunctionCall {
name: QName,
args: Vec<Expr>,
},
Sequence(Vec<Expr>),
Filter {
base: Box<Expr>,
predicates: Vec<Expr>,
},
Path(PathExpr),
Negate(Box<Expr>),
Cast {
operand: Box<Expr>,
target: SingleType,
},
Castable {
operand: Box<Expr>,
target: SingleType,
},
InstanceOf {
operand: Box<Expr>,
target: SequenceType,
},
Union(Box<Expr>, Box<Expr>),
Arithmetic {
op: ArithmeticOp,
lhs: Box<Expr>,
rhs: Box<Expr>,
},
Range(Box<Expr>, Box<Expr>),
Comparison {
op: ComparisonOp,
lhs: Box<Expr>,
rhs: Box<Expr>,
},
And(Box<Expr>, Box<Expr>),
Or(Box<Expr>, Box<Expr>),
If {
condition: Box<Expr>,
then: Box<Expr>,
otherwise: Box<Expr>,
},
For {
bindings: Vec<Binding>,
body: Box<Expr>,
},
Quantified {
quantifier: Quantifier,
bindings: Vec<Binding>,
satisfies: Box<Expr>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Binding {
pub variable: QName,
pub sequence: Expr,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Quantifier {
Some,
Every,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Literal {
String(String),
Integer(String),
Decimal(String),
Double(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArithmeticOp {
Add,
Subtract,
Multiply,
Divide,
IntegerDivide,
Modulo,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComparisonOp {
General(CompareKind),
Value(CompareKind),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CompareKind {
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathExpr {
pub start: PathStart,
pub steps: Vec<Step>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PathStart {
Relative,
Root,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Axis(AxisStep),
Expr(Expr),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AxisStep {
pub axis: Axis,
pub test: NodeTest,
pub predicates: Vec<Expr>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Axis {
Child,
Attribute,
SelfAxis,
Descendant,
DescendantOrSelf,
FollowingSibling,
Parent,
Ancestor,
AncestorOrSelf,
PrecedingSibling,
Preceding,
}
impl Axis {
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Child => "child",
Self::Attribute => "attribute",
Self::SelfAxis => "self",
Self::Descendant => "descendant",
Self::DescendantOrSelf => "descendant-or-self",
Self::FollowingSibling => "following-sibling",
Self::Parent => "parent",
Self::Ancestor => "ancestor",
Self::AncestorOrSelf => "ancestor-or-self",
Self::PrecedingSibling => "preceding-sibling",
Self::Preceding => "preceding",
}
}
#[must_use]
pub fn is_reverse(self) -> bool {
matches!(
self,
Self::Parent
| Self::Ancestor
| Self::AncestorOrSelf
| Self::PrecedingSibling
| Self::Preceding
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeTest {
Name(NameTest),
Kind(KindTest),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NameTest {
Name(QName),
Any,
AnyLocal {
prefix: String,
namespace: String,
},
AnyNamespace {
local: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KindTest {
AnyKind,
Text,
Comment,
ProcessingInstruction,
Element(Option<NameOrWildcard>),
Attribute(Option<NameOrWildcard>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NameOrWildcard {
Name(QName),
Wildcard,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SequenceType {
pub item: ItemType,
pub occurrence: Occurrence,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ItemType {
Atomic(AtomicType),
Kind(KindTest),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Occurrence {
One,
ZeroOrOne,
ZeroOrMore,
OneOrMore,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SingleType {
pub atomic: AtomicType,
pub allows_empty: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtomicType {
pub name: QName,
pub kind: AtomicKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AtomicKind {
String,
AnyUri,
Boolean,
Integer,
Decimal,
Date,
}
impl AtomicKind {
#[must_use]
pub fn from_local_name(local: &str) -> Option<Self> {
Some(match local {
"string" => Self::String,
"anyURI" => Self::AnyUri,
"boolean" => Self::Boolean,
"integer" => Self::Integer,
"decimal" => Self::Decimal,
"date" => Self::Date,
_ => return None,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pattern {
pub alternatives: Vec<PatternAlternative>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PatternAlternative {
DocumentRoot,
RootFunction,
Path(PathPattern),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathPattern {
pub anchor: PatternAnchor,
pub first: PathStep,
pub rest: Vec<(PatternLink, PathStep)>,
}
impl PathPattern {
#[must_use]
pub const fn single(anchor: PatternAnchor, step: PatternStep) -> Self {
Self {
anchor,
first: PathStep::Axis(step),
rest: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PathStep {
Axis(PatternStep),
Parenthesized {
inner: Box<Pattern>,
predicates: Vec<Expr>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternAnchor {
Relative,
Root,
RootDescendant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternLink {
Child,
Descendant,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatternStep {
pub axis: PatternAxis,
pub test: NodeTest,
pub predicates: Vec<Expr>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternAxis {
Child,
Attribute,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Avt {
pub parts: Vec<AvtPart>,
}
impl Avt {
#[must_use]
pub fn as_literal(&self) -> Option<String> {
let mut out = String::new();
for part in &self.parts {
match part {
AvtPart::Text(text) => out.push_str(text),
AvtPart::Expr(_) => return None,
}
}
Some(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AvtPart {
Text(String),
Expr(Expr),
}