luaparse-rs 0.1.1

Multi-version Lua parser supporting Lua 5.1-5.4 and Luau
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
//! Expression nodes in the syntax tree.

use alloc::{boxed::Box, string::String, vec::Vec};

use crate::Span;
use super::common::{Identifier, Block, Parameter, TypeAnnotation};

/// A single parsed expression.
///
/// Every expression has a [`kind`](Self::kind) that tells you what it is,
/// and a [`span`](Self::span) pointing back into the source.
#[derive(Debug, Clone, PartialEq)]
pub struct Expr {
    /// What kind of expression this is.
    pub kind: ExprKind,
    /// Where it appears in the source.
    pub span: Span,
}

impl Expr {
    pub fn new(kind: ExprKind, span: Span) -> Self {
        Self { kind, span }
    }
    
    pub fn synthetic(kind: ExprKind) -> Self {
        Self { kind, span: 0..0 }
    }
}

/// All the different kinds of expression the parser can produce.
#[derive(Debug, Clone, PartialEq)]
pub enum ExprKind {
    /// The `nil` literal.
    Nil,
    /// `true` or `false`.
    Boolean(bool),
    /// A number like `42`, `3.14`, or `0xFF`.
    Number(NumberLiteral),
    /// A string like `"hello"` or `'world'`.
    String(StringLiteral),
    /// The `...` vararg expression.
    Vararg,

    /// A table constructor: `{1, 2, key = "val"}`.
    Table(TableConstructor),
    /// An anonymous function: `function(x) return x end`.
    Function(FunctionExpr),

    /// A variable reference: `foo`.
    Identifier(Identifier),
    /// A dot field access: `obj.field`.
    FieldAccess(FieldAccess),
    /// A bracket index: `tbl[key]`.
    IndexAccess(IndexAccess),

    /// A unary operation: `-x`, `not x`, `#t`, `~x`.
    Unary(UnaryExpr),
    /// A binary operation: `a + b`, `x and y`, `s .. t`.
    Binary(BinaryExpr),

    /// A function call: `foo(1, 2)`.
    Call(CallExpr),
    /// A method call: `obj:method(1, 2)`.
    MethodCall(MethodCallExpr),

    /// A Luau if expression: `if cond then a else b`.
    IfExpression(IfExpression),
    /// A Luau interpolated string: `` `hello {name}` ``.
    InterpolatedString(InterpolatedString),
    /// A Luau type assertion: `expr :: Type`.
    TypeAssertion(TypeAssertion),

    /// A parenthesized expression: `(expr)`.
    Parenthesized(Box<Expr>),
}

/// A number literal, stored as the raw source text.
///
/// Use [`parse_f64`](Self::parse_f64) to get the numeric value.
#[derive(Debug, Clone, PartialEq)]
pub struct NumberLiteral {
    /// The raw text from the source (e.g. `"0xFF"`, `"3.14e2"`).
    pub raw: String,
    /// Where it appears in the source.
    pub span: Span,
}

impl NumberLiteral {
    pub fn new(raw: String, span: Span) -> Self {
        Self { raw, span }
    }
    
    pub fn parse_f64(&self) -> Option<f64> {
        let cleaned = self.raw.replace('_', "");
        
        if cleaned.starts_with("0x") || cleaned.starts_with("0X") {
            // TODO
            u64::from_str_radix(&cleaned[2..].split('.').next()?, 16)
                .ok()
                .map(|v| v as f64)
        } else if cleaned.starts_with("0b") || cleaned.starts_with("0B") {
            u64::from_str_radix(&cleaned[2..], 2)
                .ok()
                .map(|v| v as f64)
        } else {
            cleaned.parse().ok()
        }
    }
}

/// A string literal with escape sequences already resolved.
#[derive(Debug, Clone, PartialEq)]
pub struct StringLiteral {
    /// The string content after processing escapes.
    pub value: String,
    /// Where it appears in the source.
    pub span: Span,
}

impl StringLiteral {
    pub fn new(value: String, span: Span) -> Self {
        Self { value, span }
    }
}

/// A table constructor: `{1, 2, key = "val", [expr] = val}`.
#[derive(Debug, Clone, PartialEq)]
pub struct TableConstructor {
    /// The fields in the table, in order.
    pub fields: Vec<TableField>,
    /// Where it appears in the source.
    pub span: Span,
}

impl TableConstructor {
    pub fn new(fields: Vec<TableField>, span: Span) -> Self {
        Self { fields, span }
    }
}

