valua-ast 0.1.0

Lua 5.5 AST node definitions shared across the valua pipeline (Lua 5.4 is a supported subset)
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
//! Typed AST for Lua 5.5 — shared by parser, transformer, and code generator.
//!
//! Also houses [`LuaTarget`], the cross-cutting compilation-target enum used by
//! both `valua-lint` and `valua-codegen`. Placing it here avoids an illegal
//! `valua-lint → valua-codegen` dependency (see architectural note in PRD §6.2).

use valua_diagnostics::Span;

#[cfg(feature = "serde")]
use serde::Serialize;

// ── Top-level ────────────────────────────────────────────────────────────────

/// Root node: a sequence of statements followed by an optional return.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Block {
    pub stmts: Vec<Statement>,
    pub span: Span,
}

// ── Statements ───────────────────────────────────────────────────────────────

/// Every kind of statement in Lua 5.5 (Lua 5.4 is a fully supported subset).
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum Statement {
    /// `local x [<attr>] = expr`
    LocalDecl(LocalDecl),
    /// `var = expr` or multi-assign
    Assign(Assign),
    /// `do … end`
    Do(Do),
    /// `while expr do … end`
    While(While),
    /// `repeat … until expr`
    Repeat(Repeat),
    /// `if … then … [elseif …] [else …] end`
    If(If),
    /// `for i = start, limit [, step] do … end`
    NumericFor(NumericFor),
    /// `for k, v in iter do … end`
    GenericFor(GenericFor),
    /// `function name(…) … end`
    FunctionDecl(FunctionDecl),
    /// `local function name(…) … end`
    LocalFunctionDecl(LocalFunctionDecl),
    /// `return [exprlist]`
    Return(Return),
    /// `break`
    Break(Span),
    /// `goto label`
    Goto(Goto),
    /// `::label::`
    Label(Label),
    /// A function-call used as a statement.
    ExprStmt(Expression),
}

/// `local varlist [<attr>] [= exprlist]`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct LocalDecl {
    pub names: Vec<LocalName>,
    pub values: Vec<Expression>,
    pub span: Span,
}

/// One name in a local declaration, with its optional attribute.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct LocalName {
    pub name: String,
    /// Lua 5.4/5.5 `<const>` or `<close>` attribute.
    pub attribute: Option<Attribute>,
    pub span: Span,
}

/// Lua 5.4/5.5 variable attributes.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Attribute {
    /// `<const>` — value must not change after initialisation.
    Const,
    /// `<close>` — to-be-closed variable; `__close` metamethod called on scope exit.
    Close,
}

/// Simple or multi-target assignment: `targets = values`.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Assign {
    pub targets: Vec<Expression>,
    pub values: Vec<Expression>,
    pub span: Span,
}

/// `do … end` block.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Do {
    pub body: Block,
    pub span: Span,
}

/// `while cond do body end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct While {
    pub condition: Box<Expression>,
    pub body: Block,
    pub span: Span,
}

/// `repeat body until cond`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Repeat {
    pub body: Block,
    pub condition: Box<Expression>,
    pub span: Span,
}

/// `if cond then body [elseif …]* [else …] end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct If {
    pub condition: Box<Expression>,
    pub then_block: Block,
    pub elseif_clauses: Vec<ElseIf>,
    pub else_block: Option<Block>,
    pub span: Span,
}

/// One `elseif cond then body` branch.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct ElseIf {
    pub condition: Box<Expression>,
    pub body: Block,
    pub span: Span,
}

/// `for i = start, limit [, step] do body end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct NumericFor {
    pub var: String,
    pub start: Box<Expression>,
    pub limit: Box<Expression>,
    pub step: Option<Box<Expression>>,
    pub body: Block,
    pub span: Span,
}

/// `for namelist in exprlist do body end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct GenericFor {
    pub vars: Vec<String>,
    pub iterators: Vec<Expression>,
    pub body: Block,
    pub span: Span,
}

/// `function name.path:method (params) body end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct FunctionDecl {
    pub name: FunctionName,
    pub func: FunctionBody,
    pub span: Span,
}

/// `local function name (params) body end`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct LocalFunctionDecl {
    pub name: String,
    pub func: FunctionBody,
    pub span: Span,
}

/// Dotted function name with optional method receiver: `a.b.c:method`.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct FunctionName {
    pub parts: Vec<String>,
    /// Present when declared as a method (`:`).
    pub method: Option<String>,
    pub span: Span,
}

/// `return [exprlist]`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Return {
    pub values: Vec<Expression>,
    pub span: Span,
}

/// `goto label`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Goto {
    pub label: String,
    pub span: Span,
}

/// `::label::`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Label {
    pub name: String,
    pub span: Span,
}

// ── Function body ────────────────────────────────────────────────────────────

/// Shared representation for anonymous and named function bodies.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct FunctionBody {
    pub params: Vec<Param>,
    pub is_vararg: bool,
    pub body: Block,
    pub span: Span,
}

/// One parameter in a function signature.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Param {
    pub name: String,
    pub span: Span,
}

