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
use crate::{
    SpecialFunction,
    grammar::{ParseFailReason, ParseResult, lua::is_statement_start_token},
    kind::{BinaryOperator, LuaOpKind, LuaSyntaxKind, LuaTokenKind, UNARY_PRIORITY, UnaryOperator},
    parser::{LuaParser, MarkerEventContainer},
    parser_error::LuaParseError,
};

use super::{expect_token, if_token_bump, parse_block};

pub fn parse_expr(p: &mut LuaParser) -> ParseResult {
    parse_sub_expr(p, 0)
}

fn parse_sub_expr(p: &mut LuaParser, limit: i32) -> ParseResult {
    let uop = LuaOpKind::to_unary_operator(p.current_token());
    let mut cm = if uop != UnaryOperator::OpNop {
        let m = p.mark(LuaSyntaxKind::UnaryExpr);
        let op_range = p.current_token_range();
        let op_token = p.current_token();
        p.bump();
        match parse_sub_expr(p, UNARY_PRIORITY) {
            Ok(_) => {}
            Err(_) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!(
                        "unary operator '%{op}' is not followed by an expression",
                        op = op_token
                    ),
                    op_range,
                ));
            }
        };
        m.complete(p)
    } else {
        parse_simple_expr(p)?
    };

    let mut bop = LuaOpKind::to_binary_operator(p.current_token());
    while bop != BinaryOperator::OpNop && bop.get_priority().left > limit {
        let op_range = p.current_token_range();
        let op_token = p.current_token();
        let m = cm.precede(p, LuaSyntaxKind::BinaryExpr);
        p.bump();
        match parse_sub_expr(p, bop.get_priority().right) {
            Ok(_) => {}
            Err(err) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!(
                        "binary operator '%{op}' is not followed by an expression",
                        op = op_token
                    ),
                    op_range,
                ));
                m.complete(p);
                return Err(err);
            }
        }

        cm = m.complete(p);
        bop = LuaOpKind::to_binary_operator(p.current_token());
    }

    Ok(cm)
}

fn parse_simple_expr(p: &mut LuaParser) -> ParseResult {
    match p.current_token() {
        LuaTokenKind::TkInt
        | LuaTokenKind::TkFloat
        | LuaTokenKind::TkComplex
        | LuaTokenKind::TkNil
        | LuaTokenKind::TkTrue
        | LuaTokenKind::TkFalse
        | LuaTokenKind::TkDots
        | LuaTokenKind::TkString
        | LuaTokenKind::TkLongString => {
            let m = p.mark(LuaSyntaxKind::LiteralExpr);
            p.bump();
            Ok(m.complete(p))
        }
        LuaTokenKind::TkLeftBrace => parse_table_expr(p),
        LuaTokenKind::TkFunction => parse_closure_expr(p),
        LuaTokenKind::TkName | LuaTokenKind::TkLeftParen => parse_suffixed_expr(p),
        _ => {
            // Provide more specific error information
            let error_msg = match p.current_token() {
                LuaTokenKind::TkEof => t!("unexpected end of file, expected expression"),
                LuaTokenKind::TkRightParen => t!("unexpected ')', expected expression"),
                LuaTokenKind::TkRightBrace => t!("unexpected '}', expected expression"),
                LuaTokenKind::TkRightBracket => t!("unexpected ']', expected expression"),
                LuaTokenKind::TkComma => t!("unexpected ',', expected expression"),
                LuaTokenKind::TkSemicolon => t!("unexpected ';', expected expression"),
                LuaTokenKind::TkEnd => t!("unexpected 'end', expected expression"),
                LuaTokenKind::TkElse => t!("unexpected 'else', expected expression"),
                LuaTokenKind::TkElseIf => t!("unexpected 'elseif', expected expression"),
                LuaTokenKind::TkThen => t!("unexpected 'then', expected expression"),
                LuaTokenKind::TkDo => t!("unexpected 'do', expected expression"),
                LuaTokenKind::TkUntil => t!("unexpected 'until', expected expression"),
                _ => t!(
                    "unexpected token '%{token}', expected expression",
                    token = p.current_token()
                ),
            };

            p.push_error(LuaParseError::syntax_error_from(
                &error_msg,
                p.current_token_range(),
            ));
            Err(ParseFailReason::UnexpectedToken)
        }
    }
}

