luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use super::super::common::*;
use luau_common::flags;

fn first_error(source: &str) -> ParseError {
    first_error_with_options(source, ParseOptions::default())
}

fn first_error_with_options(source: &str, options: ParseOptions) -> ParseError {
    with_parse(source, options, |result| {
        result
            .unwrap()
            .metadata
            .errors
            .into_iter()
            .next()
            .expect("expected parse error")
    })
}

// Parser.test.cpp: parse_attribute_on_function_stat
#[test]
fn parse_attribute_on_function_stat() {
    with_parse_ok(
        r#"
@checked
function abs(n: number): number return n end
    "#,
        |result| {
            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let function = statement
                .as_function_declaration()
                .expect("expected function declaration")
                .function;
            assert_eq!(function.attributes.len(), 1);
            assert_eq!(function.attributes[0].kind(), AttributeKind::Checked);
            assert_eq!(
                function.attributes[0].location,
                loc!(pos!(1, 0), pos!(1, 8))
            );
        },
    );
}
// Parser.test.cpp: parse_parametrized_attribute_on_function_stat
#[test]
fn parse_parametrized_attribute_on_function_stat() {
    with_parse(
        r#"
@[deprecated{ use = "greetng", reason = "Using <hello> is too causal"}]
function hello(x, y)
    return x + y
end"#,
        ParseOptions::default(),
        |result| {
            let result = result.unwrap();

            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let function = statement
                .as_function_declaration()
                .expect("expected function declaration")
                .function;
            assert_eq!(function.attributes.len(), 1);
            assert_eq!(function.attributes[0].kind(), AttributeKind::Deprecated);
            assert_eq!(
                function.attributes[0].location,
                loc!(pos!(1, 2), pos!(1, 70))
            );
        },
    );
}
// Parser.test.cpp: non_literal_attribute_arguments_is_not_allowed
#[test]
fn non_literal_attribute_arguments_is_not_allowed() {
    let error = first_error(
        r#"
@[deprecated{ reason = reasonString }]
function hello(x, y)
    return x + y
end"#,
    );
    assert_eq!(error.location, loc!(pos!(1, 13), pos!(1, 37)));
    assert_eq!(
        error.message,
        "Only literals can be passed as arguments for attributes"
    );
}
// Parser.test.cpp: unknown_arguments_for_depricated_is_not_allowed
#[test]
fn unknown_arguments_for_deprecated_is_not_allowed() {
    for (source, expected, location) in [
        (
            r#"
@[deprecated({}, "Very deprecated")]
function hello(x, y)
    return x + y
end"#,
            "@deprecated can be parametrized only by 1 argument",
            loc!(pos!(1, 2), pos!(1, 12)),
        ),
        (
            r#"
@[deprecated "Very deprecated"]
function hello(x, y)
    return x + y
end"#,
            "Unknown argument type for @deprecated",
            loc!(pos!(1, 13), pos!(1, 30)),
        ),
        (
            r#"
@[deprecated{ foo = "bar" }]
function hello(x, y)
    return x + y
end"#,
            "Unknown argument 'foo' for @deprecated. Only string constants for 'use' and 'reason' are allowed",
            loc!(pos!(1, 14), pos!(1, 17)),
        ),
        (
            r#"
@[deprecated{ use = 5 }]
function hello(x, y)
    return x + y
end"#,
            "Only constant string allowed as value for 'use'",
            loc!(pos!(1, 20), pos!(1, 21)),
        ),
    ] {
        let error = first_error_with_options(source, ParseOptions::default());
        assert_eq!(error.location, location, "{source}");
        assert_eq!(error.message, expected, "{source}");
    }
}

