Skip to main content

xsd_schema/xpath/ast/
control_flow.rs

1// ============================================================================
2// Control Flow Nodes
3// ============================================================================
4
5use super::{AstNodeId, SourceSpan};
6
7/// Conditional expression (`if (test) then expr else expr`).
8#[derive(Debug, Clone)]
9pub struct IfNode {
10    /// Test/condition expression.
11    pub test: AstNodeId,
12    /// Then branch expression.
13    pub then_branch: AstNodeId,
14    /// Else branch expression.
15    pub else_branch: AstNodeId,
16    /// Source location.
17    pub span: SourceSpan,
18}
19
20impl IfNode {
21    pub fn new(
22        test: AstNodeId,
23        then_branch: AstNodeId,
24        else_branch: AstNodeId,
25        span: SourceSpan,
26    ) -> Self {
27        Self {
28            test,
29            then_branch,
30            else_branch,
31            span,
32        }
33    }
34}
35
36/// Variable binding in a for/quantified expression.
37#[derive(Debug, Clone)]
38pub struct ForBinding {
39    /// Variable name prefix.
40    pub prefix: String,
41    /// Variable local name.
42    pub local_name: String,
43    /// Expression producing the sequence to iterate.
44    pub in_expr: AstNodeId,
45    /// Resolved variable slot (set during binding phase).
46    pub slot: Option<u32>,
47    /// Source location.
48    pub span: SourceSpan,
49}
50
51impl ForBinding {
52    pub fn new(prefix: String, local_name: String, in_expr: AstNodeId, span: SourceSpan) -> Self {
53        Self {
54            prefix,
55            local_name,
56            in_expr,
57            slot: None,
58            span,
59        }
60    }
61}
62
63/// For expression (`for $x in expr return expr`).
64///
65/// Multiple bindings: `for $x in xs, $y in ys return ...`
66#[derive(Debug, Clone)]
67pub struct ForNode {
68    /// List of variable bindings.
69    pub bindings: Vec<ForBinding>,
70    /// Return expression.
71    pub return_expr: AstNodeId,
72    /// Source location.
73    pub span: SourceSpan,
74}
75
76impl ForNode {
77    pub fn new(bindings: Vec<ForBinding>, return_expr: AstNodeId, span: SourceSpan) -> Self {
78        Self {
79            bindings,
80            return_expr,
81            span,
82        }
83    }
84}
85
86/// Kind of quantified expression.
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub enum QuantifierKind {
89    /// `some $x in ... satisfies`
90    Some,
91    /// `every $x in ... satisfies`
92    Every,
93}
94
95/// Quantified expression (`some`/`every $x in expr satisfies expr`).
96#[derive(Debug, Clone)]
97pub struct QuantifiedNode {
98    /// Quantifier kind (some or every).
99    pub kind: QuantifierKind,
100    /// List of variable bindings.
101    pub bindings: Vec<ForBinding>,
102    /// Satisfies expression.
103    pub satisfies: AstNodeId,
104    /// Source location.
105    pub span: SourceSpan,
106}
107
108impl QuantifiedNode {
109    pub fn new(
110        kind: QuantifierKind,
111        bindings: Vec<ForBinding>,
112        satisfies: AstNodeId,
113        span: SourceSpan,
114    ) -> Self {
115        Self {
116            kind,
117            bindings,
118            satisfies,
119            span,
120        }
121    }
122}