Skip to content

Commit b685896

Browse files
committed
Simple statements
1 parent b3fe9bd commit b685896

File tree

5 files changed

+163
-62
lines changed

5 files changed

+163
-62
lines changed

ast/ast.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ type Assert struct {
363363

364364
type Import struct {
365365
StmtBase
366-
Names []Alias
366+
Names []*Alias
367367
}
368368

369369
type ImportFrom struct {
370370
StmtBase
371371
Module Identifier
372-
Names []Alias
372+
Names []*Alias
373373
Level int
374374
}
375375

@@ -673,7 +673,7 @@ type Keyword struct {
673673
type Alias struct {
674674
Pos
675675
Name Identifier
676-
Asname Identifier
676+
AsName *Identifier
677677
}
678678

679679
type WithItem struct {

ast/dump.go

+7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ func dumpItem(v interface{}) string {
1919
return fmt.Sprintf("b'%s'", string(x))
2020
case Identifier:
2121
return fmt.Sprintf("'%s'", string(x))
22+
case *Identifier:
23+
if x == nil {
24+
return "None"
25+
}
26+
return fmt.Sprintf("'%s'", string(*x))
2227
case *Keyword:
2328
return dump(x, "keyword")
2429
case ModBase:
2530
case StmtBase:
2631
case ExprBase:
2732
case SliceBase:
2833
case Pos:
34+
case *Alias:
35+
return dump(v, "alias")
2936
case Ast:
3037
return Dump(x)
3138
case py.I__str__:

parser/grammar.y

+76-56
Original file line numberDiff line numberDiff line change
@@ -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:
664673
del_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:
698710
break_stmt:
699711
BREAK
700712
{
701-
// FIXME
713+
$$ = &ast.Break{StmtBase: ast.StmtBase{$<pos>$}}
702714
}
703715

704716
continue_stmt:
705717
CONTINUE
706718
{
707-
// FIXME
719+
$$ = &ast.Continue{StmtBase: ast.StmtBase{$<pos>$}}
708720
}
709721

710722
return_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

720732
yield_stmt:
721733
yield_expr
722734
{
723-
// FIXME
735+
$$ = &ast.ExprStmt{StmtBase: ast.StmtBase{$<pos>$}, Value: $1}
724736
}
725737

726738
raise_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

740752
import_stmt:
741753
import_name
742754
{
743-
// FIXME
755+
$$ = $1
744756
}
745757
| import_from
746758
{
747-
// FIXME
759+
$$ = $1
748760
}
749761

750762
import_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
757769
dot:
758770
'.'
759771
{
760-
// FIXME
772+
$$ = 1
761773
}
762774
| ELIPSIS
763775
{
764-
// FIXME
776+
$$ = 3
765777
}
766778

767779
dots:
768780
dot
769781
{
770-
// FIXME
782+
$$ = $1
771783
}
772784
| dots dot
773785
{
774-
// FIXME
786+
$$ += $2
775787
}
776788

777789
from_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

791806
import_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

805820
import_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

811826
import_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

821837
dotted_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

831848
import_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

841859
dotted_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

851870
dotted_name:
852871
NAME
853872
{
854-
// FIXME
873+
$$ = $1
855874
}
856875
| dotted_name '.' NAME
857876
{
858-
// FIXME
877+
$$ += "." + $3
859878
}
860879

861880
names:
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

871891
global_stmt:
872892
GLOBAL names
873893
{
874-
// FIXME
894+
$$ = &ast.Global{StmtBase: ast.StmtBase{$<pos>$}, Names: $2}
875895
}
876896

877897
nonlocal_stmt:
878898
NONLOCAL names
879899
{
880-
// FIXME
900+
$$ = &ast.Nonlocal{StmtBase: ast.StmtBase{$<pos>$}, Names: $2}
881901
}
882902

883903
tests:
@@ -894,7 +914,15 @@ tests:
894914
assert_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

900928
compound_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:
17681794
yield_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

Comments
 (0)