// Parser.test.cpp: do_not_hang_on_incomplete_attribute_list
#[test]
fn do_not_hang_on_incomplete_attribute_list() {
    for (source, expected, location) in [
        (
            r#"
@[]
function hello(x, y)
    return x + y
end"#,
            "Attribute list cannot be empty",
            loc!(pos!(1, 0), pos!(1, 3)),
        ),
        (
            "@[",
            "Expected identifier when parsing attribute name, got <eof>",
            loc!(pos!(0, 2), pos!(0, 2)),
        ),
        (
            r#"@[
        function foo() end
"#,
            "Expected identifier when parsing attribute name, got 'function'",
            loc!(pos!(1, 8), pos!(1, 16)),
        ),
        (
            r#"@[deprecated
        local function foo() end
"#,
            "Expected ']' (to close '@[' at line 1), got 'local'",
            loc!(pos!(1, 8), pos!(1, 13)),
        ),
    ] {
        let error = first_error_with_options(source, ParseOptions::default());
        assert_eq!(error.location, location, "{source}");
        assert_eq!(error.message, expected, "{source}");
    }
}
// Parser.test.cpp: parse_top_level_checked_fn
#[test]
fn parse_top_level_checked_fn() {
    with_parse_ok_with_declarations(
        r#"
@checked declare function abs(n: number): number
    "#,
        |result| {
            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let attributes = statement
                .as_declare_function()
                .expect("expected declared function")
                .attributes;
            assert_eq!(attributes.len(), 1);
            assert_eq!(attributes[0].kind(), AttributeKind::Checked);
        },
    );
}
// Parser.test.cpp: parse_declared_table_checked_member
#[test]
fn parse_declared_table_checked_member() {
    with_parse_ok_with_declarations(
        r#"
declare math : {
    abs : @checked (number) -> number
}
"#,
        |result| {
            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let ty = statement
                .as_declare_global()
                .expect("expected declared table global")
                .ty;
            let TypeKind::Table { props, .. } = &ty.kind() else {
                panic!("expected declared table global");
            };
            let TypeKind::Function { attributes, .. } = &props[0].ty.kind() else {
                panic!("expected function table property");
            };
            assert_eq!(attributes.len(), 1);
            assert_eq!(attributes[0].kind(), AttributeKind::Checked);
        },
    );
}
// Parser.test.cpp: parse_checked_outside_decl_fails
#[test]
fn parse_checked_outside_decl_fails() {
    with_parse_ok_with_declarations(
        r#"
local @checked = 3
"#,
        |result| {
            assert!(!result.metadata.errors.is_empty());
            let _ = &result.metadata.errors[1].message;
        },
    );
}

// Parser.test.cpp: parse_checked_in_and_out_of_decl_fails
#[test]
fn parse_checked_in_and_out_of_decl_fails() {
    with_parse_ok_with_declarations(
        r#"
local @checked = 3
@checked declare function abs(n: number): number
"#,
        |result| {
            assert_eq!(result.metadata.errors.len(), 2);
            assert_eq!(result.metadata.errors[0].location.begin.line, 1);
            assert_eq!(result.metadata.errors[1].location.begin.line, 1);
        },
    );
}

// Parser.test.cpp: parse_checked_as_function_name_fails
#[test]
fn parse_checked_as_function_name_fails() {
    with_parse_ok_with_declarations(
        r#"
@checked function(x: number) : number
end
"#,
        |result| {
            assert!(!result.metadata.errors.is_empty());
        },
    );
}

// Parser.test.cpp: cannot_use_@_as_variable_name
#[test]
fn cannot_use_at_as_variable_name() {
    with_parse_ok_with_declarations(
        r#"
local @blah = 3
"#,
        |result| {
            assert!(!result.metadata.errors.is_empty());
        },
    );
}

