Skip to content

Commit

Permalink
go/parser: use correct precedence when parsing range clauses
Browse files Browse the repository at this point in the history
Fixes #2156.

R=rsc
CC=golang-dev
https://golang.org/cl/4899046
  • Loading branch information
griesemer committed Aug 16, 2011
1 parent 9801c8e commit 182cf98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/pkg/go/parser/parser.go
Expand Up @@ -1154,11 +1154,6 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
case *ast.CallExpr:
case *ast.StarExpr:
case *ast.UnaryExpr:
if t.Op == token.RANGE {
// the range operator is only allowed at the top of a for statement
p.errorExpected(x.Pos(), "expression")
x = &ast.BadExpr{x.Pos(), x.End()}
}
case *ast.BinaryExpr:
default:
// all other nodes are not proper expressions
Expand Down Expand Up @@ -1223,11 +1218,6 @@ func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
case *ast.ParenExpr:
panic("unreachable")
case *ast.UnaryExpr:
if t.Op == token.RANGE {
// the range operator is only allowed at the top of a for statement
p.errorExpected(x.Pos(), "expression")
x = &ast.BadExpr{x.Pos(), x.End()}
}
case *ast.ArrayType:
if len, isEllipsis := t.Len.(*ast.Ellipsis); isEllipsis {
p.error(len.Pos(), "expected array length, found '...'")
Expand Down Expand Up @@ -1300,7 +1290,7 @@ func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
}

switch p.tok {
case token.ADD, token.SUB, token.NOT, token.XOR, token.AND, token.RANGE:
case token.ADD, token.SUB, token.NOT, token.XOR, token.AND:
pos, op := p.pos, p.tok
p.next()
x := p.parseUnaryExpr(false)
Expand Down Expand Up @@ -1384,10 +1374,17 @@ func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt {
token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN,
token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN:
// assignment statement
// assignment statement, possibly part of a range clause
pos, tok := p.pos, p.tok
p.next()
y := p.parseRhsList()
var y []ast.Expr
if p.tok == token.RANGE && (tok == token.DEFINE || tok == token.ASSIGN) {
pos := p.pos
p.next()
y = []ast.Expr{&ast.UnaryExpr{pos, token.RANGE, p.parseRhs()}}
} else {
y = p.parseRhsList()
}
return &ast.AssignStmt{x, pos, tok, y}
}

Expand Down Expand Up @@ -1797,7 +1794,7 @@ func (p *parser) parseForStmt() ast.Stmt {
}
if rhs, isUnary := as.Rhs[0].(*ast.UnaryExpr); isUnary && rhs.Op == token.RANGE {
// rhs is range expression
// (any short variable declaration was handled by parseSimpleStat above)
// (any short variable declaration was handled by parseSimpleStmt above)
return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body}
}
p.errorExpected(s2.Pos(), "range clause")
Expand Down
1 change: 1 addition & 0 deletions src/pkg/go/parser/parser_test.go
Expand Up @@ -51,6 +51,7 @@ var validPrograms = []interface{}{
`package p; func f() { select { case x := (<-c): } };`,
`package p; func f() { if ; true {} };`,
`package p; func f() { switch ; {} };`,
`package p; func f() { for _ = range "foo" + "bar" {} };`,
}

func TestParseValidPrograms(t *testing.T) {
Expand Down

0 comments on commit 182cf98

Please sign in to comment.