/// A single entry in a table constructor.
#[derive(Debug, Clone, PartialEq)]
pub struct TableField {
    /// What kind of table entry this is.
    pub kind: TableFieldKind,
    /// Where it appears in the source.
    pub span: Span,
}

impl TableField {
    pub fn new(kind: TableFieldKind, span: Span) -> Self {
        Self { kind, span }
    }
}

/// The different ways to define a table entry.
#[derive(Debug, Clone, PartialEq)]
pub enum TableFieldKind {
    /// `[expr] = value`.
    Bracketed { key: Expr, value: Expr },
    /// `name = value`.
    Named { name: Identifier, value: Expr },
    /// A positional value (no key), like `{1, 2, 3}`.
    Positional(Expr),
}

/// An anonymous function expression: `function(x, y) return x + y end`.
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionExpr {
    /// The parameter list.
    pub parameters: Vec<Parameter>,
    /// An optional return type annotation (Luau).
    pub return_type: Option<TypeAnnotation>,
    /// The function body.
    pub body: Block,
    /// Where it appears in the source.
    pub span: Span,
}

impl FunctionExpr {
    pub fn new(
        parameters: Vec<Parameter>,
        return_type: Option<TypeAnnotation>,
        body: Block,
        span: Span,
    ) -> Self {
        Self {
            parameters,
            return_type,
            body,
            span,
        }
    }
}

/// A dot field access: `obj.field`.
#[derive(Debug, Clone, PartialEq)]
pub struct FieldAccess {
    /// The expression being accessed.
    pub base: Box<Expr>,
    /// The field name after the dot.
    pub field: Identifier,
    /// Where it appears in the source.
    pub span: Span,
}

impl FieldAccess {
    pub fn new(base: Expr, field: Identifier, span: Span) -> Self {
        Self {
            base: Box::new(base),
            field,
            span,
        }
    }
}

/// A bracket index: `tbl[key]`.
#[derive(Debug, Clone, PartialEq)]
pub struct IndexAccess {
    /// The expression being indexed.
    pub base: Box<Expr>,
    /// The index expression inside the brackets.
    pub index: Box<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl IndexAccess {
    pub fn new(base: Expr, index: Expr, span: Span) -> Self {
        Self {
            base: Box::new(base),
            index: Box::new(index),
            span,
        }
    }
}

/// A unary operation like `-x` or `not cond`.
#[derive(Debug, Clone, PartialEq)]
pub struct UnaryExpr {
    /// Which operator.
    pub operator: UnaryOperator,
    /// The expression it applies to.
    pub operand: Box<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl UnaryExpr {
    pub fn new(operator: UnaryOperator, operand: Expr, span: Span) -> Self {
        Self {
            operator,
            operand: Box::new(operand),
            span,
        }
    }
}

/// The unary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOperator {
    /// `-` (negation).
    Minus,
    /// `not` (logical negation).
    Not,
    /// `#` (length).
    Length,
    /// `~` (bitwise not, Lua 5.3+).
    BitwiseNot,
}

impl UnaryOperator {
    pub const fn binding_power(self) -> u8 {
        14
    }
}

/// A binary operation like `a + b` or `x and y`.
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryExpr {
    /// Which operator.
    pub operator: BinaryOperator,
    /// The left operand.
    pub left: Box<Expr>,
    /// The right operand.
    pub right: Box<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl BinaryExpr {
    pub fn new(operator: BinaryOperator, left: Expr, right: Expr, span: Span) -> Self {
        Self {
            operator,
            left: Box::new(left),
            right: Box::new(right),
            span,
        }
    }
}

/// All the binary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOperator {
    /// `+`
    Add,
    /// Subtraction.
    Subtract,
    /// `*`
    Multiply,
    /// `/`
    Divide,
    /// `//` (Lua 5.3+, Luau).
    FloorDiv,
    /// `%`
    Modulo,
    /// `^`
    Power,

    /// `..` (string concatenation).
    Concat,

    /// `==`
    Equal,
    /// `~=`
    NotEqual,
    /// `<`
    Less,
    /// `<=`
    LessEqual,
    /// `>`
    Greater,
    /// `>=`
    GreaterEqual,

    /// `and`
    And,
    /// `or`
    Or,

    /// `&` (Lua 5.3+).
    BitwiseAnd,
    /// `|` (Lua 5.3+).
    BitwiseOr,
    /// `~` (bitwise xor, Lua 5.3+).
    BitwiseXor,
    /// `<<` (Lua 5.3+).
    LeftShift,
    /// `>>` (Lua 5.3+).
    RightShift,
}

impl BinaryOperator {
    pub const fn binding_power(self) -> (u8, u8) {
        match self {
            Self::Or => (1, 2),
            Self::And => (3, 4),
            Self::Equal | Self::NotEqual | Self::Less | Self::LessEqual
            | Self::Greater | Self::GreaterEqual => (5, 6),
            Self::BitwiseOr => (6, 7),
            Self::BitwiseXor => (7, 8),
            Self::BitwiseAnd => (8, 9),
            Self::LeftShift | Self::RightShift => (9, 10),
            Self::Concat => (10, 9),
            Self::Add | Self::Subtract => (11, 12),
            Self::Multiply | Self::Divide | Self::FloorDiv | Self::Modulo => (13, 14),
            Self::Power => (16, 15),
        }
    }
    