pub fn parse_closure_expr(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ClosureExpr);

    if_token_bump(p, LuaTokenKind::TkFunction);

    parse_param_list(p)?;

    if p.current_token() != LuaTokenKind::TkEnd {
        parse_block(p)?;
    }

    if p.current_token() == LuaTokenKind::TkEnd {
        p.bump();
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected 'end' to close function definition"),
            p.current_token_range(),
        ));
    }

    Ok(m.complete(p))
}

fn parse_param_list(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ParamList);

    if p.current_token() == LuaTokenKind::TkLeftParen {
        p.bump();
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected '(' to start parameter list"),
            p.current_token_range(),
        ));
    }

    let mut is_vararg = false;
    if p.current_token() != LuaTokenKind::TkRightParen {
        loop {
            match parse_param_name(p, &mut is_vararg) {
                Ok(_) => {
                    if is_vararg {
                        break;
                    }
                }
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected parameter name"),
                        p.current_token_range(),
                    ));
                    // Try to recover to next comma or right parenthesis
                    while !matches!(
                        p.current_token(),
                        LuaTokenKind::TkComma
                            | LuaTokenKind::TkRightParen
                            | LuaTokenKind::TkEof
                            | LuaTokenKind::TkEnd
                    ) && !is_statement_start_token(p.current_token())
                    {
                        p.bump();
                    }
                }
            }

            if p.current_token() == LuaTokenKind::TkComma {
                p.bump();
                // Check if there is a parameter after comma
                if p.current_token() == LuaTokenKind::TkRightParen {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected parameter name after ','"),
                        p.current_token_range(),
                    ));
                    break;
                }
            } else {
                break;
            }
        }
    }

    if p.current_token() == LuaTokenKind::TkRightParen {
        p.bump();
    } else if is_vararg {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("vararg '...' must be the last parameter"),
            p.current_token_range(),
        ));
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected ')' to close parameter list"),
            p.current_token_range(),
        ));
    }

    Ok(m.complete(p))
}

