LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The printer package implements printing of AST nodes. | 5 // The printer package implements printing of AST nodes. |
6 package printer | 6 package printer |
7 | 7 |
8 import ( | 8 import ( |
9 "bytes" | 9 "bytes" |
10 "fmt" | 10 "fmt" |
(...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 // setup printer and print node | 1002 // setup printer and print node |
1003 var p printer | 1003 var p printer |
1004 p.init(output, cfg) | 1004 p.init(output, cfg) |
1005 go func() { | 1005 go func() { |
1006 switch n := node.(type) { | 1006 switch n := node.(type) { |
1007 case ast.Expr: | 1007 case ast.Expr: |
1008 p.useNodeComments = true | 1008 p.useNodeComments = true |
1009 p.expr(n, ignoreMultiLine) | 1009 p.expr(n, ignoreMultiLine) |
1010 case ast.Stmt: | 1010 case ast.Stmt: |
1011 p.useNodeComments = true | 1011 p.useNodeComments = true |
| 1012 // A labeled statement will un-indent to position the |
| 1013 // label. Set indent to 1 so we don't get indent "underf
low". |
| 1014 if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt { |
| 1015 p.indent = 1 |
| 1016 } |
1012 p.stmt(n, ignoreMultiLine) | 1017 p.stmt(n, ignoreMultiLine) |
1013 case ast.Decl: | 1018 case ast.Decl: |
1014 p.useNodeComments = true | 1019 p.useNodeComments = true |
1015 p.decl(n, atTop, ignoreMultiLine) | 1020 p.decl(n, atTop, ignoreMultiLine) |
1016 case *ast.File: | 1021 case *ast.File: |
1017 p.comments = n.Comments | 1022 p.comments = n.Comments |
1018 p.useNodeComments = n.Comments == nil | 1023 p.useNodeComments = n.Comments == nil |
1019 p.file(n) | 1024 p.file(n) |
1020 default: | 1025 default: |
1021 p.errors <- os.NewError(fmt.Sprintf("printer.Fprint: uns
upported node type %T", n)) | 1026 p.errors <- os.NewError(fmt.Sprintf("printer.Fprint: uns
upported node type %T", n)) |
1022 runtime.Goexit() | 1027 runtime.Goexit() |
1023 } | 1028 } |
1024 » » p.flush(infinity, false) // flush to "infinity" | 1029 » » p.flush(infinity, false) |
1025 » » p.errors <- nil // no errors | 1030 » » p.errors <- nil // no errors |
1026 }() | 1031 }() |
1027 err := <-p.errors // wait for completion of goroutine | 1032 err := <-p.errors // wait for completion of goroutine |
1028 | 1033 |
1029 // flush tabwriter, if any | 1034 // flush tabwriter, if any |
1030 if tw != nil { | 1035 if tw != nil { |
1031 tw.Flush() // ignore errors | 1036 tw.Flush() // ignore errors |
1032 } | 1037 } |
1033 | 1038 |
1034 return p.written, err | 1039 return p.written, err |
1035 } | 1040 } |
1036 | 1041 |
1037 | 1042 |
1038 // Fprint "pretty-prints" an AST node to output. | 1043 // Fprint "pretty-prints" an AST node to output. |
1039 // It calls Config.Fprint with default settings. | 1044 // It calls Config.Fprint with default settings. |
1040 // | 1045 // |
1041 func Fprint(output io.Writer, node interface{}) os.Error { | 1046 func Fprint(output io.Writer, node interface{}) os.Error { |
1042 _, err := (&Config{Tabwidth: 8}).Fprint(output, node) // don't care abou
t number of bytes written | 1047 _, err := (&Config{Tabwidth: 8}).Fprint(output, node) // don't care abou
t number of bytes written |
1043 return err | 1048 return err |
1044 } | 1049 } |
LEFT | RIGHT |