diff options
author | Robert Griesemer <gri@golang.org> | 2010-02-24 16:17:11 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-02-24 16:17:11 -0800 |
commit | 7d9ed9a739596cdbc82850e2a2ac6fa3a74db65d (patch) | |
tree | 914f933c798081034a78b3a18fdbe51cea69b6dc /src/pkg/go/printer | |
parent | d5b3aa68ecdd12c7a45b2d640ea453937a234873 (diff) | |
download | golang-7d9ed9a739596cdbc82850e2a2ac6fa3a74db65d.tar.gz |
go/ast: streamline representation of field lists
- always include position information about opening/closing parens/braces
- replace uses of []*ast.Field with *ast.FieldList
Fixes issue 473.
R=rsc
CC=golang-dev
http://codereview.appspot.com/223043
Diffstat (limited to 'src/pkg/go/printer')
-rw-r--r-- | src/pkg/go/printer/nodes.go | 41 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/comments.golden | 8 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/comments.input | 8 |
3 files changed, 36 insertions, 21 deletions
diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 6096751bd..f546f3f2a 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -218,10 +218,10 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode // Sets multiLine to true if the the parameter list spans multiple lines. -func (p *printer) parameters(list []*ast.Field, multiLine *bool) { - p.print(token.LPAREN) - if len(list) > 0 { - for i, par := range list { +func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) { + p.print(fields.Opening, token.LPAREN) + if len(fields.List) > 0 { + for i, par := range fields.List { if i > 0 { p.print(token.COMMA, blank) } @@ -232,18 +232,19 @@ func (p *printer) parameters(list []*ast.Field, multiLine *bool) { p.expr(par.Type, multiLine) } } - p.print(token.RPAREN) + p.print(fields.Closing, token.RPAREN) } // Sets multiLine to true if the signature spans multiple lines. -func (p *printer) signature(params, result []*ast.Field, multiLine *bool) { +func (p *printer) signature(params, result *ast.FieldList, multiLine *bool) { p.parameters(params, multiLine) - if result != nil { + n := result.NumFields() + if n > 0 { p.print(blank) - if len(result) == 1 && result[0].Names == nil { + if n == 1 && result.List[0].Names == nil { // single anonymous result; no ()'s - p.expr(result[0].Type, multiLine) + p.expr(result.List[0].Type, multiLine) return } p.parameters(result, multiLine) @@ -289,7 +290,11 @@ func (p *printer) setLineComment(text string) { } -func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace token.Position, isIncomplete bool, ctxt exprContext) { +func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprContext) { + lbrace := fields.Opening + list := fields.List + rbrace := fields.Closing + if !isIncomplete && !p.commentBefore(rbrace) { // possibly a one-line struct/interface if len(list) == 0 { @@ -711,7 +716,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi case *ast.StructType: p.print(token.STRUCT) - p.fieldList(x.Lbrace, x.Fields, x.Rbrace, x.Incomplete, ctxt|structType) + p.fieldList(x.Fields, x.Incomplete, ctxt|structType) case *ast.FuncType: p.print(token.FUNC) @@ -719,7 +724,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi case *ast.InterfaceType: p.print(token.INTERFACE) - p.fieldList(x.Lbrace, x.Methods, x.Rbrace, x.Incomplete, ctxt) + p.fieldList(x.Methods, x.Incomplete, ctxt) case *ast.MapType: p.print(token.MAP, token.LBRACK) @@ -1209,15 +1214,9 @@ func distance(from, to token.Position) int { func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) { p.setComment(d.Doc) p.print(d.Pos(), token.FUNC, blank) - if recv := d.Recv; recv != nil { - // method: print receiver - p.print(token.LPAREN) - if len(recv.Names) > 0 { - p.expr(recv.Names[0], multiLine) - p.print(blank) - } - p.expr(recv.Type, multiLine) - p.print(token.RPAREN, blank) + if d.Recv != nil { + p.parameters(d.Recv, multiLine) // method: print receiver + p.print(blank) } p.expr(d.Name, multiLine) p.signature(d.Type.Params, d.Type.Results, multiLine) diff --git a/src/pkg/go/printer/testdata/comments.golden b/src/pkg/go/printer/testdata/comments.golden index 2d4f43444..4242688f5 100644 --- a/src/pkg/go/printer/testdata/comments.golden +++ b/src/pkg/go/printer/testdata/comments.golden @@ -381,6 +381,14 @@ func _() { // Some interesting interspersed comments func _( /* this */ x /* is */ /* an */ int) {} +func _( /* no params */ ) {} + +func _() { + f( /* no args */ ) +} + +func ( /* comment1 */ T /* comment2 */ ) _() {} + // Line comments with tabs func _() { diff --git a/src/pkg/go/printer/testdata/comments.input b/src/pkg/go/printer/testdata/comments.input index eec88bf95..427065a8f 100644 --- a/src/pkg/go/printer/testdata/comments.input +++ b/src/pkg/go/printer/testdata/comments.input @@ -382,6 +382,14 @@ func _() { func _(/* this */x/* is *//* an */ int) { } +func _(/* no params */) {} + +func _() { + f(/* no args */) +} + +func (/* comment1 */ T /* comment2 */) _() {} + // Line comments with tabs func _() { |