xsd_schema/xpath/ast/types.rs
1// ============================================================================
2// Type Expressions
3// ============================================================================
4
5use super::{AstNodeId, KindTest, QName, SourceSpan};
6
7/// Kind of type expression.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum TypeExprKind {
10 /// `expr instance of type`
11 InstanceOf,
12 /// `expr treat as type`
13 TreatAs,
14 /// `expr cast as type`
15 CastAs,
16 /// `expr castable as type`
17 CastableAs,
18}
19
20/// Type expression (`instance of`, `treat as`, `cast as`, `castable as`).
21#[derive(Debug, Clone)]
22pub struct TypeExprNode {
23 /// Kind of type expression.
24 pub kind: TypeExprKind,
25 /// Operand expression.
26 pub operand: AstNodeId,
27 /// Target type (AST form with raw strings).
28 pub target_type: SequenceTypeNode,
29 /// Source location.
30 pub span: SourceSpan,
31 /// Resolved atomic type QName (populated during binding for Atomic item types).
32 /// Uses interned NameIds and resolved namespace URIs.
33 pub resolved_atomic_type: Option<crate::namespace::qname::QualifiedName>,
34}
35
36impl TypeExprNode {
37 pub fn new(
38 kind: TypeExprKind,
39 operand: AstNodeId,
40 target_type: SequenceTypeNode,
41 span: SourceSpan,
42 ) -> Self {
43 Self {
44 kind,
45 operand,
46 target_type,
47 span,
48 resolved_atomic_type: None,
49 }
50 }
51}
52
53/// Occurrence indicator for sequence types.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
55pub enum OccurrenceIndicator {
56 /// Exactly one (no indicator).
57 #[default]
58 One,
59 /// Zero or one (`?`).
60 ZeroOrOne,
61 /// Zero or more (`*`).
62 ZeroOrMore,
63 /// One or more (`+`).
64 OneOrMore,
65}
66
67/// Sequence type (`item-type occurrence-indicator?`).
68#[derive(Debug, Clone)]
69pub struct SequenceTypeNode {
70 /// Item type (None = `empty-sequence()`).
71 pub item_type: Option<ItemTypeNode>,
72 /// Occurrence indicator.
73 pub occurrence: OccurrenceIndicator,
74 /// Source location.
75 pub span: SourceSpan,
76}
77
78impl SequenceTypeNode {
79 /// `empty-sequence()`
80 pub fn empty(span: SourceSpan) -> Self {
81 Self {
82 item_type: None,
83 occurrence: OccurrenceIndicator::One,
84 span,
85 }
86 }
87
88 /// Single item type with optional occurrence.
89 pub fn single(
90 item_type: ItemTypeNode,
91 occurrence: OccurrenceIndicator,
92 span: SourceSpan,
93 ) -> Self {
94 Self {
95 item_type: Some(item_type),
96 occurrence,
97 span,
98 }
99 }
100}
101
102/// Item type in a sequence type.
103#[derive(Debug, Clone)]
104pub enum ItemTypeNode {
105 /// `item()` - any item.
106 Item,
107 /// Atomic type (QName like `xs:integer`).
108 Atomic(QName),
109 /// Kind test (`node()`, `element()`, etc.).
110 Kind(KindTest),
111}