full_moon 0.9.0

A lossless Lua 5.1 parser
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
//! Contains the types necessary to parse [Roblox's typed Lua](https://devforum.roblox.com/t/luau-type-checking-beta/435382).
//! Only usable when the "roblox" feature flag is enabled.
use super::{punctuated::Punctuated, span::ContainedSpan, *};
use crate::util::display_option;
use derive_more::Display;

/// Any type, such as `string`, `boolean?`, `number | boolean`, etc.
#[derive(Clone, Debug, Display, PartialEq, Owned, Node)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum TypeInfo<'a> {
    /// A shorthand type annotating the structure of an array: { number }
    #[display(fmt = "{}{}{}", "braces.tokens().0", "type_info", "braces.tokens().1")]
    Array {
        /// The braces (`{}`) containing the type info.
        #[cfg_attr(feature = "serde", serde(borrow))]
        braces: ContainedSpan<'a>,
        /// The type info for the values in the Array
        #[cfg_attr(feature = "serde", serde(borrow))]
        type_info: Box<TypeInfo<'a>>,
    },

    /// A standalone type, such as `string` or `Foo`.
    #[display(fmt = "{}", "_0")]
    Basic(#[cfg_attr(feature = "serde", serde(borrow))] Cow<'a, TokenReference<'a>>),

    /// A callback type, such as `(string, number) => boolean`.
    #[display(
        fmt = "{}{}{}{}{}",
        "parentheses.tokens().0",
        "arguments",
        "parentheses.tokens().1",
        "arrow",
        "return_type"
    )]
    Callback {
        /// The parentheses for the arguments.
        #[cfg_attr(feature = "serde", serde(borrow))]
        parentheses: ContainedSpan<'a>,
        /// The argument types: `(string, number)`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        arguments: Punctuated<'a, TypeInfo<'a>>,
        /// The "thin arrow" (`->`) in between the arguments and the return type.
        #[cfg_attr(feature = "serde", serde(borrow))]
        arrow: Cow<'a, TokenReference<'a>>,
        /// The return type: `boolean`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        return_type: Box<TypeInfo<'a>>,
    },

    /// A type using generics, such as `map<number, string>`.
    #[display(
        fmt = "{}{}{}{}",
        "base",
        "arrows.tokens().0",
        "generics",
        "arrows.tokens().1"
    )]
    Generic {
        /// The type that has generics: `map`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        base: Cow<'a, TokenReference<'a>>,
        /// The arrows (`<>`) containing the type parameters.
        #[cfg_attr(feature = "serde", serde(borrow))]
        arrows: ContainedSpan<'a>,
        /// The type parameters: `number, string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        generics: Punctuated<'a, TypeInfo<'a>>,
    },

    /// An intersection type: `string & number`, denoting both types.
    #[display(fmt = "{}{}{}", "left", "ampersand", "right")]
    Intersection {
        /// The left hand side: `string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        left: Box<TypeInfo<'a>>,
        /// The ampersand (`&`) to separate the types.
        #[cfg_attr(feature = "serde", serde(borrow))]
        ampersand: Cow<'a, TokenReference<'a>>,
        /// The right hand side: `number`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        right: Box<TypeInfo<'a>>,
    },

    /// A type coming from a module, such as `module.Foo`
    #[display(fmt = "{}{}{}", "module", "punctuation", "type_info")]
    Module {
        /// The module the type is coming from: `module`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        module: Cow<'a, TokenReference<'a>>,
        /// The punctuation (`.`) to index the module.
        #[cfg_attr(feature = "serde", serde(borrow))]
        punctuation: Cow<'a, TokenReference<'a>>,
        /// The indexed type info: `Foo`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        type_info: Box<IndexedTypeInfo<'a>>,
    },

    /// An optional type, such as `string?`.
    #[display(fmt = "{}{}", "base", "question_mark")]
    Optional {
        /// The type that is optional: `string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        base: Box<TypeInfo<'a>>,
        /// The question mark: `?`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        question_mark: Cow<'a, TokenReference<'a>>,
    },

    /// A type annotating the structure of a table: { foo: number, bar: string }
    #[display(fmt = "{}{}{}", "braces.tokens().0", "fields", "braces.tokens().1")]
    Table {
        /// The braces (`{}`) containing the fields.
        #[cfg_attr(feature = "serde", serde(borrow))]
        braces: ContainedSpan<'a>,
        /// The fields: `foo: number, bar: string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        fields: Punctuated<'a, TypeField<'a>>,
    },

    /// A type in the form of `typeof(foo)`.
    #[display(
        fmt = "{}{}{}{}",
        "typeof_token",
        "parentheses.tokens().0",
        "inner",
        "parentheses.tokens().1"
    )]
    Typeof {
        /// The token `typeof`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        typeof_token: Cow<'a, TokenReference<'a>>,
        /// The parentheses used to contain the expression.
        #[cfg_attr(feature = "serde", serde(borrow))]
        parentheses: ContainedSpan<'a>,
        /// The inner expression: `foo`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        inner: Box<Expression<'a>>,
    },

    /// A tuple expression: `(string, number)`.
    #[display(
        fmt = "{}{}{}",
        "parentheses.tokens().0",
        "types",
        "parentheses.tokens().1"
    )]
    Tuple {
        /// The parentheses used to contain the types
        #[cfg_attr(feature = "serde", serde(borrow))]
        parentheses: ContainedSpan<'a>,
        /// The types: `(string, number)`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        types: Punctuated<'a, TypeInfo<'a>>,
    },

    /// A union type: `string | number`, denoting one or the other.
    #[display(fmt = "{}{}{}", "left", "pipe", "right")]
    Union {
        /// The left hand side: `string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        left: Box<TypeInfo<'a>>,
        /// The pipe (`|`) to separate the types.
        #[cfg_attr(feature = "serde", serde(borrow))]
        pipe: Cow<'a, TokenReference<'a>>,
        /// The right hand side: `number`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        right: Box<TypeInfo<'a>>,
    },
}

