gofmt: permit omission of first index in slice expression

R=rsc
CC=golang-dev, r
https://golang.org/cl/2053041
This commit is contained in:
Robert Griesemer 2010-08-27 14:49:49 -07:00
parent 9686ab2da9
commit 8935c8489a
6 changed files with 21 additions and 4 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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)

View file

@ -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:]

View file

@ -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 :]

View file

@ -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:]