rhue-parser 2.1.0

Parser and syntax tree for the Rhue scripting language
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
use std::fmt;
use std::rc::Rc;

use rhue_common::Span;

/// One occurrence of a name, a declaration or a use. The parser hands these out
/// densely from 0, so later passes can keep side tables in a `Vec` indexed by
/// `NameId` instead of hashing AST nodes.
pub type NameId = u32;

#[derive(Clone, Debug, PartialEq)]
pub struct Name {
    pub text: Rc<str>,
    pub id: NameId,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Chunk {
    pub body: Block,
    pub span: Span,
    /// How many `NameId`s got handed out, sizes the resolver's tables.
    pub name_count: u32,
    /// The `--!strict` / `--!nonstrict` / `--!nocheck` directive, if any.
    pub mode: Option<(TypeMode, Span)>,
}

/// How hard a chunk gets type-checked, set by a directive comment at the top of
/// the file.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TypeMode {
    /// `--!nocheck`: nothing's checked.
    NoCheck,
    /// `--!nonstrict`: anything unannotated is `any`, only definite mismatches
    /// are errors.
    NonStrict,
    /// `--!strict`: also unknown globals, maybe-nil values and so on.
    Strict,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Block {
    pub stmts: Vec<Stmt>,
    pub ret: Option<Return>,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Return {
    pub values: Vec<Expr>,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Stmt {
    pub kind: StmtKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub enum StmtKind {
    /// `local a, b = 1, 2` (or `local a: number = 1`)
    Local {
        names: Vec<Name>,
        /// One per name, `None` where it's unannotated.
        types: Vec<Option<TypeExpr>>,
        values: Vec<Expr>,
    },
    /// `a, b = b, a`
    Assign {
        targets: Vec<Expr>,
        values: Vec<Expr>,
    },
    /// `x += 1`, `s ..= "!"`
    CompoundAssign {
        op: BinOp,
        op_span: Span,
        target: Expr,
        value: Expr,
    },
    Do(Block),
    While {
        cond: Expr,
        body: Block,
    },
    /// The condition can see the body's locals, same as Lua.
    Repeat {
        body: Block,
        cond: Expr,
    },
    If {
        branches: Vec<(Expr, Block)>,
        else_block: Option<Block>,
    },
    NumericFor {
        var: Name,
        var_type: Option<Box<TypeExpr>>,
        start: Expr,
        limit: Expr,
        step: Option<Expr>,
        body: Block,
    },
    Break,
    Continue,
    /// A call used as a statement (results get thrown away).
    Call(Expr),
    /// `function name(...) end`, assigns a new function to `name`.
    Function {
        target: Expr,
        func: Rc<FunctionBody>,
    },
    /// `local function name(...) end`. `name` is in scope inside the body so
    /// the function can call itself.
    LocalFunction {
        name: Name,
        func: Rc<FunctionBody>,
    },
    /// `for k, v in explist do ... end`. The list gives an iterator function, a
    /// state and a starting control value. Luau's generalized iteration (`for
    /// k, v in t`) also takes a table directly.
    GenericFor {
        names: Vec<Name>,
        types: Vec<Option<TypeExpr>>,
        exprs: Vec<Expr>,
        body: Block,
    },
    /// `type Name<T> = ...` or `export type ...`. Does nothing at runtime.
    TypeAlias(Rc<TypeAlias>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct TypeAlias {
    pub name: Rc<str>,
    pub name_span: Span,
    pub generics: Vec<GenericParam>,
    pub ty: TypeExpr,
    pub exported: bool,
}

/// Parameters and body, shared by every way of writing a function.
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionBody {
    /// `<T, U>` after the name.
    pub generics: Vec<GenericParam>,
    pub params: Vec<Name>,
    /// One per parameter, `None` where it's unannotated (always for the
    /// implicit `self` of a `function a:m()` method).
    pub param_types: Vec<Option<TypeExpr>>,
    pub is_vararg: bool,
    /// `...: T`
    pub vararg_type: Option<TypeExpr>,
    /// `): T` or `): (A, B)`
    pub return_type: Option<TypePack>,
    /// Declared with `:`, so `params[0]` is the implicit `self`.
    pub is_method: bool,
    pub body: Block,
    /// The name it was declared with, for tracebacks (`None` if anonymous).
    pub name: Option<Rc<str>>,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Expr {
    pub kind: ExprKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ExprKind {
    Nil,
    Bool(bool),
    Number(f64),
    String(Rc<str>),
    /// `` `text {expr} text` `` with at least one hole (no holes is just an
    /// [`ExprKind::String`]).
    Interp(Vec<InterpPart>),
    Name(Name),
    /// A parenthesised expression. It stays a node because `(f())` truncates
    /// multiple results to one.
    Group(Box<Expr>),
    Unary {
        op: UnOp,
        op_span: Span,
        operand: Box<Expr>,
    },
    Binary {
        op: BinOp,
        op_span: Span,
        lhs: Box<Expr>,
        rhs: Box<Expr>,
    },
    /// Luau's `if a then b elseif c then d else e` expression.
    IfElse {
        branches: Vec<(Expr, Expr)>,
        else_expr: Box<Expr>,
    },
    /// `f(a, b)` or `f "str"`.
    Call {
        callee: Box<Expr>,
        args: Vec<Expr>,
    },
    /// `...` inside a vararg function.
    Vararg,
    /// `function(...) ... end`
    Function(Rc<FunctionBody>),
    /// `t[k]`, or `t.name` (`key` is a string literal then).
    Index {
        object: Box<Expr>,
        key: Box<Expr>,
    },
    /// `obj:name(args)`, calls `obj.name` with `obj` as the first argument.
    MethodCall {
        object: Box<Expr>,
        method: Rc<str>,
        method_span: Span,
        args: Vec<Expr>,
    },
    /// `{ a, b, name = c, [k] = d }`
    Table(Vec<TableItem>),
    /// `expr :: Type`, a type assertion. At runtime it's just `expr`.
    Cast {
        expr: Box<Expr>,
        ty: Box<TypeExpr>,
    },
}

// ─ Types ─────────────────────────────────────────────────────────────────────

/// A type written in the source.
#[derive(Clone, Debug, PartialEq)]
pub struct TypeExpr {
    pub kind: TypeKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub enum TypeKind {
    /// `number`, `Map<string, T>`, `nil`, a generic parameter.
    Name {
        name: Rc<str>,
        args: Vec<TypeExpr>,
    },
    /// `"on"`, `true`
    Singleton(Singleton),
    /// `typeof(expr)`
    Typeof(Box<Expr>),
    /// `T?`
    Optional(Box<TypeExpr>),
    Union(Vec<TypeExpr>),
    Intersection(Vec<TypeExpr>),
    /// `{ x: number, [string]: boolean }`, or `{T}` (an indexer from number).
    Table {
        props: Vec<TableProp>,
        indexer: Option<Box<(TypeExpr, TypeExpr)>>,
    },
    /// `T...`, a generic type pack, wherever a variadic type can go (`...:
    /// T...`, `(A, T...) -> T...`).
    GenericPack(Rc<str>),
    /// An explicit type pack passed to a type alias: `(A, B)`, `()`, `(...T)`.
    Pack(TypePack),
    /// `<T>(a: T, ...number) -> (T, string)`
    Function {
        generics: Vec<GenericParam>,
        params: Vec<FnParam>,
        variadic: Option<Box<TypeExpr>>,
        returns: TypePack,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub enum Singleton {
    Bool(bool),
    String(Rc<str>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct TableProp {
    pub name: Rc<str>,
    pub ty: TypeExpr,
    pub span: Span,
    /// `read name: T` / `write name: T` (both if neither).
    pub access: Access,
}

/// What a table type's property allows.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Access {
    #[default]
    ReadWrite,
    /// `read x: T`: readable, can't be assigned.
    Read,
    /// `write x: T`: assignable, can't be read.
    Write,
}

impl Access {
    pub fn can_read(self) -> bool {
        self != Access::Write
    }

    pub fn can_write(self) -> bool {
        self != Access::Read
    }

    /// `read ` / `write ` / nothing, as written before a property.
    pub fn prefix(self) -> &'static str {
        match self {
            Access::ReadWrite => "",
            Access::Read => "read ",
            Access::Write => "write ",
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct FnParam {
    /// Parameter names in function types are just documentation.
    pub name: Option<Rc<str>>,
    pub ty: TypeExpr,
}

#[derive(Clone, Debug, PartialEq)]
pub struct GenericParam {
    pub name: Rc<str>,
    pub span: Span,
    /// `T...`: a generic type pack (any number of types).
    pub pack: bool,
    /// `T = string` (type aliases only). For a pack it's a [`TypeKind::Pack`]
    /// or [`TypeKind::GenericPack`].
    pub default: Option<TypeExpr>,
}

/// A list of types: function results, `(A, B)`, `...T`, `()`.
#[derive(Clone, Debug, PartialEq)]
pub struct TypePack {
    pub types: Vec<TypeExpr>,
    pub variadic: Option<Box<TypeExpr>>,
    pub span: Span,
}

impl fmt::Display for TypeExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            TypeKind::Name { name, args } => {
                f.write_str(name)?;
                if !args.is_empty() {
                    let args: Vec<String> = args.iter().map(|a| a.to_string()).collect();
                    write!(f, "<{}>", args.join(", "))?;
                }
                Ok(())
            }
            TypeKind::Singleton(Singleton::Bool(b)) => write!(f, "{b}"),
            TypeKind::Singleton(Singleton::String(s)) => write!(f, "{s:?}"),
            TypeKind::Typeof(e) => write!(f, "typeof({e})"),
            TypeKind::Optional(t) => write!(f, "{t}?"),
            TypeKind::GenericPack(name) => write!(f, "{name}..."),
            TypeKind::Pack(p) => write!(f, "{p}"),
            TypeKind::Union(ts) | TypeKind::Intersection(ts) => {
                let sep = if matches!(self.kind, TypeKind::Union(_)) {
                    " | "
                } else {
                    " & "
                };
                let parts: Vec<String> = ts.iter().map(|t| t.to_string()).collect();
                write!(f, "({})", parts.join(sep))
            }
            TypeKind::Table { props, indexer } => {
                let mut parts: Vec<String> = props
                    .iter()
                    .map(|p| format!("{}{}: {}", p.access.prefix(), p.name, p.ty))
                    .collect();
                if let Some(ix) = indexer {
                    parts.push(format!("[{}]: {}", ix.0, ix.1));
                }
                write!(f, "{{{}}}", parts.join(", "))
            }
            TypeKind::Function {
                generics,
                params,
                variadic,
                returns,
            } => {
                if !generics.is_empty() {
                    let g: Vec<String> = generics
                        .iter()
                        .map(|g| format!("{}{}", g.name, if g.pack { "..." } else { "" }))
                        .collect();
                    write!(f, "<{}>", g.join(", "))?;
                }
                let mut ps: Vec<String> = params.iter().map(|p| p.ty.to_string()).collect();
                if let Some(v) = variadic {
                    ps.push(variadic_text(v));
                }
                write!(f, "({}) -> {returns}", ps.join(", "))
            }
        }
    }
}

impl fmt::Display for TypePack {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut parts: Vec<String> = self.types.iter().map(|t| t.to_string()).collect();
        if let Some(v) = &self.variadic {
            parts.push(variadic_text(v));
        }
        write!(f, "({})", parts.join(", "))
    }
}

/// A variadic slot: `...T`, or a generic pack `T...` as-is.
fn variadic_text(v: &TypeExpr) -> String {
    match &v.kind {
        TypeKind::GenericPack(_) => v.to_string(),
        _ => format!("...{v}"),
    }
}

/// One entry of a table constructor.
#[derive(Clone, Debug, PartialEq)]
pub enum TableItem {
    /// `expr`, stored at the next array index (1, 2, ...). A trailing call or
    /// `...` expands to all its values.
    Positional(Expr),
    /// `name = expr` or `[key] = expr`.
    Keyed { key: Expr, value: Expr },
}

impl ExprKind {
    /// Calls and `...` can give any number of values, everything else gives
    /// exactly one.
    pub fn is_multi(&self) -> bool {
        matches!(
            self,
            ExprKind::Call { .. } | ExprKind::MethodCall { .. } | ExprKind::Vararg
        )
    }
}

/// A piece of an interpolated string.
#[derive(Clone, Debug, PartialEq)]
pub enum InterpPart {
    Text(Rc<str>),
    /// A hole's expression, its value goes through `tostring`.
    Expr(Expr),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnOp {
    Neg,
    Not,
    Len,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    IDiv,
    Mod,
    Pow,
    Concat,
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
    And,
    Or,
}

/// Binding power for every prefix operator. Higher than `* / // %` but lower
/// than `^`, so `-2^2 == -(2^2)` while `-2*3 == (-2)*3`.
pub const UNARY_BP: u8 = 30;

impl BinOp {
    /// `(left, right)` binding powers, Pratt-style. Left-associative operators
    /// have `right = left + 1`, right-associative ones (`..`, `^`) have
    /// `right < left`.
    ///
    /// Luau's precedence, lowest to highest:
    /// `or` < `and` < comparison < `..` < `+ -` < `* / // %` < unary < `^`.
    pub fn binding_power(self) -> (u8, u8) {
        use BinOp::*;
        match self {
            Or => (1, 2),
            And => (3, 4),
            Eq | Ne | Lt | Le | Gt | Ge => (5, 6),
            Concat => (9, 8),
            Add | Sub => (10, 11),
            Mul | Div | IDiv | Mod => (20, 21),
            Pow => (41, 40),
        }
    }

    pub fn symbol(self) -> &'static str {
        use BinOp::*;
        match self {
            Add => "+",
            Sub => "-",
            Mul => "*",
            Div => "/",
            IDiv => "//",
            Mod => "%",
            Pow => "^",
            Concat => "..",
            Eq => "==",
            Ne => "~=",
            Lt => "<",
            Le => "<=",
            Gt => ">",
            Ge => ">=",
            And => "and",
            Or => "or",
        }
    }
}

impl UnOp {
    pub fn symbol(self) -> &'static str {
        match self {
            UnOp::Neg => "-",
            UnOp::Not => "not",
            UnOp::Len => "#",
        }
    }
}

/// S-expression printing, the tests lean on this a lot: `1 + 2 * 3` → `(+ 1 (*
/// 2 3))`.
impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            ExprKind::Nil => f.write_str("nil"),
            ExprKind::Bool(b) => write!(f, "{b}"),
            ExprKind::Number(n) => write!(f, "{n}"),
            ExprKind::String(s) => write!(f, "{s:?}"),
            ExprKind::Name(n) => f.write_str(&n.text),
            ExprKind::Group(e) => write!(f, "(group {e})"),
            ExprKind::Unary { op, operand, .. } => write!(f, "({} {operand})", op.symbol()),
            ExprKind::Binary { op, lhs, rhs, .. } => write!(f, "({} {lhs} {rhs})", op.symbol()),
            ExprKind::IfElse {
                branches,
                else_expr,
            } => {
                f.write_str("(if")?;
                for (c, v) in branches {
                    write!(f, " {c} {v}")?;
                }
                write!(f, " {else_expr})")
            }
            ExprKind::Call { callee, args } => {
                write!(f, "(call {callee}")?;
                for a in args {
                    write!(f, " {a}")?;
                }
                f.write_str(")")
            }
            ExprKind::Vararg => f.write_str("..."),
            ExprKind::Interp(parts) => {
                f.write_str("(interp")?;
                for part in parts {
                    match part {
                        InterpPart::Text(t) => write!(f, " {t:?}")?,
                        InterpPart::Expr(e) => write!(f, " {e}")?,
                    }
                }
                f.write_str(")")
            }
            ExprKind::Function(func) => {
                let mut params: Vec<&str> = func.params.iter().map(|p| &*p.text).collect();
                if func.is_vararg {
                    params.push("...");
                }
                write!(f, "(function ({}))", params.join(" "))
            }
            ExprKind::Index { object, key } => write!(f, "(index {object} {key})"),
            ExprKind::Cast { expr, ty } => write!(f, "(:: {expr} {ty})"),
            ExprKind::MethodCall {
                object,
                method,
                args,
                ..
            } => {
                write!(f, "(method {object} {method}")?;
                for a in args {
                    write!(f, " {a}")?;
                }
                f.write_str(")")
            }
            ExprKind::Table(items) => {
                f.write_str("(table")?;
                for item in items {
                    match item {
                        TableItem::Positional(e) => write!(f, " {e}")?,
                        TableItem::Keyed { key, value } => write!(f, " [{key}]={value}")?,
                    }
                }
                f.write_str(")")
            }
        }
    }
}