/// A subset of TypeInfo that consists of items which can only be used as an index, such as `Foo` and `Foo<Bar>`,
#[derive(Clone, Debug, Display, PartialEq, Owned, Node)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum IndexedTypeInfo<'a> {
    /// A standalone type, such as `string` or `Foo`.
    #[display(fmt = "{}", "_0")]
    Basic(#[cfg_attr(feature = "serde", serde(borrow))] Cow<'a, TokenReference<'a>>),

    /// A type using generics, such as `map<number, string>`.
    #[display(
        fmt = "{}{}{}{}",
        "base",
        "arrows.tokens().0",
        "generics",
        "arrows.tokens().1"
    )]
    Generic {
        /// The type that has generics: `map`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        base: Cow<'a, TokenReference<'a>>,
        /// The arrows (`<>`) containing the type parameters.
        #[cfg_attr(feature = "serde", serde(borrow))]
        arrows: ContainedSpan<'a>,
        /// The type parameters: `number, string`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        generics: Punctuated<'a, TypeInfo<'a>>,
    },
}

/// A type field used within table types.
/// The `foo: number` in `{ foo: number }`.
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}{}", "key", "colon", "value")]
pub struct TypeField<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) key: TypeFieldKey<'a>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) colon: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) value: TypeInfo<'a>,
}

impl<'a> TypeField<'a> {
    /// The key of the field, `foo` in `foo: number`.
    pub fn key(&self) -> &TypeFieldKey<'a> {
        &self.key
    }

    /// The colon in between the key name and the value type.
    pub fn colon_token(&self) -> &TokenReference<'a> {
        &self.colon
    }

    /// The type for the field, `number` in `foo: number`.
    pub fn value(&self) -> &TypeInfo<'a> {
        &self.value
    }

    /// Returns a new TypeField with the given key
    pub fn with_key(self, key: TypeFieldKey<'a>) -> Self {
        Self { key, ..self }
    }

    /// Returns a new TypeField with the `:` token
    pub fn with_colon_token(self, colon_token: Cow<'a, TokenReference<'a>>) -> Self {
        Self {
            colon: colon_token,
            ..self
        }
    }

    /// Returns a new TypeField with the `:` token
    pub fn with_value(self, value: TypeInfo<'a>) -> Self {
        Self { value, ..self }
    }
}

/// A key in a [`TypeField`]. Can either be a name or an index signature.
#[derive(Clone, Debug, Display, PartialEq, Owned, Node)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum TypeFieldKey<'a> {
    /// A name, such as `foo`.
    #[display(fmt = "{}", "_0")]
    Name(Cow<'a, TokenReference<'a>>),

    /// An index signature, such as `[number]`.
    #[display(fmt = "{}{}{}", "brackets.tokens().0", "inner", "brackets.tokens().1")]
    IndexSignature {
        /// The brackets (`[]`) used to contain the type.
        #[cfg_attr(feature = "serde", serde(borrow))]
        brackets: ContainedSpan<'a>,

        /// The type for the index signature, `number` in `[number]`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        inner: TypeInfo<'a>,
    },
}

