@@ -17,8 +17,6 @@ import (
17
17
18
18
// FIXME can put code blocks in not just at the end - help with list initialisation
19
19
20
- // FIXME testlist should be a single expression according to the grammar
21
-
22
20
// A stack of []ast.Expr
23
21
type exprsStack [][]ast.Expr
24
22
@@ -80,6 +78,7 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt) {
80
78
exprs []ast.Expr
81
79
exprsStack exprsStack
82
80
trailers []ast.Expr // list of trailer expressions
81
+ comma bool
83
82
}
84
83
85
84
%type <str> strings
@@ -88,11 +87,12 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt) {
88
87
%type <stmtsStack> nl_or_stmt small_stmts stmts
89
88
%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
90
89
%type <op> augassign
91
- %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
92
- %type <exprs> testlist testlist_star_expr yield_expr_or_testlist yield_expr yield_expr_or_testlist_star_expr exprlist
90
+ %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
91
+ %type <exprs> exprlist
93
92
%type <exprsStack> expr_or_star_exprs test_or_star_exprs tests
94
93
%type <trailers> trailers
95
94
%type <cmpop> comp_op
95
+ %type <comma> optional_comma
96
96
97
97
%token NEWLINE
98
98
%token ENDMARKER
@@ -239,16 +239,7 @@ nl_or_stmt:
239
239
eval_input :
240
240
testlist nls ENDMARKER
241
241
{
242
- // FIXME testlist should be a single expression
243
- e := &ast.Expression{ModBase: ast.ModBase{$<pos>$}}
244
- $$ = e
245
- if len ($1 ) > 1 {
246
- e.Body = $1 [0 ]
247
- }
248
- if len ($1 ) > 2 {
249
- panic (" FIXME eval_input with more than one testlist" )
250
- }
251
-
242
+ $$ = &ast.Expression{ModBase: ast.ModBase{$<pos>$}, Body: $1 }
252
243
}
253
244
254
245
// NEWLINE *
@@ -430,8 +421,7 @@ expr_stmt:
430
421
}
431
422
| testlist_star_expr
432
423
{
433
- // FIXME should testlist really be a single Expr?
434
- $$ = &ast.ExprStmt{StmtBase: ast.StmtBase{$<pos>$}, Value: $1 [0 ]}
424
+ $$ = &ast.ExprStmt{StmtBase: ast.StmtBase{$<pos>$}, Value: $1 }
435
425
}
436
426
437
427
yield_expr_or_testlist :
@@ -483,12 +473,24 @@ test_or_star_expr:
483
473
$$ = $1
484
474
}
485
475
486
- optional_comma : | ' ,'
476
+ optional_comma :
477
+ {
478
+ $$ = false
479
+ }
480
+ | ' ,'
481
+ {
482
+ $$ = true
483
+ }
487
484
488
485
testlist_star_expr :
489
486
test_or_star_exprs optional_comma
490
487
{
491
- $$ = $1 .Pop()
488
+ elts := $1 .Pop()
489
+ if $2 || len(elts) > 1 {
490
+ $$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts} // FIXME Ctx
491
+ } else {
492
+ $$ = elts[0 ]
493
+ }
492
494
}
493
495
494
496
augassign :
@@ -1057,7 +1059,7 @@ strings:
1057
1059
atom :
1058
1060
' (' ' )'
1059
1061
{
1060
- $$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}}
1062
+ $$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}} // FIXME Ctx
1061
1063
}
1062
1064
| ' (' yield_expr ' )'
1063
1065
{
@@ -1206,7 +1208,12 @@ exprlist:
1206
1208
testlist :
1207
1209
tests optional_comma
1208
1210
{
1209
- $$ = $1 .Pop()
1211
+ elts := $1 .Pop()
1212
+ if $2 || len(elts) > 1 {
1213
+ $$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts} // FIXME Ctx
1214
+ } else {
1215
+ $$ = elts[0 ]
1216
+ }
1210
1217
}
1211
1218
1212
1219
// (' ,' test ' :' test )*
0 commit comments