Commit 8935c848 authored by Robert Griesemer's avatar Robert Griesemer

gofmt: permit omission of first index in slice expression

R=rsc
CC=golang-dev, r
https://golang.org/cl/2053041
parent 9686ab2d
......@@ -196,7 +196,7 @@ type (
// An SliceExpr node represents an expression followed by slice indices.
SliceExpr struct {
X Expr // expression
Index Expr // beginning of slice range
Index Expr // beginning of slice range; or nil
End Expr // end of slice range; or nil
}
......
......@@ -913,7 +913,10 @@ func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr {
p.expect(token.LBRACK)
p.exprLev++
index := p.parseExpr()
var index ast.Expr
if p.tok != token.COLON {
index = p.parseExpr()
}
if p.tok == token.COLON {
p.next()
var end ast.Expr
......
......@@ -826,9 +826,11 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
// TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1, 0, multiLine)
p.print(token.LBRACK)
p.expr0(x.Index, depth+1, multiLine)
if x.Index != nil {
p.expr0(x.Index, depth+1, multiLine)
}
// blanks around ":" if both sides exist and either side is a binary expression
if depth <= 1 && x.End != nil && (isBinary(x.Index) || isBinary(x.End)) {
if depth <= 1 && x.Index != nil && x.End != nil && (isBinary(x.Index) || isBinary(x.End)) {
p.print(blank, token.COLON, blank)
} else {
p.print(token.COLON)
......
......@@ -31,6 +31,9 @@ func _() {
_ = 1 + a
_ = a + 1
_ = a + b + 1
_ = s[a]
_ = s[a:]
_ = s[:b]
_ = s[1:2]
_ = s[a:b]
_ = s[0:len(s)]
......@@ -56,6 +59,7 @@ func _() {
_ = s[a : b-c]
_ = s[0:]
_ = s[a+b]
_ = s[:b-c]
_ = s[a+b:]
_ = a[a<<b+1]
_ = a[a<<b+1:]
......
......@@ -31,6 +31,9 @@ func _() {
_ = 1+a
_ = a+1
_ = a+b+1
_ = s[a]
_ = s[a:]
_ = s[:b]
_ = s[1:2]
_ = s[a:b]
_ = s[0:len(s)]
......@@ -56,6 +59,7 @@ func _() {
_ = s[a : b-c]
_ = s[0:]
_ = s[a+b]
_ = s[: b-c]
_ = s[a+b :]
_ = a[a<<b+1]
_ = a[a<<b+1 :]
......
......@@ -31,6 +31,9 @@ func _() {
_ = 1 + a
_ = a + 1
_ = a + b + 1
_ = s[a]
_ = s[a:]
_ = s[:b]
_ = s[1:2]
_ = s[a:b]
_ = s[0:len(s)]
......@@ -56,6 +59,7 @@ func _() {
_ = s[a : b-c]
_ = s[0:]
_ = s[a+b]
_ = s[:b-c]
_ = s[a+b:]
_ = a[a<<b+1]
_ = a[a<<b+1:]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment