Skip to main content

xsd_schema/xpath/ast/
operators.rs

1// ============================================================================
2// Operators
3// ============================================================================
4
5use super::{AstNodeId, SourceSpan};
6
7/// Operator flags for optimization hints.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub struct OpFlags {
10    pub atomize: bool,
11    pub singleton: bool,
12    pub ordered: bool,
13}
14
15impl OpFlags {
16    pub fn atomized() -> Self {
17        Self {
18            atomize: true,
19            ..Default::default()
20        }
21    }
22    pub fn singleton() -> Self {
23        Self {
24            singleton: true,
25            ..Default::default()
26        }
27    }
28    pub fn ordered() -> Self {
29        Self {
30            ordered: true,
31            ..Default::default()
32        }
33    }
34}
35
36/// Range expression (`expr to expr`).
37#[derive(Debug, Clone)]
38pub struct RangeNode {
39    /// Start of range.
40    pub start: AstNodeId,
41    /// End of range.
42    pub end: AstNodeId,
43    /// Source location.
44    pub span: SourceSpan,
45}
46
47impl RangeNode {
48    pub fn new(start: AstNodeId, end: AstNodeId, span: SourceSpan) -> Self {
49        Self { start, end, span }
50    }
51}
52
53/// Unary operator kind.
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum UnaryOpKind {
56    /// Unary minus (`-expr`).
57    Negate,
58    /// Unary plus / identity (`+expr`).
59    Identity,
60}
61
62/// Unary operator expression.
63#[derive(Debug, Clone)]
64pub struct UnaryOpNode {
65    /// Operator kind.
66    pub kind: UnaryOpKind,
67    /// Operand expression.
68    pub operand: AstNodeId,
69    /// Source location.
70    pub span: SourceSpan,
71}
72
73impl UnaryOpNode {
74    pub fn new(kind: UnaryOpKind, operand: AstNodeId, span: SourceSpan) -> Self {
75        Self {
76            kind,
77            operand,
78            span,
79        }
80    }
81}
82
83/// Binary operator kind.
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub enum BinaryOpKind {
86    // Logical
87    /// `or`
88    Or,
89    /// `and`
90    And,
91
92    // General comparisons
93    /// `=`
94    GeneralEq,
95    /// `!=`
96    GeneralNe,
97    /// `<`
98    GeneralLt,
99    /// `<=`
100    GeneralLe,
101    /// `>`
102    GeneralGt,
103    /// `>=`
104    GeneralGe,
105
106    // Value comparisons
107    /// `eq`
108    ValueEq,
109    /// `ne`
110    ValueNe,
111    /// `lt`
112    ValueLt,
113    /// `le`
114    ValueLe,
115    /// `gt`
116    ValueGt,
117    /// `ge`
118    ValueGe,
119
120    // Node comparisons
121    /// `is`
122    Is,
123    /// `<<`
124    Before,
125    /// `>>`
126    After,
127
128    // Arithmetic
129    /// `+`
130    Add,
131    /// `-`
132    Sub,
133    /// `*`
134    Mul,
135    /// `div`
136    Div,
137    /// `idiv`
138    IDiv,
139    /// `mod`
140    Mod,
141
142    // Sequence operations
143    /// `union` or `|`
144    Union,
145    /// `intersect`
146    Intersect,
147    /// `except`
148    Except,
149}
150
151/// Binary operator expression.
152#[derive(Debug, Clone)]
153pub struct BinaryOpNode {
154    /// Operator kind.
155    pub kind: BinaryOpKind,
156    /// Left operand.
157    pub left: AstNodeId,
158    /// Right operand.
159    pub right: AstNodeId,
160    /// Source location.
161    pub span: SourceSpan,
162    /// Operator flags for optimization hints.
163    pub flags: OpFlags,
164}
165
166impl BinaryOpNode {
167    pub fn new(kind: BinaryOpKind, left: AstNodeId, right: AstNodeId, span: SourceSpan) -> Self {
168        Self {
169            kind,
170            left,
171            right,
172            span,
173            flags: OpFlags::default(),
174        }
175    }
176}