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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use crate::nodes::{Identifier, Token, Trivia};

use super::{StringType, Type};

/// Represents an indexer in a table type annotation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableIndexerType {
    key_type: Box<Type>,
    value_type: Box<Type>,
    modifier: Option<TablePropertyModifier>,
    tokens: Option<TableIndexTypeTokens>,
}

impl TableIndexerType {
    /// Creates a new table indexer with the specified key and value types.
    pub fn new(key_type: impl Into<Type>, value_type: impl Into<Type>) -> Self {
        Self {
            key_type: Box::new(key_type.into()),
            value_type: Box::new(value_type.into()),
            modifier: None,
            tokens: None,
        }
    }

    /// Associates a modifier with this property.
    pub fn with_modifier(mut self, modifier: TablePropertyModifier) -> Self {
        self.set_modifier(modifier);
        self
    }

    /// Sets the modifier for this property.
    pub fn set_modifier(&mut self, modifier: TablePropertyModifier) {
        if self.modifier.as_ref() == Some(&modifier) {
            return;
        }

        self.modifier = Some(modifier);
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Removes the modifier for this property.
    pub fn remove_modifier(&mut self) {
        self.modifier = None;
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Returns the modifier for this property, if any.
    pub fn get_modifier(&self) -> Option<&TablePropertyModifier> {
        self.modifier.as_ref()
    }

    /// Returns the key type of this indexer.
    #[inline]
    pub fn get_key_type(&self) -> &Type {
        &self.key_type
    }

    /// Returns a mutable reference to the key type of this indexer.
    #[inline]
    pub fn mutate_key_type(&mut self) -> &mut Type {
        &mut self.key_type
    }

    /// Returns the value type of this indexer.
    #[inline]
    pub fn get_value_type(&self) -> &Type {
        &self.value_type
    }

    /// Returns a mutable reference to the value type of this indexer.
    #[inline]
    pub fn mutate_value_type(&mut self) -> &mut Type {
        &mut self.value_type
    }

    /// Associates tokens with this indexer and returns the modified indexer.
    pub fn with_tokens(mut self, token: TableIndexTypeTokens) -> Self {
        self.tokens = Some(token);
        self
    }

    /// Sets the tokens associated with this indexer.
    #[inline]
    pub fn set_tokens(&mut self, token: TableIndexTypeTokens) {
        self.tokens = Some(token);
    }

    /// Returns the tokens associated with this indexer, if any.
    #[inline]
    pub fn get_tokens(&self) -> Option<&TableIndexTypeTokens> {
        self.tokens.as_ref()
    }

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

/// Contains the tokens that define an indexer's syntax.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableIndexTypeTokens {
    /// The opening bracket token.
    pub opening_bracket: Token,
    /// The closing bracket token.
    pub closing_bracket: Token,
    /// The colon token.
    pub colon: Token,
    /// The modifier token.
    pub modifier: Option<Token>,
}

impl TableIndexTypeTokens {
    super::impl_token_fns!(
        target = [opening_bracket, closing_bracket, colon]
        iter = [modifier]
    );
}

/// Represents a named property in a table type annotation (i.e. `name: Type`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TablePropertyType {
    property: Identifier,
    r#type: Box<Type>,
    modifier: Option<TablePropertyModifier>,
    tokens: Option<TablePropertyTypeTokens>,
}

/// Contains the tokens that define a property's syntax.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TablePropertyTypeTokens {
    /// The colon token.
    pub colon: Token,
    /// The modifier token.
    pub modifier: Option<Token>,
}

impl TablePropertyTypeTokens {
    super::impl_token_fns!(
        target = [colon]
        iter = [modifier]
    );
}

impl TablePropertyType {
    /// Creates a new table property with the specified name and type.
    pub fn new(property: impl Into<Identifier>, r#type: impl Into<Type>) -> Self {
        Self {
            property: property.into(),
            r#type: Box::new(r#type.into()),
            modifier: None,
            tokens: None,
        }
    }

    /// Associates a modifier with this property.
    pub fn with_modifier(mut self, modifier: TablePropertyModifier) -> Self {
        self.set_modifier(modifier);
        self
    }

