Skip to content

Commit 13b545c

Browse files
committed
Make simple set and dict literals work
1 parent debd25b commit 13b545c

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

‎parser/grammar.y‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt){
8383
exprsStack exprsStack
8484
trailers []ast.Expr// list of trailer expressions
8585
comma bool
86+
comprehension ast.Comprehension
87+
comprehensions []ast.Comprehension
8688
}
8789

8890
%type<obj>strings
@@ -91,12 +93,13 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt){
9193
%type<stmtsStack>nl_or_stmtsmall_stmtsstmts
9294
%type<stmt>compound_stmtsmall_stmtexpr_stmtdel_stmtpass_stmtflow_stmtimport_stmtglobal_stmtnonlocal_stmtassert_stmtbreak_stmtcontinue_stmtreturn_stmtraise_stmtyield_stmt
9395
%type<op>augassign
94-
%type<expr>expr_or_star_exprexprstar_exprxor_exprand_exprshift_exprarith_exprtermfactorpowertraileratomtest_or_star_exprtestnot_testlambdeftest_nocondlambdef_nocondor_testand_testcomparisontestlisttestlist_star_expryield_expr_or_testlistyield_expryield_expr_or_testlist_star_expr
96+
%type<expr>expr_or_star_exprexprstar_exprxor_exprand_exprshift_exprarith_exprtermfactorpowertraileratomtest_or_star_exprtestnot_testlambdeftest_nocondlambdef_nocondor_testand_testcomparisontestlisttestlist_star_expryield_expr_or_testlistyield_expryield_expr_or_testlist_star_exprdictorsetmaker
9597
%type<exprs>exprlist
96-
%type<exprsStack>expr_or_star_exprstest_or_star_exprstests
98+
%type<exprsStack>expr_or_star_exprstest_or_star_exprsteststest_colon_tests
9799
%type<trailers>trailers
98100
%type<cmpop>comp_op
99101
%type<comma>optional_comma
102+
%type<comprehensions>comp_for
100103

101104
%tokenNEWLINE
102105
%tokenENDMARKER
@@ -1108,8 +1111,7 @@ atom:
11081111
}
11091112
|'{'dictorsetmaker'}'
11101113
{
1111-
// FIXME
1112-
$$ = nil
1114+
$$ = $2
11131115
}
11141116
|NAME
11151117
{
@@ -1242,13 +1244,46 @@ testlist:
12421244
// (',' test ':' test)*
12431245
test_colon_tests:
12441246
test ':' test
1247+
{
1248+
$$.Push()
1249+
$$.Add($1, $3) // key, value order
1250+
}
12451251
| test_colon_tests ',' test ':' test
1252+
{
1253+
$$.Add($3, $5)
1254+
}
12461255

12471256
dictorsetmaker:
12481257
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+
}
12491267
| test ':' test comp_for
1268+
{
1269+
// FIXME DictComp
1270+
$$ = &ast.DictComp{ExprBase: ast.ExprBase{$<pos>$}, Key: $1, Value: $3, Generators: $4}
1271+
}
12501272
| 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+
}
12511282
| test comp_for
1283+
{
1284+
// FIXME SetComp
1285+
$$ = &ast.SetComp{ExprBase: ast.ExprBase{$<pos>$}, Elt: $1, Generators: $2}
1286+
}
12521287

12531288
classdef:
12541289
CLASS NAME optional_arglist_call ':' suite
@@ -1282,7 +1317,15 @@ comp_iter:
12821317

12831318
comp_for:
12841319
FOR exprlist IN or_test
1320+
{
1321+
// FIXME
1322+
$$ = nil
1323+
}
12851324
| FOR exprlist IN or_test comp_iter
1325+
{
1326+
// FIXME
1327+
$$ = nil
1328+
}
12861329

12871330
comp_if:
12881331
IF test_nocond

‎parser/grammar_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ func TestGrammar(t *testing.T){
3333
{"0x1234", "eval", "Expression(body=Num(n=4660))"},
3434
{"12.34", "eval", "Expression(body=Num(n=12.34))"},
3535
{"{}", "eval", "Expression(body=Dict(keys=[], values=[]))"},
36+
{"{1}", "eval", "Expression(body=Set(elts=[Num(n=1)]))"},
37+
{"{1,2}", "eval", "Expression(body=Set(elts=[Num(n=1), Num(n=2)]))"},
38+
{"{1,2,3,}", "eval", "Expression(body=Set(elts=[Num(n=1), Num(n=2), Num(n=3)]))"},
39+
{"{'a':1 }", "eval", "Expression(body=Dict(keys=[Str(s='a')], values=[Num(n=1)]))"},
40+
{"{'a':1, 'b':2 }", "eval", "Expression(body=Dict(keys=[Str(s='a'), Str(s='b')], values=[Num(n=1), Num(n=2)]))"},
41+
{"{'a':{'aa':11, 'bb':{'aa':11, 'bb':22}}, 'b':{'aa':11, 'bb':22} }", "eval", "Expression(body=Dict(keys=[Str(s='a'), Str(s='b')], values=[Dict(keys=[Str(s='aa'), Str(s='bb')], values=[Num(n=11), Dict(keys=[Str(s='aa'), Str(s='bb')], values=[Num(n=11), Num(n=22)])]), Dict(keys=[Str(s='aa'), Str(s='bb')], values=[Num(n=11), Num(n=22)])]))"},
3642
// END TESTS
3743
}{
3844
Ast, err:=ParseString(test.in, test.mode)

‎parser/make_grammar_test.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
("0x1234", "eval"),
2626
("12.34", "eval"),
2727
("{}", "eval"),
28+
("{1}", "eval"),
29+
("{1,2}", "eval"),
30+
("{1,2,3,}", "eval"),
31+
("{'a':1 }", "eval"),
32+
("{'a':1, 'b':2 }", "eval"),
33+
("{'a':{'aa':11, 'bb':{'aa':11, 'bb':22}}, 'b':{'aa':11, 'bb':22} }", "eval"),
2834
]
2935

3036
defdump(source, mode):

0 commit comments

Comments
(0)