darklua_core/nodes/expressions/
function.rs

1use crate::nodes::{
2    Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType, GenericParameters,
3    TypedIdentifier,
4};
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
7pub struct FunctionExpression {
8    block: Block,
9    parameters: Vec<TypedIdentifier>,
10    is_variadic: bool,
11    variadic_type: Option<FunctionVariadicType>,
12    return_type: Option<FunctionReturnType>,
13    generic_parameters: Option<GenericParameters>,
14    tokens: Option<Box<FunctionBodyTokens>>,
15}
16
17impl FunctionExpression {
18    pub fn new(block: Block, parameters: Vec<TypedIdentifier>, is_variadic: bool) -> Self {
19        Self {
20            block,
21            parameters,
22            is_variadic,
23            variadic_type: None,
24            return_type: None,
25            generic_parameters: None,
26            tokens: None,
27        }
28    }
29
30    pub fn from_block<B: Into<Block>>(block: B) -> Self {
31        Self {
32            block: block.into(),
33            parameters: Vec::new(),
34            is_variadic: false,
35            variadic_type: None,
36            return_type: None,
37            generic_parameters: None,
38            tokens: None,
39        }
40    }
41
42    pub fn with_parameters(mut self, parameters: Vec<TypedIdentifier>) -> Self {
43        self.parameters = parameters;
44        self
45    }
46
47    pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
48        self.parameters.push(parameter.into());
49        self
50    }
51
52    pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
53        self.is_variadic = true;
54        self.variadic_type = Some(r#type.into());
55        self
56    }
57
58    pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
59        self.return_type = Some(return_type.into());
60        self
61    }
62
63    #[inline]
64    pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
65        self.return_type = Some(return_type.into());
66    }
67
68    #[inline]
69    pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
70        self.return_type.as_ref()
71    }
72
73    #[inline]
74    pub fn has_return_type(&self) -> bool {
75        self.return_type.is_some()
76    }
77
78    #[inline]
79    pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
80        self.return_type.as_mut()
81    }
82
83    pub fn variadic(mut self) -> Self {
84        self.is_variadic = true;
85        self
86    }
87
88    pub fn set_variadic(&mut self, is_variadic: bool) {
89        self.is_variadic = is_variadic;
90        if !is_variadic && self.variadic_type.is_some() {
91            self.variadic_type.take();
92        }
93    }
94
95    pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
96        self.is_variadic = true;
97        self.variadic_type = Some(r#type.into());
98    }
99
100    #[inline]
101    pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
102        self.variadic_type.as_ref()
103    }
104
105    #[inline]
106    pub fn has_variadic_type(&self) -> bool {
107        self.variadic_type.is_some()
108    }
109
110    #[inline]
111    pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
112        self.variadic_type.as_mut()
113    }
114
115    pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
116        self.generic_parameters = Some(generic_parameters);
117        self
118    }
119
120    #[inline]
121    pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
122        self.generic_parameters = Some(generic_parameters);
123    }
124
125    #[inline]
126    pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
127        self.generic_parameters.as_ref()
128    }
129
130    #[inline]
131    pub fn is_generic(&self) -> bool {
132        self.generic_parameters.is_some()
133    }
134
135    pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
136        self.tokens = Some(tokens.into());
137        self
138    }
139
140    #[inline]
141    pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
142        self.tokens = Some(tokens.into());
143    }
144
145    #[inline]
146    pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
147        self.tokens.as_ref().map(|tokens| tokens.as_ref())
148    }
149
150    #[inline]
151    pub fn get_block(&self) -> &Block {
152        &self.block
153    }
154
155    #[inline]
156    pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
157        &self.parameters
158    }
159
160    #[inline]
161    pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
162        self.parameters.iter()
163    }
164
165    #[inline]
166    pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
167        self.parameters.iter_mut()
168    }
169
170    #[inline]
171    pub fn is_variadic(&self) -> bool {
172        self.is_variadic
173    }
174
175    #[inline]
176    pub fn mutate_block(&mut self) -> &mut Block {
177        &mut self.block
178    }
179
180    #[inline]
181    pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
182        &mut self.parameters
183    }
184
185    #[inline]
186    pub fn parameters_count(&self) -> usize {
187        self.parameters.len()
188    }
189
190    #[inline]
191    pub fn has_parameters(&self) -> bool {
192        !self.parameters.is_empty()
193    }
194
195    pub fn clear_types(&mut self) {
196        self.return_type.take();
197        self.variadic_type.take();
198        self.generic_parameters.take();
199        for parameter in &mut self.parameters {
200            parameter.remove_type();
201        }
202        if let Some(tokens) = &mut self.tokens {
203            tokens.variable_arguments_colon.take();
204        }
205    }
206
207    super::impl_token_fns!(iter = [parameters, generic_parameters, tokens]);
208}