    /// Sets the modifier for this property.
    pub fn set_modifier(&mut self, modifier: TablePropertyModifier) {
        if self.modifier.as_ref() == Some(&modifier) {
            return;
        }

        self.modifier = Some(modifier);
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Removes the modifier for this property.
    pub fn remove_modifier(&mut self) {
        self.modifier = None;
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Returns the modifier for this property, if any.
    pub fn get_modifier(&self) -> Option<&TablePropertyModifier> {
        self.modifier.as_ref()
    }

    /// Returns the identifier of this property.
    #[inline]
    pub fn get_identifier(&self) -> &Identifier {
        &self.property
    }

    /// Returns a mutable reference to the identifier of this property.
    #[inline]
    pub fn mutate_identifier(&mut self) -> &mut Identifier {
        &mut self.property
    }

    /// Returns the type of this property.
    #[inline]
    pub fn get_type(&self) -> &Type {
        &self.r#type
    }

    /// Returns a mutable reference to the type of this property.
    #[inline]
    pub fn mutate_type(&mut self) -> &mut Type {
        &mut self.r#type
    }

    /// Associates a token for the colon with this property.
    pub fn with_tokens(mut self, tokens: TablePropertyTypeTokens) -> Self {
        self.tokens = Some(tokens);
        self
    }

    /// Sets the token for the colon associated with this property.
    #[inline]
    pub fn set_tokens(&mut self, tokens: TablePropertyTypeTokens) {
        self.tokens = Some(tokens);
    }

    /// Returns the token for the colon associated with this property, if any.
    #[inline]
    pub fn get_tokens(&self) -> Option<&TablePropertyTypeTokens> {
        self.tokens.as_ref()
    }

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

/// Represents a string literal property in a table type annotation (i.e. `["key"]: Type`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableLiteralPropertyType {
    string: StringType,
    r#type: Box<Type>,
    modifier: Option<TablePropertyModifier>,
    tokens: Option<TableIndexTypeTokens>,
}

impl TableLiteralPropertyType {
    /// Creates a new string literal property with the specified key and type.
    pub fn new(string: StringType, r#type: impl Into<Type>) -> Self {
        Self {
            string,
            r#type: Box::new(r#type.into()),
            modifier: None,
            tokens: None,
        }
    }

    /// Associates a modifier with this property.
    pub fn with_modifier(mut self, modifier: TablePropertyModifier) -> Self {
        self.set_modifier(modifier);
        self
    }

    /// Sets the modifier for this property.
    pub fn set_modifier(&mut self, modifier: TablePropertyModifier) {
        if self.modifier.as_ref() == Some(&modifier) {
            return;
        }

        self.modifier = Some(modifier);
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Removes the modifier for this property.
    pub fn remove_modifier(&mut self) {
        self.modifier = None;
        if let Some(tokens) = self.tokens.as_mut() {
            tokens.modifier = None;
        }
    }

    /// Returns the modifier for this property, if any.
    pub fn get_modifier(&self) -> Option<&TablePropertyModifier> {
        self.modifier.as_ref()
    }

    /// Returns the string key of this property.
    #[inline]
    pub fn get_string(&self) -> &StringType {
        &self.string
    }

    /// Returns a mutable reference to the string key of this property.
    #[inline]
    pub fn mutate_string(&mut self) -> &mut StringType {
        &mut self.string
    }

    /// Returns the type of this property.
    #[inline]
    pub fn get_type(&self) -> &Type {
        &self.r#type
    }

    /// Returns a mutable reference to the type of this property.
    #[inline]
    pub fn mutate_type(&mut self) -> &mut Type {
        &mut self.r#type
    }

    /// Associates tokens with this property.
    pub fn with_tokens(mut self, tokens: TableIndexTypeTokens) -> Self {
        self.tokens = Some(tokens);
        self
    }

    /// Sets the tokens associated with this property.
    #[inline]
    pub fn set_tokens(&mut self, tokens: TableIndexTypeTokens) {
        self.tokens = Some(tokens);
    }

    /// Returns the tokens associated with this property, if any.
    #[inline]
    pub fn get_tokens(&self) -> Option<&TableIndexTypeTokens> {
        self.tokens.as_ref()
    }

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

/// Represents an entry in a table type annotation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TablePropertyModifier {
    Read,
    Write,
}

/// Represents an entry in a table type annotation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TableEntryType {
    /// A named property entry.
    Property(TablePropertyType),
    /// A string literal property entry.
    Literal(TableLiteralPropertyType),
    /// An indexer entry.
    Indexer(TableIndexerType),
}

impl TableEntryType {
    /// Associates a modifier with this property.
    pub fn with_modifier(mut self, modifier: TablePropertyModifier) -> Self {
        match &mut self {
            Self::Property(property) => property.set_modifier(modifier),
            Self::Literal(literal) => literal.set_modifier(modifier),
            Self::Indexer(indexer) => indexer.set_modifier(modifier),
        }
        self
    }

    /// Sets the modifier for this property.
    pub fn set_modifier(&mut self, modifier: TablePropertyModifier) {
        match self {
            Self::Property(property) => property.set_modifier(modifier),
            Self::Literal(literal) => literal.set_modifier(modifier),
            Self::Indexer(indexer) => indexer.set_modifier(modifier),
        }
    }

    /// Removes the modifier for this property.
    pub fn remove_modifier(&mut self) {
        match self {
            Self::Property(property) => property.remove_modifier(),
            Self::Literal(literal) => literal.remove_modifier(),
            Self::Indexer(indexer) => indexer.remove_modifier(),
        }
    }

    /// Returns the modifier for this property, if any.
    pub fn get_modifier(&self) -> Option<&TablePropertyModifier> {
        match self {
            Self::Property(property) => property.get_modifier(),
            Self::Literal(literal) => literal.get_modifier(),
            Self::Indexer(indexer) => indexer.get_modifier(),
        }
    }

    /// Removes all comments from this table entry.
    pub fn clear_comments(&mut self) {
        match self {
            TableEntryType::Property(property) => property.clear_comments(),
            TableEntryType::Literal(literal) => literal.clear_comments(),
            TableEntryType::Indexer(indexer) => indexer.clear_comments(),
        }
    }

    /// Removes all whitespace from this table entry.
    pub fn clear_whitespaces(&mut self) {
        match self {
            TableEntryType::Property(property) => property.clear_whitespaces(),
            TableEntryType::Literal(literal) => literal.clear_whitespaces(),
            TableEntryType::Indexer(indexer) => indexer.clear_whitespaces(),
        }
    }

    pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
        match self {
            TableEntryType::Property(property) => property.replace_referenced_tokens(code),
            TableEntryType::Literal(literal) => literal.replace_referenced_tokens(code),
            TableEntryType::Indexer(indexer) => indexer.replace_referenced_tokens(code),
        }
    }

    pub(crate) fn shift_token_line(&mut self, amount: isize) {
        match self {
            TableEntryType::Property(property) => property.shift_token_line(amount),
            TableEntryType::Literal(literal) => literal.shift_token_line(amount),
            TableEntryType::Indexer(indexer) => indexer.shift_token_line(amount),
        }
    }

    pub(crate) fn filter_comments(&mut self, filter: impl Fn(&Trivia) -> bool) {
        match self {
            TableEntryType::Property(property) => property.filter_comments(filter),
            TableEntryType::Literal(literal) => literal.filter_comments(filter),
            TableEntryType::Indexer(indexer) => indexer.filter_comments(filter),
        }
    }
}

impl From<TablePropertyType> for TableEntryType {
    fn from(value: TablePropertyType) -> Self {
        Self::Property(value)
    }
}

impl From<TableLiteralPropertyType> for TableEntryType {
    fn from(value: TableLiteralPropertyType) -> Self {
        Self::Literal(value)
    }
}

impl From<TableIndexerType> for TableEntryType {
    fn from(value: TableIndexerType) -> Self {
        Self::Indexer(value)
    }
}

/// Represents a table type annotation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TableType {
    entries: Vec<TableEntryType>,
    tokens: Option<TableTypeTokens>,
}

