Skip to main content

darklua_core/nodes/expressions/
function.rs

1use crate::nodes::{
2    Attribute, Attributes, Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType,
3    GenericParameters, Token, 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    attributes: Attributes,
25    tokens: Option<Box<FunctionBodyTokens>>,
26}
27
28impl FunctionExpression {
29    /// Creates a new function expression with the given block, parameters, and variadic flag.
30    pub fn new(block: Block, parameters: Vec<TypedIdentifier>, is_variadic: bool) -> Self {
31        Self {
32            block,
33            parameters,
34            is_variadic,
35            variadic_type: None,
36            return_type: None,
37            generic_parameters: None,
38            attributes: Attributes::new(),
39            tokens: None,
40        }
41    }
42
43    /// Creates a new function expression from a block with no parameters.
44    pub fn from_block<B: Into<Block>>(block: B) -> Self {
45        Self {
46            block: block.into(),
47            parameters: Vec::new(),
48            is_variadic: false,
49            variadic_type: None,
50            return_type: None,
51            generic_parameters: None,
52            attributes: Attributes::new(),
53            tokens: None,
54        }
55    }
56
57    /// Returns a reference to the attributes of this function expression.
58    pub fn attributes(&self) -> &Attributes {
59        &self.attributes
60    }
61
62    /// Returns a mutable reference to the attributes of this function expression.
63    pub fn mutate_attributes(&mut self) -> &mut Attributes {
64        &mut self.attributes
65    }
66
67    /// Associates attributes with this function expression. Replaces any existing attributes.
68    pub fn with_attributes(mut self, attributes: Attributes) -> Self {
69        self.attributes = attributes;
70        self
71    }
72
73    /// Adds an attribute to this function statement. Adds to any existing attributes.
74    pub fn with_attribute(mut self, attribute: impl Into<Attribute>) -> Self {
75        self.attributes.append_attribute(attribute.into());
76        self
77    }
78
79    /// Sets the parameters of this function expression.
80    pub fn with_parameters(mut self, parameters: Vec<TypedIdentifier>) -> Self {
81        self.parameters = parameters;
82        self
83    }
84
85    /// Adds a parameter to this function expression.
86    pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
87        self.parameters.push(parameter.into());
88        self
89    }
90
91    /// Sets the variadic type of this function expression.
92    ///
93    /// This also marks the function as variadic if it is not already.
94    pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
95        self.is_variadic = true;
96        self.variadic_type = Some(r#type.into());
97        self
98    }
99
100    /// Sets the return type of this function expression.
101    pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
102        self.return_type = Some(return_type.into());
103        self
104    }
105
106    /// Sets the return type of this function expression.
107    #[inline]
108    pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
109        self.return_type = Some(return_type.into());
110    }
111
112    /// Returns a reference to the return type of this function expression, if any.
113    #[inline]
114    pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
115        self.return_type.as_ref()
116    }
117
118    /// Returns whether this function expression has a return type.
119    #[inline]
120    pub fn has_return_type(&self) -> bool {
121        self.return_type.is_some()
122    }
123
124    /// Returns a mutable reference to the return type of this function expression, if any.
125    #[inline]
126    pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
127        self.return_type.as_mut()
128    }
129
130    /// Marks this function expression as variadic and returns the updated expression.
131    pub fn variadic(mut self) -> Self {
132        self.is_variadic = true;
133        self
134    }
135
136    /// Sets whether this function expression is variadic.
137    ///
138    /// If set to false, the variadic type is cleared if present.
139    pub fn set_variadic(&mut self, is_variadic: bool) {
140        self.is_variadic = is_variadic;
141        if !is_variadic && self.variadic_type.is_some() {
142            self.variadic_type.take();
143        }
144    }
145
146    /// Sets the variadic type of this function expression.
147    ///
148    /// This also marks the function as variadic if it is not already.
149    pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
150        self.is_variadic = true;
151        self.variadic_type = Some(r#type.into());
152    }
153
154    /// Returns a reference to the variadic type of this function expression, if any.
155    #[inline]
156    pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
157        self.variadic_type.as_ref()
158    }
159
160    /// Returns whether this function expression has a variadic type.
161    #[inline]
162    pub fn has_variadic_type(&self) -> bool {
163        self.variadic_type.is_some()
164    }
165
166    /// Returns a mutable reference to the variadic type of this function expression, if any.
167    #[inline]
168    pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
169        self.variadic_type.as_mut()
170    }
171
172    /// Sets the generic parameters of this function expression and returns the updated expression.
173    pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
174        self.generic_parameters = Some(generic_parameters);
175        self
176    }
177
178    /// Sets the generic parameters of this function expression.
179    #[inline]
180    pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
181        self.generic_parameters = Some(generic_parameters);
182    }
183
184    /// Returns a reference to the generic parameters of this function expression, if any.
185    #[inline]
186    pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
187        self.generic_parameters.as_ref()
188    }
189
190    /// Returns whether this function expression has generic parameters.
191    #[inline]
192    pub fn is_generic(&self) -> bool {
193        self.generic_parameters.is_some()
194    }
195
196    /// Associates a token with this function expression.
197    pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
198        self.tokens = Some(tokens.into());
199        self
200    }
201
202    /// Associates a token with this function expression.
203    #[inline]
204    pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
205        self.tokens = Some(tokens.into());
206    }
207
208    /// Returns a reference to the token attached to this function expression, if any.
209    #[inline]
210    pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
211        self.tokens.as_ref().map(|tokens| tokens.as_ref())
212    }
213
214    /// Returns a reference to the block of this function expression.
215    #[inline]
216    pub fn get_block(&self) -> &Block {
217        &self.block
218    }
219
220    /// Returns a reference to the parameters of this function expression.
221    #[inline]
222    pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
223        &self.parameters
224    }
225
226    /// Returns an iterator over the parameters of this function expression.
227    #[inline]
228    pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
229        self.parameters.iter()
230    }
231
232    /// Returns a mutable iterator over the parameters of this function expression.
233    #[inline]
234    pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
235        self.parameters.iter_mut()
236    }
237
238    /// Returns whether this function expression is variadic.
239    #[inline]
240    pub fn is_variadic(&self) -> bool {
241        self.is_variadic
242    }
243
244    /// Returns a mutable reference to the block of this function expression.
245    #[inline]
246    pub fn mutate_block(&mut self) -> &mut Block {
247        &mut self.block
248    }
249
250    /// Returns a mutable reference to the parameters of this function expression.
251    #[inline]
252    pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
253        &mut self.parameters
254    }
255
256    /// Returns the number of parameters of this function expression.
257    #[inline]
258    pub fn parameters_count(&self) -> usize {
259        self.parameters.len()
260    }
261
262    /// Returns whether this function expression has parameters.
263    #[inline]
264    pub fn has_parameters(&self) -> bool {
265        !self.parameters.is_empty()
266    }
267
268    /// Removes all type information from this function expression.
269    ///
270    /// This includes return type, variadic type, generic parameters, and parameter types.
271    pub fn clear_types(&mut self) {
272        self.return_type.take();
273        self.variadic_type.take();
274        self.generic_parameters.take();
275        for parameter in &mut self.parameters {
276            parameter.remove_type();
277        }
278        if let Some(tokens) = &mut self.tokens {
279            tokens.variable_arguments_colon.take();
280        }
281    }
282
283    /// Returns a mutable reference to the first token for this function expression,
284    /// creating it if missing.
285    pub fn mutate_first_token(&mut self) -> &mut Token {
286        self.set_default_tokens();
287        &mut self.tokens.as_mut().unwrap().function
288    }
289
290    /// Returns a mutable reference to the last token for this function expression,
291    /// creating it if missing.
292    pub fn mutate_last_token(&mut self) -> &mut Token {
293        self.set_default_tokens();
294        &mut self.tokens.as_mut().unwrap().end
295    }
296
297    fn set_default_tokens(&mut self) {
298        if self.tokens.is_none() {
299            self.tokens = Some(Box::new(FunctionBodyTokens {
300                function: Token::from_content("function"),
301                opening_parenthese: Token::from_content("("),
302                closing_parenthese: Token::from_content(")"),
303                end: Token::from_content("end"),
304                parameter_commas: Vec::new(),
305                variable_arguments: self.is_variadic.then(|| Token::from_content("...")),
306                variable_arguments_colon: self
307                    .variadic_type
308                    .as_ref()
309                    .map(|_| Token::from_content(":")),
310                return_type_colon: self.return_type.as_ref().map(|_| Token::from_content(":")),
311            }));
312        }
313    }
314
315    super::impl_token_fns!(iter = [parameters, generic_parameters, tokens]);
316}