darklua 0.18.0

Transform Lua scripts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::{iter, mem};

use crate::nodes::{Expression, StringExpression, TableExpression, Token};

/// Tokens associated with tuple arguments.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TupleArgumentsTokens {
    pub opening_parenthese: Token,
    pub closing_parenthese: Token,
    pub commas: Vec<Token>,
}

impl TupleArgumentsTokens {
    super::impl_token_fns!(
        target = [opening_parenthese, closing_parenthese]
        iter = [commas]
    );
}

/// Represents a list of arguments enclosed in parentheses.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TupleArguments {
    values: Vec<Expression>,
    tokens: Option<TupleArgumentsTokens>,
}

impl TupleArguments {
    /// Creates a new tuple of arguments with the given expressions.
    pub fn new(values: Vec<Expression>) -> Self {
        Self {
            values,
            tokens: None,
        }
    }

    /// Converts this tuple into expressions.
    #[inline]
    pub fn to_expressions(self) -> Vec<Expression> {
        self.values
    }

    /// Sets the tokens for this tuple.
    pub fn with_tokens(mut self, tokens: TupleArgumentsTokens) -> Self {
        self.tokens = Some(tokens);
        self
    }

    /// Sets the tokens for this tuple.
    #[inline]
    pub fn set_tokens(&mut self, tokens: TupleArgumentsTokens) {
        self.tokens = Some(tokens);
    }

    /// Returns the tokens for this tuple, if any.
    #[inline]
    pub fn get_tokens(&self) -> Option<&TupleArgumentsTokens> {
        self.tokens.as_ref()
    }

    /// Adds an argument to this tuple.
    pub fn with_argument<T: Into<Expression>>(mut self, argument: T) -> Self {
        self.push(argument.into());
        self
    }

    /// Pushes an argument to this tuple.
    pub fn push(&mut self, argument: impl Into<Expression>) {
        let argument = argument.into();
        let initial_len = self.values.len();

        self.values.push(argument);

        if initial_len != 0 {
            if let Some(tokens) = &mut self.tokens {
                if tokens.commas.len() == initial_len - 1 {
                    tokens.commas.push(Token::from_content(","));
                }
            }
        }
    }

    /// Inserts an argument at the specified index.
    pub fn insert(&mut self, index: usize, argument: impl Into<Expression>) {
        if index >= self.values.len() {
            self.push(argument.into());
        } else {
            self.values.insert(index, argument.into());

            if let Some(tokens) = &mut self.tokens {
                if index <= tokens.commas.len() {
                    tokens.commas.insert(index, Token::from_content(","));
                }
            }
        }
    }

    /// Returns a mutable reference to the last token of this tuple of arguments,
    /// creating it if missing.
    pub fn mutate_last_token(&mut self) -> &mut Token {
        if self.get_tokens().is_none() {
            self.set_tokens(TupleArgumentsTokens {
                opening_parenthese: Token::from_content("("),
                closing_parenthese: Token::from_content(")"),
                commas: (0..self.len().saturating_sub(1))
                    .map(|_| Token::from_content(","))
                    .collect(),
            });
        }
        &mut self.tokens.as_mut().unwrap().closing_parenthese
    }

    /// Returns the number of arguments in this tuple.
    #[inline]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns whether this tuple has no arguments.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Returns an iterator over the argument expressions.
    #[inline]
    pub fn iter_values(&self) -> impl Iterator<Item = &Expression> {
        self.values.iter()
    }

    /// Returns a mutable iterator over the argument expressions.
    #[inline]
    pub fn iter_mut_values(&mut self) -> impl Iterator<Item = &mut Expression> {
        self.values.iter_mut()
    }

    super::impl_token_fns!(iter = [tokens]);
}

impl From<Arguments> for TupleArguments {
    fn from(arguments: Arguments) -> Self {
        match arguments {
            Arguments::Tuple(tuple) => tuple,
            Arguments::String(string) => TupleArguments::default().with_argument(string),
            Arguments::Table(table) => TupleArguments::default().with_argument(table),
        }
    }
}

impl iter::FromIterator<Expression> for TupleArguments {
    fn from_iter<T: IntoIterator<Item = Expression>>(iter: T) -> Self {
        Self {
            values: iter.into_iter().collect(),
            tokens: None,
        }
    }
}

