1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::nodes::Token;
use super::Expression;
/// Represents an if expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IfExpression {
condition: Expression,
result: Expression,
else_result: Expression,
branches: Vec<ElseIfExpressionBranch>,
tokens: Option<IfExpressionTokens>,
}
impl IfExpression {
/// Creates a new if expression with the given condition, result, and else result.
pub fn new<E: Into<Expression>, E2: Into<Expression>, E3: Into<Expression>>(
condition: E,
result: E2,
else_result: E3,
) -> Self {
Self {
condition: condition.into(),
result: result.into(),
else_result: else_result.into(),
branches: Vec::new(),
tokens: None,
}
}
/// Attaches tokens to this if expression.
pub fn with_tokens(mut self, tokens: IfExpressionTokens) -> Self {
self.tokens = Some(tokens);
self
}
/// Adds an elseif branch to this if expression.
pub fn with_branch<E: Into<Expression>, E2: Into<Expression>>(
mut self,
condition: E,
result: E2,
) -> Self {
self.branches
.push(ElseIfExpressionBranch::new(condition, result));
self
}
/// Adds an elseif branch to this if expression.
#[inline]
pub fn push_branch(&mut self, branch: ElseIfExpressionBranch) {
self.branches.push(branch);
}
/// Attaches tokens to this if expression.
#[inline]
pub fn set_tokens(&mut self, tokens: IfExpressionTokens) {
self.tokens = Some(tokens);
}
/// Returns a reference to the tokens attached to this if expression, if any.
#[inline]
pub fn get_tokens(&self) -> Option<&IfExpressionTokens> {
self.tokens.as_ref()
}
/// Returns a reference to the condition of this if expression.
#[inline]
pub fn get_condition(&self) -> &Expression {
&self.condition
}
/// Returns a mutable reference to the condition of this if expression.
#[inline]
pub fn mutate_condition(&mut self) -> &mut Expression {
&mut self.condition
}
/// Returns a reference to the result of this if expression (returned when condition is true).
#[inline]
pub fn get_result(&self) -> &Expression {
&self.result
}
/// Returns a mutable reference to the result of this if expression.
#[inline]
pub fn mutate_result(&mut self) -> &mut Expression {
&mut self.result
}
/// Returns a reference to the else result of this if expression.
#[inline]
pub fn get_else_result(&self) -> &Expression {
&self.else_result
}
/// Returns a mutable reference to the else result of this if expression.
#[inline]
pub fn mutate_else_result(&mut self) -> &mut Expression {
&mut self.else_result
}
/// Returns whether this if expression has any elseif branches.
#[inline]
pub fn has_elseif_branch(&self) -> bool {
!self.branches.is_empty()
}
/// Returns an iterator over the elseif branches of this if expression.
#[inline]
pub fn iter_branches(&self) -> impl Iterator<Item = &ElseIfExpressionBranch> {
self.branches.iter()
}
/// Removes all elseif branches from this if expression.
#[inline]
pub fn clear_elseif_branches(&mut self) {
self.branches.clear();
}
/// Retains only the elseif branches that satisfy the predicate.
#[inline]
pub fn retain_elseif_branches_mut(
&mut self,
filter: impl FnMut(&mut ElseIfExpressionBranch) -> bool,
) {
self.branches.retain_mut(filter);
}
/// Removes an elseif branch at the specified index and returns it.
pub fn remove_branch(&mut self, index: usize) -> Option<ElseIfExpressionBranch> {
if index < self.branches.len() {
Some(self.branches.remove(index))
} else {
None
}
}
/// Returns a mutable iterator over the elseif branches of this if expression.
#[inline]
pub fn iter_mut_branches(&mut self) -> impl Iterator<Item = &mut ElseIfExpressionBranch> {
self.branches.iter_mut()
}
/// Returns a mutable reference to the first token for this if expression,
/// creating it if missing.
pub fn mutate_first_token(&mut self) -> &mut Token {
if self.tokens.is_none() {
self.tokens = Some(IfExpressionTokens {
r#if: Token::from_content("if"),
then: Token::from_content("then"),
r#else: Token::from_content("else"),
});
}
&mut self.tokens.as_mut().unwrap().r#if
}
/// Returns a mutable reference to the last token for this if expression,
/// creating it if missing.
pub fn mutate_last_token(&mut self) -> &mut Token {
self.result.mutate_last_token()
}
super::impl_token_fns!(iter = [tokens, branches]);
}
/// Represents an elseif branch in an if expression.
///
/// Each branch has a condition and a result expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ElseIfExpressionBranch {
condition: Expression,
result: Expression,
tokens: Option<ElseIfExpressionBranchTokens>,
}
impl ElseIfExpressionBranch {
/// Creates a new elseif branch with the given condition and result.
pub fn new<E: Into<Expression>, E2: Into<Expression>>(condition: E, result: E2) -> Self {
Self {
condition: condition.into(),
result: result.into(),
tokens: None,
}
}
/// Attaches tokens to this elseif branch.
#[inline]
pub fn set_tokens(&mut self, tokens: ElseIfExpressionBranchTokens) {
self.tokens = Some(tokens);
}
/// Returns a reference to the tokens attached to this elseif branch, if any.
#[inline]
pub fn get_tokens(&self) -> Option<&ElseIfExpressionBranchTokens> {
self.tokens.as_ref()
}
/// Returns a reference to the condition of this elseif branch.
#[inline]
pub fn get_condition(&self) -> &Expression {
&self.condition
}
/// Returns a mutable reference to the condition of this elseif branch.
#[inline]
pub fn mutate_condition(&mut self) -> &mut Expression {
&mut self.condition
}
/// Returns a reference to the result of this elseif branch.
#[inline]
pub fn get_result(&self) -> &Expression {
&self.result
}
/// Returns a mutable reference to the result of this elseif branch.
#[inline]
pub fn mutate_result(&mut self) -> &mut Expression {
&mut self.result
}
/// Consumes this branch and returns a tuple of (condition, result).
pub fn into_expressions(self) -> (Expression, Expression) {
(self.condition, self.result)
}
super::impl_token_fns!(iter = [tokens]);
}
/// Contains token information for an if expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IfExpressionTokens {
/// The 'if' keyword token
pub r#if: Token,
/// The 'then' keyword token
pub then: Token,
/// The 'else' keyword token
pub r#else: Token,
}
impl IfExpressionTokens {
super::impl_token_fns!(target = [r#if, then, r#else]);
}
/// Contains token information for an elseif branch in an if expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ElseIfExpressionBranchTokens {
/// The 'elseif' keyword token
pub elseif: Token,
/// The 'then' keyword token
pub then: Token,
}
impl ElseIfExpressionBranchTokens {
super::impl_token_fns!(target = [elseif, then]);
}