darklua_core/nodes/statements/
numeric_for.rs

1use crate::nodes::{Block, Expression, Token, TypedIdentifier};
2
3/// Tokens associated with a numeric for statement.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct NumericForTokens {
6    pub r#for: Token,
7    pub equal: Token,
8    pub r#do: Token,
9    pub end: Token,
10    /// The token for the comma between the start and end values.
11    pub end_comma: Token,
12    /// The token for the comma between the end and step values.
13    pub step_comma: Option<Token>,
14}
15
16impl NumericForTokens {
17    super::impl_token_fns!(
18        target = [r#for, equal, r#do, end, end_comma]
19        iter = [step_comma]
20    );
21}
22
23/// Represents a numeric for loop statement.
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct NumericForStatement {
26    identifier: TypedIdentifier,
27    start: Expression,
28    end: Expression,
29    step: Option<Expression>,
30    block: Block,
31    tokens: Option<NumericForTokens>,
32}
33
34impl NumericForStatement {
35    /// Creates a new numeric for statement.
36    pub fn new<
37        S: Into<TypedIdentifier>,
38        E1: Into<Expression>,
39        E2: Into<Expression>,
40        B: Into<Block>,
41    >(
42        identifier: S,
43        start: E1,
44        end: E2,
45        step: Option<Expression>,
46        block: B,
47    ) -> Self {
48        Self {
49            identifier: identifier.into(),
50            start: start.into(),
51            end: end.into(),
52            step,
53            block: block.into(),
54            tokens: None,
55        }
56    }
57
58    /// Sets the tokens for this numeric for statement.
59    pub fn with_tokens(mut self, tokens: NumericForTokens) -> Self {
60        self.tokens = Some(tokens);
61        self
62    }
63
64    /// Sets the tokens for this numeric for statement.
65    #[inline]
66    pub fn set_tokens(&mut self, tokens: NumericForTokens) {
67        self.tokens = Some(tokens);
68    }
69
70    /// Returns the tokens for this numeric for statement, if any.
71    #[inline]
72    pub fn get_tokens(&self) -> Option<&NumericForTokens> {
73        self.tokens.as_ref()
74    }
75
76    /// Returns a mutable reference to the tokens, if any.
77    #[inline]
78    pub fn mutate_tokens(&mut self) -> Option<&mut NumericForTokens> {
79        self.tokens.as_mut()
80    }
81
82    /// Returns the loop's block.
83    #[inline]
84    pub fn get_block(&self) -> &Block {
85        &self.block
86    }
87
88    /// Returns a mutable reference to the block.
89    #[inline]
90    pub fn mutate_block(&mut self) -> &mut Block {
91        &mut self.block
92    }
93
94    /// Returns the start expression for the range.
95    #[inline]
96    pub fn get_start(&self) -> &Expression {
97        &self.start
98    }
99
100    /// Returns a mutable reference to the start expression.
101    #[inline]
102    pub fn mutate_start(&mut self) -> &mut Expression {
103        &mut self.start
104    }
105
106    /// Returns the end expression for the range.
107    #[inline]
108    pub fn get_end(&self) -> &Expression {
109        &self.end
110    }
111
112    /// Returns a mutable reference to the end expression.
113    #[inline]
114    pub fn mutate_end(&mut self) -> &mut Expression {
115        &mut self.end
116    }
117
118    /// Returns the step expression for the range, if any.
119    #[inline]
120    pub fn get_step(&self) -> Option<&Expression> {
121        self.step.as_ref()
122    }
123
124    /// Returns a mutable reference to the step expression option.
125    #[inline]
126    pub fn mutate_step(&mut self) -> &mut Option<Expression> {
127        &mut self.step
128    }
129
130    /// Returns the loop variable identifier.
131    #[inline]
132    pub fn get_identifier(&self) -> &TypedIdentifier {
133        &self.identifier
134    }
135
136    /// Returns a mutable reference to the loop variable identifier.
137    #[inline]
138    pub fn mutate_identifier(&mut self) -> &mut TypedIdentifier {
139        &mut self.identifier
140    }
141
142    /// Sets the loop variable identifier.
143    #[inline]
144    pub fn set_identifier<S: Into<TypedIdentifier>>(&mut self, identifier: S) {
145        self.identifier = identifier.into();
146    }
147
148    /// Removes type annotations from the loop variable.
149    pub fn clear_types(&mut self) {
150        self.identifier.remove_type();
151    }
152
153    /// Returns a mutable reference to the first token for this statement, creating it if missing.
154    pub fn mutate_first_token(&mut self) -> &mut Token {
155        self.set_default_tokens();
156        &mut self.tokens.as_mut().unwrap().r#for
157    }
158
159    /// Returns a mutable reference to the last token for this statement,
160    /// creating it if missing.
161    pub fn mutate_last_token(&mut self) -> &mut Token {
162        self.set_default_tokens();
163        &mut self.tokens.as_mut().unwrap().end
164    }
165
166    fn set_default_tokens(&mut self) {
167        if self.tokens.is_none() {
168            self.tokens = Some(NumericForTokens {
169                r#for: Token::from_content("for"),
170                equal: Token::from_content("="),
171                r#do: Token::from_content("do"),
172                end: Token::from_content("end"),
173                end_comma: Token::from_content(","),
174                step_comma: None,
175            });
176        }
177    }
178
179    super::impl_token_fns!(target = [identifier] iter = [tokens]);
180}