/// Represents the different ways arguments can be passed to a function call.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Arguments {
    /// Multiple arguments in parentheses: `func(arg1, arg2)`
    Tuple(TupleArguments),
    /// A single string argument without parentheses: `func "string"`
    String(StringExpression),
    /// A single table argument without parentheses: `func {key=value}`
    Table(TableExpression),
}

impl Arguments {
    /// Returns the total number of arguments.
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            Self::Tuple(tuple) => tuple.len(),
            Self::String(_) | Self::Table(_) => 1,
        }
    }

    /// Returns true if this is an empty tuple of arguments.
    #[inline]
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Tuple(tuple) => tuple.is_empty(),
            Self::String(_) | Self::Table(_) => false,
        }
    }

    /// Converts these arguments into a vector of expressions.
    pub fn to_expressions(self) -> Vec<Expression> {
        match self {
            Self::Tuple(expressions) => expressions.to_expressions(),
            Self::String(string) => vec![string.into()],
            Self::Table(table) => vec![table.into()],
        }
    }

    /// Adds an argument to these arguments, converting to a tuple if needed.
    pub fn with_argument<T: Into<Expression>>(self, argument: T) -> Self {
        TupleArguments::from(self).with_argument(argument).into()
    }

    /// Pushes an argument to these arguments, converting to a tuple if needed.
    pub fn push(&mut self, argument: impl Into<Expression>) {
        let argument = argument.into();

        let tuple_args = match self {
            Arguments::Tuple(tuple) => {
                tuple.push(argument);
                return;
            }
            Arguments::String(value) => TupleArguments::default()
                .with_argument(mem::replace(value, StringExpression::empty())),
            Arguments::Table(value) => TupleArguments::default().with_argument(mem::take(value)),
        };

        *self = tuple_args.with_argument(argument).into();
    }

    /// Inserts an argument at the specified index, converting to a tuple if needed.
    pub fn insert(&mut self, index: usize, argument: impl Into<Expression>) {
        let argument = argument.into();

        let mut tuple_args = match self {
            Arguments::Tuple(tuple) => {
                tuple.insert(index, argument);
                return;
            }
            Arguments::String(value) => {
                let string = mem::replace(value, StringExpression::empty());
                TupleArguments::default().with_argument(Expression::from(string))
            }
            Arguments::Table(value) => {
                let table = mem::take(value);
                TupleArguments::default().with_argument(Expression::from(table))
            }
        };

        tuple_args.insert(index, argument);

        *self = tuple_args.into();
    }

    /// Returns a mutable reference to the last token of these arguments,
    /// creating it if missing.
    pub fn mutate_last_token(&mut self) -> &mut Token {
        match self {
            Arguments::Tuple(tuple) => tuple.mutate_last_token(),
            Arguments::String(string) => string.mutate_or_insert_token(),
            Arguments::Table(table) => table.mutate_last_token(),
        }
    }

    /// Removes all comments from these arguments.
    pub fn clear_comments(&mut self) {
        match self {
            Arguments::Tuple(tuple) => tuple.clear_comments(),
            Arguments::String(_) | Arguments::Table(_) => {}
        }
    }

    /// Removes all whitespace from these arguments.
    pub fn clear_whitespaces(&mut self) {
        match self {
            Arguments::Tuple(tuple) => tuple.clear_whitespaces(),
            Arguments::String(_) | Arguments::Table(_) => {}
        }
    }

    /// Replaces referenced tokens with the actual content from the source code.
    pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
        match self {
            Arguments::Tuple(tuple) => tuple.replace_referenced_tokens(code),
            Arguments::String(_) | Arguments::Table(_) => {}
        }
    }

    /// Shifts token line numbers by the specified amount.
    pub(crate) fn shift_token_line(&mut self, amount: isize) {
        match self {
            Arguments::Tuple(tuple) => tuple.shift_token_line(amount),
            Arguments::String(_) | Arguments::Table(_) => {}
        }
    }

    /// Filters comments using the provided predicate.
    pub(crate) fn filter_comments(&mut self, filter: impl Fn(&super::Trivia) -> bool) {
        match self {
            Arguments::Tuple(tuple) => tuple.filter_comments(filter),
            Arguments::String(_) | Arguments::Table(_) => {}
        }
    }
}

impl Default for Arguments {
    fn default() -> Self {
        Self::Tuple(TupleArguments::default())
    }
}

