darklua_core/nodes/statements/
while_statement.rs

1use crate::nodes::{token::Token, Block, Expression};
2
3/// Tokens associated with a while statement.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct WhileTokens {
6    pub r#while: Token,
7    pub r#do: Token,
8    pub end: Token,
9}
10
11impl WhileTokens {
12    super::impl_token_fns!(target = [r#while, r#do, end]);
13}
14
15/// Represents a while loop statement.
16#[derive(Clone, Debug, PartialEq, Eq)]
17pub struct WhileStatement {
18    block: Block,
19    condition: Expression,
20    tokens: Option<WhileTokens>,
21}
22
23impl WhileStatement {
24    /// Creates a new while statement with the given block and condition.
25    pub fn new<B: Into<Block>, E: Into<Expression>>(block: B, condition: E) -> Self {
26        Self {
27            block: block.into(),
28            condition: condition.into(),
29            tokens: None,
30        }
31    }
32
33    /// Sets the tokens for this while statement.
34    pub fn with_tokens(mut self, tokens: WhileTokens) -> Self {
35        self.tokens = Some(tokens);
36        self
37    }
38
39    /// Returns the loop's block.
40    #[inline]
41    pub fn get_block(&self) -> &Block {
42        &self.block
43    }
44
45    /// Returns the condition for this while loop.
46    #[inline]
47    pub fn get_condition(&self) -> &Expression {
48        &self.condition
49    }
50
51    /// Returns a mutable reference to the block.
52    #[inline]
53    pub fn mutate_block(&mut self) -> &mut Block {
54        &mut self.block
55    }
56
57    /// Returns a mutable reference to the condition.
58    #[inline]
59    pub fn mutate_condition(&mut self) -> &mut Expression {
60        &mut self.condition
61    }
62
63    /// Sets the tokens for this while statement.
64    #[inline]
65    pub fn set_tokens(&mut self, tokens: WhileTokens) {
66        self.tokens = Some(tokens);
67    }
68
69    /// Returns the tokens for this while statement, if any.
70    #[inline]
71    pub fn get_tokens(&self) -> Option<&WhileTokens> {
72        self.tokens.as_ref()
73    }
74
75    /// Returns a mutable reference to the tokens, if any.
76    #[inline]
77    pub fn mutate_tokens(&mut self) -> Option<&mut WhileTokens> {
78        self.tokens.as_mut()
79    }
80
81    /// Returns a mutable reference to the first token for this statement, creating it if missing.
82    pub fn mutate_first_token(&mut self) -> &mut Token {
83        self.set_default_tokens();
84        &mut self.tokens.as_mut().unwrap().r#while
85    }
86
87    /// Returns a mutable reference to the last token for this statement,
88    /// creating it if missing.
89    pub fn mutate_last_token(&mut self) -> &mut Token {
90        self.set_default_tokens();
91        &mut self.tokens.as_mut().unwrap().end
92    }
93
94    fn set_default_tokens(&mut self) {
95        if self.tokens.is_none() {
96            self.tokens = Some(WhileTokens {
97                r#while: Token::from_content("while"),
98                r#do: Token::from_content("do"),
99                end: Token::from_content("end"),
100            });
101        }
102    }
103
104    super::impl_token_fns!(iter = [tokens]);
105}