/// A type assertion using `as`, such as `as number`.
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}", "as_token", "cast_to")]
pub struct AsAssertion<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) as_token: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) cast_to: TypeInfo<'a>,
}

impl<'a> AsAssertion<'a> {
    /// The token `as`.
    pub fn as_token(&self) -> &TokenReference<'a> {
        &self.as_token
    }

    /// The type to cast the expression into, `number` in `as number`.
    pub fn cast_to(&self) -> &TypeInfo<'a> {
        &self.cast_to
    }

    /// Returns a new AsAssertion with the given `as` token
    pub fn with_as_token(self, as_token: Cow<'a, TokenReference<'a>>) -> Self {
        Self { as_token, ..self }
    }

    /// Returns a new AsAssertion with the given TypeInfo to cast to
    pub fn with_cast_to(self, cast_to: TypeInfo<'a>) -> Self {
        Self { cast_to, ..self }
    }
}

/// A type declaration, such as `type Meters = number`
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(
    fmt = "{}{}{}{}{}",
    "type_token",
    "base",
    "display_option(generics)",
    "equal_token",
    "declare_as"
)]
pub struct TypeDeclaration<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) type_token: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) base: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) generics: Option<GenericDeclaration<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) equal_token: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) declare_as: TypeInfo<'a>,
}

impl<'a> TypeDeclaration<'a> {
    /// The token `type`.
    pub fn type_token(&self) -> &TokenReference<'a> {
        &self.type_token
    }

    /// The name of the type, `Meters` in `type Meters = number`.
    pub fn type_name(&self) -> &TokenReference<'a> {
        &self.base
    }

    /// The generics of the type, if there are any. `<T>` in `type Foo<T> = T`.
    pub fn generics(&self) -> Option<&GenericDeclaration<'a>> {
        self.generics.as_ref()
    }

    /// The `=` token in between the type name and the definition.
    pub fn equal_token(&self) -> &TokenReference<'a> {
        &self.equal_token
    }

    /// The definition of the type, `number` in `type Meters = number`.
    pub fn type_definition(&self) -> &TypeInfo<'a> {
        &self.declare_as
    }

    /// Returns a new TypeDeclaration with the given `type` token
    pub fn with_type_token(self, type_token: Cow<'a, TokenReference<'a>>) -> Self {
        Self { type_token, ..self }
    }

    /// Returns a new TypeDeclaration with the given type name
    pub fn with_type_name(self, type_name: Cow<'a, TokenReference<'a>>) -> Self {
        Self {
            base: type_name,
            ..self
        }
    }

    /// Returns a new TypeDeclaration with the given generics of the type
    pub fn with_generics(self, generics: Option<GenericDeclaration<'a>>) -> Self {
        Self { generics, ..self }
    }

    /// Returns a new TypeDeclaration with the given generics of the type
    pub fn with_equal_token(self, equal_token: Cow<'a, TokenReference<'a>>) -> Self {
        Self {
            equal_token,
            ..self
        }
    }

    /// Returns a new TypeDeclaration with the given generics of the type
    pub fn with_type_definition(self, type_definition: TypeInfo<'a>) -> Self {
        Self {
            declare_as: type_definition,
            ..self
        }
    }
}

/// The generics used in a [`TypeDeclaration`].
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}{}", "arrows.tokens().0", "generics", "arrows.tokens().1")]
pub struct GenericDeclaration<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    #[visit(contains = "generics")]
    pub(crate) arrows: ContainedSpan<'a>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) generics: Punctuated<'a, Cow<'a, TokenReference<'a>>>,
}

impl<'a> GenericDeclaration<'a> {
    /// The arrows (`<>`) containing the types.
    pub fn arrows(&self) -> &ContainedSpan<'a> {
        &self.arrows
    }

    /// The names of the generics: `T, U` in `<T, U>`.
    pub fn generics(&self) -> &Punctuated<'a, Cow<'a, TokenReference<'a>>> {
        &self.generics
    }

    /// Returns a new GenericDeclaration with the given arrows containing the types
    pub fn with_arrows(self, arrows: ContainedSpan<'a>) -> Self {
        Self { arrows, ..self }
    }

    /// Returns a new TypeDeclaration with the given names of the generics
    pub fn with_generics(self, generics: Punctuated<'a, Cow<'a, TokenReference<'a>>>) -> Self {
        Self { generics, ..self }
    }
}

