emmylua_parser 0.25.0

A parser for EmmyLua and luals
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
use crate::{
    LuaLanguageLevel,
    grammar::{ParseFailReason, ParseResult, lua::is_statement_start_token},
    kind::{LuaSyntaxKind, LuaTokenKind},
    parser::{CompleteMarker, LuaParser, MarkerEventContainer},
    parser_error::LuaParseError,
};

use super::{
    expect_token,
    expr::{parse_closure_expr, parse_expr},
    if_token_bump, parse_block,
};

/// Push expression parsing error with lazy error message generation
fn push_expr_error_lazy<F>(p: &mut LuaParser, error_msg_fn: F)
where
    F: FnOnce() -> std::borrow::Cow<'static, str>,
{
    let error_msg = error_msg_fn();
    p.push_error(LuaParseError::syntax_error_from(
        &error_msg,
        p.current_token_range(),
    ));
}

/// Generic keyword expectation with error recovery and lazy error message generation
fn expect_keyword_with_recovery<F>(
    p: &mut LuaParser,
    expected: LuaTokenKind,
    error_msg_fn: F,
) -> bool
where
    F: FnOnce() -> std::borrow::Cow<'static, str>,
{
    if p.current_token() == expected {
        p.bump();
        true
    } else {
        let error_msg = error_msg_fn();
        p.push_error(LuaParseError::syntax_error_from(
            &error_msg,
            p.current_token_range(),
        ));

        // Check if we can continue parsing (assume user forgot the keyword)
        is_statement_start_token(p.current_token())
    }
}

/// Expect 'end' keyword, report error at start keyword location if missing
fn expect_end_keyword<F>(p: &mut LuaParser, start_range: crate::text::SourceRange, error_msg_fn: F)
where
    F: FnOnce() -> std::borrow::Cow<'static, str>,
{
    if p.current_token() == LuaTokenKind::TkEnd {
        p.bump();
    } else {
        let error_msg = error_msg_fn();
        // Report error at the start keyword location
        p.push_error(LuaParseError::syntax_error_from(&error_msg, start_range));

        // Try to recover: look for possible 'end' or other structure terminators
        recover_to_block_end(p);
    }
}

/// Error recovery: skip to block end markers
fn recover_to_block_end(p: &mut LuaParser) {
    let mut depth = 1;

    while p.current_token() != LuaTokenKind::TkEof && depth > 0 {
        match p.current_token() {
            // Nested structure starts
            LuaTokenKind::TkIf
            | LuaTokenKind::TkWhile
            | LuaTokenKind::TkFor
            | LuaTokenKind::TkDo
            | LuaTokenKind::TkFunction => {
                depth += 1;
                p.bump();
            }
            // Structure ends
            LuaTokenKind::TkEnd => {
                depth -= 1;
                if depth == 0 {
                    p.bump(); // Consume the found 'end'
                }
            }
            // Other possible recovery points
            LuaTokenKind::TkElseIf | LuaTokenKind::TkElse => {
                if depth == 1 {
                    // Found same-level elseif/else, can recover
                    break;
                }
                p.bump();
            }
            // Other control flow end markers
            LuaTokenKind::TkUntil => {
                depth -= 1;
                if depth == 0 {
                    // This might be the end of repeat-until
                    break;
                }
                p.bump();
            }
            _ => {
                p.bump();
            }
        }
    }
}

/// Error recovery: skip to specified keywords
fn recover_to_keywords(p: &mut LuaParser, keywords: &[LuaTokenKind]) {
    while p.current_token() != LuaTokenKind::TkEof {
        if keywords.contains(&p.current_token()) {
            break;
        }

        // Also stop recovery if we encounter statement start markers
        if is_statement_start_token(p.current_token()) {
            break;
        }

        p.bump();
    }
}

// Parse a comma-separated list of expressions, returning an error message only if there's an error.
fn parse_expr_list_impl(p: &mut LuaParser) -> Result<(), &'static str> {
    parse_expr(p).map_err(|_| "expected expression")?;

    while p.current_token() == LuaTokenKind::TkComma {
        p.bump();
        parse_expr(p).map_err(|_| "expected expression after ','")?;
    }

    Ok(())
}

