@@ -499,16 +499,39 @@ fn parse_cast_expr(p: &mut LuaDocParser) -> DocParseResult {
499499 let m = p. mark ( LuaSyntaxKind :: NameExpr ) ;
500500 p. bump ( ) ;
501501 let mut cm = m. complete ( p) ;
502- // 处理多级字段访问
503- while p. current_token ( ) == LuaTokenKind :: TkDot {
504- let index_m = cm. precede ( p, LuaSyntaxKind :: IndexExpr ) ;
505- p. bump ( ) ;
506- if p. current_token ( ) == LuaTokenKind :: TkName {
507- p. bump ( ) ;
508- } else {
509- // 找不到也不报错
502+ // 处理多级字段访问(支持 `.` 和 `[]` 索引)
503+ loop {
504+ match p. current_token ( ) {
505+ LuaTokenKind :: TkDot => {
506+ let index_m = cm. precede ( p, LuaSyntaxKind :: IndexExpr ) ;
507+ p. bump ( ) ;
508+ if p. current_token ( ) == LuaTokenKind :: TkName {
509+ p. bump ( ) ;
510+ }
511+ cm = index_m. complete ( p) ;
512+ }
513+ LuaTokenKind :: TkLeftBracket => {
514+ let index_m = cm. precede ( p, LuaSyntaxKind :: IndexExpr ) ;
515+ p. bump ( ) ;
516+ // Wrap the index value in a LiteralExpr node so that
517+ // LuaIndexExpr::get_index_key() can find it (it expects
518+ // a child Node, not a bare token).
519+ if p. current_token ( ) == LuaTokenKind :: TkInt
520+ || p. current_token ( ) == LuaTokenKind :: TkString
521+ {
522+ let literal_m = p. mark ( LuaSyntaxKind :: LiteralExpr ) ;
523+ p. bump ( ) ;
524+ literal_m. complete ( p) ;
525+ } else if p. current_token ( ) == LuaTokenKind :: TkName {
526+ p. bump ( ) ;
527+ }
528+ if p. current_token ( ) == LuaTokenKind :: TkRightBracket {
529+ p. bump ( ) ;
530+ }
531+ cm = index_m. complete ( p) ;
532+ }
533+ _ => break ,
510534 }
511- cm = index_m. complete ( p) ;
512535 }
513536
514537 Ok ( cm)
0 commit comments