fn parse_param_name(p: &mut LuaParser, is_vararg: &mut bool) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::ParamName);
    let token = p.current_token();
    match token {
        LuaTokenKind::TkName => {
            p.bump();
        }
        LuaTokenKind::TkDots => {
            *is_vararg = true;
            p.bump();
            if token == LuaTokenKind::TkDots
                && p.parse_config.support_named_var_args()
                && p.current_token() == LuaTokenKind::TkName
            {
                p.bump();
            }
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected parameter name or '...' (vararg)"),
                p.current_token_range(),
            ));
            m.complete(p);
            return Err(ParseFailReason::UnexpectedToken);
        }
    }

    Ok(m.complete(p))
}

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

    if p.current_token() == LuaTokenKind::TkRightBrace {
        p.bump();
        return Ok(m.complete(p));
    }

    // Parse first field
    match parse_field_with_recovery(p) {
        Ok(cm) => match cm.kind {
            LuaSyntaxKind::TableFieldAssign => {
                m.set_kind(p, LuaSyntaxKind::TableObjectExpr);
            }
            LuaSyntaxKind::TableFieldValue => {
                m.set_kind(p, LuaSyntaxKind::TableArrayExpr);
            }
            _ => {}
        },
        Err(_) => {
            // If first field parsing failed, continue trying to recover
            recover_to_table_boundary(p);
        }
    }

    // Parse remaining fields
    while matches!(
        p.current_token(),
        LuaTokenKind::TkComma | LuaTokenKind::TkSemicolon
    ) {
        let separator_token = p.current_token();
        p.bump(); // consume separator

        if p.current_token() == LuaTokenKind::TkRightBrace {
            // Allow trailing separator
            break;
        }

        match parse_field_with_recovery(p) {
            Ok(cm) => {
                if cm.kind == LuaSyntaxKind::TableFieldAssign {
                    m.set_kind(p, LuaSyntaxKind::TableObjectExpr);
                }
            }
            Err(_) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("invalid table field after '%{sep}'", sep = separator_token),
                    p.current_token_range(),
                ));
                // Recover to next field boundary
                recover_to_table_boundary(p);
                if p.current_token() == LuaTokenKind::TkRightBrace {
                    break;
                }
            }
        }
    }

    // Handle closing brace
    if p.current_token() == LuaTokenKind::TkRightBrace {
        p.bump();
    } else {
        p.push_error(LuaParseError::syntax_error_from(
            &t!("expected '}' to close table constructor"),
            p.current_token_range(),
        ));

        // Try to recover: look for possible closing brace
        let mut found_brace = false;
        let mut brace_count = 1; // 我们已经在表中
        let mut lookahead_count = 0;
        const MAX_LOOKAHEAD: usize = 50; // 限制向前查看的token数量

        while p.current_token() != LuaTokenKind::TkEof && lookahead_count < MAX_LOOKAHEAD {
            match p.current_token() {
                LuaTokenKind::TkRightBrace => {
                    brace_count -= 1;
                    if brace_count == 0 {
                        p.bump(); // 消费闭合括号
                        found_brace = true;
                        break;
                    }
                    p.bump();
                }
                LuaTokenKind::TkLeftBrace => {
                    brace_count += 1;
                    p.bump();
                }
                // 如果遇到看起来像是表外部的token,停止寻找
                LuaTokenKind::TkEnd
                | LuaTokenKind::TkElse
                | LuaTokenKind::TkElseIf
                | LuaTokenKind::TkUntil
                | LuaTokenKind::TkThen
                | LuaTokenKind::TkDo => {
                    break;
                }
                _ => {
                    p.bump();
                }
            }
            lookahead_count += 1;
        }

        if !found_brace {
            // 如果没有找到闭合括号,在当前位置创建一个错误标记
            p.push_error(LuaParseError::syntax_error_from(
                &t!("table constructor was not properly closed"),
                p.current_token_range(),
            ));
        }
    }

    Ok(m.complete(p))
}

fn parse_field_with_recovery(p: &mut LuaParser) -> ParseResult {
    let mut m = p.mark(LuaSyntaxKind::TableFieldValue);

    match p.current_token() {
        LuaTokenKind::TkLeftBracket => {
            // [expr] = expr 形式
            m.set_kind(p, LuaSyntaxKind::TableFieldAssign);
            p.bump(); // consume '['

            match parse_expr(p) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected expression inside table index brackets"),
                        p.current_token_range(),
                    ));
                    // 恢复到边界
                    while !matches!(
                        p.current_token(),
                        LuaTokenKind::TkRightBracket
                            | LuaTokenKind::TkAssign
                            | LuaTokenKind::TkComma
                            | LuaTokenKind::TkSemicolon
                            | LuaTokenKind::TkRightBrace
                            | LuaTokenKind::TkEof
                    ) {
                        p.bump();
                    }
                }
            }

            if p.current_token() == LuaTokenKind::TkRightBracket {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected ']' to close table index"),
                    p.current_token_range(),
                ));
            }

            if p.current_token() == LuaTokenKind::TkAssign {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected '=' after table index"),
                    p.current_token_range(),
                ));
            }

            match parse_expr(p) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected value expression after '='"),
                        p.current_token_range(),
                    ));
                }
            }
        }
        LuaTokenKind::TkName => {
            // 可能是 name = expr 或者只是 expr
            if p.peek_next_token() == LuaTokenKind::TkAssign {
                m.set_kind(p, LuaSyntaxKind::TableFieldAssign);
                p.bump(); // consume name
                p.bump(); // consume '='
                match parse_expr(p) {
                    Ok(_) => {}
                    Err(_) => {
                        p.push_error(LuaParseError::syntax_error_from(
                            &t!("expected value expression after field name"),
                            p.current_token_range(),
                        ));
                    }
                }
            } else {
                // 作为表达式解析
                match parse_expr(p) {
                    Ok(_) => {}
                    Err(_) => {
                        p.push_error(LuaParseError::syntax_error_from(
                            &t!("invalid table field expression"),
                            p.current_token_range(),
                        ));
                    }
                }
            }
        }
        // 表示表实际上已经结束的token
        LuaTokenKind::TkEof | LuaTokenKind::TkLocal => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("unexpected end of table field"),
                p.current_token_range(),
            ));
        }
        _ => {
            // 尝试解析为普通表达式
            match parse_expr(p) {
                Ok(_) => {}
                Err(_) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("invalid table field, expected expression, field assignment, or table end"),
                        p.current_token_range(),
                    ));
                }
            }
        }
    }

    Ok(m.complete(p))
}

