@@ -60,12 +60,16 @@ func applyTrailers(expr ast.Expr, trailers []ast.Expr) ast.Expr {
60
60
isExpr bool
61
61
slice ast.Slicer
62
62
call *ast.Call
63
+ level int
64
+ alias *ast.Alias
65
+ aliases []*ast.Alias
66
+ identifiers []ast.Identifier
63
67
}
64
68
65
69
%type <obj> strings
66
70
%type <mod> inputs file_input single_input eval_input
67
71
%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
69
73
%type <op> augassign
70
74
%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
71
75
%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 {
74
78
%type <comprehensions> comp_for
75
79
%type <slice> subscript subscriptlist subscripts
76
80
%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
77
86
78
87
%token NEWLINE
79
88
%token ENDMARKER
@@ -664,6 +673,9 @@ augassign:
664
673
del_stmt :
665
674
DEL exprlist
666
675
{
676
+ for i := range $2 {
677
+ $2 [i].(ast.SetCtxer).SetCtx(ast.Del)
678
+ }
667
679
$$ = &ast.Delete{StmtBase: ast.StmtBase{$<pos>$}, Targets: $2 }
668
680
}
669
681
@@ -698,186 +710,194 @@ flow_stmt:
698
710
break_stmt :
699
711
BREAK
700
712
{
701
- // FIXME
713
+ $$ = &ast.Break{StmtBase: ast.StmtBase{$<pos>$}}
702
714
}
703
715
704
716
continue_stmt :
705
717
CONTINUE
706
718
{
707
- // FIXME
719
+ $$ = &ast.Continue{StmtBase: ast.StmtBase{$<pos>$}}
708
720
}
709
721
710
722
return_stmt :
711
723
RETURN
712
724
{
713
- // FIXME
725
+ $$ = &ast.Return{StmtBase: ast.StmtBase{$<pos>$}}
714
726
}
715
727
| RETURN testlist
716
728
{
717
- // FIXME
729
+ $$ = &ast.Return{StmtBase: ast.StmtBase{$<pos>$}, Value: $2 }
718
730
}
719
731
720
732
yield_stmt :
721
733
yield_expr
722
734
{
723
- // FIXME
735
+ $$ = &ast.ExprStmt{StmtBase: ast.StmtBase{$<pos>$}, Value: $1 }
724
736
}
725
737
726
738
raise_stmt :
727
739
RAISE
728
740
{
729
- // FIXME
741
+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}}
730
742
}
731
743
| RAISE test
732
744
{
733
- // FIXME
745
+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}, Exc: $2 }
734
746
}
735
747
| RAISE test FROM test
736
748
{
737
- // FIXME
749
+ $$ = &ast.Raise{StmtBase: ast.StmtBase{$<pos>$}, Exc: $2 , Cause: $4 }
738
750
}
739
751
740
752
import_stmt :
741
753
import_name
742
754
{
743
- // FIXME
755
+ $$ = $1
744
756
}
745
757
| import_from
746
758
{
747
- // FIXME
759
+ $$ = $1
748
760
}
749
761
750
762
import_name :
751
763
IMPORT dotted_as_names
752
764
{
753
- // FIXME
765
+ $$ = &ast.Import{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
754
766
}
755
767
756
768
// note below : the ' .' | ELIPSIS is necessary because ' ...' is tokenized as ELIPSIS
757
769
dot :
758
770
' .'
759
771
{
760
- // FIXME
772
+ $$ = 1
761
773
}
762
774
| ELIPSIS
763
775
{
764
- // FIXME
776
+ $$ = 3
765
777
}
766
778
767
779
dots :
768
780
dot
769
781
{
770
- // FIXME
782
+ $$ = $1
771
783
}
772
784
| dots dot
773
785
{
774
- // FIXME
786
+ $$ += $2
775
787
}
776
788
777
789
from_arg :
778
790
dotted_name
779
791
{
780
- // FIXME
792
+ $<level>$ = 0
793
+ $$ = $1
781
794
}
782
795
| dots dotted_name
783
796
{
784
- // FIXME
797
+ $<level>$ = $1
798
+ $$ = $2
785
799
}
786
800
| dots
787
801
{
788
- // FIXME
802
+ $<level>$ = $1
803
+ $$ = " "
789
804
}
790
805
791
806
import_from_arg :
792
807
' *'
793
808
{
794
- // FIXME
809
+ $$ = []*ast.Alias{&ast.Alias{Pos: $<pos>$, Name: ast.Identifier( " * " )}}
795
810
}
796
- | ' (' import_as_names ' )'
811
+ | ' (' import_as_names optional_comma ' )'
797
812
{
798
- // FIXME
813
+ $$ = $2
799
814
}
800
- | import_as_names
815
+ | import_as_names optional_comma
801
816
{
802
- // FIXME
817
+ $$ = $1
803
818
}
804
819
805
820
import_from :
806
821
FROM from_arg IMPORT import_from_arg
807
822
{
808
- // FIXME
823
+ $$ = &ast.ImportFrom{StmtBase: ast.StmtBase{$<pos>$}, Module: ast.Identifier( $2 ), Names: $4 , Level: $<level> 2 }
809
824
}
810
825
811
826
import_as_name :
812
827
NAME
813
828
{
814
- // FIXME
829
+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier( $1 )}
815
830
}
816
831
| NAME AS NAME
817
832
{
818
- // FIXME
833
+ as := ast.Identifier($3 )
834
+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1 ), AsName: &as}
819
835
}
820
836
821
837
dotted_as_name :
822
838
dotted_name
823
839
{
824
- // FIXME
840
+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier( $1 )}
825
841
}
826
842
| dotted_name AS NAME
827
843
{
828
- // FIXME
844
+ as := ast.Identifier($3 )
845
+ $$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1 ), AsName: &as}
829
846
}
830
847
831
848
import_as_names :
832
- import_as_name optional_comma
849
+ import_as_name
833
850
{
834
- // FIXME
851
+ $$ = nil
852
+ $$ = append($$ , $1 )
835
853
}
836
- | import_as_name ' ,' import_as_names
854
+ | import_as_names ' ,' import_as_name
837
855
{
838
- // FIXME
856
+ $$ = append( $$ , $3 )
839
857
}
840
858
841
859
dotted_as_names :
842
860
dotted_as_name
843
861
{
844
- // FIXME
862
+ $$ = nil
863
+ $$ = append($$ , $1 )
845
864
}
846
865
| dotted_as_names ' ,' dotted_as_name
847
866
{
848
- // FIXME
867
+ $$ = append( $$ , $3 )
849
868
}
850
869
851
870
dotted_name :
852
871
NAME
853
872
{
854
- // FIXME
873
+ $$ = $1
855
874
}
856
875
| dotted_name ' .' NAME
857
876
{
858
- // FIXME
877
+ $$ += " . " + $3
859
878
}
860
879
861
880
names :
862
881
NAME
863
882
{
864
- // FIXME
883
+ $$ = nil
884
+ $$ = append($$ , ast.Identifier($1 ))
865
885
}
866
886
| names ' ,' NAME
867
887
{
868
- // FIXME
888
+ $$ = append( $$ , ast.Identifier( $3 ))
869
889
}
870
890
871
891
global_stmt :
872
892
GLOBAL names
873
893
{
874
- // FIXME
894
+ $$ = &ast.Global{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
875
895
}
876
896
877
897
nonlocal_stmt :
878
898
NONLOCAL names
879
899
{
880
- // FIXME
900
+ $$ = &ast.Nonlocal{StmtBase: ast.StmtBase{$<pos>$}, Names: $2 }
881
901
}
882
902
883
903
tests :
@@ -894,7 +914,15 @@ tests:
894
914
assert_stmt :
895
915
ASSERT tests
896
916
{
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
+ }
898
926
}
899
927
900
928
compound_stmt :
@@ -1363,9 +1391,7 @@ atom:
1363
1391
}
1364
1392
| ' (' yield_expr ' )'
1365
1393
{
1366
- // FIXME
1367
- panic (" yield_expr not implemented" )
1368
- $$ = nil
1394
+ $$ = $2
1369
1395
}
1370
1396
| ' (' test_or_star_expr comp_for ' )'
1371
1397
{
@@ -1768,19 +1794,13 @@ comp_if:
1768
1794
yield_expr:
1769
1795
YIELD
1770
1796
{
1771
- // FIXME
1772
- }
1773
- | YIELD yield_arg
1774
- {
1775
- // FIXME
1797
+ $$ = &ast.Yield{ExprBase: ast.ExprBase{$<pos>$}}
1776
1798
}
1777
-
1778
- yield_arg:
1779
- FROM test
1799
+ | YIELD FROM test
1780
1800
{
1781
- // FIXME
1801
+ $$ = &ast.YieldFrom{ExprBase: ast.ExprBase{$<pos>$}, Value: $3 }
1782
1802
}
1783
- | testlist
1803
+ | YIELD testlist
1784
1804
{
1785
- // FIXME
1805
+ $$ = &ast.Yield{ExprBase: ast.ExprBase{$<pos>$}, Value: $2 }
1786
1806
}
0 commit comments