darklua_core/nodes/expressions/
function.rs

1use crate::nodes::{
2    Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType, GenericParameters, Token,
3    TypedIdentifier,
4};
5
6/// Represents a function expression.
7///
8/// A function expression defines an anonymous function with parameters,
9/// a body, and optional features like variadics, return types, and generic parameters.
10///
11/// ```lua
12/// local add = function(a: number, b: number): number
13///     return a + b
14/// end
15/// ```
16#[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    /// Creates a new function expression with the given block, parameters, and variadic flag.
29    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    /// Creates a new function expression from a block with no parameters.
42    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    /// Sets the parameters of this function expression.
55    pub fn with_parameters(mut self, parameters: Vec<TypedIdentifier>) -> Self {
56        self.parameters = parameters;
57        self
58    }
59
60    /// Adds a parameter to this function expression.
61    pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
62        self.parameters.push(parameter.into());
63        self
64    }
65
66    /// Sets the variadic type of this function expression.
67    ///
68    /// This also marks the function as variadic if it is not already.
69    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    /// Sets the return type of this function expression.
76    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    /// Sets the return type of this function expression.
82    #[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    /// Returns a reference to the return type of this function expression, if any.
88    #[inline]
89    pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
90        self.return_type.as_ref()
91    }
92
93    /// Returns whether this function expression has a return type.
94    #[inline]
95    pub fn has_return_type(&self) -> bool {
96        self.return_type.is_some()
97    }
98
99    /// Returns a mutable reference to the return type of this function expression, if any.
100    #[inline]
101    pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
102        self.return_type.as_mut()
103    }
104
105    /// Marks this function expression as variadic and returns the updated expression.
106    pub fn variadic(mut self) -> Self {
107        self.is_variadic = true;
108        self
109    }
110
111    /// Sets whether this function expression is variadic.
112    ///
113    /// If set to false, the variadic type is cleared if present.
114    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    /// Sets the variadic type of this function expression.
122    ///
123    /// This also marks the function as variadic if it is not already.
124    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    /// Returns a reference to the variadic type of this function expression, if any.
130    #[inline]
131    pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
132        self.variadic_type.as_ref()
133    }
134
135    /// Returns whether this function expression has a variadic type.
136    #[inline]
137    pub fn has_variadic_type(&self) -> bool {
138        self.variadic_type.is_some()
139    }
140
141    /// Returns a mutable reference to the variadic type of this function expression, if any.
142    #[inline]
143    pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
144        self.variadic_type.as_mut()
145    }
146
147    /// Sets the generic parameters of this function expression and returns the updated expression.
148    pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
149        self.generic_parameters = Some(generic_parameters);
150        self
151    }
152
153    /// Sets the generic parameters of this function expression.
154    #[inline]
155    pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
156        self.generic_parameters = Some(generic_parameters);
157    }
158
159    /// Returns a reference to the generic parameters of this function expression, if any.
160    #[inline]
161    pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
162        self.generic_parameters.as_ref()
163    }
164
165    /// Returns whether this function expression has generic parameters.
166    #[inline]
167    pub fn is_generic(&self) -> bool {
168        self.generic_parameters.is_some()
169    }
170
171    /// Associates a token with this function expression.
172    pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
173        self.tokens = Some(tokens.into());
174        self
175    }
176
177    /// Associates a token with this function expression.
178    #[inline]
179    pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
180        self.tokens = Some(tokens.into());
181    }
182
183    /// Returns a reference to the token attached to this function expression, if any.
184    #[inline]
185    pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
186        self.tokens.as_ref().map(|tokens| tokens.as_ref())
187    }
188
189    /// Returns a reference to the block of this function expression.
190    #[inline]
191    pub fn get_block(&self) -> &Block {
192        &self.block
193    }
194
195    /// Returns a reference to the parameters of this function expression.
196    #[inline]
197    pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
198        &self.parameters
199    }
200
201    /// Returns an iterator over the parameters of this function expression.
202    #[inline]
203    pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
204        self.parameters.iter()
205    }
206
207    /// Returns a mutable iterator over the parameters of this function expression.
208    #[inline]
209    pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
210        self.parameters.iter_mut()
211    }
212
213    /// Returns whether this function expression is variadic.
214    #[inline]
215    pub fn is_variadic(&self) -> bool {
216        self.is_variadic
217    }
218
219    /// Returns a mutable reference to the block of this function expression.
220    #[inline]
221    pub fn mutate_block(&mut self) -> &mut Block {
222        &mut self.block
223    }
224
225    /// Returns a mutable reference to the parameters of this function expression.
226    #[inline]
227    pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
228        &mut self.parameters
229    }
230
231    /// Returns the number of parameters of this function expression.
232    #[inline]
233    pub fn parameters_count(&self) -> usize {
234        self.parameters.len()
235    }
236
237    /// Returns whether this function expression has parameters.
238    #[inline]
239    pub fn has_parameters(&self) -> bool {
240        !self.parameters.is_empty()
241    }
242
243    /// Removes all type information from this function expression.
244    ///
245    /// This includes return type, variadic type, generic parameters, and parameter types.
246    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    /// Returns a mutable reference to the first token for this function expression,
259    /// creating it if missing.
260    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    /// Returns a mutable reference to the last token for this function expression,
266    /// creating it if missing.
267    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}