@@ -60,12 +60,16 @@ func applyTrailers(expr ast.Expr, trailers []ast.Expr) ast.Expr {
6060 isExpr bool
6161 slice ast.Slicer
6262 call *ast.Call
63+ level int
64+ alias *ast.Alias
65+ aliases []*ast.Alias
66+ identifiers []ast.Identifier
6367}
6468
6569%type <obj> strings
6670%type <mod> inputs file_input single_input eval_input
6771%type <stmts> simple_stmt stmt nl_or_stmt small_stmts stmts
68- %type <stmt> compound_stmt small_stmt expr_stmt del_stmt pass_stmt flow_stmt import_stmt global_stmt nonlocal_stmt assert_stmt break_stmt continue_stmt return_stmt raise_stmt yield_stmt
72+ %type <stmt> compound_stmt small_stmt expr_stmt del_stmt pass_stmt flow_stmt import_stmt global_stmt nonlocal_stmt assert_stmt break_stmt continue_stmt return_stmt raise_stmt yield_stmt import_name import_from
6973%type <op> augassign
7074%type <expr> expr_or_star_expr expr star_expr xor_expr and_expr shift_expr arith_expr term factor power trailer atom test_or_star_expr test not_test lambdef test_nocond lambdef_nocond or_test and_test comparison testlist testlist_star_expr yield_expr_or_testlist yield_expr yield_expr_or_testlist_star_expr dictorsetmaker sliceop arglist
7175%type <exprs> exprlist testlistraw comp_if comp_iter expr_or_star_exprs test_or_star_exprs tests test_colon_tests trailers
@@ -74,6 +78,11 @@ func applyTrailers(expr ast.Expr, trailers []ast.Expr) ast.Expr {
7478%type <comprehensions> comp_for
7579%type <slice> subscript subscriptlist subscripts
7680%type <call> argument arguments optional_arguments arguments2
81+ %type <level> dot dots
82+ %type <str> dotted_name from_arg
83+ %type <identifiers> names
84+ %type <alias> dotted_as_name import_as_name
85+ %type <aliases> dotted_as_names import_as_names import_from_arg
7786
7887%token NEWLINE
7988%token ENDMARKER
@@ -664,6 +673,9 @@ augassign:
664673del_stmt :
665674 DEL exprlist
666675 {
676+ for i := range $2 {
677+ $2 [i].(ast.SetCtxer).SetCtx(ast.Del)
678+ }
667679 $$ = &ast.Delete{StmtBase: ast.StmtBase{$<pos>$}, Targets: $2 }
668680 }
669681
@@ -698,186 +710,194 @@ flow_stmt:
698710break_stmt :
699711 BREAK
700712 {
701- // FIXME
713+ $$ = &ast.Break{StmtBase: ast.StmtBase{$<pos>$}}
702714 }
703715
704716continue_stmt :
705717 CONTINUE
706718 {
707- // FIXME
719+ $$ = &ast.Continue{StmtBase: ast.StmtBase{$<pos>$}}
708720 }
709721
710722return_stmt :
711723 RETURN
712724 {
713- // FIXME
725+ $$ = &ast.Return{StmtBase: ast.StmtBase{$<pos>$}}
714726 }
715727| RETURN testlist
716728 {
717- // FIXME
729+ $$ = &ast.Return{StmtBase: ast.StmtBase{$<pos>$}, Value: $2 }
718730 }
719731
720732yield_stmt :
721733 yield_expr
722734 {
723- // FIXME
735+ $$ = &ast.ExprStmt{StmtBase: ast.StmtBase{$<pos>$}, Value: $1 }
724736 }
725737
726738raise_stmt :
727739 RAISE
728740 {
729- // FIXME
741+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}}
730742 }
731743| RAISE test
732744 {
733- // FIXME
745+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}, Exc: $2 }
734746 }
735747| RAISE test FROM test
736748 {
737- // FIXME
749+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}, Exc: $2 , Cause: $4 }
738750 }
739751
740752import_stmt :
741753 import_name
742754 {
743- // FIXME
755+ $$ = $1
744756 }
745757| import_from
746758 {
747- // FIXME
759+ $$ = $1
748760 }
749761
750762import_name :
751763 IMPORT dotted_as_names
752764 {
753- // FIXME
765+ $$ = &ast.Import{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
754766 }
755767
756768// note below : the ' .' | ELIPSIS is necessary because ' ...' is tokenized as ELIPSIS
757769dot :
758770 ' .'
759771 {
760- // FIXME
772+ $$ = 1
761773 }
762774| ELIPSIS
763775 {
764- // FIXME
776+ $$ = 3
765777 }
766778
767779dots :
768780 dot
769781 {
770- // FIXME
782+ $$ = $1
771783 }
772784| dots dot
773785 {
774- // FIXME
786+ $$ += $2
775787 }
776788
777789from_arg :
778790 dotted_name
779791 {
780- // FIXME
792+ $<level>$ = 0
793+ $$ = $1
781794 }
782795| dots dotted_name
783796 {
784- // FIXME
797+ $<level>$ = $1
798+ $$ = $2
785799 }
786800| dots
787801 {
788- // FIXME
802+ $<level>$ = $1
803+ $$ = " "
789804 }
790805
791806import_from_arg :
792807 ' *'
793808 {
794- // FIXME
809+ $$ = []*ast.Alias{&ast.Alias{Pos: $<pos>$, Name: ast.Identifier( " * " )}}
795810 }
796- | ' (' import_as_names ' )'
811+ | ' (' import_as_names optional_comma ' )'
797812 {
798- // FIXME
813+ $$ = $2
799814 }
800- | import_as_names
815+ | import_as_names optional_comma
801816 {
802- // FIXME
817+ $$ = $1
803818 }
804819
805820import_from :
806821 FROM from_arg IMPORT import_from_arg
807822 {
808- // FIXME
823+ $$ = &ast.ImportFrom{StmtBase: ast.StmtBase{$<pos>$}, Module: ast.Identifier( $2 ), Names: $4 , Level: $<level> 2 }
809824 }
810825
811826import_as_name :
812827 NAME
813828 {
814- // FIXME
829+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier( $1 )}
815830 }
816831| NAME AS NAME
817832 {
818- // FIXME
833+ as := ast.Identifier($3 )
834+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1 ), AsName: &as}
819835 }
820836
821837dotted_as_name :
822838 dotted_name
823839 {
824- // FIXME
840+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier( $1 )}
825841 }
826842| dotted_name AS NAME
827843 {
828- // FIXME
844+ as := ast.Identifier($3 )
845+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1 ), AsName: &as}
829846 }
830847
831848import_as_names :
832- import_as_name optional_comma
849+ import_as_name
833850 {
834- // FIXME
851+ $$ = nil
852+ $$ = append($$ , $1 )
835853 }
836- | import_as_name ' ,' import_as_names
854+ | import_as_names ' ,' import_as_name
837855 {
838- // FIXME
856+ $$ = append( $$ , $3 )
839857 }
840858
841859dotted_as_names :
842860 dotted_as_name
843861 {
844- // FIXME
862+ $$ = nil
863+ $$ = append($$ , $1 )
845864 }
846865| dotted_as_names ' ,' dotted_as_name
847866 {
848- // FIXME
867+ $$ = append( $$ , $3 )
849868 }
850869
851870dotted_name :
852871 NAME
853872 {
854- // FIXME
873+ $$ = $1
855874 }
856875| dotted_name ' .' NAME
857876 {
858- // FIXME
877+ $$ += " . " + $3
859878 }
860879
861880names :
862881 NAME
863882 {
864- // FIXME
883+ $$ = nil
884+ $$ = append($$ , ast.Identifier($1 ))
865885 }
866886| names ' ,' NAME
867887 {
868- // FIXME
888+ $$ = append( $$ , ast.Identifier( $3 ))
869889 }
870890
871891global_stmt :
872892 GLOBAL names
873893 {
874- // FIXME
894+ $$ = &ast.Global{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
875895 }
876896
877897nonlocal_stmt :
878898 NONLOCAL names
879899 {
880- // FIXME
900+ $$ = &ast.Nonlocal{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
881901 }
882902
883903tests :
@@ -894,7 +914,15 @@ tests:
894914assert_stmt :
895915 ASSERT tests
896916 {
897- // FIXME
917+ tests := $2
918+ switch len (tests) {
919+ case 1 :
920+ $$ = &ast.Assert {StmtBase: ast.StmtBase {$<pos>$}, Test: tests[0 ]}
921+ case 2 :
922+ $$ = &ast.Assert {StmtBase: ast.StmtBase {$<pos>$}, Test: tests[0 ], Msg: tests[1 ]}
923+ default :
924+ yylex.Error (" Invalid syntax" )
925+ }
898926 }
899927
900928compound_stmt :
@@ -1363,9 +1391,7 @@ atom:
13631391 }
13641392| ' (' yield_expr ' )'
13651393 {
1366- // FIXME
1367- panic (" yield_expr not implemented" )
1368- $$ = nil
1394+ $$ = $2
13691395 }
13701396| ' (' test_or_star_expr comp_for ' )'
13711397 {
@@ -1768,19 +1794,13 @@ comp_if:
17681794yield_expr:
17691795 YIELD
17701796 {
1771- // FIXME
1772- }
1773- | YIELD yield_arg
1774- {
1775- // FIXME
1797+ $$ = &ast.Yield{ExprBase: ast.ExprBase{$<pos>$}}
17761798 }
1777-
1778- yield_arg:
1779- FROM test
1799+ | YIELD FROM test
17801800 {
1781- // FIXME
1801+ $$ = &ast.YieldFrom{ExprBase: ast.ExprBase{$<pos>$}, Value: $3 }
17821802 }
1783- | testlist
1803+ | YIELD testlist
17841804 {
1785- // FIXME
1805+ $$ = &ast.Yield{ExprBase: ast.ExprBase{$<pos>$}, Value: $2 }
17861806 }
0 commit comments