// Parse a comma-separated list of variable names.
fn parse_variable_name_list(p: &mut LuaParser, support_attrib: bool) -> ParseResult {
    parse_local_name(p, support_attrib)?;

    while p.current_token() == LuaTokenKind::TkComma {
        p.bump();
        match parse_local_name(p, support_attrib) {
            Ok(_) => {}
            Err(_) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected variable name after ','"),
                    p.current_token_range(),
                ));
            }
        }
    }

    Ok(CompleteMarker::empty())
}

fn parse_global_name_list(p: &mut LuaParser) -> ParseResult {
    parse_local_name(p, true)?;

    while p.current_token() == LuaTokenKind::TkComma {
        p.bump();
        match parse_local_name(p, true) {
            Ok(_) => {}
            Err(_) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected variable name after ','"),
                    p.current_token_range(),
                ));
            }
        }
    }

    if p.current_token() == LuaTokenKind::TkAssign {
        p.bump();
        match parse_expr_list_impl(p) {
            Ok(_) => {}
            Err(_) => {
                push_expr_error_lazy(p, || t!("expected expression after '='"));
            }
        }
    }

    Ok(CompleteMarker::empty())
}

pub fn parse_stats(p: &mut LuaParser) {
    while !block_follow(p) {
        let level = p.get_mark_level();
        match parse_stat(p) {
            Ok(_) => {}
            Err(_) => {
                let current_level = p.get_mark_level();
                for _ in 0..(current_level - level) {
                    p.push_node_end();
                }

                let mut can_continue = false;
                // error recover
                while p.current_token() != LuaTokenKind::TkEof {
                    if is_statement_start_token(p.current_token()) {
                        can_continue = true;
                        break;
                    }

                    p.bump();
                }

                if can_continue {
                    continue;
                }
                break;
            }
        }
    }
}

fn block_follow(p: &LuaParser) -> bool {
    matches!(
        p.current_token(),
        LuaTokenKind::TkElse
            | LuaTokenKind::TkElseIf
            | LuaTokenKind::TkEnd
            | LuaTokenKind::TkEof
            | LuaTokenKind::TkUntil
    )
}

fn parse_stat(p: &mut LuaParser) -> ParseResult {
    let cm = match p.current_token() {
        LuaTokenKind::TkIf => parse_if(p)?,
        LuaTokenKind::TkWhile => parse_while(p)?,
        LuaTokenKind::TkFor => parse_for(p)?,
        LuaTokenKind::TkFunction => parse_function(p)?,
        LuaTokenKind::TkLocal => parse_local(p)?,
        LuaTokenKind::TkReturn => parse_return(p)?,
        LuaTokenKind::TkBreak => parse_break(p)?,
        LuaTokenKind::TkDo => parse_do(p)?,
        LuaTokenKind::TkRepeat => parse_repeat(p)?,
        LuaTokenKind::TkGoto => parse_goto(p)?,
        LuaTokenKind::TkDbColon => parse_label_stat(p)?,
        LuaTokenKind::TkSemicolon => parse_empty_stat(p)?,
        _ => parse_assign_or_expr_or_global_stat(p)?,
    };

    Ok(cm)
}

