darklua_core/nodes/statements/
function.rs1use crate::nodes::{
2 Attribute, Attributes, Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType,
3 GenericParameters, Identifier, Token, TypedIdentifier,
4};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct FunctionNameTokens {
9 pub periods: Vec<Token>,
11 pub colon: Option<Token>,
13}
14
15impl FunctionNameTokens {
16 super::impl_token_fns!(iter = [periods, colon]);
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct FunctionName {
25 name: Identifier,
26 field_names: Vec<Identifier>,
27 method: Option<Identifier>,
28 tokens: Option<FunctionNameTokens>,
29}
30
31impl FunctionName {
32 pub fn new(name: Identifier, field_names: Vec<Identifier>, method: Option<Identifier>) -> Self {
34 Self {
35 name,
36 field_names,
37 method,
38 tokens: None,
39 }
40 }
41
42 pub fn from_name<S: Into<Identifier>>(name: S) -> Self {
44 Self {
45 name: name.into(),
46 field_names: Vec::new(),
47 method: None,
48 tokens: None,
49 }
50 }
51
52 pub fn with_tokens(mut self, tokens: FunctionNameTokens) -> Self {
54 self.tokens = Some(tokens);
55 self
56 }
57
58 #[inline]
60 pub fn set_tokens(&mut self, tokens: FunctionNameTokens) {
61 self.tokens = Some(tokens);
62 }
63
64 #[inline]
66 pub fn get_tokens(&self) -> Option<&FunctionNameTokens> {
67 self.tokens.as_ref()
68 }
69
70 pub fn with_field<S: Into<Identifier>>(mut self, field: S) -> Self {
72 self.field_names.push(field.into());
73 self
74 }
75
76 pub fn with_fields(mut self, field_names: Vec<Identifier>) -> Self {
78 self.field_names = field_names;
79 self
80 }
81
82 pub fn with_method<S: Into<Identifier>>(mut self, method: S) -> Self {
84 self.method.replace(method.into());
85 self
86 }
87
88 pub fn push_field<S: Into<Identifier>>(&mut self, field: S) {
90 self.field_names.push(field.into());
91 }
92
93 #[inline]
95 pub fn remove_method(&mut self) -> Option<Identifier> {
96 self.method.take()
97 }
98
99 #[inline]
101 pub fn get_method(&self) -> Option<&Identifier> {
102 self.method.as_ref()
103 }
104
105 #[inline]
107 pub fn has_method(&self) -> bool {
108 self.method.is_some()
109 }
110
111 #[inline]
113 pub fn get_name(&self) -> &Identifier {
114 &self.name
115 }
116
117 #[inline]
119 pub fn set_name(&mut self, name: Identifier) {
120 self.name = name;
121 }
122
123 #[inline]
125 pub fn get_field_names(&self) -> &Vec<Identifier> {
126 &self.field_names
127 }
128
129 #[inline]
131 pub fn mutate_identifier(&mut self) -> &mut Identifier {
132 &mut self.name
133 }
134
135 super::impl_token_fns!(iter = [tokens, field_names, method]);
136}
137
138#[derive(Clone, Debug, PartialEq, Eq)]
140pub struct FunctionStatement {
141 name: FunctionName,
142 block: Block,
143 parameters: Vec<TypedIdentifier>,
144 is_variadic: bool,
145 variadic_type: Option<FunctionVariadicType>,
146 return_type: Option<FunctionReturnType>,
147 generic_parameters: Option<GenericParameters>,
148 attributes: Attributes,
149 tokens: Option<Box<FunctionBodyTokens>>,
150}
151
152impl FunctionStatement {
153 pub fn new(
155 name: FunctionName,
156 block: Block,
157 parameters: Vec<TypedIdentifier>,
158 is_variadic: bool,
159 ) -> Self {
160 Self {
161 name,
162 block,
163 parameters,
164 is_variadic,
165 variadic_type: None,
166 return_type: None,
167 generic_parameters: None,
168 attributes: Attributes::new(),
169 tokens: None,
170 }
171 }
172
173 pub fn from_name<S: Into<String>, B: Into<Block>>(name: S, block: B) -> Self {
175 Self {
176 name: FunctionName::from_name(name),
177 block: block.into(),
178 parameters: Vec::new(),
179 is_variadic: false,
180 variadic_type: None,
181 return_type: None,
182 generic_parameters: None,
183 attributes: Attributes::new(),
184 tokens: None,
185 }
186 }
187
188 pub fn attributes(&self) -> &Attributes {
190 &self.attributes
191 }
192
193 pub fn with_attributes(mut self, attributes: Attributes) -> Self {
195 self.attributes = attributes;
196 self
197 }
198
199 pub fn mutate_attributes(&mut self) -> &mut Attributes {
201 &mut self.attributes
202 }
203
204 pub fn with_attribute(mut self, attribute: impl Into<Attribute>) -> Self {
206 self.attributes.append_attribute(attribute.into());
207 self
208 }
209
210 pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
212 self.tokens = Some(tokens.into());
213 self
214 }
215
216 #[inline]
218 pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
219 self.tokens = Some(tokens.into());
220 }
221
222 #[inline]
224 pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
225 self.tokens.as_deref()
226 }
227
228 #[inline]
230 pub fn mutate_tokens(&mut self) -> Option<&mut FunctionBodyTokens> {
231 self.tokens.as_deref_mut()
232 }
233
234 pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
236 self.parameters.push(parameter.into());
237 self
238 }
239
240 pub fn variadic(mut self) -> Self {
242 self.is_variadic = true;
243 self
244 }
245
246 pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
248 self.is_variadic = true;
249 self.variadic_type = Some(r#type.into());
250 self
251 }
252
253 pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
255 self.is_variadic = true;
256 self.variadic_type = Some(r#type.into());
257 }
258
259 #[inline]
261 pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
262 self.variadic_type.as_ref()
263 }
264
265 #[inline]
267 pub fn has_variadic_type(&self) -> bool {
268 self.variadic_type.is_some()
269 }
270
271 #[inline]
273 pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
274 self.variadic_type.as_mut()
275 }
276
277 pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
279 self.return_type = Some(return_type.into());
280 self
281 }
282
283 pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
285 self.return_type = Some(return_type.into());
286 }
287
288 #[inline]
290 pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
291 self.return_type.as_ref()
292 }
293
294 #[inline]
296 pub fn has_return_type(&self) -> bool {
297 self.return_type.is_some()
298 }
299
300 #[inline]
302 pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
303 self.return_type.as_mut()
304 }
305
306 pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
308 self.generic_parameters = Some(generic_parameters);
309 self
310 }
311
312 #[inline]
314 pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
315 self.generic_parameters = Some(generic_parameters);
316 }
317
318 #[inline]
320 pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
321 self.generic_parameters.as_ref()
322 }
323
324 #[inline]
326 pub fn get_block(&self) -> &Block {
327 &self.block
328 }
329
330 #[inline]
332 pub fn get_name(&self) -> &FunctionName {
333 &self.name
334 }
335
336 #[inline]
338 pub fn parameters_count(&self) -> usize {
339 self.parameters.len()
340 }
341
342 #[inline]
344 pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
345 &self.parameters
346 }
347
348 #[inline]
350 pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
351 self.parameters.iter()
352 }
353
354 #[inline]
356 pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
357 self.parameters.iter_mut()
358 }
359
360 #[inline]
362 pub fn is_variadic(&self) -> bool {
363 self.is_variadic
364 }
365
366 #[inline]
368 pub fn mutate_block(&mut self) -> &mut Block {
369 &mut self.block
370 }
371
372 #[inline]
374 pub fn mutate_function_name(&mut self) -> &mut FunctionName {
375 &mut self.name
376 }
377
378 #[inline]
380 pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
381 &mut self.parameters
382 }
383
384 pub fn remove_method(&mut self) {
389 if let Some(method_name) = self.name.remove_method() {
390 self.name.push_field(method_name);
391 self.parameters.insert(0, TypedIdentifier::new("self"));
392 }
393 }
394
395 #[inline]
397 pub fn has_parameters(&self) -> bool {
398 !self.parameters.is_empty()
399 }
400
401 pub fn clear_types(&mut self) {
404 self.return_type.take();
405 self.variadic_type.take();
406 self.generic_parameters.take();
407 for parameter in &mut self.parameters {
408 parameter.remove_type();
409 }
410 if let Some(tokens) = &mut self.tokens {
411 tokens.variable_arguments_colon.take();
412 }
413 }
414
415 pub fn mutate_first_token(&mut self) -> &mut Token {
417 self.set_default_tokens();
418 &mut self.tokens.as_deref_mut().unwrap().function
419 }
420
421 pub fn mutate_last_token(&mut self) -> &mut Token {
424 self.set_default_tokens();
425 &mut self.tokens.as_deref_mut().unwrap().end
426 }
427
428 fn set_default_tokens(&mut self) {
429 if self.tokens.is_none() {
430 self.tokens = Some(
431 FunctionBodyTokens {
432 function: Token::from_content("function"),
433 opening_parenthese: Token::from_content("("),
434 closing_parenthese: Token::from_content(")"),
435 end: Token::from_content("end"),
436 parameter_commas: Vec::new(),
437 variable_arguments: None,
438 variable_arguments_colon: None,
439 return_type_colon: None,
440 }
441 .into(),
442 );
443 }
444 }
445
446 super::impl_token_fns!(
447 target = [name]
448 iter = [parameters, generic_parameters, tokens]
449 );
450}