darklua_core/nodes/statements/
generic_for.rs

1use crate::nodes::{Block, Expression, Token, TypedIdentifier};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub struct GenericForTokens {
5    pub r#for: Token,
6    pub r#in: Token,
7    pub r#do: Token,
8    pub end: Token,
9    pub identifier_commas: Vec<Token>,
10    pub value_commas: Vec<Token>,
11}
12
13impl GenericForTokens {
14    super::impl_token_fns!(
15        target = [r#for, r#in, r#do, end]
16        iter = [identifier_commas, value_commas]
17    );
18}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct GenericForStatement {
22    identifiers: Vec<TypedIdentifier>,
23    expressions: Vec<Expression>,
24    block: Block,
25    tokens: Option<GenericForTokens>,
26}
27
28impl GenericForStatement {
29    pub fn new<B: Into<Block>>(
30        identifiers: Vec<TypedIdentifier>,
31        expressions: Vec<Expression>,
32        block: B,
33    ) -> Self {
34        Self {
35            identifiers,
36            expressions,
37            block: block.into(),
38            tokens: None,
39        }
40    }
41
42    pub fn with_tokens(mut self, tokens: GenericForTokens) -> Self {
43        self.tokens = Some(tokens);
44        self
45    }
46
47    #[inline]
48    pub fn set_tokens(&mut self, tokens: GenericForTokens) {
49        self.tokens = Some(tokens);
50    }
51
52    #[inline]
53    pub fn get_tokens(&self) -> Option<&GenericForTokens> {
54        self.tokens.as_ref()
55    }
56
57    #[inline]
58    pub fn mutate_tokens(&mut self) -> Option<&mut GenericForTokens> {
59        self.tokens.as_mut()
60    }
61
62    #[inline]
63    pub fn get_block(&self) -> &Block {
64        &self.block
65    }
66
67    #[inline]
68    pub fn get_identifiers(&self) -> &Vec<TypedIdentifier> {
69        &self.identifiers
70    }
71
72    #[inline]
73    pub fn iter_identifiers(&self) -> impl Iterator<Item = &TypedIdentifier> {
74        self.identifiers.iter()
75    }
76
77    #[inline]
78    pub fn get_expressions(&self) -> &Vec<Expression> {
79        &self.expressions
80    }
81
82    #[inline]
83    pub fn iter_expressions(&self) -> impl Iterator<Item = &Expression> {
84        self.expressions.iter()
85    }
86
87    #[inline]
88    pub fn iter_mut_identifiers(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
89        self.identifiers.iter_mut()
90    }
91
92    #[inline]
93    pub fn iter_mut_expressions(&mut self) -> impl Iterator<Item = &mut Expression> {
94        self.expressions.iter_mut()
95    }
96
97    #[inline]
98    pub fn mutate_expressions(&mut self) -> &mut Vec<Expression> {
99        &mut self.expressions
100    }
101
102    #[inline]
103    pub fn mutate_block(&mut self) -> &mut Block {
104        &mut self.block
105    }
106
107    #[inline]
108    pub fn identifiers_len(&self) -> usize {
109        self.identifiers.len()
110    }
111
112    #[inline]
113    pub fn expressions_len(&self) -> usize {
114        self.expressions.len()
115    }
116
117    pub fn clear_types(&mut self) {
118        for identifier in &mut self.identifiers {
119            identifier.remove_type();
120        }
121    }
122
123    super::impl_token_fns!(iter = [tokens, identifiers]);
124}