fn parse_if(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::IfStat);
    let if_start_range = p.current_token_range();
    p.bump(); // consume 'if'

    // Parse condition expression
    if parse_expr(p).is_err() {
        push_expr_error_lazy(p, || t!("expected condition expression after 'if'"));
        // 尝试恢复到 'then' 或语句开始
        recover_to_keywords(p, &[LuaTokenKind::TkThen, LuaTokenKind::TkEnd]);
    }

    // Expect 'then'
    if !expect_keyword_with_recovery(p, LuaTokenKind::TkThen, || {
        t!("expected 'then' after if condition")
    }) {
        // 如果没有找到 'then',尝试恢复
        recover_to_keywords(
            p,
            &[
                LuaTokenKind::TkEnd,
                LuaTokenKind::TkElseIf,
                LuaTokenKind::TkElse,
            ],
        );
    }

    // 只有在找到合适的恢复点时才解析块
    if !matches!(
        p.current_token(),
        LuaTokenKind::TkEnd | LuaTokenKind::TkElseIf | LuaTokenKind::TkElse | LuaTokenKind::TkEof
    ) {
        parse_block(p)?;
    }

    while p.current_token() == LuaTokenKind::TkElseIf {
        parse_elseif_clause(p)?;
    }

    if p.current_token() == LuaTokenKind::TkElse {
        parse_else_clause(p)?;
    }

    // Use new end expectation function to associate error with 'if' keyword
    expect_end_keyword(p, if_start_range, || {
        t!("expected 'end' to close if statement")
    });

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_elseif_clause(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ElseIfClauseStat);
    p.bump();

    if parse_expr(p).is_err() {
        push_expr_error_lazy(p, || t!("expected condition expression after 'elseif'"));
    }

    expect_keyword_with_recovery(p, LuaTokenKind::TkThen, || {
        t!("expected 'then' after 'elseif' condition")
    });

    parse_block(p)?;

    Ok(m.complete(p))
}

fn parse_else_clause(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ElseClauseStat);
    p.bump();
    parse_block(p)?;

    Ok(m.complete(p))
}

