diff options
Diffstat (limited to 'src/pkg/go/printer/printer.go')
| -rw-r--r-- | src/pkg/go/printer/printer.go | 55 |
1 files changed, 5 insertions, 50 deletions
diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index 40b15dd79..871fefa0c 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -17,7 +17,6 @@ import ( "tabwriter" ) - const debug = false // enable for debugging @@ -33,7 +32,6 @@ const ( unindent = whiteSpace('<') ) - var ( esc = []byte{tabwriter.Escape} htab = []byte{'\t'} @@ -42,16 +40,13 @@ var ( formfeeds = []byte("\f\f\f\f\f\f\f\f") // more than the max determined by nlines ) - // Special positions var noPos token.Position // use noPos when a position is needed but not known var infinity = 1 << 30 - // Use ignoreMultiLine if the multiLine information is not important. var ignoreMultiLine = new(bool) - // A pmode value represents the current printer mode. type pmode int @@ -60,7 +55,6 @@ const ( noExtraLinebreak ) - type printer struct { // Configuration (does not change after initialization) output io.Writer @@ -69,7 +63,6 @@ type printer struct { errors chan os.Error // Current state - nesting int // nesting level (0: top-level (package scope), >0: functions/decls.) written int // number of bytes written indent int // current indentation mode pmode // current printer mode @@ -98,7 +91,6 @@ type printer struct { nodeSizes map[ast.Node]int } - func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) { p.output = output p.Config = *cfg @@ -108,7 +100,6 @@ func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet, nodeS p.nodeSizes = nodeSizes } - func (p *printer) internalError(msg ...interface{}) { if debug { fmt.Print(p.pos.String() + ": ") @@ -117,7 +108,6 @@ func (p *printer) internalError(msg ...interface{}) { } } - // escape escapes string s by bracketing it with tabwriter.Escape. // Escaped strings pass through tabwriter unchanged. (Note that // valid Go programs cannot contain tabwriter.Escape bytes since @@ -131,26 +121,20 @@ func (p *printer) escape(s string) string { return p.litbuf.String() } - // nlines returns the adjusted number of linebreaks given the desired number -// of breaks n such that min <= result <= max where max depends on the current -// nesting level. +// of breaks n such that min <= result <= max. // func (p *printer) nlines(n, min int) int { - if n < min { + const max = 2 // max. number of newlines + switch { + case n < min: return min - } - max := 3 // max. number of newlines at the top level (p.nesting == 0) - if p.nesting > 0 { - max = 2 // max. number of newlines everywhere else - } - if n > max { + case n > max: return max } return n } - // write0 writes raw (uninterpreted) data to p.output and handles errors. // write0 does not indent after newlines, and does not HTML-escape or update p.pos. // @@ -165,7 +149,6 @@ func (p *printer) write0(data []byte) { } } - // write interprets data and writes it to p.output. It inserts indentation // after a line break unless in a tabwriter escape sequence. // It updates p.pos as a side-effect. @@ -220,7 +203,6 @@ func (p *printer) write(data []byte) { p.pos.Column += d } - func (p *printer) writeNewlines(n int, useFF bool) { if n > 0 { n = p.nlines(n, 0) @@ -232,7 +214,6 @@ func (p *printer) writeNewlines(n int, useFF bool) { } } - // writeItem writes data at position pos. data is the text corresponding to // a single lexical token, but may also be comment text. pos is the actual // (or at least very accurately estimated) position of the data in the original @@ -261,7 +242,6 @@ func (p *printer) writeItem(pos token.Position, data string) { p.last = p.pos } - // writeCommentPrefix writes the whitespace before a comment. // If there is any pending whitespace, it consumes as much of // it as is likely to help position the comment nicely. @@ -368,7 +348,6 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment } } - // TODO(gri): It should be possible to convert the code below from using // []byte to string and in the process eliminate some conversions. @@ -398,7 +377,6 @@ func split(text []byte) [][]byte { return lines } - func isBlank(s []byte) bool { for _, b := range s { if b > ' ' { @@ -408,7 +386,6 @@ func isBlank(s []byte) bool { return true } - func commonPrefix(a, b []byte) []byte { i := 0 for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') { @@ -417,7 +394,6 @@ func commonPrefix(a, b []byte) []byte { return a[0:i] } - func stripCommonPrefix(lines [][]byte) { if len(lines) < 2 { return // at most one line - nothing to do @@ -545,7 +521,6 @@ func stripCommonPrefix(lines [][]byte) { } } - func (p *printer) writeComment(comment *ast.Comment) { text := comment.Text @@ -575,7 +550,6 @@ func (p *printer) writeComment(comment *ast.Comment) { } } - // writeCommentSuffix writes a line break after a comment if indicated // and processes any leftover indentation information. If a line break // is needed, the kind of break (newline vs formfeed) depends on the @@ -613,7 +587,6 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) { return } - // intersperseComments consumes all comments that appear before the next token // tok and prints it together with the buffered whitespace (i.e., the whitespace // that needs to be written before the next token). A heuristic is used to mix @@ -651,7 +624,6 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (dro return false } - // whiteWhitespace writes the first n whitespace entries. func (p *printer) writeWhitespace(n int) { // write entries @@ -701,7 +673,6 @@ func (p *printer) writeWhitespace(n int) { p.wsbuf = p.wsbuf[0:i] } - // ---------------------------------------------------------------------------- // Printing interface @@ -724,7 +695,6 @@ func mayCombine(prev token.Token, next byte) (b bool) { return } - // print prints a list of "items" (roughly corresponding to syntactic // tokens, but also including whitespace and formatting information). // It is the only print function that should be called directly from @@ -812,7 +782,6 @@ func (p *printer) print(args ...interface{}) { } } - // commentBefore returns true iff the current comment occurs // before the next position in the source code. // @@ -820,7 +789,6 @@ func (p *printer) commentBefore(next token.Position) bool { return p.cindex < len(p.comments) && p.fset.Position(p.comments[p.cindex].List[0].Pos()).Offset < next.Offset } - // Flush prints any pending comments and whitespace occurring // textually before the position of the next token tok. Flush // returns true if a pending formfeed character was dropped @@ -838,7 +806,6 @@ func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) { return } - // ---------------------------------------------------------------------------- // Trimmer @@ -854,7 +821,6 @@ type trimmer struct { space bytes.Buffer } - // trimmer is implemented as a state machine. // It can be in one of the following states: const ( @@ -863,7 +829,6 @@ const ( inText // inside text ) - // Design note: It is tempting to eliminate extra blanks occurring in // whitespace in this function as it could simplify some // of the blanks logic in the node printing functions. @@ -941,7 +906,6 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) { return } - // ---------------------------------------------------------------------------- // Public interface @@ -952,14 +916,12 @@ const ( UseSpaces // use spaces instead of tabs for alignment ) - // A Config node controls the output of Fprint. type Config struct { Mode uint // default: 0 Tabwidth int // default: 8 } - // fprint implements Fprint and takes a nodesSizes map for setting up the printer state. func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (int, os.Error) { // redirect output through a trimmer to eliminate trailing whitespace @@ -994,11 +956,9 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{ go func() { switch n := node.(type) { case ast.Expr: - p.nesting = 1 p.useNodeComments = true p.expr(n, ignoreMultiLine) case ast.Stmt: - p.nesting = 1 p.useNodeComments = true // A labeled statement will un-indent to position the // label. Set indent to 1 so we don't get indent "underflow". @@ -1007,15 +967,12 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{ } p.stmt(n, false, ignoreMultiLine) case ast.Decl: - p.nesting = 1 p.useNodeComments = true p.decl(n, ignoreMultiLine) case ast.Spec: - p.nesting = 1 p.useNodeComments = true p.spec(n, 1, false, ignoreMultiLine) case *ast.File: - p.nesting = 0 p.comments = n.Comments p.useNodeComments = n.Comments == nil p.file(n) @@ -1036,7 +993,6 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{ return p.written, err } - // Fprint "pretty-prints" an AST node to output and returns the number // of bytes written and an error (if any) for a given configuration cfg. // Position information is interpreted relative to the file set fset. @@ -1047,7 +1003,6 @@ func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{ return cfg.fprint(output, fset, node, make(map[ast.Node]int)) } - // Fprint "pretty-prints" an AST node to output. // It calls Config.Fprint with default settings. // |
