darklua_core/nodes/statements/
numeric_for.rs1use crate::nodes::{Block, Expression, Token, TypedIdentifier};
2
3#[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 pub end_comma: Token,
12 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#[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 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 pub fn with_tokens(mut self, tokens: NumericForTokens) -> Self {
60 self.tokens = Some(tokens);
61 self
62 }
63
64 #[inline]
66 pub fn set_tokens(&mut self, tokens: NumericForTokens) {
67 self.tokens = Some(tokens);
68 }
69
70 #[inline]
72 pub fn get_tokens(&self) -> Option<&NumericForTokens> {
73 self.tokens.as_ref()
74 }
75
76 #[inline]
78 pub fn mutate_tokens(&mut self) -> Option<&mut NumericForTokens> {
79 self.tokens.as_mut()
80 }
81
82 #[inline]
84 pub fn get_block(&self) -> &Block {
85 &self.block
86 }
87
88 #[inline]
90 pub fn mutate_block(&mut self) -> &mut Block {
91 &mut self.block
92 }
93
94 #[inline]
96 pub fn get_start(&self) -> &Expression {
97 &self.start
98 }
99
100 #[inline]
102 pub fn mutate_start(&mut self) -> &mut Expression {
103 &mut self.start
104 }
105
106 #[inline]
108 pub fn get_end(&self) -> &Expression {
109 &self.end
110 }
111
112 #[inline]
114 pub fn mutate_end(&mut self) -> &mut Expression {
115 &mut self.end
116 }
117
118 #[inline]
120 pub fn get_step(&self) -> Option<&Expression> {
121 self.step.as_ref()
122 }
123
124 #[inline]
126 pub fn mutate_step(&mut self) -> &mut Option<Expression> {
127 &mut self.step
128 }
129
130 #[inline]
132 pub fn get_identifier(&self) -> &TypedIdentifier {
133 &self.identifier
134 }
135
136 #[inline]
138 pub fn mutate_identifier(&mut self) -> &mut TypedIdentifier {
139 &mut self.identifier
140 }
141
142 #[inline]
144 pub fn set_identifier<S: Into<TypedIdentifier>>(&mut self, identifier: S) {
145 self.identifier = identifier.into();
146 }
147
148 pub fn clear_types(&mut self) {
150 self.identifier.remove_type();
151 }
152
153 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 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}