@@ -451,12 +451,54 @@ impl Parser {
451451 expr : Box :: new ( lhs) ,
452452 field : Box :: new ( field) ,
453453 } ;
454- if self . peek_token ( TokenKind :: Dot ) . is_ok ( ) {
455- self . parse_field_access ( expr)
456- } else if HBinOp :: try_from ( self . peek ( ) ?. kind ) . is_ok ( ) {
457- self . parse_bin_op ( Some ( expr) )
458- } else {
459- Ok ( expr)
454+
455+ // Look ahead to determine next steps
456+ match self . peek ( ) ?. kind {
457+ TokenKind :: Dot => self . parse_field_access ( expr) ,
458+ TokenKind :: SquareBraceOpen => {
459+ self . match_token ( TokenKind :: SquareBraceOpen ) ?;
460+ let index_expr = self . parse_expression ( ) ?;
461+ self . match_token ( TokenKind :: SquareBraceClose ) ?;
462+
463+ let array_expr = HExpression :: ArrayAccess {
464+ name : format ! ( "{:?}" , expr) ,
465+ index : Box :: new ( index_expr) ,
466+ } ;
467+
468+ // Handle any further chaining (like .foo[0] or [0][0])
469+ match self . peek ( ) ?. kind {
470+ TokenKind :: Dot => self . parse_field_access ( array_expr) ,
471+ TokenKind :: SquareBraceOpen => {
472+ // Handle nested array access
473+ let mut current_expr = array_expr;
474+ while self . peek_token ( TokenKind :: SquareBraceOpen ) . is_ok ( ) {
475+ self . match_token ( TokenKind :: SquareBraceOpen ) ?;
476+ let index = self . parse_expression ( ) ?;
477+ self . match_token ( TokenKind :: SquareBraceClose ) ?;
478+
479+ current_expr = HExpression :: ArrayAccess {
480+ name : format ! ( "{:?}" , current_expr) ,
481+ index : Box :: new ( index) ,
482+ } ;
483+ }
484+
485+ // If there's a dot after the nested array access
486+ if self . peek_token ( TokenKind :: Dot ) . is_ok ( ) {
487+ self . parse_field_access ( current_expr)
488+ } else if HBinOp :: try_from ( self . peek ( ) ?. kind ) . is_ok ( ) {
489+ self . parse_bin_op ( Some ( current_expr) )
490+ } else {
491+ Ok ( current_expr)
492+ }
493+ }
494+ kind if HBinOp :: try_from ( kind. clone ( ) ) . is_ok ( ) => {
495+ self . parse_bin_op ( Some ( array_expr) )
496+ }
497+ _ => Ok ( array_expr) ,
498+ }
499+ }
500+ kind if HBinOp :: try_from ( kind. clone ( ) ) . is_ok ( ) => self . parse_bin_op ( Some ( expr) ) ,
501+ _ => Ok ( expr) ,
460502 }
461503 }
462504
0 commit comments