@@ -358,6 +358,63 @@ impl Parser {
358358 self . parse_primary ( )
359359 }
360360
361+ fn parse_array_assignment ( & mut self , name : String ) -> Statement {
362+ let mut indices = Vec :: new ( ) ;
363+ while Some ( & Token :: SquareLeft ) == self . peek ( ) {
364+ self . advance ( ) ;
365+ indices. push ( self . parse_first ( ) ) ;
366+ match self . advance ( ) {
367+ Some ( Token :: SquareRight ) => { }
368+ _ => panic ! ( "Expected ']' after array index" ) ,
369+ }
370+ }
371+ match self . advance ( ) {
372+ Some ( Token :: Equals ) => { }
373+ _ => panic ! ( "Expected '=' after array index" ) ,
374+ }
375+ let value = self . parse_first ( ) ;
376+ match self . advance ( ) {
377+ Some ( Token :: Semicolon ) => { }
378+ _ => panic ! ( "Expected ';' after array assignment" ) ,
379+ }
380+ Statement :: AssignmentIndex {
381+ array : name,
382+ index : indices,
383+ value,
384+ }
385+ }
386+
387+ fn parse_obj_assignment ( & mut self , name : String ) -> Statement {
388+ let mut properties = Vec :: new ( ) ;
389+ while let Some ( token) = self . peek ( ) {
390+ if * token != Token :: Dot {
391+ break ;
392+ }
393+ self . advance ( ) ;
394+ let prop_name = match self . advance ( ) {
395+ Some ( Token :: Identifier ( name) ) => name. to_owned ( ) ,
396+ _ => panic ! ( "Expected identifier" ) ,
397+ } ;
398+ properties. push ( prop_name) ;
399+ }
400+
401+ match self . advance ( ) {
402+ Some ( Token :: Equals ) => { }
403+ _ => panic ! ( "Expected '=' after object property" ) ,
404+ }
405+ let value = self . parse_first ( ) ;
406+ match self . advance ( ) {
407+ Some ( Token :: Semicolon ) => { }
408+ _ => panic ! ( "Expected ';' after object assignment" ) ,
409+ }
410+
411+ Statement :: AssignmentProperty {
412+ object : name,
413+ property : properties,
414+ value,
415+ }
416+ }
417+
361418 fn parse_call ( & mut self , name : String ) -> Statement {
362419 self . advance ( ) ; // consume the identifier
363420 if let Some ( Token :: LeftParentheses ) = self . peek ( ) {
@@ -369,29 +426,9 @@ impl Parser {
369426 }
370427 Statement :: Expression ( call_expr)
371428 } else if let Some ( Token :: SquareLeft ) = self . peek ( ) {
372- let mut indices = Vec :: new ( ) ;
373- while Some ( & Token :: SquareLeft ) == self . peek ( ) {
374- self . advance ( ) ;
375- indices. push ( self . parse_first ( ) ) ;
376- match self . advance ( ) {
377- Some ( Token :: SquareRight ) => { }
378- _ => panic ! ( "Expected ']' after array index" ) ,
379- }
380- }
381- match self . advance ( ) {
382- Some ( Token :: Equals ) => { }
383- _ => panic ! ( "Expected '=' after array index" ) ,
384- }
385- let value = self . parse_first ( ) ;
386- match self . advance ( ) {
387- Some ( Token :: Semicolon ) => { }
388- _ => panic ! ( "Expected ';' after array assignment" ) ,
389- }
390- Statement :: AssignmentIndex {
391- array : name,
392- index : indices,
393- value,
394- }
429+ self . parse_array_assignment ( name)
430+ } else if let Some ( Token :: Dot ) = self . peek ( ) {
431+ self . parse_obj_assignment ( name)
395432 } else {
396433 self . parse_assignment ( name)
397434 }
@@ -421,6 +458,60 @@ impl Parser {
421458 Expression :: Call { name, args }
422459 }
423460
461+ fn parse_object ( & mut self ) -> Expression {
462+ let mut properties = Vec :: new ( ) ;
463+ while let Some ( token) = self . peek ( ) {
464+ if * token == Token :: RightBrace {
465+ break ;
466+ }
467+ let key = match self . advance ( ) {
468+ Some ( Token :: Identifier ( name) ) => name. to_owned ( ) ,
469+ _ => panic ! ( "Expected identifier" ) ,
470+ } ;
471+ match self . advance ( ) {
472+ Some ( Token :: Colon ) => { }
473+ _ => panic ! ( "Expected ':'" ) ,
474+ }
475+ let value = self . parse_first ( ) ;
476+ properties. push ( ( key, value) ) ;
477+ if let Some ( Token :: Comma ) = self . peek ( ) {
478+ self . advance ( ) ;
479+ }
480+ }
481+
482+ match self . advance ( ) {
483+ Some ( Token :: RightBrace ) => { }
484+ _ => panic ! ( "Expected '}}'" ) ,
485+ }
486+
487+ Expression :: ObjectLiteral ( properties)
488+ }
489+
490+ fn parse_index_access ( & mut self , array : Expression ) -> Expression {
491+ self . advance ( ) ; // consume '['
492+ let index_expr = self . parse_first ( ) ;
493+ match self . advance ( ) {
494+ Some ( Token :: SquareRight ) => { }
495+ _ => panic ! ( "Expected ']' after array index" ) ,
496+ }
497+ Expression :: Index {
498+ array : Box :: new ( array) ,
499+ index : Box :: new ( index_expr) ,
500+ }
501+ }
502+
503+ fn parse_property_access ( & mut self , object : Expression ) -> Expression {
504+ self . advance ( ) ; // consume '.'
505+ let prop_name = match self . advance ( ) {
506+ Some ( Token :: Identifier ( name) ) => name. to_owned ( ) ,
507+ _ => panic ! ( "Expected identifier after '.'" ) ,
508+ } ;
509+ Expression :: PropertyAccess {
510+ object : Box :: new ( object) ,
511+ property : prop_name,
512+ }
513+ }
514+
424515 fn parse_primary ( & mut self ) -> Expression {
425516 match self . advance ( ) {
426517 Some ( Token :: LeftParentheses ) => {
@@ -433,39 +524,34 @@ impl Parser {
433524 }
434525 Some ( Token :: Number ( n) ) => Expression :: Number ( * n) ,
435526 Some ( Token :: Identifier ( name) ) => {
436- let name = name. clone ( ) ;
437- // If followed by '(', it's a function call expression
438- if let Some ( Token :: LeftParentheses ) = self . peek ( ) {
439- self . parse_call_args ( name)
440- } else if let Some ( Token :: SquareLeft ) = self . peek ( ) {
441- self . parse_index ( name)
442- } else {
443- Expression :: Identifier ( name)
527+ let mut expr = Expression :: Identifier ( name. clone ( ) ) ;
528+ loop {
529+ match self . peek ( ) {
530+ Some ( Token :: LeftParentheses ) => {
531+ let name = match expr {
532+ Expression :: Identifier ( ref n) => n. clone ( ) ,
533+ _ => panic ! ( "Expected function name before '('" ) ,
534+ } ;
535+ expr = self . parse_call_args ( name) ;
536+ }
537+ Some ( Token :: SquareLeft ) => {
538+ expr = self . parse_index_access ( expr) ;
539+ }
540+ Some ( Token :: Dot ) => {
541+ expr = self . parse_property_access ( expr) ;
542+ }
543+ _ => break ,
544+ }
444545 }
546+ expr
445547 }
446548 Some ( Token :: SquareLeft ) => self . parse_array_literal ( ) ,
549+ Some ( Token :: LeftBrace ) => self . parse_object ( ) ,
447550 Some ( Token :: String ( s) ) => Expression :: String ( s. clone ( ) ) ,
448551 _ => panic ! ( "Invalid expression" ) ,
449552 }
450553 }
451554
452- fn parse_index ( & mut self , array_name : String ) -> Expression {
453- let mut result = Expression :: Identifier ( array_name. clone ( ) ) ;
454- while Some ( & Token :: SquareLeft ) == self . peek ( ) {
455- self . advance ( ) ; // consume '['
456- let index_expr = self . parse_first ( ) ;
457- match self . advance ( ) {
458- Some ( Token :: SquareRight ) => { }
459- _ => panic ! ( "Expected ']' after array index" ) ,
460- }
461- result = Expression :: Index {
462- array : Box :: new ( result) ,
463- index : Box :: new ( index_expr) ,
464- } ;
465- }
466- result
467- }
468-
469555 fn parse_array_literal ( & mut self ) -> Expression {
470556 let mut elements = Vec :: new ( ) ;
471557 while let Some ( token) = self . peek ( ) {
0 commit comments