@@ -83,6 +83,8 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt) {
83
83
exprsStack exprsStack
84
84
trailers []ast.Expr // list of trailer expressions
85
85
comma bool
86
+ comprehension ast.Comprehension
87
+ comprehensions []ast.Comprehension
86
88
}
87
89
88
90
%type <obj> strings
@@ -91,12 +93,13 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt) {
91
93
%type <stmtsStack> nl_or_stmt small_stmts stmts
92
94
%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
93
95
%type <op> augassign
94
- %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
96
+ %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
95
97
%type <exprs> exprlist
96
- %type <exprsStack> expr_or_star_exprs test_or_star_exprs tests
98
+ %type <exprsStack> expr_or_star_exprs test_or_star_exprs tests test_colon_tests
97
99
%type <trailers> trailers
98
100
%type <cmpop> comp_op
99
101
%type <comma> optional_comma
102
+ %type <comprehensions> comp_for
100
103
101
104
%token NEWLINE
102
105
%token ENDMARKER
@@ -1108,8 +1111,7 @@ atom:
1108
1111
}
1109
1112
| ' {' dictorsetmaker ' }'
1110
1113
{
1111
- // FIXME
1112
- $$ = nil
1114
+ $$ = $2
1113
1115
}
1114
1116
| NAME
1115
1117
{
@@ -1242,13 +1244,46 @@ testlist:
1242
1244
// (',' test ':' test)*
1243
1245
test_colon_tests:
1244
1246
test ' :' test
1247
+ {
1248
+ $$ .Push()
1249
+ $$ .Add($1 , $3 ) // key, value order
1250
+ }
1245
1251
| test_colon_tests ' ,' test ' :' test
1252
+ {
1253
+ $$ .Add($3 , $5 )
1254
+ }
1246
1255
1247
1256
dictorsetmaker:
1248
1257
test_colon_tests optional_comma
1258
+ {
1259
+ keyValues := $1 .Pop()
1260
+ d := &ast.Dict{ExprBase: ast.ExprBase{$<pos>$}, Keys: nil, Values: nil}
1261
+ for i := 0 ; i < len(keyValues)-1 ; i += 2 {
1262
+ d.Keys = append(d.Keys, keyValues[i])
1263
+ d.Values = append(d.Values, keyValues[i+1 ])
1264
+ }
1265
+ $$ = d
1266
+ }
1249
1267
| test ' :' test comp_for
1268
+ {
1269
+ // FIXME DictComp
1270
+ $$ = &ast.DictComp{ExprBase: ast.ExprBase{$<pos>$}, Key: $1 , Value: $3 , Generators: $4 }
1271
+ }
1250
1272
| testlist
1273
+ {
1274
+ var elts []ast.Expr
1275
+ if x, ok := $1 .(*ast.Tuple); ok {
1276
+ elts = x.Elts
1277
+ } else {
1278
+ elts = []ast.Expr{$1 }
1279
+ }
1280
+ $$ = &ast.Set{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts}
1281
+ }
1251
1282
| test comp_for
1283
+ {
1284
+ // FIXME SetComp
1285
+ $$ = &ast.SetComp{ExprBase: ast.ExprBase{$<pos>$}, Elt: $1 , Generators: $2 }
1286
+ }
1252
1287
1253
1288
classdef:
1254
1289
CLASS NAME optional_arglist_call ' :' suite
@@ -1282,7 +1317,15 @@ comp_iter:
1282
1317
1283
1318
comp_for:
1284
1319
FOR exprlist IN or_test
1320
+ {
1321
+ // FIXME
1322
+ $$ = nil
1323
+ }
1285
1324
| FOR exprlist IN or_test comp_iter
1325
+ {
1326
+ // FIXME
1327
+ $$ = nil
1328
+ }
1286
1329
1287
1330
comp_if:
1288
1331
IF test_nocond
0 commit comments