fn recover_to_table_boundary(p: &mut LuaParser) {
    // 跳过直到找到表边界或字段分隔符
    while !matches!(
        p.current_token(),
        LuaTokenKind::TkComma
            | LuaTokenKind::TkSemicolon
            | LuaTokenKind::TkRightBrace
            | LuaTokenKind::TkEof
    ) {
        p.bump();
    }
}

fn parse_suffixed_expr(p: &mut LuaParser) -> ParseResult {
    let mut cm = match p.current_token() {
        LuaTokenKind::TkName => parse_name_or_special_function(p)?,
        LuaTokenKind::TkLeftParen => {
            let m = p.mark(LuaSyntaxKind::ParenExpr);
            let paren_range = p.current_token_range();
            p.bump();
            match parse_expr(p) {
                Ok(_) => {}
                Err(err) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected expression inside parentheses"),
                        paren_range,
                    ));
                    m.complete(p);
                    return Err(err);
                }
            }
            if p.current_token() == LuaTokenKind::TkRightParen {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected ')' to close parentheses"),
                    paren_range,
                ));
            }
            m.complete(p)
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expect primary expression (identifier or parenthesized expression)"),
                p.current_token_range(),
            ));
            return Err(ParseFailReason::UnexpectedToken);
        }
    };

    loop {
        match p.current_token() {
            LuaTokenKind::TkDot | LuaTokenKind::TkColon | LuaTokenKind::TkLeftBracket => {
                let m = cm.precede(p, LuaSyntaxKind::IndexExpr);
                if let Err(err) = parse_index_struct(p) {
                    m.complete(p);
                    return Err(err);
                }
                cm = m.complete(p);
            }
            LuaTokenKind::TkLeftParen
            | LuaTokenKind::TkLongString
            | LuaTokenKind::TkString
            | LuaTokenKind::TkLeftBrace => {
                let m = cm.precede(p, LuaSyntaxKind::CallExpr);
                if let Err(err) = parse_args(p) {
                    m.complete(p);
                    return Err(err);
                }
                cm = m.complete(p);
            }
            _ => {
                return Ok(cm);
            }
        }
    }
}

fn parse_name_or_special_function(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::NameExpr);
    let special_kind = match p.parse_config.get_special_function(p.current_token_text()) {
        SpecialFunction::Require => LuaSyntaxKind::RequireCallExpr,
        SpecialFunction::Assert => LuaSyntaxKind::AssertCallExpr,
        SpecialFunction::Error => LuaSyntaxKind::ErrorCallExpr,
        SpecialFunction::Type => LuaSyntaxKind::TypeCallExpr,
        SpecialFunction::Setmetaatable => LuaSyntaxKind::SetmetatableCallExpr,
        _ => LuaSyntaxKind::None,
    };
    p.bump();
    let mut cm = m.complete(p);
    if special_kind == LuaSyntaxKind::None {
        return Ok(cm);
    }

    if matches!(
        p.current_token(),
        LuaTokenKind::TkLeftParen
            | LuaTokenKind::TkLongString
            | LuaTokenKind::TkString
            | LuaTokenKind::TkLeftBrace
    ) {
        let m1 = cm.precede(p, special_kind);
        if let Err(err) = parse_args(p) {
            m1.complete(p);
            return Err(err);
        }
        cm = m1.complete(p);
    }

    Ok(cm)
}

