darklua_core/nodes/expressions/
function.rs1use crate::nodes::{
2 Attribute, Attributes, Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType,
3 GenericParameters, Token, 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 attributes: Attributes,
25 tokens: Option<Box<FunctionBodyTokens>>,
26}
27
28impl FunctionExpression {
29 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 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 pub fn attributes(&self) -> &Attributes {
59 &self.attributes
60 }
61
62 pub fn mutate_attributes(&mut self) -> &mut Attributes {
64 &mut self.attributes
65 }
66
67 pub fn with_attributes(mut self, attributes: Attributes) -> Self {
69 self.attributes = attributes;
70 self
71 }
72
73 pub fn with_attribute(mut self, attribute: impl Into<Attribute>) -> Self {
75 self.attributes.append_attribute(attribute.into());
76 self
77 }
78
79 pub fn with_parameters(mut self, parameters: Vec<TypedIdentifier>) -> Self {
81 self.parameters = parameters;
82 self
83 }
84
85 pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
87 self.parameters.push(parameter.into());
88 self
89 }
90
91 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 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 #[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 #[inline]
114 pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
115 self.return_type.as_ref()
116 }
117
118 #[inline]
120 pub fn has_return_type(&self) -> bool {
121 self.return_type.is_some()
122 }
123
124 #[inline]
126 pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
127 self.return_type.as_mut()
128 }
129
130 pub fn variadic(mut self) -> Self {
132 self.is_variadic = true;
133 self
134 }
135
136 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 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 #[inline]
156 pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
157 self.variadic_type.as_ref()
158 }
159
160 #[inline]
162 pub fn has_variadic_type(&self) -> bool {
163 self.variadic_type.is_some()
164 }
165
166 #[inline]
168 pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
169 self.variadic_type.as_mut()
170 }
171
172 pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
174 self.generic_parameters = Some(generic_parameters);
175 self
176 }
177
178 #[inline]
180 pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
181 self.generic_parameters = Some(generic_parameters);
182 }
183
184 #[inline]
186 pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
187 self.generic_parameters.as_ref()
188 }
189
190 #[inline]
192 pub fn is_generic(&self) -> bool {
193 self.generic_parameters.is_some()
194 }
195
196 pub fn with_tokens(mut self, tokens: FunctionBodyTokens) -> Self {
198 self.tokens = Some(tokens.into());
199 self
200 }
201
202 #[inline]
204 pub fn set_tokens(&mut self, tokens: FunctionBodyTokens) {
205 self.tokens = Some(tokens.into());
206 }
207
208 #[inline]
210 pub fn get_tokens(&self) -> Option<&FunctionBodyTokens> {
211 self.tokens.as_ref().map(|tokens| tokens.as_ref())
212 }
213
214 #[inline]
216 pub fn get_block(&self) -> &Block {
217 &self.block
218 }
219
220 #[inline]
222 pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
223 &self.parameters
224 }
225
226 #[inline]
228 pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
229 self.parameters.iter()
230 }
231
232 #[inline]
234 pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
235 self.parameters.iter_mut()
236 }
237
238 #[inline]
240 pub fn is_variadic(&self) -> bool {
241 self.is_variadic
242 }
243
244 #[inline]
246 pub fn mutate_block(&mut self) -> &mut Block {
247 &mut self.block
248 }
249
250 #[inline]
252 pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
253 &mut self.parameters
254 }
255
256 #[inline]
258 pub fn parameters_count(&self) -> usize {
259 self.parameters.len()
260 }
261
262 #[inline]
264 pub fn has_parameters(&self) -> bool {
265 !self.parameters.is_empty()
266 }
267
268 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 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 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}