    pub const fn is_right_associative(self) -> bool {
        matches!(self, Self::Concat | Self::Power)
    }
}

/// A function call: `foo(1, 2)` or `foo "hello"` or `foo {1,2}`.
#[derive(Debug, Clone, PartialEq)]
pub struct CallExpr {
    /// The expression being called.
    pub function: Box<Expr>,
    /// The arguments passed.
    pub arguments: Vec<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl CallExpr {
    pub fn new(function: Expr, arguments: Vec<Expr>, span: Span) -> Self {
        Self {
            function: Box::new(function),
            arguments,
            span,
        }
    }
}

/// A method call: `obj:method(1, 2)`.
#[derive(Debug, Clone, PartialEq)]
pub struct MethodCallExpr {
    /// The object being called on.
    pub base: Box<Expr>,
    /// The method name.
    pub method: Identifier,
    /// The arguments passed.
    pub arguments: Vec<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl MethodCallExpr {
    pub fn new(base: Expr, method: Identifier, arguments: Vec<Expr>, span: Span) -> Self {
        Self {
            base: Box::new(base),
            method,
            arguments,
            span,
        }
    }
}

/// A Luau if/then/else expression: `if cond then a elseif cond2 then b else c`.
#[derive(Debug, Clone, PartialEq)]
pub struct IfExpression {
    /// The condition after `if`.
    pub condition: Box<Expr>,
    /// The value when the condition is true.
    pub then_branch: Box<Expr>,
    /// Zero or more `elseif` branches.
    pub elseif_branches: Vec<ElseIfExprBranch>,
    /// The `else` value (always present in if expressions).
    pub else_branch: Box<Expr>,
    /// Where it appears in the source.
    pub span: Span,
}

impl IfExpression {
    pub fn new(
        condition: Expr,
        then_branch: Expr,
        elseif_branches: Vec<ElseIfExprBranch>,
        else_branch: Expr,
        span: Span,
    ) -> Self {
        Self {
            condition: Box::new(condition),
            then_branch: Box::new(then_branch),
            elseif_branches,
            else_branch: Box::new(else_branch),
            span,
        }
    }
}

/// One `elseif cond then value` branch inside an [`IfExpression`].
#[derive(Debug, Clone, PartialEq)]
pub struct ElseIfExprBranch {
    /// The condition after `elseif`.
    pub condition: Expr,
    /// The value when this condition is true.
    pub then_branch: Expr,
}

impl ElseIfExprBranch {
    pub fn new(condition: Expr, then_branch: Expr) -> Self {
        Self {
            condition,
            then_branch,
        }
    }
}

/// A Luau interpolated string: `` `hello {name}, you are {age}` ``.
#[derive(Debug, Clone, PartialEq)]
pub struct InterpolatedString {
    /// The alternating text and expression segments.
    pub segments: Vec<InterpolationSegment>,
    /// Where it appears in the source.
    pub span: Span,
}

impl InterpolatedString {
    pub fn new(segments: Vec<InterpolationSegment>, span: Span) -> Self {
        Self { segments, span }
    }
}

/// A piece of an interpolated string.
#[derive(Debug, Clone, PartialEq)]
pub enum InterpolationSegment {
    /// A literal text segment between expressions.
    Text(String),
    /// An embedded `{expression}`.
    Expression(Expr),
}

/// A Luau type assertion: `expr :: Type`.
#[derive(Debug, Clone, PartialEq)]
pub struct TypeAssertion {
    /// The expression being asserted.
    pub expression: Box<Expr>,
    /// The type being asserted.
    pub type_annotation: TypeAnnotation,
    /// Where it appears in the source.
    pub span: Span,
}

impl TypeAssertion {
    pub fn new(expression: Expr, type_annotation: TypeAnnotation, span: Span) -> Self {
        Self {
            expression: Box::new(expression),
            type_annotation,
            span,
        }
    }
}