fn parse_while(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::WhileStat);
    let while_start_range = p.current_token_range();
    p.bump(); // consume 'while'

    // Parse condition expression
    if parse_expr(p).is_err() {
        push_expr_error_lazy(p, || t!("expected condition expression after 'while'"));
        recover_to_keywords(p, &[LuaTokenKind::TkDo, LuaTokenKind::TkEnd]);
    }

    // Expect 'do'
    if !expect_keyword_with_recovery(p, LuaTokenKind::TkDo, || {
        t!("expected 'do' after while condition")
    }) {
        recover_to_keywords(p, &[LuaTokenKind::TkEnd]);
    }

    // 只有在找到合适的恢复点时才解析块
    if p.current_token() != LuaTokenKind::TkEnd && p.current_token() != LuaTokenKind::TkEof {
        parse_block(p)?;
    }

    // Use new end expectation function to associate error with 'while' keyword
    expect_end_keyword(p, while_start_range, || {
        t!("expected 'end' to close while statement")
    });

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_do(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::DoStat);
    let do_start_range = p.current_token_range();
    p.bump();

    parse_block(p)?;

    expect_end_keyword(p, do_start_range, || t!("expected 'end' after 'do' block"));

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_for(p: &mut LuaParser) -> ParseResult {
    let mut m = p.mark(LuaSyntaxKind::ForStat);
    let for_start_range = p.current_token_range();
    p.bump(); // consume 'for'

    // Expect variable name
    if p.current_token() == LuaTokenKind::TkName {
        p.bump();
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected variable name after 'for'"),
            p.current_token_range(),
        ));
        // Try to recover: skip to '=' or 'in'
        recover_to_keywords(
            p,
            &[
                LuaTokenKind::TkAssign,
                LuaTokenKind::TkIn,
                LuaTokenKind::TkComma,
                LuaTokenKind::TkDo,
                LuaTokenKind::TkEnd,
            ],
        );
    }

    match p.current_token() {
        LuaTokenKind::TkAssign => {
            // Numeric for loop
            p.bump();
            // Start value
            if parse_expr(p).is_err() {
                push_expr_error_lazy(p, || {
                    t!("expected start value expression in numeric for loop")
                });
            }

            if p.current_token() == LuaTokenKind::TkComma {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected ',' after start value in numeric for loop"),
                    p.current_token_range(),
                ));
            }

            // End value
            if parse_expr(p).is_err() {
                push_expr_error_lazy(p, || {
                    t!("expected end value expression in numeric for loop")
                });
            }

            // Optional step value
            if p.current_token() == LuaTokenKind::TkComma {
                p.bump();
                if parse_expr(p).is_err() {
                    push_expr_error_lazy(p, || {
                        t!("expected step value expression in numeric for loop")
                    });
                }
            }
        }
        LuaTokenKind::TkComma | LuaTokenKind::TkIn => {
            // Generic for loop
            m.set_kind(p, LuaSyntaxKind::ForRangeStat);
            while p.current_token() == LuaTokenKind::TkComma {
                p.bump();
                if p.current_token() == LuaTokenKind::TkName {
                    p.bump();
                } else {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected variable name after ','"),
                        p.current_token_range(),
                    ));
                }
            }

            if p.current_token() == LuaTokenKind::TkIn {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected 'in' after variable list in generic for loop"),
                    p.current_token_range(),
                ));
            }

            // Iterator expression list
            if parse_expr_list_impl(p).is_err() {
                push_expr_error_lazy(p, || t!("expected iterator expression after 'in'"));
            }
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected '=' for numeric for loop or ',' or 'in' for generic for loop"),
                p.current_token_range(),
            ));
        }
    }

    // Expect 'do'
    if !expect_keyword_with_recovery(p, LuaTokenKind::TkDo, || {
        t!("expected 'do' in for statement")
    }) {
        recover_to_keywords(p, &[LuaTokenKind::TkEnd]);
    }

    // 只有在找到合适的恢复点时才解析块
    if p.current_token() != LuaTokenKind::TkEnd && p.current_token() != LuaTokenKind::TkEof {
        parse_block(p)?;
    }

    expect_end_keyword(p, for_start_range, || {
        t!("expected 'end' to close for statement")
    });

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_function(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::FuncStat);
    p.bump();
    parse_func_name(p)?;
    parse_closure_expr(p)?;
    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_func_name(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::NameExpr);
    match expect_token(p, LuaTokenKind::TkName) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected function name after 'function'"),
                p.current_token_range(),
            ));
            return Err(ParseFailReason::UnexpectedToken);
        }
    }

    let cm =
        if p.current_token() == LuaTokenKind::TkDot || p.current_token() == LuaTokenKind::TkColon {
            let mut cm = m.complete(p);
            while p.current_token() == LuaTokenKind::TkDot {
                let m = cm.precede(p, LuaSyntaxKind::IndexExpr);
                p.bump();
                match expect_token(p, LuaTokenKind::TkName) {
                    Ok(_) => {}
                    Err(_) => {
                        p.push_error(LuaParseError::syntax_error_from(
                            &t!("expected name after '.'"),
                            p.current_token_range(),
                        ));
                    }
                }
                cm = m.complete(p);
            }

            if p.current_token() == LuaTokenKind::TkColon {
                let m = cm.precede(p, LuaSyntaxKind::IndexExpr);
                p.bump();
                match expect_token(p, LuaTokenKind::TkName) {
                    Ok(_) => {}
                    Err(_) => {
                        p.push_error(LuaParseError::syntax_error_from(
                            &t!("expected name after ':'"),
                            p.current_token_range(),
                        ));
                    }
                }
                cm = m.complete(p);
            }

            cm
        } else {
            m.complete(p)
        };

    Ok(cm)
}