/// A type specifier, the `: number` in `local foo: number`
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}", "punctuation", "type_info")]
pub struct TypeSpecifier<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) punctuation: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) type_info: TypeInfo<'a>,
}

impl<'a> TypeSpecifier<'a> {
    /// The punctuation being used.
    /// `:` for `local foo: number`.
    pub fn punctuation(&self) -> &TokenReference<'a> {
        &self.punctuation
    }

    /// The type being specified: `number` in `local foo: number`.
    pub fn type_info(&self) -> &TypeInfo<'a> {
        &self.type_info
    }

    /// Returns a new TypeSpecifier with the given punctuation
    pub fn with_punctuation(self, punctuation: Cow<'a, TokenReference<'a>>) -> Self {
        Self {
            punctuation,
            ..self
        }
    }

    /// Returns a new TypeSpecifier with the given type being specified
    pub fn with_type_info(self, type_info: TypeInfo<'a>) -> Self {
        Self { type_info, ..self }
    }
}

/// An exported type declaration, such as `export type Meters = number`
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}", "export_token", "type_declaration")]
pub struct ExportedTypeDeclaration<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) export_token: Cow<'a, TokenReference<'a>>,
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) type_declaration: TypeDeclaration<'a>,
}

impl<'a> ExportedTypeDeclaration<'a> {
    /// The token `export`.
    pub fn export_token(&self) -> &TokenReference<'a> {
        &self.export_token
    }

    /// The type declaration, `type Meters = number`.
    pub fn type_declaration(&self) -> &TypeDeclaration<'a> {
        &self.type_declaration
    }

    /// Returns a new ExportedTypeDeclaration with the `export` token
    pub fn with_export_token(self, export_token: Cow<'a, TokenReference<'a>>) -> Self {
        Self {
            export_token,
            ..self
        }
    }

    /// Returns a new TypeDeclaration with the given type declaration
    pub fn with_type_declaration(self, type_declaration: TypeDeclaration<'a>) -> Self {
        Self {
            type_declaration,
            ..self
        }
    }
}

make_op!(CompoundOp,
    #[doc = "Compound operators, such as X += Y or X -= Y"]
    {
        PlusEqual,
        MinusEqual,
        StarEqual,
        SlashEqual,
        PercentEqual,
        CaretEqual,
        TwoDotsEqual,
    }
);

/// A Compound Assignment statement, such as `x += 1` or `x -= 1`
#[derive(Clone, Debug, Display, PartialEq, Owned, Node, Visit)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[display(fmt = "{}{}{}", "lhs", "compound_operator", "rhs")]
pub struct CompoundAssignment<'a> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub(crate) lhs: Var<'a>,
    pub(crate) compound_operator: CompoundOp<'a>,
    pub(crate) rhs: Expression<'a>,
}

impl<'a> CompoundAssignment<'a> {
    /// Creates a new CompoundAssignment from the left and right hand side
    pub fn new(lhs: Var<'a>, compound_operator: CompoundOp<'a>, rhs: Expression<'a>) -> Self {
        Self {
            lhs,
            compound_operator,
            rhs,
        }
    }

    /// The variable assigned to, the `x` part of `x += 1`
    pub fn lhs(&self) -> &Var<'a> {
        &self.lhs
    }

    /// The operator used, the `+=` part of `x += 1`
    pub fn compound_operator(&self) -> &CompoundOp<'a> {
        &self.compound_operator
    }

    /// The value being assigned, the `1` part of `x += 1`
    pub fn rhs(&self) -> &Expression<'a> {
        &self.rhs
    }

    /// Returns a new CompoundAssignment with the given variable being assigned to
    pub fn with_lhs(self, lhs: Var<'a>) -> Self {
        Self { lhs, ..self }
    }

    /// Returns a new CompoundAssignment with the given operator used
    pub fn with_compound_operator(self, compound_operator: CompoundOp<'a>) -> Self {
        Self {
            compound_operator,
            ..self
        }
    }

    /// Returns a new CompoundAssignment with the given value being assigned
    pub fn with_rhs(self, rhs: Expression<'a>) -> Self {
        Self { rhs, ..self }
    }
}