// ── Expressions ──────────────────────────────────────────────────────────────

/// All expression forms in Lua 5.5.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum Expression {
    Nil(Span),
    True(Span),
    False(Span),
    Integer(i64, Span),
    Float(f64, Span),
    String(String, Span),
    Vararg(Span),
    Name(String, Span),
    /// `expr.field`
    Index(Box<Expression>, String, Span),
    /// `expr[key]`
    IndexExpr(Box<Expression>, Box<Expression>, Span),
    /// Binary operation: `lhs op rhs`.
    BinOp(Box<Expression>, BinaryOp, Box<Expression>, Span),
    /// Unary operation: `op expr`.
    UnOp(UnaryOp, Box<Expression>, Span),
    /// Function call: `func(args)` or `obj:method(args)`.
    Call(Call),
    /// `function (params) body end`
    Function(FunctionBody),
    /// Table constructor: `{ [field,]* }`.
    Table(TableConstructor),
}

impl Expression {
    /// Returns the span of this expression.
    #[must_use]
    #[allow(clippy::match_same_arms)]
    pub fn span(&self) -> Span {
        match self {
            Expression::Nil(s)
            | Expression::True(s)
            | Expression::False(s)
            | Expression::Vararg(s) => *s,
            Expression::Integer(_, s) => *s,
            Expression::Float(_, s) => *s,
            Expression::String(_, s) => *s,
            Expression::Name(_, s) => *s,
            Expression::Index(_, _, s) => *s,
            Expression::IndexExpr(_, _, s) => *s,
            Expression::BinOp(_, _, _, s) => *s,
            Expression::UnOp(_, _, s) => *s,
            Expression::Call(c) => c.span(),
            Expression::Function(f) => f.span,
            Expression::Table(t) => t.span,
        }
    }
}

// ── Operators ────────────────────────────────────────────────────────────────

/// Binary operators in Lua 5.5 (includes bitwise and integer division).
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    // Arithmetic
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Pow,
    /// `//` integer division (5.3+).
    IDiv,
    // Comparison
    Lt,
    Le,
    Gt,
    Ge,
    Eq,
    Ne,
    // Logical
    And,
    Or,
    // Bitwise (5.3+)
    /// `&`
    BitwiseAnd,
    /// `|`
    BitwiseOr,
    /// `~` (binary)
    BitwiseXor,
    /// `<<`
    Shl,
    /// `>>`
    Shr,
    // String
    /// `..`
    Concat,
}

/// Unary operators in Lua 5.5.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
    /// `-`
    Neg,
    /// `not`
    Not,
    /// `#`
    Len,
    /// `~` (unary bitwise NOT, 5.3+)
    BitwiseNot,
}

// ── Function calls ───────────────────────────────────────────────────────────

/// A function-call expression or method-call expression.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum Call {
    /// `func(args)`
    Call {
        func: Box<Expression>,
        args: Vec<Expression>,
        span: Span,
    },
    /// `obj:method(args)`
    MethodCall {
        obj: Box<Expression>,
        method: String,
        args: Vec<Expression>,
        span: Span,
    },
}

impl Call {
    #[must_use]
    pub fn span(&self) -> Span {
        match self {
            Call::Call { span, .. } | Call::MethodCall { span, .. } => *span,
        }
    }
}

// ── Table constructor ────────────────────────────────────────────────────────

/// `{ field, field, … }`
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct TableConstructor {
    pub fields: Vec<TableField>,
    pub span: Span,
}

/// One field in a table constructor.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum TableField {
    /// `[expr] = expr`
    ExprKey {
        key: Box<Expression>,
        value: Box<Expression>,
        span: Span,
    },
    /// `name = expr`
    NameKey {
        key: String,
        value: Box<Expression>,
        span: Span,
    },
    /// `expr` (positional)
    Positional(Expression),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[ignore = "TODO: verify Block can be constructed and iterated"]
    fn test_block_construction() {
        todo!()
    }

    #[test]
    #[ignore = "TODO: verify Attribute variants are distinct"]
    fn test_attribute_variants() {
        todo!()
    }

    #[test]
    #[ignore = "TODO: Expression::span() returns the correct span for each variant"]
    fn test_expression_span() {
        todo!()
    }

    #[test]
    #[ignore = "TODO: BinaryOp includes all bitwise variants"]
    fn test_binary_op_bitwise_variants() {
        todo!()
    }
}

// ── Compilation target ────────────────────────────────────────────────────────

/// Target Lua runtime — controls which built-ins and polyfills are assumed available.
///
/// Lives in `valua-ast` (not `valua-codegen`) so that both `valua-lint` and
/// `valua-codegen` can depend on it without creating a cross-layer coupling.
/// `valua-codegen` re-exports this type as `pub use valua_ast::LuaTarget`.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LuaTarget {
    /// Plain Lua 5.1 (PUC reference implementation).
    #[default]
    Lua51,
    /// `LuaJIT` 2.x — has the `bit` library and some Lua 5.1 extensions.
    LuaJIT,
}