impl TableType {
    /// Creates a new table type with a property and returns the modified table type.
    pub fn with_new_property(
        mut self,
        property: impl Into<Identifier>,
        r#type: impl Into<Type>,
    ) -> Self {
        self.entries
            .push(TablePropertyType::new(property.into(), r#type.into()).into());
        self
    }

    /// Adds a property to this table type and returns the modified table type.
    pub fn with_property(mut self, property: impl Into<TableEntryType>) -> Self {
        self.push_property(property.into());
        self
    }

    /// Adds a property to this table type.
    #[inline]
    pub fn push_property(&mut self, property: impl Into<TableEntryType>) {
        let property = property.into();
        match property {
            TableEntryType::Indexer(indexer) => {
                self.set_indexer_type(indexer);
            }
            _ => {
                self.entries.push(property);
            }
        }
    }

    /// Sets the indexer type for this table type and returns the modified table type.
    pub fn with_indexer_type(mut self, indexer_type: TableIndexerType) -> Self {
        self.set_indexer_type(indexer_type);
        self
    }

    /// Sets the indexer type for this table type.
    /// If the key type is a string, converts it to a string literal property.
    /// Returns the previous indexer type if one existed.
    #[inline]
    pub fn set_indexer_type(&mut self, indexer_type: TableIndexerType) -> Option<TableIndexerType> {
        match *indexer_type.key_type {
            Type::String(string_type) => {
                self.entries
                    .push(TableEntryType::Literal(TableLiteralPropertyType {
                        string: string_type,
                        r#type: indexer_type.value_type,
                        modifier: indexer_type.modifier,
                        tokens: indexer_type.tokens,
                    }));
                None
            }
            _ => {
                let removed = if let Some((remove_index, _)) = self
                    .entries
                    .iter()
                    .enumerate()
                    .find(|(_, entry)| matches!(entry, TableEntryType::Indexer(_)))
                {
                    match self.entries.remove(remove_index) {
                        TableEntryType::Indexer(indexer) => Some(indexer),
                        TableEntryType::Property(_) | TableEntryType::Literal(_) => unreachable!(),
                    }
                } else {
                    None
                };
                self.entries.push(TableEntryType::Indexer(indexer_type));
                removed
            }
        }
    }

    /// Returns whether this table type has an indexer.
    #[inline]
    pub fn has_indexer_type(&self) -> bool {
        self.entries
            .iter()
            .any(|entry| matches!(entry, TableEntryType::Indexer(_)))
    }

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

    /// Returns whether this table type has no entries.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Returns an iterator over the entries in this table type.
    #[inline]
    pub fn iter_entries(&self) -> impl Iterator<Item = &TableEntryType> {
        self.entries.iter()
    }

    /// Returns a mutable iterator over the entries in this table type.
    #[inline]
    pub fn iter_mut_entries(&mut self) -> impl Iterator<Item = &mut TableEntryType> {
        self.entries.iter_mut()
    }

    /// Associates tokens with this table type and returns the modified table type.
    pub fn with_tokens(mut self, tokens: TableTypeTokens) -> Self {
        self.tokens = Some(tokens);
        self
    }

    /// Sets the tokens associated with this table type.
    #[inline]
    pub fn set_tokens(&mut self, tokens: TableTypeTokens) {
        self.tokens = Some(tokens);
    }

    /// Returns the tokens associated with this table type, if any.
    #[inline]
    pub fn get_tokens(&self) -> Option<&TableTypeTokens> {
        self.tokens.as_ref()
    }

    pub fn mutate_last_token(&mut self) -> &mut Token {
        if self.tokens.is_none() {
            self.tokens = Some(TableTypeTokens {
                opening_brace: Token::from_content("{"),
                closing_brace: Token::from_content("}"),
                separators: Vec::new(),
            });
        }
        &mut self.tokens.as_mut().unwrap().closing_brace
    }

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

/// Contains the tokens that define a table type's syntax.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableTypeTokens {
    /// The opening brace token.
    pub opening_brace: Token,
    /// The closing brace token.
    pub closing_brace: Token,
    /// The comma tokens separating the entries.
    pub separators: Vec<Token>,
}

impl TableTypeTokens {
    super::impl_token_fns!(
        target = [opening_brace, closing_brace]
        iter = [separators]
    );
}