impl From<TupleArguments> for Arguments {
    fn from(tuple: TupleArguments) -> Self {
        Self::Tuple(tuple)
    }
}

impl From<TableExpression> for Arguments {
    fn from(table: TableExpression) -> Self {
        Self::Table(table)
    }
}

impl From<StringExpression> for Arguments {
    fn from(string: StringExpression) -> Self {
        Self::String(string)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        nodes::{Identifier, Statement},
        Parser,
    };

    fn parse_arguments_with_tokens(lua: &str) -> Arguments {
        let parser = Parser::default().preserve_tokens();

        let code = format!("f {}", lua);

        let block = parser.parse(&code).expect("code should parse");
        if let Some(Statement::Call(call)) = block.first_statement() {
            return call.get_arguments().clone();
        }
        panic!("failed to parse call arguments from: {}", lua);
    }

    fn get_tuple_tokens(args: &Arguments) -> &TupleArgumentsTokens {
        match args {
            Arguments::Tuple(tuple) => tuple.get_tokens().expect("tuple should have tokens"),
            Arguments::String(_) | Arguments::Table(_) => panic!("expected tuple arguments"),
        }
    }

    fn expect_comma_tokens(args: &Arguments, index: usize) {
        let tokens = get_tuple_tokens(args);
        assert_eq!(tokens.commas[index], Token::from_content(","));
    }

    mod arguments_len {
        use super::*;

        #[test]
        fn empty_tuple() {
            let empty_tuple = Arguments::Tuple(TupleArguments::new(vec![]));
            assert_eq!(empty_tuple.len(), 0);
        }

        #[test]
        fn single_tuple() {
            let single_tuple = Arguments::Tuple(TupleArguments::new(vec![Expression::Identifier(
                Identifier::new("x"),
            )]));
            assert_eq!(single_tuple.len(), 1);
        }

        #[test]
        fn multi_tuple() {
            let multi_tuple = Arguments::Tuple(TupleArguments::new(vec![
                Expression::Identifier(Identifier::new("x")),
                Expression::Identifier(Identifier::new("y")),
                Expression::Identifier(Identifier::new("z")),
            ]));
            assert_eq!(multi_tuple.len(), 3);
        }

        #[test]
        fn string() {
            let string_args = Arguments::String(StringExpression::from_value("test"));
            assert_eq!(string_args.len(), 1);
        }

        #[test]
        fn table() {
            let table_args = Arguments::Table(TableExpression::new(vec![]));
            assert_eq!(table_args.len(), 1);
        }
    }

    mod arguments_is_empty {
        use super::*;

        #[test]
        fn empty_tuple() {
            let empty_tuple = Arguments::Tuple(TupleArguments::new(vec![]));
            assert!(empty_tuple.is_empty());
        }

        #[test]
        fn single_tuple() {
            let single_tuple = Arguments::Tuple(TupleArguments::new(vec![Expression::Identifier(
                Identifier::new("x"),
            )]));
            assert!(!single_tuple.is_empty());
        }

        #[test]
        fn multi_tuple() {
            let multi_tuple = Arguments::Tuple(TupleArguments::new(vec![
                Expression::Identifier(Identifier::new("x")),
                Expression::Identifier(Identifier::new("y")),
            ]));
            assert!(!multi_tuple.is_empty());
        }

        #[test]
        fn string() {
            let string_args = Arguments::String(StringExpression::from_value("test"));
            assert!(!string_args.is_empty());
        }

        #[test]
        fn table() {
            let table_args = Arguments::Table(TableExpression::new(vec![]));
            assert!(!table_args.is_empty());
        }
    }

    #[test]
    fn push_argument_handles_commas() {
        let mut args = parse_arguments_with_tokens("()");

        args.push(Identifier::new("first"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 0);

        args.push(Identifier::new("second"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 1);
        expect_comma_tokens(&args, 0);

        args.push(Identifier::new("third"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 2);
        expect_comma_tokens(&args, 1);
    }

    #[test]
    fn insert_argument_handles_commas() {
        let mut args = parse_arguments_with_tokens("(first, third)");

        args.insert(1, Identifier::new("second"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 2);
        expect_comma_tokens(&args, 1);

        args.insert(3, Identifier::new("fourth"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 3);

        args.insert(0, Identifier::new("zero"));
        assert_eq!(get_tuple_tokens(&args).commas.len(), 4);
    }
}