darklua_core/nodes/statements/
if_statement.rs1use std::mem;
2
3use crate::nodes::{Block, Expression, Token};
4
5#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct IfBranchTokens {
8 pub elseif: Token,
9 pub then: Token,
10}
11
12impl IfBranchTokens {
13 super::impl_token_fns!(target = [elseif, then]);
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct IfBranch {
19 condition: Expression,
20 block: Block,
21 tokens: Option<IfBranchTokens>,
22}
23
24impl IfBranch {
25 pub fn new<E: Into<Expression>, B: Into<Block>>(condition: E, block: B) -> Self {
27 Self {
28 condition: condition.into(),
29 block: block.into(),
30 tokens: None,
31 }
32 }
33
34 pub fn empty<E: Into<Expression>>(condition: E) -> Self {
36 Self {
37 condition: condition.into(),
38 block: Block::default(),
39 tokens: None,
40 }
41 }
42
43 pub fn with_tokens(mut self, tokens: IfBranchTokens) -> Self {
45 self.tokens = Some(tokens);
46 self
47 }
48
49 #[inline]
51 pub fn set_tokens(&mut self, tokens: IfBranchTokens) {
52 self.tokens = Some(tokens);
53 }
54
55 #[inline]
57 pub fn get_tokens(&self) -> Option<&IfBranchTokens> {
58 self.tokens.as_ref()
59 }
60
61 #[inline]
63 pub fn get_block(&self) -> &Block {
64 &self.block
65 }
66
67 #[inline]
69 pub fn get_condition(&self) -> &Expression {
70 &self.condition
71 }
72
73 #[inline]
75 pub fn mutate_block(&mut self) -> &mut Block {
76 &mut self.block
77 }
78
79 #[inline]
81 pub fn take_block(&mut self) -> Block {
82 mem::take(&mut self.block)
83 }
84
85 #[inline]
87 pub fn mutate_condition(&mut self) -> &mut Expression {
88 &mut self.condition
89 }
90
91 super::impl_token_fns!(iter = [tokens]);
92}
93
94#[derive(Clone, Debug, PartialEq, Eq)]
96pub struct IfStatementTokens {
97 pub r#if: Token,
98 pub then: Token,
99 pub end: Token,
100 pub r#else: Option<Token>,
101}
102
103impl IfStatementTokens {
104 super::impl_token_fns!(
105 target = [r#if, then, end]
106 iter = [r#else]
107 );
108}
109
110#[derive(Clone, Debug, PartialEq, Eq)]
112pub struct IfStatement {
113 branches: Vec<IfBranch>,
114 else_block: Option<Block>,
115 tokens: Option<IfStatementTokens>,
116}
117
118impl IfStatement {
119 pub fn new(branches: Vec<IfBranch>, else_block: Option<Block>) -> Self {
121 Self {
122 branches,
123 else_block,
124 tokens: None,
125 }
126 }
127
128 pub fn create(condition: impl Into<Expression>, block: impl Into<Block>) -> Self {
130 Self {
131 branches: vec![IfBranch::new(condition, block)],
132 else_block: None,
133 tokens: None,
134 }
135 }
136
137 pub fn with_tokens(mut self, tokens: IfStatementTokens) -> Self {
139 self.tokens = Some(tokens);
140 self
141 }
142
143 #[inline]
145 pub fn set_tokens(&mut self, tokens: IfStatementTokens) {
146 self.tokens = Some(tokens);
147 }
148
149 #[inline]
151 pub fn get_tokens(&self) -> Option<&IfStatementTokens> {
152 self.tokens.as_ref()
153 }
154
155 #[inline]
157 pub fn mutate_tokens(&mut self) -> Option<&mut IfStatementTokens> {
158 self.tokens.as_mut()
159 }
160
161 pub fn with_branch(mut self, branch: IfBranch) -> Self {
163 self.branches.push(branch);
164 self
165 }
166
167 pub fn with_new_branch(
169 mut self,
170 condition: impl Into<Expression>,
171 block: impl Into<Block>,
172 ) -> Self {
173 self.branches.push(IfBranch::new(condition, block));
174 self
175 }
176
177 pub fn with_else_block<B: Into<Block>>(mut self, block: B) -> Self {
179 self.else_block.replace(block.into());
180 self
181 }
182
183 pub fn mutate_all_blocks(&mut self) -> Vec<&mut Block> {
185 let mut blocks: Vec<&mut Block> = self
186 .branches
187 .iter_mut()
188 .map(|branch| branch.mutate_block())
189 .collect();
190
191 if let Some(else_block) = &mut self.else_block {
192 blocks.push(else_block);
193 };
194
195 blocks
196 }
197
198 #[inline]
200 pub fn get_branches(&self) -> &Vec<IfBranch> {
201 &self.branches
202 }
203
204 #[inline]
206 pub fn iter_branches(&self) -> impl Iterator<Item = &IfBranch> {
207 self.branches.iter()
208 }
209
210 #[inline]
212 pub fn branch_count(&self) -> usize {
213 self.branches.len()
214 }
215
216 #[inline]
218 pub fn mutate_branches(&mut self) -> &mut Vec<IfBranch> {
219 &mut self.branches
220 }
221
222 #[inline]
224 pub fn push_new_branch(&mut self, condition: impl Into<Expression>, block: impl Into<Block>) {
225 self.branches
226 .push(IfBranch::new(condition.into(), block.into()));
227 }
228
229 #[inline]
231 pub fn push_branch(&mut self, branch: IfBranch) {
232 self.branches.push(branch);
233 }
234
235 #[inline]
237 pub fn get_else_block(&self) -> Option<&Block> {
238 self.else_block.as_ref()
239 }
240
241 #[inline]
243 pub fn mutate_else_block(&mut self) -> &mut Option<Block> {
244 &mut self.else_block
245 }
246
247 #[inline]
249 pub fn set_else_block(&mut self, block: impl Into<Block>) {
250 self.else_block = Some(block.into());
251 }
252
253 #[inline]
255 pub fn take_else_block(&mut self) -> Option<Block> {
256 self.else_block.take()
257 }
258
259 pub fn retain_branches_mut(&mut self, filter: impl FnMut(&mut IfBranch) -> bool) -> bool {
261 self.branches.retain_mut(filter);
262 if self.branches.is_empty() {
263 self.branches.push(IfBranch::new(false, Block::default()));
265 true
266 } else {
267 false
268 }
269 }
270
271 pub fn mutate_first_token(&mut self) -> &mut Token {
273 self.set_default_tokens();
274 &mut self.tokens.as_mut().unwrap().r#if
275 }
276
277 pub fn mutate_last_token(&mut self) -> &mut Token {
280 self.set_default_tokens();
281 &mut self.tokens.as_mut().unwrap().end
282 }
283
284 fn set_default_tokens(&mut self) {
285 if self.tokens.is_none() {
286 self.tokens = Some(IfStatementTokens {
287 r#if: Token::from_content("if"),
288 then: Token::from_content("then"),
289 end: Token::from_content("end"),
290 r#else: None,
291 });
292 }
293 }
294
295 super::impl_token_fns!(iter = [tokens, branches]);
296}