darklua_core/nodes/expressions/
function.rs1use crate::nodes::{
2 Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType, GenericParameters, Token,
3 TypedIdentifier,
4};
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
17pub struct FunctionExpression {
18 block: Block,
19 parameters: Vec<TypedIdentifier>,
20 is_variadic: bool,
21 variadic_type: Option<FunctionVariadicType>,
22 return_type: Option<FunctionReturnType>,
23 generic_parameters: Option<GenericParameters>,
24 tokens: Option<Box<FunctionBodyTokens>>,
25}
26
27impl FunctionExpression {
28 pub fn new(block: Block, parameters: Vec<TypedIdentifier>, is_variadic: bool) -> Self {
30 Self {
31 block,
32 parameters,
33 is_variadic,
34 variadic_type: None,
35 return_type: None,
36 generic_parameters: None,
37 tokens: None,
38 }
39 }
40
41 pub fn from_block<B: Into<Block>>(block: B) -> Self {
43 Self {
44 block: block.into(),
45 parameters: Vec::new(),
46 is_variadic: false,
47 variadic_type: None,
48 return_type: None,
49 generic_parameters: None,
50 tokens: None,
51 }
52 }
53
54 pub fn with_parameters(mut self, parameters: Vec<TypedIdentifier>) -> Self {
56 self.parameters = parameters;
57 self
58 }
59
60 pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
62 self.parameters.push(parameter.into());
63 self
64 }
65
66 pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
70 self.is_variadic = true;
71 self.variadic_type = Some(r#type.into());
72 self
73 }
74
75 pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
77 self.return_type = Some(return_type.into());
78 self
79 }
80
81 #[inline]
83 pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
84 self.return_type = Some(return_type.into());
85 }
86
87 #[inline]
89 pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
90 self.return_type.as_ref()
91 }
92
93 #[inline]
95 pub fn has_return_type(&self) -> bool {
96 self.return_type.is_some()
97 }
98
99 #[inline]
101 pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
102 self.return_type.as_mut()
103 }
104
105 pub fn variadic(mut self) -> Self {
107 self.is_variadic = true;
108 self
109 }
110
111 pub fn set_variadic(&mut self, is_variadic: bool) {
115 self.is_variadic = is_variadic;
116 if !is_variadic && self.variadic_type.is_some() {
117 self.variadic_type.take();
118 }
119 }
120
121 pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
125 self.is_variadic = true;
126 self.variadic_type = Some(r#type.into());
127 }
128
129 #[inline]
131 pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
132 self.variadic_type.as_ref()
133 }
134
135 #[inline]
137 pub fn has_variadic_type(&self) -> bool {
138 self.variadic_type.is_some()
139 }
140
141 #[inline]
143 pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
144 self.variadic_type.as_mut()
145 }
146
147 pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
149 self.generic_parameters = Some(generic_parameters);
150 self
151 }
152
153 #[inline]
155 pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
156 self.generic_parameters = Some(generic_parameters);
157 }
158
159 #[inline]
161 pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
162 self.generic_parameters.as_ref()
163 }
164
165 #[inline]
167 pub fn is_generic(&self) -> bool {
168 self.generic_parameters.is_some()
169 }
170
171 pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
173 self.tokens = Some(tokens.into());
174 self
175 }
176
177 #[inline]
179 pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
180 self.tokens = Some(tokens.into());
181 }
182
183 #[inline]
185 pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
186 self.tokens.as_ref().map(|tokens| tokens.as_ref())
187 }
188
189 #[inline]
191 pub fn get_block(&self) -> &Block {
192 &self.block
193 }
194
195 #[inline]
197 pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
198 &self.parameters
199 }
200
201 #[inline]
203 pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
204 self.parameters.iter()
205 }
206
207 #[inline]
209 pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
210 self.parameters.iter_mut()
211 }
212
213 #[inline]
215 pub fn is_variadic(&self) -> bool {
216 self.is_variadic
217 }
218
219 #[inline]
221 pub fn mutate_block(&mut self) -> &mut Block {
222 &mut self.block
223 }
224
225 #[inline]
227 pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
228 &mut self.parameters
229 }
230
231 #[inline]
233 pub fn parameters_count(&self) -> usize {
234 self.parameters.len()
235 }
236
237 #[inline]
239 pub fn has_parameters(&self) -> bool {
240 !self.parameters.is_empty()
241 }
242
243 pub fn clear_types(&mut self) {
247 self.return_type.take();
248 self.variadic_type.take();
249 self.generic_parameters.take();
250 for parameter in &mut self.parameters {
251 parameter.remove_type();
252 }
253 if let Some(tokens) = &mut self.tokens {
254 tokens.variable_arguments_colon.take();
255 }
256 }
257
258 pub fn mutate_first_token(&mut self) -> &mut Token {
261 self.set_default_tokens();
262 &mut self.tokens.as_mut().unwrap().function
263 }
264
265 pub fn mutate_last_token(&mut self) -> &mut Token {
268 self.set_default_tokens();
269 &mut self.tokens.as_mut().unwrap().end
270 }
271
272 fn set_default_tokens(&mut self) {
273 if self.tokens.is_none() {
274 self.tokens = Some(Box::new(FunctionBodyTokens {
275 function: Token::from_content("function"),
276 opening_parenthese: Token::from_content("("),
277 closing_parenthese: Token::from_content(")"),
278 end: Token::from_content("end"),
279 parameter_commas: Vec::new(),
280 variable_arguments: self.is_variadic.then(|| Token::from_content("...")),
281 variable_arguments_colon: self
282 .variadic_type
283 .as_ref()
284 .map(|_| Token::from_content(":")),
285 return_type_colon: self.return_type.as_ref().map(|_| Token::from_content(":")),
286 }));
287 }
288 }
289
290 super::impl_token_fns!(iter = [parameters, generic_parameters, tokens]);
291}