// Parser.test.cpp: recover_from_bad_table_type
#[test]
fn recover_from_bad_table_type() {
    with_parse_ok_with_declarations(
        r#"
declare class Widget
    state: {string: function(string, Widget)}
end
"#,
        |result| {
            assert_eq!(result.metadata.errors.len(), 2);
        },
    );
}
// Parser.test.cpp: parse_attribute_for_function_expression
#[test]
fn parse_attribute_for_function_expression() {
    with_parse_ok(
        r#"
local function invoker(f)
    return f(1)
end

invoker(@checked function(x) return (x + 2) end)
"#,
        |call_result| {
            let [_, statement] = statement_kinds(call_result.root.as_slice()).exact();
            let expression = statement
                .as_expression()
                .expect("expected call expression after invoker declaration")
                .expr;
            let ExpressionKind::Call { args, .. } = expression.kind() else {
                panic!("expected invoker call");
            };
            let [argument] = args else {
                panic!("expected function literal call argument");
            };
            let ExpressionKind::FunctionLiteral(function) = argument.kind() else {
                panic!("expected function literal call argument");
            };
            assert_eq!(function.attributes.len(), 1);
            assert_eq!(function.attributes[0].kind(), AttributeKind::Checked);
            assert_eq!(
                function.attributes[0].location,
                loc!(pos!(5, 8), pos!(5, 16))
            );

            with_parse(
                r#"
local f = @checked function(x) return (x + 2) end
    "#,
                ParseOptions::default(),
                |local_result| {
                    let local_result = local_result.unwrap();

                    let [statement] = statement_kinds(local_result.root.as_slice()).exact();
                    let local = statement.as_local().expect("expected local declaration");
                    let [value] = local.values else {
                        panic!("expected function expression");
                    };
                    let ExpressionKind::FunctionLiteral(function) = value.kind() else {
                        panic!("expected function expression");
                    };
                    assert_eq!(function.attributes.len(), 1);
                    assert_eq!(function.attributes[0].kind(), AttributeKind::Checked);
                    assert_eq!(
                        function.attributes[0].location,
                        loc!(pos!(1, 10), pos!(1, 18))
                    );
                },
            );
        },
    );
}
// Parser.test.cpp: parse_attribute_on_local_function_stat
#[test]
fn parse_attribute_on_local_function_stat() {
    with_parse(
        r#"
    @checked
local function hello(x, y)
    return x + y
end"#,
        ParseOptions::default(),
        |result| {
            let result = result.unwrap();

            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let function = statement
                .as_local_function()
                .expect("expected local function declaration")
                .function;
            assert_eq!(function.attributes.len(), 1);
            assert_eq!(function.attributes[0].kind(), AttributeKind::Checked);
            assert_eq!(
                function.attributes[0].location,
                loc!(pos!(1, 4), pos!(1, 12))
            );
        },
    );
}
// Parser.test.cpp: parse_debugnoinline_on_local_function
#[test]
fn parse_debugnoinline_on_local_function() {
    let _debug = flags::DebugLuauNoInline.scoped(true);
    with_parse(
        r#"
    @debugnoinline
local function hello(x, y)
    return x + y
end"#,
        ParseOptions::default(),
        |result| {
            let result = result.unwrap();

            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let function = statement
                .as_local_function()
                .expect("expected local function declaration")
                .function;
            assert_eq!(function.attributes.len(), 1);
            assert_eq!(function.attributes[0].kind(), AttributeKind::DebugNoinline);
            assert_eq!(
                function.attributes[0].location,
                loc!(pos!(1, 4), pos!(1, 18))
            );
        },
    );
}
// Parser.test.cpp: debugnoinline_not_allowed_without_flag
#[test]
fn debugnoinline_not_allowed_without_flag() {
    let error = first_error(
        r#"
@debugnoinline
local function hello(x, y)
    return x + y
end"#,
    );
    assert_eq!(error.location, loc!(pos!(1, 0), pos!(1, 14)));
    assert_eq!(error.message, "Invalid attribute '@debugnoinline'");
}
// Parser.test.cpp: empty_attribute_name_is_not_allowed
#[test]
fn empty_attribute_name_is_not_allowed() {
    let error = first_error(
        r#"
@
function hello(x, y)
    return x + y
end"#,
    );
    assert_eq!(error.location, loc!(pos!(1, 0), pos!(1, 1)));
    assert_eq!(error.message, "Attribute name is missing");
}
// Parser.test.cpp: dont_parse_attributes_on_non_function_stat
#[test]
fn dont_parse_attributes_on_non_function_stat() {
    for (source, expected, location) in [
        (
            r#"
@checked
if a<0 then a = 0 end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'if' instead",
            loc!(pos!(2, 0), pos!(2, 2)),
        ),
        (
            r#"
local i = 1
@checked
while a[i] do
    print(a[i])
    i = i + 1
end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'while' instead",
            loc!(pos!(3, 0), pos!(3, 5)),
        ),
        (
            r#"
@checked
do
    local a2 = 2*a
end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'do' instead",
            loc!(pos!(2, 0), pos!(2, 2)),
        ),
        (
            r#"
@checked
for i=1,10 do print(i) end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'for' instead",
            loc!(pos!(2, 0), pos!(2, 3)),
        ),
        (
            r#"
@checked
repeat
    line = io.read()
until line ~= """#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'repeat' instead",
            loc!(pos!(2, 0), pos!(2, 6)),
        ),
        (
            r#"
@checked
local x = 10"#,
            "Expected 'function' after local declaration with attribute, but got 'x' instead",
            loc!(pos!(2, 6), pos!(2, 7)),
        ),
        (
            r#"
local i = 1
while a[i] do
    if a[i] == v then @checked break end
    i = i + 1
end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'break' instead",
            loc!(pos!(3, 31), pos!(3, 36)),
        ),
        (
            r#"
function foo1 () @checked return 'a' end"#,
            "Expected 'function', 'local function', 'const function', 'declare function' or a function type declaration after attribute, but got 'return' instead",
            loc!(pos!(1, 26), pos!(1, 32)),
        ),
    ] {
        let error = first_error_with_options(source, ParseOptions::default());
        assert_eq!(error.location, location, "{source}");
        assert_eq!(error.message, expected, "{source}");
    }
}
// Parser.test.cpp: dont_parse_attribute_on_argument_non_function
#[test]
fn dont_parse_attribute_on_argument_non_function() {
    let error = first_error(
        r#"
local function invoker(f, y)
    return f(y)
end

invoker(function(x) return (x + 2) end, @checked 1)"#,
    );
    assert_eq!(error.location, loc!(pos!(5, 40), pos!(5, 48)));
    assert_eq!(
        error.message,
        "Expected 'function' declaration after attribute, but got '1' instead"
    );
}
// Parser.test.cpp: parse_attributes_on_function_type_declaration_in_table
#[test]
fn parse_attributes_on_function_type_declaration_in_table() {
    with_parse_ok_with_declarations(
        r#"
declare bit32: {
    band: @checked (...number) -> number
}"#,
        |result| {
            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let ty = statement
                .as_declare_global()
                .expect("expected table type alias")
                .ty;
            let TypeKind::Table { props, .. } = &ty.kind() else {
                panic!("expected table type alias");
            };
            let TypeKind::Function { attributes, .. } = &props[0].ty.kind() else {
                panic!("expected function type");
            };
            assert_eq!(attributes.len(), 1);
            assert_eq!(attributes[0].kind(), AttributeKind::Checked);
            assert_eq!(attributes[0].location, loc!(pos!(2, 10), pos!(2, 18)));
        },
    );
}
// Parser.test.cpp: parse_attribute_on_function_type_declaration
#[test]
fn parse_attribute_on_function_type_declaration() {
    with_parse_ok_with_declarations(
        r#"
@checked declare function abs(n: number): number
    "#,
        |result| {
            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let attributes = statement
                .as_declare_function()
                .expect("expected declared function")
                .attributes;
            assert_eq!(attributes.len(), 1);
            assert_eq!(attributes[0].kind(), AttributeKind::Checked);
            assert_eq!(attributes[0].location, loc!(pos!(1, 0), pos!(1, 8)));
        },
    );
}
// Parser.test.cpp: dont_parse_attributes_on_non_function_type_declarations
#[test]
fn dont_parse_attributes_on_non_function_type_declarations() {
    for (source, expected, location) in [
        (
            r#"
@checked declare foo: number
    "#,
            "Expected a function type declaration after attribute, but got 'foo' instead",
            loc!(pos!(1, 17), pos!(1, 20)),
        ),
        (
            r#"
@checked declare class Foo
    prop: number
    function method(self, foo: number): string
end
    "#,
            "Expected a function type declaration after attribute, but got 'class' instead",
            loc!(pos!(1, 17), pos!(1, 22)),
        ),
        (
            r#"
declare bit32: {
    band: @checked number
}"#,
            "Expected '(' when parsing function parameters, got 'number'",
            loc!(pos!(2, 19), pos!(2, 25)),
        ),
    ] {
        let error = first_error_with_options(
            source,
            ParseOptions::default().with_declaration_syntax(true),
        );
        assert_eq!(error.location, location, "{source}");
        assert_eq!(error.message, expected, "{source}");
    }
}
// Parser.test.cpp: attributes_cannot_be_duplicated
#[test]
fn attributes_cannot_be_duplicated() {
    let error = first_error(
        r#"
@checked
    @checked
function hello(x, y)
    return x + y
end"#,
    );
    assert_eq!(error.location, loc!(pos!(2, 4), pos!(2, 12)));
    assert_eq!(error.message, "Cannot duplicate attribute '@checked'");
}
// Parser.test.cpp: unsupported_attributes_are_not_allowed
#[test]
fn unsupported_attributes_are_not_allowed() {
    let error = first_error(
        r#"
@checked
    @cool_attribute
function hello(x, y)
    return x + y
end"#,
    );
    assert_eq!(error.location, loc!(pos!(2, 4), pos!(2, 19)));
    assert_eq!(error.message, "Invalid attribute '@cool_attribute'");
}
// Conformance.test.cpp: NativeAttribute
#[test]
fn native_attribute_marks_function_native() {
    with_parse(
        r#"
@native
function fast() end
    "#,
        ParseOptions::default(),
        |result| {
            let result = result.unwrap();

            let [statement] = statement_kinds(result.root.as_slice()).exact();
            let function = statement
                .as_function_declaration()
                .expect("expected function declaration")
                .function;
            assert_eq!(function.attributes[0].kind(), AttributeKind::Native);
        },
    );
}