fn parse_index_struct(p: &mut LuaParser) -> Result<(), ParseFailReason> {
    let index_op_range = p.current_token_range();
    match p.current_token() {
        LuaTokenKind::TkLeftBracket => {
            p.bump();
            match parse_expr(p) {
                Ok(_) => {}
                Err(err) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected expression inside table index brackets"),
                        index_op_range,
                    ));
                    return Err(err);
                }
            }
            match expect_token(p, LuaTokenKind::TkRightBracket) {
                Ok(_) => {}
                Err(err) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected ']' to close table index"),
                        index_op_range,
                    ));
                    return Err(err);
                }
            }
        }
        LuaTokenKind::TkDot => {
            p.bump();
            match expect_token(p, LuaTokenKind::TkName) {
                Ok(_) => {}
                Err(err) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected field name after '.'"),
                        index_op_range,
                    ));
                    return Err(err);
                }
            }
        }
        LuaTokenKind::TkColon => {
            p.bump();
            let name_token_range = p.current_token_range();
            match expect_token(p, LuaTokenKind::TkName) {
                Ok(_) => {}
                Err(err) => {
                    p.push_error(LuaParseError::syntax_error_from(
                        &t!("expected method name after ':'"),
                        index_op_range,
                    ));
                    return Err(err);
                }
            }
            if !matches!(
                p.current_token(),
                LuaTokenKind::TkLeftParen
                    | LuaTokenKind::TkLeftBrace
                    | LuaTokenKind::TkString
                    | LuaTokenKind::TkLongString
            ) {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!(
                        "colon accessor must be followed by a function call or table constructor or string literal"
                    ),
                    name_token_range,
                ));

                return Err(ParseFailReason::UnexpectedToken);
            }
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expect index struct"),
                p.current_token_range(),
            ));

            return Err(ParseFailReason::UnexpectedToken);
        }
    }

    Ok(())
}

fn parse_args(p: &mut LuaParser) -> ParseResult {
    let m = p.mark(LuaSyntaxKind::CallArgList);
    match p.current_token() {
        LuaTokenKind::TkLeftParen => {
            p.bump();
            if p.current_token() != LuaTokenKind::TkRightParen {
                loop {
                    match parse_expr(p) {
                        Ok(_) => {}
                        Err(_) => {
                            p.push_error(LuaParseError::syntax_error_from(
                                &t!("expected argument expression"),
                                p.current_token_range(),
                            ));
                            // 跳过到下一个逗号或右括号
                            while !matches!(
                                p.current_token(),
                                LuaTokenKind::TkComma
                                    | LuaTokenKind::TkRightParen
                                    | LuaTokenKind::TkEof
                            ) && !is_statement_start_token(p.current_token())
                            {
                                p.bump();
                            }

                            if p.current_token() == LuaTokenKind::TkComma {
                                p.bump();
                                continue;
                            }
                            break;
                        }
                    }

                    if p.current_token() == LuaTokenKind::TkComma {
                        p.bump();
                        if p.current_token() == LuaTokenKind::TkRightParen {
                            p.push_error(LuaParseError::syntax_error_from(
                                &t!("expected expression after ','"),
                                p.current_token_range(),
                            ));
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }

            if p.current_token() == LuaTokenKind::TkRightParen {
                p.bump();
            } else {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("expected ')' to close argument list"),
                    p.current_token_range(),
                ));
            }
        }
        LuaTokenKind::TkLeftBrace => match parse_table_expr(p) {
            Ok(_) => {}
            Err(err) => {
                p.push_error(LuaParseError::syntax_error_from(
                    &t!("invalid table constructor in function call"),
                    p.current_token_range(),
                ));
                m.complete(p);
                return Err(err);
            }
        },
        LuaTokenKind::TkString | LuaTokenKind::TkLongString => {
            let m1 = p.mark(LuaSyntaxKind::LiteralExpr);
            p.bump();
            m1.complete(p);
        }
        _ => {
            p.push_error(LuaParseError::syntax_error_from(
                &t!("expected '(', string, or table constructor for function call"),
                p.current_token_range(),
            ));

            m.complete(p);
            return Err(ParseFailReason::UnexpectedToken);
        }
    }

    Ok(m.complete(p))
}