fn parse_local(p: &mut LuaParser) -> ParseResult {
    let mut m = p.mark(LuaSyntaxKind::LocalStat);
    p.bump(); // consume 'local'

    match p.current_token() {
        LuaTokenKind::TkFunction => {
            p.bump();
            m.set_kind(p, LuaSyntaxKind::LocalFuncStat);

            match parse_local_name(p, false) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected function name after 'local function'"),
                        p.current_token_range(),
                    ));
                }
            }

            match parse_closure_expr(p) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("invalid function definition"),
                        p.current_token_range(),
                    ));
                }
            }
        }
        LuaTokenKind::TkName => {
            parse_variable_name_list(p, true)?;

            // 可选的初始化表达式
            if p.current_token().is_assign_op() {
                p.bump();
                if parse_expr_list_impl(p).is_err() {
                    push_expr_error_lazy(p, || t!("expected initialization expression after '='"));
                }
            }
        }
        LuaTokenKind::TkLt => {
            if p.parse_config.level >= LuaLanguageLevel::Lua55 {
                match parse_attrib(p) {
                    Ok(_) => {}
                    Err(_) => {
                        p.push_error(LuaParseError::syntax_error_from(
                            &t!("invalid attribute syntax"),
                            p.current_token_range(),
                        ));
                    }
                }

                parse_variable_name_list(p, true)?;

                if p.current_token().is_assign_op() {
                    p.bump();
                    if parse_expr_list_impl(p).is_err() {
                        push_expr_error_lazy(p, || {
                            t!("expected initialization expression after '='")
                        });
                    }
                }
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!(
                        "local attributes are not supported in Lua version %{level}",
                        level = p.parse_config.level
                    ),
                    p.current_token_range(),
                ));

                return Err(ParseFailReason::UnexpectedToken);
            }
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected 'function', variable name, or attribute after 'local'"),
                p.current_token_range(),
            ));

            return Err(ParseFailReason::UnexpectedToken);
        }
    }

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_local_name(p: &mut LuaParser, support_attrib: bool) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::LocalName);
    match expect_token(p, LuaTokenKind::TkName) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected variable name after 'local'"),
                p.current_token_range(),
            ));
        }
    }
    if support_attrib && p.current_token() == LuaTokenKind::TkLt {
        parse_attrib(p)?;
    }

    Ok(m.complete(p))
}

fn parse_attrib(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::Attribute);
    let range = p.current_token_range();
    p.bump();
    match expect_token(p, LuaTokenKind::TkName) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected attribute name after '<'"),
                p.current_token_range(),
            ));
        }
    }
    match expect_token(p, LuaTokenKind::TkGt) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected '>' after attribute name"),
                p.current_token_range(),
            ));
        }
    }
    if !p.parse_config.support_local_attrib() {
        p.errors.push(LuaParseError::syntax_error_from(
            &t!(
                "local attribute is not supported for current version: %{level}",
                level = p.parse_config.level
            ),
            range,
        ));
    }

    Ok(m.complete(p))
}

fn parse_return(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ReturnStat);
    p.bump();
    if !block_follow(p)
        && p.current_token() != LuaTokenKind::TkSemicolon
        && parse_expr_list_impl(p).is_err()
    {
        push_expr_error_lazy(p, || t!("expected expression in return statement"));
    }

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_break(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::BreakStat);
    p.bump();
    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_repeat(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::RepeatStat);
    p.bump();
    parse_block(p)?;
    match expect_token(p, LuaTokenKind::TkUntil) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected 'until' after repeat block"),
                p.current_token_range(),
            ));
        }
    }
    if parse_expr(p).is_err() {
        push_expr_error_lazy(p, || t!("expected condition expression after 'until'"));
    }
    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_goto(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::GotoStat);
    p.bump();
    match expect_token(p, LuaTokenKind::TkName) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected label name after 'goto'"),
                p.current_token_range(),
            ));
        }
    }
    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_empty_stat(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::EmptyStat);
    p.bump();
    Ok(m.complete(p))
}

