Skip to content

Commit bdbd2d0

Browse files
committed
Fix parsing of Tuple within Set
1 parent 13b545c commit bdbd2d0

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

parser/grammar.y

+24-10
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (es *stmtsStack) Add(stmt ...ast.Stmt) {
9494
%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
9595
%type <op> augassign
9696
%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
97-
%type <exprs> exprlist
97+
%type <exprs> exprlist testlistraw
9898
%type <exprsStack> expr_or_star_exprs test_or_star_exprs tests test_colon_tests
9999
%type <trailers> trailers
100100
%type <cmpop> comp_op
@@ -816,17 +816,21 @@ test_nocond:
816816
lambdef:
817817
LAMBDA ':' test
818818
{
819+
// FIXME
819820
}
820821
| LAMBDA varargslist ':' test
821822
{
823+
// FIXME
822824
}
823825

824826
lambdef_nocond:
825827
LAMBDA ':' test_nocond
826828
{
829+
// FIXME
827830
}
828831
| LAMBDA varargslist ':' test_nocond
829832
{
833+
// FIXME
830834
}
831835

832836
or_test:
@@ -1089,11 +1093,13 @@ atom:
10891093
| '(' yield_expr ')'
10901094
{
10911095
// FIXME
1096+
panic("yield_expr not implemented")
10921097
$$ = nil
10931098
}
10941099
| '(' testlist_comp ')'
10951100
{
10961101
// FIXME
1102+
panic("testlist_comp not implemented")
10971103
$$ = nil
10981104
}
10991105
| '[' ']'
@@ -1103,6 +1109,7 @@ atom:
11031109
| '[' testlist_comp ']'
11041110
{
11051111
// FIXME
1112+
panic("testlist_comp not implemented")
11061113
$$ = nil
11071114
}
11081115
| '{' '}'
@@ -1156,19 +1163,23 @@ testlist_comp:
11561163
}
11571164
| test_or_star_exprs optional_comma
11581165
{
1166+
// FIXME
11591167
// $1.Pop()
11601168
}
11611169

11621170
// Trailers are half made Call, Attribute or Subscript
11631171
trailer:
11641172
'(' ')'
11651173
{
1174+
// FIXME
11661175
}
11671176
| '(' arglist ')'
11681177
{
1178+
// FIXME
11691179
}
11701180
| '[' subscriptlist ']'
11711181
{
1182+
// FIXME
11721183
}
11731184
| '.' NAME
11741185
{
@@ -1178,14 +1189,17 @@ trailer:
11781189
subscripts:
11791190
subscript
11801191
{
1192+
// FIXME
11811193
}
11821194
| subscripts ',' subscript
11831195
{
1196+
// FIXME
11841197
}
11851198

11861199
subscriptlist:
11871200
subscripts optional_comma
11881201
{
1202+
// FIXME
11891203
}
11901204

11911205
subscript:
@@ -1235,12 +1249,18 @@ testlist:
12351249
{
12361250
elts := $1.Pop()
12371251
if $2 || len(elts) > 1 {
1238-
$$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts} // FIXME Ctx
1252+
$$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts, Ctx: ast.Load}
12391253
} else {
12401254
$$ = elts[0]
12411255
}
12421256
}
12431257

1258+
testlistraw:
1259+
tests optional_comma
1260+
{
1261+
$$ = $1.Pop()
1262+
}
1263+
12441264
// (',' test ':' test)*
12451265
test_colon_tests:
12461266
test ':' test
@@ -1269,15 +1289,9 @@ dictorsetmaker:
12691289
// FIXME DictComp
12701290
$$ = &ast.DictComp{ExprBase: ast.ExprBase{$<pos>$}, Key: $1, Value: $3, Generators: $4}
12711291
}
1272-
| testlist
1292+
| testlistraw
12731293
{
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}
1294+
$$ = &ast.Set{ExprBase: ast.ExprBase{$<pos>$}, Elts: $1}
12811295
}
12821296
| test comp_for
12831297
{

parser/grammar_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package parser
22

33
import (
4+
"flag"
45
"testing"
56

67
"github.com/ncw/gpython/ast"
78
)
89

10+
var debugLevel = flag.Int("debugLevel", 0, "Debug level 0-4")
11+
912
// FIXME test pos is correct
1013

1114
func TestGrammar(t *testing.T) {
15+
SetDebug(*debugLevel)
1216
for _, test := range []struct {
1317
in string
1418
mode string
@@ -32,8 +36,12 @@ func TestGrammar(t *testing.T) {
3236
{"1234", "eval", "Expression(body=Num(n=1234))"},
3337
{"0x1234", "eval", "Expression(body=Num(n=4660))"},
3438
{"12.34", "eval", "Expression(body=Num(n=12.34))"},
39+
{"1,", "eval", "Expression(body=Tuple(elts=[Num(n=1)], ctx=Load()))"},
40+
{"1,2", "eval", "Expression(body=Tuple(elts=[Num(n=1), Num(n=2)], ctx=Load()))"},
41+
{"1,2,", "eval", "Expression(body=Tuple(elts=[Num(n=1), Num(n=2)], ctx=Load()))"},
3542
{"{ }", "eval", "Expression(body=Dict(keys=[], values=[]))"},
3643
{"{1}", "eval", "Expression(body=Set(elts=[Num(n=1)]))"},
44+
{"{1,}", "eval", "Expression(body=Set(elts=[Num(n=1)]))"},
3745
{"{1,2}", "eval", "Expression(body=Set(elts=[Num(n=1), Num(n=2)]))"},
3846
{"{1,2,3,}", "eval", "Expression(body=Set(elts=[Num(n=1), Num(n=2), Num(n=3)]))"},
3947
{"{ 'a':1 }", "eval", "Expression(body=Dict(keys=[Str(s='a')], values=[Num(n=1)]))"},

parser/make_grammar_test.py

+5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
("1234", "eval"),
2525
("0x1234", "eval"),
2626
("12.34", "eval"),
27+
("1,", "eval"),
28+
("1,2", "eval"),
29+
("1,2,", "eval"),
2730
("{ }", "eval"),
2831
("{1}", "eval"),
32+
("{1,}", "eval"),
2933
("{1,2}", "eval"),
3034
("{1,2,3,}", "eval"),
35+
# Need to implement tuple first ("{(1,2)}", "eval"),
3136
("{ 'a':1 }", "eval"),
3237
("{ 'a':1, 'b':2 }", "eval"),
3338
("{ 'a':{'aa':11, 'bb':{'aa':11, 'bb':22}}, 'b':{'aa':11, 'bb':22} }", "eval"),

0 commit comments

Comments
 (0)