darklua_core/nodes/statements/
repeat_statement.rs

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