fn try_parse_global_stat(p: &mut LuaParser) -> ParseResult {
    let mut m = p.mark(LuaSyntaxKind::GlobalStat);
    match p.peek_next_token() {
        LuaTokenKind::TkName => {
            p.set_current_token_kind(LuaTokenKind::TkGlobal);
            p.bump();
            parse_global_name_list(p)?;
        }
        LuaTokenKind::TkLt => {
            p.set_current_token_kind(LuaTokenKind::TkGlobal);
            p.bump();
            parse_attrib(p)?;
            if p.current_token() == LuaTokenKind::TkName {
                parse_global_name_list(p)?;
            } else if p.current_token() == LuaTokenKind::TkMul {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected variable name after global attribute"),
                    p.current_token_range(),
                ));
            }
        }
        // global function
        LuaTokenKind::TkFunction => {
            p.set_current_token_kind(LuaTokenKind::TkGlobal);
            p.bump(); // consume 'global'
            m.set_kind(p, LuaSyntaxKind::FuncStat);
            p.bump(); // consume 'function'
            let m2 = p.mark(LuaSyntaxKind::NameExpr);
            match expect_token(p, LuaTokenKind::TkName) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected function name after 'global function'"),
                        p.current_token_range(),
                    ));
                    return Err(ParseFailReason::UnexpectedToken);
                }
            }
            m2.complete(p);
            parse_closure_expr(p)?;
        }
        // global *
        LuaTokenKind::TkMul => {
            p.set_current_token_kind(LuaTokenKind::TkGlobal);
            p.bump(); // consume 'global'
            p.bump(); // consume '*'
        }
        _ => {
            return Ok(m.undo(p));
        }
    }

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_assign_or_expr_or_global_stat(p: &mut LuaParser) -> ParseResult {
    if p.parse_config.level >= LuaLanguageLevel::Lua55 && p.current_token() == LuaTokenKind::TkName
    {
        let token_text = p.current_token_text();
        if token_text == "global" {
            let cm = try_parse_global_stat(p)?;
            if !cm.is_invalid() {
                return Ok(cm);
            }
        }
    }

    let mut m = p.mark(LuaSyntaxKind::AssignStat);
    let range = p.current_token_range();

    // 解析第一个表达式
    let cm = match parse_expr(p) {
        Ok(cm) => cm,
        Err(err) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected expression in assignment or statement"),
                range,
            ));
            return Err(err);
        }
    };

    // 检查是否是函数调用语句
    if matches!(
        cm.kind,
        LuaSyntaxKind::CallExpr
            | LuaSyntaxKind::AssertCallExpr
            | LuaSyntaxKind::ErrorCallExpr
            | LuaSyntaxKind::RequireCallExpr
            | LuaSyntaxKind::TypeCallExpr
            | LuaSyntaxKind::SetmetatableCallExpr
    ) {
        m.set_kind(p, LuaSyntaxKind::CallExprStat);
        if_token_bump(p, LuaTokenKind::TkSemicolon);
        return Ok(m.complete(p));
    }

    // 验证左值
    if !matches!(cm.kind, LuaSyntaxKind::NameExpr | LuaSyntaxKind::IndexExpr) {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("invalid left-hand side in assignment (expected variable or table index)"),
            range,
        ));

        return Err(ParseFailReason::UnexpectedToken);
    }

    // 解析更多左值(如果有逗号)
    while p.current_token() == LuaTokenKind::TkComma {
        p.bump();
        match parse_expr(p) {
            Ok(expr_cm) => {
                if !matches!(
                    expr_cm.kind,
                    LuaSyntaxKind::NameExpr | LuaSyntaxKind::IndexExpr
                ) {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!(
                            "invalid left-hand side in assignment (expected variable or table index)"
                        ),
                        p.current_token_range(),
                    ));
                    return Err(ParseFailReason::UnexpectedToken);
                }
            }
            Err(_) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected variable after ',' in assignment"),
                    p.current_token_range(),
                ));
            }
        }
    }

    // 期望赋值操作符
    if p.current_token().is_assign_op() {
        p.bump();

        // 解析右值表达式列表
        if parse_expr_list_impl(p).is_err() {
            push_expr_error_lazy(p, || t!("expected expression after '=' in assignment"));
        }
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected '=' for assignment or this is an incomplete statement"),
            p.current_token_range(),
        ));

        return Err(ParseFailReason::UnexpectedToken);
    }

    if_token_bump(p, LuaTokenKind::TkSemicolon);
    Ok(m.complete(p))
}

fn parse_label_stat(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::LabelStat);
    p.bump();
    match expect_token(p, LuaTokenKind::TkName) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected label name after 'goto'"),
                p.current_token_range(),
            ));
        }
    }
    match expect_token(p, LuaTokenKind::TkDbColon) {
        Ok(_) => {}
        Err(_) => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected '::' after label name"),
                p.current_token_range(),
            ));
        }
    }
    Ok(m.complete(p))
}