| 1 | 
         747 | 
         jeremybenn | 
         // Copyright 2009 The Go Authors. All rights reserved.
  | 
      
      
         | 2 | 
          | 
          | 
         // Use of this source code is governed by a BSD-style
  | 
      
      
         | 3 | 
          | 
          | 
         // license that can be found in the LICENSE file.
  | 
      
      
         | 4 | 
          | 
          | 
          
  | 
      
      
         | 5 | 
          | 
          | 
         // Package printer implements printing of AST nodes.
  | 
      
      
         | 6 | 
          | 
          | 
         package printer
  | 
      
      
         | 7 | 
          | 
          | 
          
  | 
      
      
         | 8 | 
          | 
          | 
         import (
  | 
      
      
         | 9 | 
          | 
          | 
                 "bytes"
  | 
      
      
         | 10 | 
          | 
          | 
                 "fmt"
  | 
      
      
         | 11 | 
          | 
          | 
                 "go/ast"
  | 
      
      
         | 12 | 
          | 
          | 
                 "go/token"
  | 
      
      
         | 13 | 
          | 
          | 
                 "io"
  | 
      
      
         | 14 | 
          | 
          | 
                 "os"
  | 
      
      
         | 15 | 
          | 
          | 
                 "strconv"
  | 
      
      
         | 16 | 
          | 
          | 
                 "strings"
  | 
      
      
         | 17 | 
          | 
          | 
                 "text/tabwriter"
  | 
      
      
         | 18 | 
          | 
          | 
         )
  | 
      
      
         | 19 | 
          | 
          | 
          
  | 
      
      
         | 20 | 
          | 
          | 
         const (
  | 
      
      
         | 21 | 
          | 
          | 
                 maxNewlines = 2     // max. number of newlines between source text
  | 
      
      
         | 22 | 
          | 
          | 
                 debug       = false // enable for debugging
  | 
      
      
         | 23 | 
          | 
          | 
                 infinity    = 1 << 30
  | 
      
      
         | 24 | 
          | 
          | 
         )
  | 
      
      
         | 25 | 
          | 
          | 
          
  | 
      
      
         | 26 | 
          | 
          | 
         type whiteSpace byte
  | 
      
      
         | 27 | 
          | 
          | 
          
  | 
      
      
         | 28 | 
          | 
          | 
         const (
  | 
      
      
         | 29 | 
          | 
          | 
                 ignore   = whiteSpace(0)
  | 
      
      
         | 30 | 
          | 
          | 
                 blank    = whiteSpace(' ')
  | 
      
      
         | 31 | 
          | 
          | 
                 vtab     = whiteSpace('\v')
  | 
      
      
         | 32 | 
          | 
          | 
                 newline  = whiteSpace('\n')
  | 
      
      
         | 33 | 
          | 
          | 
                 formfeed = whiteSpace('\f')
  | 
      
      
         | 34 | 
          | 
          | 
                 indent   = whiteSpace('>')
  | 
      
      
         | 35 | 
          | 
          | 
                 unindent = whiteSpace('<')
  | 
      
      
         | 36 | 
          | 
          | 
         )
  | 
      
      
         | 37 | 
          | 
          | 
          
  | 
      
      
         | 38 | 
          | 
          | 
         // Use ignoreMultiLine if the multiLine information is not important.
  | 
      
      
         | 39 | 
          | 
          | 
         var ignoreMultiLine = new(bool)
  | 
      
      
         | 40 | 
          | 
          | 
          
  | 
      
      
         | 41 | 
          | 
          | 
         // A pmode value represents the current printer mode.
  | 
      
      
         | 42 | 
          | 
          | 
         type pmode int
  | 
      
      
         | 43 | 
          | 
          | 
          
  | 
      
      
         | 44 | 
          | 
          | 
         const (
  | 
      
      
         | 45 | 
          | 
          | 
                 noExtraLinebreak pmode = 1 << iota
  | 
      
      
         | 46 | 
          | 
          | 
         )
  | 
      
      
         | 47 | 
          | 
          | 
          
  | 
      
      
         | 48 | 
          | 
          | 
         type printer struct {
  | 
      
      
         | 49 | 
          | 
          | 
                 // Configuration (does not change after initialization)
  | 
      
      
         | 50 | 
          | 
          | 
                 Config
  | 
      
      
         | 51 | 
          | 
          | 
                 fset *token.FileSet
  | 
      
      
         | 52 | 
          | 
          | 
          
  | 
      
      
         | 53 | 
          | 
          | 
                 // Current state
  | 
      
      
         | 54 | 
          | 
          | 
                 output      bytes.Buffer // raw printer result
  | 
      
      
         | 55 | 
          | 
          | 
                 indent      int          // current indentation
  | 
      
      
         | 56 | 
          | 
          | 
                 mode        pmode        // current printer mode
  | 
      
      
         | 57 | 
          | 
          | 
                 impliedSemi bool         // if set, a linebreak implies a semicolon
  | 
      
      
         | 58 | 
          | 
          | 
                 lastTok     token.Token  // the last token printed (token.ILLEGAL if it's whitespace)
  | 
      
      
         | 59 | 
          | 
          | 
                 wsbuf       []whiteSpace // delayed white space
  | 
      
      
         | 60 | 
          | 
          | 
          
  | 
      
      
         | 61 | 
          | 
          | 
                 // The (possibly estimated) position in the generated output;
  | 
      
      
         | 62 | 
          | 
          | 
                 // in AST space (i.e., pos is set whenever a token position is
  | 
      
      
         | 63 | 
          | 
          | 
                 // known accurately, and updated dependending on what has been
  | 
      
      
         | 64 | 
          | 
          | 
                 // written).
  | 
      
      
         | 65 | 
          | 
          | 
                 pos token.Position
  | 
      
      
         | 66 | 
          | 
          | 
          
  | 
      
      
         | 67 | 
          | 
          | 
                 // The value of pos immediately after the last item has been
  | 
      
      
         | 68 | 
          | 
          | 
                 // written using writeItem.
  | 
      
      
         | 69 | 
          | 
          | 
                 last token.Position
  | 
      
      
         | 70 | 
          | 
          | 
          
  | 
      
      
         | 71 | 
          | 
          | 
                 // The list of all source comments, in order of appearance.
  | 
      
      
         | 72 | 
          | 
          | 
                 comments        []*ast.CommentGroup // may be nil
  | 
      
      
         | 73 | 
          | 
          | 
                 cindex          int                 // current comment index
  | 
      
      
         | 74 | 
          | 
          | 
                 useNodeComments bool                // if not set, ignore lead and line comments of nodes
  | 
      
      
         | 75 | 
          | 
          | 
          
  | 
      
      
         | 76 | 
          | 
          | 
                 // Information about p.comments[p.cindex]; set up by nextComment.
  | 
      
      
         | 77 | 
          | 
          | 
                 comment        *ast.CommentGroup // = p.comments[p.cindex]; or nil
  | 
      
      
         | 78 | 
          | 
          | 
                 commentOffset  int               // = p.posFor(p.comments[p.cindex].List[0].Pos()).Offset; or infinity
  | 
      
      
         | 79 | 
          | 
          | 
                 commentNewline bool              // true if the comment group contains newlines
  | 
      
      
         | 80 | 
          | 
          | 
          
  | 
      
      
         | 81 | 
          | 
          | 
                 // Cache of already computed node sizes.
  | 
      
      
         | 82 | 
          | 
          | 
                 nodeSizes map[ast.Node]int
  | 
      
      
         | 83 | 
          | 
          | 
          
  | 
      
      
         | 84 | 
          | 
          | 
                 // Cache of most recently computed line position.
  | 
      
      
         | 85 | 
          | 
          | 
                 cachedPos  token.Pos
  | 
      
      
         | 86 | 
          | 
          | 
                 cachedLine int // line corresponding to cachedPos
  | 
      
      
         | 87 | 
          | 
          | 
         }
  | 
      
      
         | 88 | 
          | 
          | 
          
  | 
      
      
         | 89 | 
          | 
          | 
         func (p *printer) init(cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) {
  | 
      
      
         | 90 | 
          | 
          | 
                 p.Config = *cfg
  | 
      
      
         | 91 | 
          | 
          | 
                 p.fset = fset
  | 
      
      
         | 92 | 
          | 
          | 
                 p.wsbuf = make([]whiteSpace, 0, 16) // whitespace sequences are short
  | 
      
      
         | 93 | 
          | 
          | 
                 p.nodeSizes = nodeSizes
  | 
      
      
         | 94 | 
          | 
          | 
                 p.cachedPos = -1
  | 
      
      
         | 95 | 
          | 
          | 
         }
  | 
      
      
         | 96 | 
          | 
          | 
          
  | 
      
      
         | 97 | 
          | 
          | 
         // commentsHaveNewline reports whether a list of comments belonging to
  | 
      
      
         | 98 | 
          | 
          | 
         // an *ast.CommentGroup contains newlines. Because the position information
  | 
      
      
         | 99 | 
          | 
          | 
         // may only be partially correct, we also have to read the comment text.
  | 
      
      
         | 100 | 
          | 
          | 
         func (p *printer) commentsHaveNewline(list []*ast.Comment) bool {
  | 
      
      
         | 101 | 
          | 
          | 
                 // len(list) > 0
  | 
      
      
         | 102 | 
          | 
          | 
                 line := p.lineFor(list[0].Pos())
  | 
      
      
         | 103 | 
          | 
          | 
                 for i, c := range list {
  | 
      
      
         | 104 | 
          | 
          | 
                         if i > 0 && p.lineFor(list[i].Pos()) != line {
  | 
      
      
         | 105 | 
          | 
          | 
                                 // not all comments on the same line
  | 
      
      
         | 106 | 
          | 
          | 
                                 return true
  | 
      
      
         | 107 | 
          | 
          | 
                         }
  | 
      
      
         | 108 | 
          | 
          | 
                         if t := c.Text; len(t) >= 2 && (t[1] == '/' || strings.Contains(t, "\n")) {
  | 
      
      
         | 109 | 
          | 
          | 
                                 return true
  | 
      
      
         | 110 | 
          | 
          | 
                         }
  | 
      
      
         | 111 | 
          | 
          | 
                 }
  | 
      
      
         | 112 | 
          | 
          | 
                 _ = line
  | 
      
      
         | 113 | 
          | 
          | 
                 return false
  | 
      
      
         | 114 | 
          | 
          | 
         }
  | 
      
      
         | 115 | 
          | 
          | 
          
  | 
      
      
         | 116 | 
          | 
          | 
         func (p *printer) nextComment() {
  | 
      
      
         | 117 | 
          | 
          | 
                 for p.cindex < len(p.comments) {
  | 
      
      
         | 118 | 
          | 
          | 
                         c := p.comments[p.cindex]
  | 
      
      
         | 119 | 
          | 
          | 
                         p.cindex++
  | 
      
      
         | 120 | 
          | 
          | 
                         if list := c.List; len(list) > 0 {
  | 
      
      
         | 121 | 
          | 
          | 
                                 p.comment = c
  | 
      
      
         | 122 | 
          | 
          | 
                                 p.commentOffset = p.posFor(list[0].Pos()).Offset
  | 
      
      
         | 123 | 
          | 
          | 
                                 p.commentNewline = p.commentsHaveNewline(list)
  | 
      
      
         | 124 | 
          | 
          | 
                                 return
  | 
      
      
         | 125 | 
          | 
          | 
                         }
  | 
      
      
         | 126 | 
          | 
          | 
                         // we should not reach here (correct ASTs don't have empty
  | 
      
      
         | 127 | 
          | 
          | 
                         // ast.CommentGroup nodes), but be conservative and try again
  | 
      
      
         | 128 | 
          | 
          | 
                 }
  | 
      
      
         | 129 | 
          | 
          | 
                 // no more comments
  | 
      
      
         | 130 | 
          | 
          | 
                 p.commentOffset = infinity
  | 
      
      
         | 131 | 
          | 
          | 
         }
  | 
      
      
         | 132 | 
          | 
          | 
          
  | 
      
      
         | 133 | 
          | 
          | 
         func (p *printer) internalError(msg ...interface{}) {
  | 
      
      
         | 134 | 
          | 
          | 
                 if debug {
  | 
      
      
         | 135 | 
          | 
          | 
                         fmt.Print(p.pos.String() + ": ")
  | 
      
      
         | 136 | 
          | 
          | 
                         fmt.Println(msg...)
  | 
      
      
         | 137 | 
          | 
          | 
                         panic("go/printer")
  | 
      
      
         | 138 | 
          | 
          | 
                 }
  | 
      
      
         | 139 | 
          | 
          | 
         }
  | 
      
      
         | 140 | 
          | 
          | 
          
  | 
      
      
         | 141 | 
          | 
          | 
         func (p *printer) posFor(pos token.Pos) token.Position {
  | 
      
      
         | 142 | 
          | 
          | 
                 // not used frequently enough to cache entire token.Position
  | 
      
      
         | 143 | 
          | 
          | 
                 return p.fset.Position(pos)
  | 
      
      
         | 144 | 
          | 
          | 
         }
  | 
      
      
         | 145 | 
          | 
          | 
          
  | 
      
      
         | 146 | 
          | 
          | 
         func (p *printer) lineFor(pos token.Pos) int {
  | 
      
      
         | 147 | 
          | 
          | 
                 if pos != p.cachedPos {
  | 
      
      
         | 148 | 
          | 
          | 
                         p.cachedPos = pos
  | 
      
      
         | 149 | 
          | 
          | 
                         p.cachedLine = p.fset.Position(pos).Line
  | 
      
      
         | 150 | 
          | 
          | 
                 }
  | 
      
      
         | 151 | 
          | 
          | 
                 return p.cachedLine
  | 
      
      
         | 152 | 
          | 
          | 
         }
  | 
      
      
         | 153 | 
          | 
          | 
          
  | 
      
      
         | 154 | 
          | 
          | 
         // writeByte writes ch to p.output and updates p.pos.
  | 
      
      
         | 155 | 
          | 
          | 
         func (p *printer) writeByte(ch byte) {
  | 
      
      
         | 156 | 
          | 
          | 
                 p.output.WriteByte(ch)
  | 
      
      
         | 157 | 
          | 
          | 
                 p.pos.Offset++
  | 
      
      
         | 158 | 
          | 
          | 
                 p.pos.Column++
  | 
      
      
         | 159 | 
          | 
          | 
          
  | 
      
      
         | 160 | 
          | 
          | 
                 if ch == '\n' || ch == '\f' {
  | 
      
      
         | 161 | 
          | 
          | 
                         // write indentation
  | 
      
      
         | 162 | 
          | 
          | 
                         // use "hard" htabs - indentation columns
  | 
      
      
         | 163 | 
          | 
          | 
                         // must not be discarded by the tabwriter
  | 
      
      
         | 164 | 
          | 
          | 
                         const htabs = "\t\t\t\t\t\t\t\t"
  | 
      
      
         | 165 | 
          | 
          | 
                         j := p.indent
  | 
      
      
         | 166 | 
          | 
          | 
                         for j > len(htabs) {
  | 
      
      
         | 167 | 
          | 
          | 
                                 p.output.WriteString(htabs)
  | 
      
      
         | 168 | 
          | 
          | 
                                 j -= len(htabs)
  | 
      
      
         | 169 | 
          | 
          | 
                         }
  | 
      
      
         | 170 | 
          | 
          | 
                         p.output.WriteString(htabs[0:j])
  | 
      
      
         | 171 | 
          | 
          | 
          
  | 
      
      
         | 172 | 
          | 
          | 
                         // update p.pos
  | 
      
      
         | 173 | 
          | 
          | 
                         p.pos.Line++
  | 
      
      
         | 174 | 
          | 
          | 
                         p.pos.Offset += p.indent
  | 
      
      
         | 175 | 
          | 
          | 
                         p.pos.Column = 1 + p.indent
  | 
      
      
         | 176 | 
          | 
          | 
                 }
  | 
      
      
         | 177 | 
          | 
          | 
         }
  | 
      
      
         | 178 | 
          | 
          | 
          
  | 
      
      
         | 179 | 
          | 
          | 
         // writeByteN writes ch n times to p.output and updates p.pos.
  | 
      
      
         | 180 | 
          | 
          | 
         func (p *printer) writeByteN(ch byte, n int) {
  | 
      
      
         | 181 | 
          | 
          | 
                 for n > 0 {
  | 
      
      
         | 182 | 
          | 
          | 
                         p.writeByte(ch)
  | 
      
      
         | 183 | 
          | 
          | 
                         n--
  | 
      
      
         | 184 | 
          | 
          | 
                 }
  | 
      
      
         | 185 | 
          | 
          | 
         }
  | 
      
      
         | 186 | 
          | 
          | 
          
  | 
      
      
         | 187 | 
          | 
          | 
         // writeString writes the string s to p.output and updates p.pos.
  | 
      
      
         | 188 | 
          | 
          | 
         // If isLit is set, s is escaped w/ tabwriter.Escape characters
  | 
      
      
         | 189 | 
          | 
          | 
         // to protect s from being interpreted by the tabwriter.
  | 
      
      
         | 190 | 
          | 
          | 
         //
  | 
      
      
         | 191 | 
          | 
          | 
         // Note: writeString is only used to write Go tokens, literals, and
  | 
      
      
         | 192 | 
          | 
          | 
         // comments, all of which must be written literally. Thus, it is correct
  | 
      
      
         | 193 | 
          | 
          | 
         // to always set isLit = true. However, setting it explicitly only when
  | 
      
      
         | 194 | 
          | 
          | 
         // needed (i.e., when we don't know that s contains no tabs or line breaks)
  | 
      
      
         | 195 | 
          | 
          | 
         // avoids processing extra escape characters and reduces run time of the
  | 
      
      
         | 196 | 
          | 
          | 
         // printer benchmark by up to 10%.
  | 
      
      
         | 197 | 
          | 
          | 
         //
  | 
      
      
         | 198 | 
          | 
          | 
         func (p *printer) writeString(s string, isLit bool) {
  | 
      
      
         | 199 | 
          | 
          | 
                 if isLit {
  | 
      
      
         | 200 | 
          | 
          | 
                         // Protect s such that is passes through the tabwriter
  | 
      
      
         | 201 | 
          | 
          | 
                         // unchanged. Note that valid Go programs cannot contain
  | 
      
      
         | 202 | 
          | 
          | 
                         // tabwriter.Escape bytes since they do not appear in legal
  | 
      
      
         | 203 | 
          | 
          | 
                         // UTF-8 sequences.
  | 
      
      
         | 204 | 
          | 
          | 
                         p.output.WriteByte(tabwriter.Escape)
  | 
      
      
         | 205 | 
          | 
          | 
                 }
  | 
      
      
         | 206 | 
          | 
          | 
          
  | 
      
      
         | 207 | 
          | 
          | 
                 p.output.WriteString(s)
  | 
      
      
         | 208 | 
          | 
          | 
          
  | 
      
      
         | 209 | 
          | 
          | 
                 // update p.pos
  | 
      
      
         | 210 | 
          | 
          | 
                 nlines := 0
  | 
      
      
         | 211 | 
          | 
          | 
                 column := p.pos.Column + len(s)
  | 
      
      
         | 212 | 
          | 
          | 
                 for i := 0; i < len(s); i++ {
  | 
      
      
         | 213 | 
          | 
          | 
                         if s[i] == '\n' {
  | 
      
      
         | 214 | 
          | 
          | 
                                 nlines++
  | 
      
      
         | 215 | 
          | 
          | 
                                 column = len(s) - i
  | 
      
      
         | 216 | 
          | 
          | 
                         }
  | 
      
      
         | 217 | 
          | 
          | 
                 }
  | 
      
      
         | 218 | 
          | 
          | 
                 p.pos.Offset += len(s)
  | 
      
      
         | 219 | 
          | 
          | 
                 p.pos.Line += nlines
  | 
      
      
         | 220 | 
          | 
          | 
                 p.pos.Column = column
  | 
      
      
         | 221 | 
          | 
          | 
          
  | 
      
      
         | 222 | 
          | 
          | 
                 if isLit {
  | 
      
      
         | 223 | 
          | 
          | 
                         p.output.WriteByte(tabwriter.Escape)
  | 
      
      
         | 224 | 
          | 
          | 
                 }
  | 
      
      
         | 225 | 
          | 
          | 
         }
  | 
      
      
         | 226 | 
          | 
          | 
          
  | 
      
      
         | 227 | 
          | 
          | 
         // writeItem writes data at position pos. data is the text corresponding to
  | 
      
      
         | 228 | 
          | 
          | 
         // a single lexical token, but may also be comment text. pos is the actual
  | 
      
      
         | 229 | 
          | 
          | 
         // (or at least very accurately estimated) position of the data in the original
  | 
      
      
         | 230 | 
          | 
          | 
         // source text. writeItem updates p.last to the position immediately following
  | 
      
      
         | 231 | 
          | 
          | 
         // the data.
  | 
      
      
         | 232 | 
          | 
          | 
         //
  | 
      
      
         | 233 | 
          | 
          | 
         func (p *printer) writeItem(pos token.Position, data string, isLit bool) {
  | 
      
      
         | 234 | 
          | 
          | 
                 if pos.IsValid() {
  | 
      
      
         | 235 | 
          | 
          | 
                         // continue with previous position if we don't have a valid pos
  | 
      
      
         | 236 | 
          | 
          | 
                         if p.last.IsValid() && p.last.Filename != pos.Filename {
  | 
      
      
         | 237 | 
          | 
          | 
                                 // the file has changed - reset state
  | 
      
      
         | 238 | 
          | 
          | 
                                 // (used when printing merged ASTs of different files
  | 
      
      
         | 239 | 
          | 
          | 
                                 // e.g., the result of ast.MergePackageFiles)
  | 
      
      
         | 240 | 
          | 
          | 
                                 p.indent = 0
  | 
      
      
         | 241 | 
          | 
          | 
                                 p.mode = 0
  | 
      
      
         | 242 | 
          | 
          | 
                                 p.wsbuf = p.wsbuf[0:0]
  | 
      
      
         | 243 | 
          | 
          | 
                         }
  | 
      
      
         | 244 | 
          | 
          | 
                         p.pos = pos
  | 
      
      
         | 245 | 
          | 
          | 
                 }
  | 
      
      
         | 246 | 
          | 
          | 
                 if debug {
  | 
      
      
         | 247 | 
          | 
          | 
                         // do not update p.pos - use write0
  | 
      
      
         | 248 | 
          | 
          | 
                         fmt.Fprintf(&p.output, "/*%s*/", pos)
  | 
      
      
         | 249 | 
          | 
          | 
                 }
  | 
      
      
         | 250 | 
          | 
          | 
                 p.writeString(data, isLit)
  | 
      
      
         | 251 | 
          | 
          | 
                 p.last = p.pos
  | 
      
      
         | 252 | 
          | 
          | 
         }
  | 
      
      
         | 253 | 
          | 
          | 
          
  | 
      
      
         | 254 | 
          | 
          | 
         const linePrefix = "//line "
  | 
      
      
         | 255 | 
          | 
          | 
          
  | 
      
      
         | 256 | 
          | 
          | 
         // writeCommentPrefix writes the whitespace before a comment.
  | 
      
      
         | 257 | 
          | 
          | 
         // If there is any pending whitespace, it consumes as much of
  | 
      
      
         | 258 | 
          | 
          | 
         // it as is likely to help position the comment nicely.
  | 
      
      
         | 259 | 
          | 
          | 
         // pos is the comment position, next the position of the item
  | 
      
      
         | 260 | 
          | 
          | 
         // after all pending comments, prev is the previous comment in
  | 
      
      
         | 261 | 
          | 
          | 
         // a group of comments (or nil), and isKeyword indicates if the
  | 
      
      
         | 262 | 
          | 
          | 
         // next item is a keyword.
  | 
      
      
         | 263 | 
          | 
          | 
         //
  | 
      
      
         | 264 | 
          | 
          | 
         func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *ast.Comment, isKeyword bool) {
  | 
      
      
         | 265 | 
          | 
          | 
                 if p.output.Len() == 0 {
  | 
      
      
         | 266 | 
          | 
          | 
                         // the comment is the first item to be printed - don't write any whitespace
  | 
      
      
         | 267 | 
          | 
          | 
                         return
  | 
      
      
         | 268 | 
          | 
          | 
                 }
  | 
      
      
         | 269 | 
          | 
          | 
          
  | 
      
      
         | 270 | 
          | 
          | 
                 if pos.IsValid() && pos.Filename != p.last.Filename {
  | 
      
      
         | 271 | 
          | 
          | 
                         // comment in a different file - separate with newlines
  | 
      
      
         | 272 | 
          | 
          | 
                         p.writeByteN('\f', maxNewlines)
  | 
      
      
         | 273 | 
          | 
          | 
                         return
  | 
      
      
         | 274 | 
          | 
          | 
                 }
  | 
      
      
         | 275 | 
          | 
          | 
          
  | 
      
      
         | 276 | 
          | 
          | 
                 if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') {
  | 
      
      
         | 277 | 
          | 
          | 
                         // comment on the same line as last item:
  | 
      
      
         | 278 | 
          | 
          | 
                         // separate with at least one separator
  | 
      
      
         | 279 | 
          | 
          | 
                         hasSep := false
  | 
      
      
         | 280 | 
          | 
          | 
                         if prev == nil {
  | 
      
      
         | 281 | 
          | 
          | 
                                 // first comment of a comment group
  | 
      
      
         | 282 | 
          | 
          | 
                                 j := 0
  | 
      
      
         | 283 | 
          | 
          | 
                                 for i, ch := range p.wsbuf {
  | 
      
      
         | 284 | 
          | 
          | 
                                         switch ch {
  | 
      
      
         | 285 | 
          | 
          | 
                                         case blank:
  | 
      
      
         | 286 | 
          | 
          | 
                                                 // ignore any blanks before a comment
  | 
      
      
         | 287 | 
          | 
          | 
                                                 p.wsbuf[i] = ignore
  | 
      
      
         | 288 | 
          | 
          | 
                                                 continue
  | 
      
      
         | 289 | 
          | 
          | 
                                         case vtab:
  | 
      
      
         | 290 | 
          | 
          | 
                                                 // respect existing tabs - important
  | 
      
      
         | 291 | 
          | 
          | 
                                                 // for proper formatting of commented structs
  | 
      
      
         | 292 | 
          | 
          | 
                                                 hasSep = true
  | 
      
      
         | 293 | 
          | 
          | 
                                                 continue
  | 
      
      
         | 294 | 
          | 
          | 
                                         case indent:
  | 
      
      
         | 295 | 
          | 
          | 
                                                 // apply pending indentation
  | 
      
      
         | 296 | 
          | 
          | 
                                                 continue
  | 
      
      
         | 297 | 
          | 
          | 
                                         }
  | 
      
      
         | 298 | 
          | 
          | 
                                         j = i
  | 
      
      
         | 299 | 
          | 
          | 
                                         break
  | 
      
      
         | 300 | 
          | 
          | 
                                 }
  | 
      
      
         | 301 | 
          | 
          | 
                                 p.writeWhitespace(j)
  | 
      
      
         | 302 | 
          | 
          | 
                         }
  | 
      
      
         | 303 | 
          | 
          | 
                         // make sure there is at least one separator
  | 
      
      
         | 304 | 
          | 
          | 
                         if !hasSep {
  | 
      
      
         | 305 | 
          | 
          | 
                                 sep := byte('\t')
  | 
      
      
         | 306 | 
          | 
          | 
                                 if pos.Line == next.Line {
  | 
      
      
         | 307 | 
          | 
          | 
                                         // next item is on the same line as the comment
  | 
      
      
         | 308 | 
          | 
          | 
                                         // (which must be a /*-style comment): separate
  | 
      
      
         | 309 | 
          | 
          | 
                                         // with a blank instead of a tab
  | 
      
      
         | 310 | 
          | 
          | 
                                         sep = ' '
  | 
      
      
         | 311 | 
          | 
          | 
                                 }
  | 
      
      
         | 312 | 
          | 
          | 
                                 p.writeByte(sep)
  | 
      
      
         | 313 | 
          | 
          | 
                         }
  | 
      
      
         | 314 | 
          | 
          | 
          
  | 
      
      
         | 315 | 
          | 
          | 
                 } else {
  | 
      
      
         | 316 | 
          | 
          | 
                         // comment on a different line:
  | 
      
      
         | 317 | 
          | 
          | 
                         // separate with at least one line break
  | 
      
      
         | 318 | 
          | 
          | 
                         droppedLinebreak := false
  | 
      
      
         | 319 | 
          | 
          | 
                         if prev == nil {
  | 
      
      
         | 320 | 
          | 
          | 
                                 // first comment of a comment group
  | 
      
      
         | 321 | 
          | 
          | 
                                 j := 0
  | 
      
      
         | 322 | 
          | 
          | 
                                 for i, ch := range p.wsbuf {
  | 
      
      
         | 323 | 
          | 
          | 
                                         switch ch {
  | 
      
      
         | 324 | 
          | 
          | 
                                         case blank, vtab:
  | 
      
      
         | 325 | 
          | 
          | 
                                                 // ignore any horizontal whitespace before line breaks
  | 
      
      
         | 326 | 
          | 
          | 
                                                 p.wsbuf[i] = ignore
  | 
      
      
         | 327 | 
          | 
          | 
                                                 continue
  | 
      
      
         | 328 | 
          | 
          | 
                                         case indent:
  | 
      
      
         | 329 | 
          | 
          | 
                                                 // apply pending indentation
  | 
      
      
         | 330 | 
          | 
          | 
                                                 continue
  | 
      
      
         | 331 | 
          | 
          | 
                                         case unindent:
  | 
      
      
         | 332 | 
          | 
          | 
                                                 // if the next token is a keyword, apply the outdent
  | 
      
      
         | 333 | 
          | 
          | 
                                                 // if it appears that the comment is aligned with the
  | 
      
      
         | 334 | 
          | 
          | 
                                                 // keyword; otherwise assume the outdent is part of a
  | 
      
      
         | 335 | 
          | 
          | 
                                                 // closing block and stop (this scenario appears with
  | 
      
      
         | 336 | 
          | 
          | 
                                                 // comments before a case label where the comments
  | 
      
      
         | 337 | 
          | 
          | 
                                                 // apply to the next case instead of the current one)
  | 
      
      
         | 338 | 
          | 
          | 
                                                 if isKeyword && pos.Column == next.Column {
  | 
      
      
         | 339 | 
          | 
          | 
                                                         continue
  | 
      
      
         | 340 | 
          | 
          | 
                                                 }
  | 
      
      
         | 341 | 
          | 
          | 
                                         case newline, formfeed:
  | 
      
      
         | 342 | 
          | 
          | 
                                                 // TODO(gri): may want to keep formfeed info in some cases
  | 
      
      
         | 343 | 
          | 
          | 
                                                 p.wsbuf[i] = ignore
  | 
      
      
         | 344 | 
          | 
          | 
                                                 droppedLinebreak = true
  | 
      
      
         | 345 | 
          | 
          | 
                                         }
  | 
      
      
         | 346 | 
          | 
          | 
                                         j = i
  | 
      
      
         | 347 | 
          | 
          | 
                                         break
  | 
      
      
         | 348 | 
          | 
          | 
                                 }
  | 
      
      
         | 349 | 
          | 
          | 
                                 p.writeWhitespace(j)
  | 
      
      
         | 350 | 
          | 
          | 
                         }
  | 
      
      
         | 351 | 
          | 
          | 
          
  | 
      
      
         | 352 | 
          | 
          | 
                         // determine number of linebreaks before the comment
  | 
      
      
         | 353 | 
          | 
          | 
                         n := 0
  | 
      
      
         | 354 | 
          | 
          | 
                         if pos.IsValid() && p.last.IsValid() {
  | 
      
      
         | 355 | 
          | 
          | 
                                 n = pos.Line - p.last.Line
  | 
      
      
         | 356 | 
          | 
          | 
                                 if n < 0 { // should never happen
  | 
      
      
         | 357 | 
          | 
          | 
                                         n = 0
  | 
      
      
         | 358 | 
          | 
          | 
                                 }
  | 
      
      
         | 359 | 
          | 
          | 
                         }
  | 
      
      
         | 360 | 
          | 
          | 
          
  | 
      
      
         | 361 | 
          | 
          | 
                         // at the package scope level only (p.indent == 0),
  | 
      
      
         | 362 | 
          | 
          | 
                         // add an extra newline if we dropped one before:
  | 
      
      
         | 363 | 
          | 
          | 
                         // this preserves a blank line before documentation
  | 
      
      
         | 364 | 
          | 
          | 
                         // comments at the package scope level (issue 2570)
  | 
      
      
         | 365 | 
          | 
          | 
                         if p.indent == 0 && droppedLinebreak {
  | 
      
      
         | 366 | 
          | 
          | 
                                 n++
  | 
      
      
         | 367 | 
          | 
          | 
                         }
  | 
      
      
         | 368 | 
          | 
          | 
          
  | 
      
      
         | 369 | 
          | 
          | 
                         // make sure there is at least one line break
  | 
      
      
         | 370 | 
          | 
          | 
                         // if the previous comment was a line comment
  | 
      
      
         | 371 | 
          | 
          | 
                         if n == 0 && prev != nil && prev.Text[1] == '/' {
  | 
      
      
         | 372 | 
          | 
          | 
                                 n = 1
  | 
      
      
         | 373 | 
          | 
          | 
                         }
  | 
      
      
         | 374 | 
          | 
          | 
          
  | 
      
      
         | 375 | 
          | 
          | 
                         if n > 0 {
  | 
      
      
         | 376 | 
          | 
          | 
                                 // turn off indent if we're about to print a line directive
  | 
      
      
         | 377 | 
          | 
          | 
                                 indent := p.indent
  | 
      
      
         | 378 | 
          | 
          | 
                                 if strings.HasPrefix(comment.Text, linePrefix) {
  | 
      
      
         | 379 | 
          | 
          | 
                                         p.indent = 0
  | 
      
      
         | 380 | 
          | 
          | 
                                 }
  | 
      
      
         | 381 | 
          | 
          | 
                                 // use formfeeds to break columns before a comment;
  | 
      
      
         | 382 | 
          | 
          | 
                                 // this is analogous to using formfeeds to separate
  | 
      
      
         | 383 | 
          | 
          | 
                                 // individual lines of /*-style comments
  | 
      
      
         | 384 | 
          | 
          | 
                                 p.writeByteN('\f', nlimit(n))
  | 
      
      
         | 385 | 
          | 
          | 
                                 p.indent = indent // restore indent
  | 
      
      
         | 386 | 
          | 
          | 
                         }
  | 
      
      
         | 387 | 
          | 
          | 
                 }
  | 
      
      
         | 388 | 
          | 
          | 
         }
  | 
      
      
         | 389 | 
          | 
          | 
          
  | 
      
      
         | 390 | 
          | 
          | 
         // Split comment text into lines
  | 
      
      
         | 391 | 
          | 
          | 
         // (using strings.Split(text, "\n") is significantly slower for
  | 
      
      
         | 392 | 
          | 
          | 
         // this specific purpose, as measured with: gotest -bench=Print)
  | 
      
      
         | 393 | 
          | 
          | 
         func split(text string) []string {
  | 
      
      
         | 394 | 
          | 
          | 
                 // count lines (comment text never ends in a newline)
  | 
      
      
         | 395 | 
          | 
          | 
                 n := 1
  | 
      
      
         | 396 | 
          | 
          | 
                 for i := 0; i < len(text); i++ {
  | 
      
      
         | 397 | 
          | 
          | 
                         if text[i] == '\n' {
  | 
      
      
         | 398 | 
          | 
          | 
                                 n++
  | 
      
      
         | 399 | 
          | 
          | 
                         }
  | 
      
      
         | 400 | 
          | 
          | 
                 }
  | 
      
      
         | 401 | 
          | 
          | 
          
  | 
      
      
         | 402 | 
          | 
          | 
                 // split
  | 
      
      
         | 403 | 
          | 
          | 
                 lines := make([]string, n)
  | 
      
      
         | 404 | 
          | 
          | 
                 n = 0
  | 
      
      
         | 405 | 
          | 
          | 
                 i := 0
  | 
      
      
         | 406 | 
          | 
          | 
                 for j := 0; j < len(text); j++ {
  | 
      
      
         | 407 | 
          | 
          | 
                         if text[j] == '\n' {
  | 
      
      
         | 408 | 
          | 
          | 
                                 lines[n] = text[i:j] // exclude newline
  | 
      
      
         | 409 | 
          | 
          | 
                                 i = j + 1            // discard newline
  | 
      
      
         | 410 | 
          | 
          | 
                                 n++
  | 
      
      
         | 411 | 
          | 
          | 
                         }
  | 
      
      
         | 412 | 
          | 
          | 
                 }
  | 
      
      
         | 413 | 
          | 
          | 
                 lines[n] = text[i:]
  | 
      
      
         | 414 | 
          | 
          | 
          
  | 
      
      
         | 415 | 
          | 
          | 
                 return lines
  | 
      
      
         | 416 | 
          | 
          | 
         }
  | 
      
      
         | 417 | 
          | 
          | 
          
  | 
      
      
         | 418 | 
          | 
          | 
         // Returns true if s contains only white space
  | 
      
      
         | 419 | 
          | 
          | 
         // (only tabs and blanks can appear in the printer's context).
  | 
      
      
         | 420 | 
          | 
          | 
         func isBlank(s string) bool {
  | 
      
      
         | 421 | 
          | 
          | 
                 for i := 0; i < len(s); i++ {
  | 
      
      
         | 422 | 
          | 
          | 
                         if s[i] > ' ' {
  | 
      
      
         | 423 | 
          | 
          | 
                                 return false
  | 
      
      
         | 424 | 
          | 
          | 
                         }
  | 
      
      
         | 425 | 
          | 
          | 
                 }
  | 
      
      
         | 426 | 
          | 
          | 
                 return true
  | 
      
      
         | 427 | 
          | 
          | 
         }
  | 
      
      
         | 428 | 
          | 
          | 
          
  | 
      
      
         | 429 | 
          | 
          | 
         func commonPrefix(a, b string) string {
  | 
      
      
         | 430 | 
          | 
          | 
                 i := 0
  | 
      
      
         | 431 | 
          | 
          | 
                 for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
  | 
      
      
         | 432 | 
          | 
          | 
                         i++
  | 
      
      
         | 433 | 
          | 
          | 
                 }
  | 
      
      
         | 434 | 
          | 
          | 
                 return a[0:i]
  | 
      
      
         | 435 | 
          | 
          | 
         }
  | 
      
      
         | 436 | 
          | 
          | 
          
  | 
      
      
         | 437 | 
          | 
          | 
         func stripCommonPrefix(lines []string) {
  | 
      
      
         | 438 | 
          | 
          | 
                 if len(lines) < 2 {
  | 
      
      
         | 439 | 
          | 
          | 
                         return // at most one line - nothing to do
  | 
      
      
         | 440 | 
          | 
          | 
                 }
  | 
      
      
         | 441 | 
          | 
          | 
                 // len(lines) >= 2
  | 
      
      
         | 442 | 
          | 
          | 
          
  | 
      
      
         | 443 | 
          | 
          | 
                 // The heuristic in this function tries to handle a few
  | 
      
      
         | 444 | 
          | 
          | 
                 // common patterns of /*-style comments: Comments where
  | 
      
      
         | 445 | 
          | 
          | 
                 // the opening /* and closing */ are aligned and the
  | 
      
      
         | 446 | 
          | 
          | 
                 // rest of the comment text is aligned and indented with
  | 
      
      
         | 447 | 
          | 
          | 
                 // blanks or tabs, cases with a vertical "line of stars"
  | 
      
      
         | 448 | 
          | 
          | 
                 // on the left, and cases where the closing */ is on the
  | 
      
      
         | 449 | 
          | 
          | 
                 // same line as the last comment text.
  | 
      
      
         | 450 | 
          | 
          | 
          
  | 
      
      
         | 451 | 
          | 
          | 
                 // Compute maximum common white prefix of all but the first,
  | 
      
      
         | 452 | 
          | 
          | 
                 // last, and blank lines, and replace blank lines with empty
  | 
      
      
         | 453 | 
          | 
          | 
                 // lines (the first line starts with /* and has no prefix).
  | 
      
      
         | 454 | 
          | 
          | 
                 // In case of two-line comments, consider the last line for
  | 
      
      
         | 455 | 
          | 
          | 
                 // the prefix computation since otherwise the prefix would
  | 
      
      
         | 456 | 
          | 
          | 
                 // be empty.
  | 
      
      
         | 457 | 
          | 
          | 
                 //
  | 
      
      
         | 458 | 
          | 
          | 
                 // Note that the first and last line are never empty (they
  | 
      
      
         | 459 | 
          | 
          | 
                 // contain the opening /* and closing */ respectively) and
  | 
      
      
         | 460 | 
          | 
          | 
                 // thus they can be ignored by the blank line check.
  | 
      
      
         | 461 | 
          | 
          | 
                 var prefix string
  | 
      
      
         | 462 | 
          | 
          | 
                 if len(lines) > 2 {
  | 
      
      
         | 463 | 
          | 
          | 
                         first := true
  | 
      
      
         | 464 | 
          | 
          | 
                         for i, line := range lines[1 : len(lines)-1] {
  | 
      
      
         | 465 | 
          | 
          | 
                                 switch {
  | 
      
      
         | 466 | 
          | 
          | 
                                 case isBlank(line):
  | 
      
      
         | 467 | 
          | 
          | 
                                         lines[1+i] = "" // range starts at line 1
  | 
      
      
         | 468 | 
          | 
          | 
                                 case first:
  | 
      
      
         | 469 | 
          | 
          | 
                                         prefix = commonPrefix(line, line)
  | 
      
      
         | 470 | 
          | 
          | 
                                         first = false
  | 
      
      
         | 471 | 
          | 
          | 
                                 default:
  | 
      
      
         | 472 | 
          | 
          | 
                                         prefix = commonPrefix(prefix, line)
  | 
      
      
         | 473 | 
          | 
          | 
                                 }
  | 
      
      
         | 474 | 
          | 
          | 
                         }
  | 
      
      
         | 475 | 
          | 
          | 
                 } else { // len(lines) == 2, lines cannot be blank (contain /* and */)
  | 
      
      
         | 476 | 
          | 
          | 
                         line := lines[1]
  | 
      
      
         | 477 | 
          | 
          | 
                         prefix = commonPrefix(line, line)
  | 
      
      
         | 478 | 
          | 
          | 
                 }
  | 
      
      
         | 479 | 
          | 
          | 
          
  | 
      
      
         | 480 | 
          | 
          | 
                 /*
  | 
      
      
         | 481 | 
          | 
          | 
                  * Check for vertical "line of stars" and correct prefix accordingly.
  | 
      
      
         | 482 | 
          | 
          | 
                  */
  | 
      
      
         | 483 | 
          | 
          | 
                 lineOfStars := false
  | 
      
      
         | 484 | 
          | 
          | 
                 if i := strings.Index(prefix, "*"); i >= 0 {
  | 
      
      
         | 485 | 
          | 
          | 
                         // Line of stars present.
  | 
      
      
         | 486 | 
          | 
          | 
                         if i > 0 && prefix[i-1] == ' ' {
  | 
      
      
         | 487 | 
          | 
          | 
                                 i-- // remove trailing blank from prefix so stars remain aligned
  | 
      
      
         | 488 | 
          | 
          | 
                         }
  | 
      
      
         | 489 | 
          | 
          | 
                         prefix = prefix[0:i]
  | 
      
      
         | 490 | 
          | 
          | 
                         lineOfStars = true
  | 
      
      
         | 491 | 
          | 
          | 
                 } else {
  | 
      
      
         | 492 | 
          | 
          | 
                         // No line of stars present.
  | 
      
      
         | 493 | 
          | 
          | 
                         // Determine the white space on the first line after the /*
  | 
      
      
         | 494 | 
          | 
          | 
                         // and before the beginning of the comment text, assume two
  | 
      
      
         | 495 | 
          | 
          | 
                         // blanks instead of the /* unless the first character after
  | 
      
      
         | 496 | 
          | 
          | 
                         // the /* is a tab. If the first comment line is empty but
  | 
      
      
         | 497 | 
          | 
          | 
                         // for the opening /*, assume up to 3 blanks or a tab. This
  | 
      
      
         | 498 | 
          | 
          | 
                         // whitespace may be found as suffix in the common prefix.
  | 
      
      
         | 499 | 
          | 
          | 
                         first := lines[0]
  | 
      
      
         | 500 | 
          | 
          | 
                         if isBlank(first[2:]) {
  | 
      
      
         | 501 | 
          | 
          | 
                                 // no comment text on the first line:
  | 
      
      
         | 502 | 
          | 
          | 
                                 // reduce prefix by up to 3 blanks or a tab
  | 
      
      
         | 503 | 
          | 
          | 
                                 // if present - this keeps comment text indented
  | 
      
      
         | 504 | 
          | 
          | 
                                 // relative to the /* and */'s if it was indented
  | 
      
      
         | 505 | 
          | 
          | 
                                 // in the first place
  | 
      
      
         | 506 | 
          | 
          | 
                                 i := len(prefix)
  | 
      
      
         | 507 | 
          | 
          | 
                                 for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ {
  | 
      
      
         | 508 | 
          | 
          | 
                                         i--
  | 
      
      
         | 509 | 
          | 
          | 
                                 }
  | 
      
      
         | 510 | 
          | 
          | 
                                 if i == len(prefix) && i > 0 && prefix[i-1] == '\t' {
  | 
      
      
         | 511 | 
          | 
          | 
                                         i--
  | 
      
      
         | 512 | 
          | 
          | 
                                 }
  | 
      
      
         | 513 | 
          | 
          | 
                                 prefix = prefix[0:i]
  | 
      
      
         | 514 | 
          | 
          | 
                         } else {
  | 
      
      
         | 515 | 
          | 
          | 
                                 // comment text on the first line
  | 
      
      
         | 516 | 
          | 
          | 
                                 suffix := make([]byte, len(first))
  | 
      
      
         | 517 | 
          | 
          | 
                                 n := 2 // start after opening /*
  | 
      
      
         | 518 | 
          | 
          | 
                                 for n < len(first) && first[n] <= ' ' {
  | 
      
      
         | 519 | 
          | 
          | 
                                         suffix[n] = first[n]
  | 
      
      
         | 520 | 
          | 
          | 
                                         n++
  | 
      
      
         | 521 | 
          | 
          | 
                                 }
  | 
      
      
         | 522 | 
          | 
          | 
                                 if n > 2 && suffix[2] == '\t' {
  | 
      
      
         | 523 | 
          | 
          | 
                                         // assume the '\t' compensates for the /*
  | 
      
      
         | 524 | 
          | 
          | 
                                         suffix = suffix[2:n]
  | 
      
      
         | 525 | 
          | 
          | 
                                 } else {
  | 
      
      
         | 526 | 
          | 
          | 
                                         // otherwise assume two blanks
  | 
      
      
         | 527 | 
          | 
          | 
                                         suffix[0], suffix[1] = ' ', ' '
  | 
      
      
         | 528 | 
          | 
          | 
                                         suffix = suffix[0:n]
  | 
      
      
         | 529 | 
          | 
          | 
                                 }
  | 
      
      
         | 530 | 
          | 
          | 
                                 // Shorten the computed common prefix by the length of
  | 
      
      
         | 531 | 
          | 
          | 
                                 // suffix, if it is found as suffix of the prefix.
  | 
      
      
         | 532 | 
          | 
          | 
                                 if strings.HasSuffix(prefix, string(suffix)) {
  | 
      
      
         | 533 | 
          | 
          | 
                                         prefix = prefix[0 : len(prefix)-len(suffix)]
  | 
      
      
         | 534 | 
          | 
          | 
                                 }
  | 
      
      
         | 535 | 
          | 
          | 
                         }
  | 
      
      
         | 536 | 
          | 
          | 
                 }
  | 
      
      
         | 537 | 
          | 
          | 
          
  | 
      
      
         | 538 | 
          | 
          | 
                 // Handle last line: If it only contains a closing */, align it
  | 
      
      
         | 539 | 
          | 
          | 
                 // with the opening /*, otherwise align the text with the other
  | 
      
      
         | 540 | 
          | 
          | 
                 // lines.
  | 
      
      
         | 541 | 
          | 
          | 
                 last := lines[len(lines)-1]
  | 
      
      
         | 542 | 
          | 
          | 
                 closing := "*/"
  | 
      
      
         | 543 | 
          | 
          | 
                 i := strings.Index(last, closing) // i >= 0 (closing is always present)
  | 
      
      
         | 544 | 
          | 
          | 
                 if isBlank(last[0:i]) {
  | 
      
      
         | 545 | 
          | 
          | 
                         // last line only contains closing */
  | 
      
      
         | 546 | 
          | 
          | 
                         if lineOfStars {
  | 
      
      
         | 547 | 
          | 
          | 
                                 closing = " */" // add blank to align final star
  | 
      
      
         | 548 | 
          | 
          | 
                         }
  | 
      
      
         | 549 | 
          | 
          | 
                         lines[len(lines)-1] = prefix + closing
  | 
      
      
         | 550 | 
          | 
          | 
                 } else {
  | 
      
      
         | 551 | 
          | 
          | 
                         // last line contains more comment text - assume
  | 
      
      
         | 552 | 
          | 
          | 
                         // it is aligned like the other lines and include
  | 
      
      
         | 553 | 
          | 
          | 
                         // in prefix computation
  | 
      
      
         | 554 | 
          | 
          | 
                         prefix = commonPrefix(prefix, last)
  | 
      
      
         | 555 | 
          | 
          | 
                 }
  | 
      
      
         | 556 | 
          | 
          | 
          
  | 
      
      
         | 557 | 
          | 
          | 
                 // Remove the common prefix from all but the first and empty lines.
  | 
      
      
         | 558 | 
          | 
          | 
                 for i, line := range lines[1:] {
  | 
      
      
         | 559 | 
          | 
          | 
                         if len(line) != 0 {
  | 
      
      
         | 560 | 
          | 
          | 
                                 lines[1+i] = line[len(prefix):] // range starts at line 1
  | 
      
      
         | 561 | 
          | 
          | 
                         }
  | 
      
      
         | 562 | 
          | 
          | 
                 }
  | 
      
      
         | 563 | 
          | 
          | 
         }
  | 
      
      
         | 564 | 
          | 
          | 
          
  | 
      
      
         | 565 | 
          | 
          | 
         func (p *printer) writeComment(comment *ast.Comment) {
  | 
      
      
         | 566 | 
          | 
          | 
                 text := comment.Text
  | 
      
      
         | 567 | 
          | 
          | 
          
  | 
      
      
         | 568 | 
          | 
          | 
                 if strings.HasPrefix(text, linePrefix) {
  | 
      
      
         | 569 | 
          | 
          | 
                         pos := strings.TrimSpace(text[len(linePrefix):])
  | 
      
      
         | 570 | 
          | 
          | 
                         i := strings.LastIndex(pos, ":")
  | 
      
      
         | 571 | 
          | 
          | 
                         if i >= 0 {
  | 
      
      
         | 572 | 
          | 
          | 
                                 // The line directive we are about to print changed
  | 
      
      
         | 573 | 
          | 
          | 
                                 // the Filename and Line number used by go/token
  | 
      
      
         | 574 | 
          | 
          | 
                                 // as it was reading the input originally.
  | 
      
      
         | 575 | 
          | 
          | 
                                 // In order to match the original input, we have to
  | 
      
      
         | 576 | 
          | 
          | 
                                 // update our own idea of the file and line number
  | 
      
      
         | 577 | 
          | 
          | 
                                 // accordingly, after printing the directive.
  | 
      
      
         | 578 | 
          | 
          | 
                                 file := pos[:i]
  | 
      
      
         | 579 | 
          | 
          | 
                                 line, _ := strconv.Atoi(pos[i+1:])
  | 
      
      
         | 580 | 
          | 
          | 
                                 defer func() {
  | 
      
      
         | 581 | 
          | 
          | 
                                         p.pos.Filename = file
  | 
      
      
         | 582 | 
          | 
          | 
                                         p.pos.Line = line
  | 
      
      
         | 583 | 
          | 
          | 
                                         p.pos.Column = 1
  | 
      
      
         | 584 | 
          | 
          | 
                                 }()
  | 
      
      
         | 585 | 
          | 
          | 
                         }
  | 
      
      
         | 586 | 
          | 
          | 
                 }
  | 
      
      
         | 587 | 
          | 
          | 
          
  | 
      
      
         | 588 | 
          | 
          | 
                 // shortcut common case of //-style comments
  | 
      
      
         | 589 | 
          | 
          | 
                 if text[1] == '/' {
  | 
      
      
         | 590 | 
          | 
          | 
                         p.writeItem(p.posFor(comment.Pos()), text, true)
  | 
      
      
         | 591 | 
          | 
          | 
                         return
  | 
      
      
         | 592 | 
          | 
          | 
                 }
  | 
      
      
         | 593 | 
          | 
          | 
          
  | 
      
      
         | 594 | 
          | 
          | 
                 // for /*-style comments, print line by line and let the
  | 
      
      
         | 595 | 
          | 
          | 
                 // write function take care of the proper indentation
  | 
      
      
         | 596 | 
          | 
          | 
                 lines := split(text)
  | 
      
      
         | 597 | 
          | 
          | 
                 stripCommonPrefix(lines)
  | 
      
      
         | 598 | 
          | 
          | 
          
  | 
      
      
         | 599 | 
          | 
          | 
                 // write comment lines, separated by formfeed,
  | 
      
      
         | 600 | 
          | 
          | 
                 // without a line break after the last line
  | 
      
      
         | 601 | 
          | 
          | 
                 pos := p.posFor(comment.Pos())
  | 
      
      
         | 602 | 
          | 
          | 
                 for i, line := range lines {
  | 
      
      
         | 603 | 
          | 
          | 
                         if i > 0 {
  | 
      
      
         | 604 | 
          | 
          | 
                                 p.writeByte('\f')
  | 
      
      
         | 605 | 
          | 
          | 
                                 pos = p.pos
  | 
      
      
         | 606 | 
          | 
          | 
                         }
  | 
      
      
         | 607 | 
          | 
          | 
                         if len(line) > 0 {
  | 
      
      
         | 608 | 
          | 
          | 
                                 p.writeItem(pos, line, true)
  | 
      
      
         | 609 | 
          | 
          | 
                         }
  | 
      
      
         | 610 | 
          | 
          | 
                 }
  | 
      
      
         | 611 | 
          | 
          | 
         }
  | 
      
      
         | 612 | 
          | 
          | 
          
  | 
      
      
         | 613 | 
          | 
          | 
         // writeCommentSuffix writes a line break after a comment if indicated
  | 
      
      
         | 614 | 
          | 
          | 
         // and processes any leftover indentation information. If a line break
  | 
      
      
         | 615 | 
          | 
          | 
         // is needed, the kind of break (newline vs formfeed) depends on the
  | 
      
      
         | 616 | 
          | 
          | 
         // pending whitespace. The writeCommentSuffix result indicates if a
  | 
      
      
         | 617 | 
          | 
          | 
         // newline was written or if a formfeed was dropped from the whitespace
  | 
      
      
         | 618 | 
          | 
          | 
         // buffer.
  | 
      
      
         | 619 | 
          | 
          | 
         //
  | 
      
      
         | 620 | 
          | 
          | 
         func (p *printer) writeCommentSuffix(needsLinebreak bool) (wroteNewline, droppedFF bool) {
  | 
      
      
         | 621 | 
          | 
          | 
                 for i, ch := range p.wsbuf {
  | 
      
      
         | 622 | 
          | 
          | 
                         switch ch {
  | 
      
      
         | 623 | 
          | 
          | 
                         case blank, vtab:
  | 
      
      
         | 624 | 
          | 
          | 
                                 // ignore trailing whitespace
  | 
      
      
         | 625 | 
          | 
          | 
                                 p.wsbuf[i] = ignore
  | 
      
      
         | 626 | 
          | 
          | 
                         case indent, unindent:
  | 
      
      
         | 627 | 
          | 
          | 
                                 // don't lose indentation information
  | 
      
      
         | 628 | 
          | 
          | 
                         case newline, formfeed:
  | 
      
      
         | 629 | 
          | 
          | 
                                 // if we need a line break, keep exactly one
  | 
      
      
         | 630 | 
          | 
          | 
                                 // but remember if we dropped any formfeeds
  | 
      
      
         | 631 | 
          | 
          | 
                                 if needsLinebreak {
  | 
      
      
         | 632 | 
          | 
          | 
                                         needsLinebreak = false
  | 
      
      
         | 633 | 
          | 
          | 
                                         wroteNewline = true
  | 
      
      
         | 634 | 
          | 
          | 
                                 } else {
  | 
      
      
         | 635 | 
          | 
          | 
                                         if ch == formfeed {
  | 
      
      
         | 636 | 
          | 
          | 
                                                 droppedFF = true
  | 
      
      
         | 637 | 
          | 
          | 
                                         }
  | 
      
      
         | 638 | 
          | 
          | 
                                         p.wsbuf[i] = ignore
  | 
      
      
         | 639 | 
          | 
          | 
                                 }
  | 
      
      
         | 640 | 
          | 
          | 
                         }
  | 
      
      
         | 641 | 
          | 
          | 
                 }
  | 
      
      
         | 642 | 
          | 
          | 
                 p.writeWhitespace(len(p.wsbuf))
  | 
      
      
         | 643 | 
          | 
          | 
          
  | 
      
      
         | 644 | 
          | 
          | 
                 // make sure we have a line break
  | 
      
      
         | 645 | 
          | 
          | 
                 if needsLinebreak {
  | 
      
      
         | 646 | 
          | 
          | 
                         p.writeByte('\n')
  | 
      
      
         | 647 | 
          | 
          | 
                         wroteNewline = true
  | 
      
      
         | 648 | 
          | 
          | 
                 }
  | 
      
      
         | 649 | 
          | 
          | 
          
  | 
      
      
         | 650 | 
          | 
          | 
                 return
  | 
      
      
         | 651 | 
          | 
          | 
         }
  | 
      
      
         | 652 | 
          | 
          | 
          
  | 
      
      
         | 653 | 
          | 
          | 
         // intersperseComments consumes all comments that appear before the next token
  | 
      
      
         | 654 | 
          | 
          | 
         // tok and prints it together with the buffered whitespace (i.e., the whitespace
  | 
      
      
         | 655 | 
          | 
          | 
         // that needs to be written before the next token). A heuristic is used to mix
  | 
      
      
         | 656 | 
          | 
          | 
         // the comments and whitespace. The intersperseComments result indicates if a
  | 
      
      
         | 657 | 
          | 
          | 
         // newline was written or if a formfeed was dropped from the whitespace buffer.
  | 
      
      
         | 658 | 
          | 
          | 
         //
  | 
      
      
         | 659 | 
          | 
          | 
         func (p *printer) intersperseComments(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) {
  | 
      
      
         | 660 | 
          | 
          | 
                 var last *ast.Comment
  | 
      
      
         | 661 | 
          | 
          | 
                 for p.commentBefore(next) {
  | 
      
      
         | 662 | 
          | 
          | 
                         for _, c := range p.comment.List {
  | 
      
      
         | 663 | 
          | 
          | 
                                 p.writeCommentPrefix(p.posFor(c.Pos()), next, last, c, tok.IsKeyword())
  | 
      
      
         | 664 | 
          | 
          | 
                                 p.writeComment(c)
  | 
      
      
         | 665 | 
          | 
          | 
                                 last = c
  | 
      
      
         | 666 | 
          | 
          | 
                         }
  | 
      
      
         | 667 | 
          | 
          | 
                         p.nextComment()
  | 
      
      
         | 668 | 
          | 
          | 
                 }
  | 
      
      
         | 669 | 
          | 
          | 
          
  | 
      
      
         | 670 | 
          | 
          | 
                 if last != nil {
  | 
      
      
         | 671 | 
          | 
          | 
                         if last.Text[1] == '*' && p.lineFor(last.Pos()) == next.Line {
  | 
      
      
         | 672 | 
          | 
          | 
                                 // the last comment is a /*-style comment and the next item
  | 
      
      
         | 673 | 
          | 
          | 
                                 // follows on the same line: separate with an extra blank
  | 
      
      
         | 674 | 
          | 
          | 
                                 p.writeByte(' ')
  | 
      
      
         | 675 | 
          | 
          | 
                         }
  | 
      
      
         | 676 | 
          | 
          | 
                         // ensure that there is a line break after a //-style comment,
  | 
      
      
         | 677 | 
          | 
          | 
                         // before a closing '}' unless explicitly disabled, or at eof
  | 
      
      
         | 678 | 
          | 
          | 
                         needsLinebreak :=
  | 
      
      
         | 679 | 
          | 
          | 
                                 last.Text[1] == '/' ||
  | 
      
      
         | 680 | 
          | 
          | 
                                         tok == token.RBRACE && p.mode&noExtraLinebreak == 0 ||
  | 
      
      
         | 681 | 
          | 
          | 
                                         tok == token.EOF
  | 
      
      
         | 682 | 
          | 
          | 
                         return p.writeCommentSuffix(needsLinebreak)
  | 
      
      
         | 683 | 
          | 
          | 
                 }
  | 
      
      
         | 684 | 
          | 
          | 
          
  | 
      
      
         | 685 | 
          | 
          | 
                 // no comment was written - we should never reach here since
  | 
      
      
         | 686 | 
          | 
          | 
                 // intersperseComments should not be called in that case
  | 
      
      
         | 687 | 
          | 
          | 
                 p.internalError("intersperseComments called without pending comments")
  | 
      
      
         | 688 | 
          | 
          | 
                 return
  | 
      
      
         | 689 | 
          | 
          | 
         }
  | 
      
      
         | 690 | 
          | 
          | 
          
  | 
      
      
         | 691 | 
          | 
          | 
         // whiteWhitespace writes the first n whitespace entries.
  | 
      
      
         | 692 | 
          | 
          | 
         func (p *printer) writeWhitespace(n int) {
  | 
      
      
         | 693 | 
          | 
          | 
                 // write entries
  | 
      
      
         | 694 | 
          | 
          | 
                 for i := 0; i < n; i++ {
  | 
      
      
         | 695 | 
          | 
          | 
                         switch ch := p.wsbuf[i]; ch {
  | 
      
      
         | 696 | 
          | 
          | 
                         case ignore:
  | 
      
      
         | 697 | 
          | 
          | 
                                 // ignore!
  | 
      
      
         | 698 | 
          | 
          | 
                         case indent:
  | 
      
      
         | 699 | 
          | 
          | 
                                 p.indent++
  | 
      
      
         | 700 | 
          | 
          | 
                         case unindent:
  | 
      
      
         | 701 | 
          | 
          | 
                                 p.indent--
  | 
      
      
         | 702 | 
          | 
          | 
                                 if p.indent < 0 {
  | 
      
      
         | 703 | 
          | 
          | 
                                         p.internalError("negative indentation:", p.indent)
  | 
      
      
         | 704 | 
          | 
          | 
                                         p.indent = 0
  | 
      
      
         | 705 | 
          | 
          | 
                                 }
  | 
      
      
         | 706 | 
          | 
          | 
                         case newline, formfeed:
  | 
      
      
         | 707 | 
          | 
          | 
                                 // A line break immediately followed by a "correcting"
  | 
      
      
         | 708 | 
          | 
          | 
                                 // unindent is swapped with the unindent - this permits
  | 
      
      
         | 709 | 
          | 
          | 
                                 // proper label positioning. If a comment is between
  | 
      
      
         | 710 | 
          | 
          | 
                                 // the line break and the label, the unindent is not
  | 
      
      
         | 711 | 
          | 
          | 
                                 // part of the comment whitespace prefix and the comment
  | 
      
      
         | 712 | 
          | 
          | 
                                 // will be positioned correctly indented.
  | 
      
      
         | 713 | 
          | 
          | 
                                 if i+1 < n && p.wsbuf[i+1] == unindent {
  | 
      
      
         | 714 | 
          | 
          | 
                                         // Use a formfeed to terminate the current section.
  | 
      
      
         | 715 | 
          | 
          | 
                                         // Otherwise, a long label name on the next line leading
  | 
      
      
         | 716 | 
          | 
          | 
                                         // to a wide column may increase the indentation column
  | 
      
      
         | 717 | 
          | 
          | 
                                         // of lines before the label; effectively leading to wrong
  | 
      
      
         | 718 | 
          | 
          | 
                                         // indentation.
  | 
      
      
         | 719 | 
          | 
          | 
                                         p.wsbuf[i], p.wsbuf[i+1] = unindent, formfeed
  | 
      
      
         | 720 | 
          | 
          | 
                                         i-- // do it again
  | 
      
      
         | 721 | 
          | 
          | 
                                         continue
  | 
      
      
         | 722 | 
          | 
          | 
                                 }
  | 
      
      
         | 723 | 
          | 
          | 
                                 fallthrough
  | 
      
      
         | 724 | 
          | 
          | 
                         default:
  | 
      
      
         | 725 | 
          | 
          | 
                                 p.writeByte(byte(ch))
  | 
      
      
         | 726 | 
          | 
          | 
                         }
  | 
      
      
         | 727 | 
          | 
          | 
                 }
  | 
      
      
         | 728 | 
          | 
          | 
          
  | 
      
      
         | 729 | 
          | 
          | 
                 // shift remaining entries down
  | 
      
      
         | 730 | 
          | 
          | 
                 i := 0
  | 
      
      
         | 731 | 
          | 
          | 
                 for ; n < len(p.wsbuf); n++ {
  | 
      
      
         | 732 | 
          | 
          | 
                         p.wsbuf[i] = p.wsbuf[n]
  | 
      
      
         | 733 | 
          | 
          | 
                         i++
  | 
      
      
         | 734 | 
          | 
          | 
                 }
  | 
      
      
         | 735 | 
          | 
          | 
                 p.wsbuf = p.wsbuf[0:i]
  | 
      
      
         | 736 | 
          | 
          | 
         }
  | 
      
      
         | 737 | 
          | 
          | 
          
  | 
      
      
         | 738 | 
          | 
          | 
         // ----------------------------------------------------------------------------
  | 
      
      
         | 739 | 
          | 
          | 
         // Printing interface
  | 
      
      
         | 740 | 
          | 
          | 
          
  | 
      
      
         | 741 | 
          | 
          | 
         // nlines limits n to maxNewlines.
  | 
      
      
         | 742 | 
          | 
          | 
         func nlimit(n int) int {
  | 
      
      
         | 743 | 
          | 
          | 
                 if n > maxNewlines {
  | 
      
      
         | 744 | 
          | 
          | 
                         n = maxNewlines
  | 
      
      
         | 745 | 
          | 
          | 
                 }
  | 
      
      
         | 746 | 
          | 
          | 
                 return n
  | 
      
      
         | 747 | 
          | 
          | 
         }
  | 
      
      
         | 748 | 
          | 
          | 
          
  | 
      
      
         | 749 | 
          | 
          | 
         func mayCombine(prev token.Token, next byte) (b bool) {
  | 
      
      
         | 750 | 
          | 
          | 
                 switch prev {
  | 
      
      
         | 751 | 
          | 
          | 
                 case token.INT:
  | 
      
      
         | 752 | 
          | 
          | 
                         b = next == '.' // 1.
  | 
      
      
         | 753 | 
          | 
          | 
                 case token.ADD:
  | 
      
      
         | 754 | 
          | 
          | 
                         b = next == '+' // ++
  | 
      
      
         | 755 | 
          | 
          | 
                 case token.SUB:
  | 
      
      
         | 756 | 
          | 
          | 
                         b = next == '-' // --
  | 
      
      
         | 757 | 
          | 
          | 
                 case token.QUO:
  | 
      
      
         | 758 | 
          | 
          | 
                         b = next == '*' // /*
  | 
      
      
         | 759 | 
          | 
          | 
                 case token.LSS:
  | 
      
      
         | 760 | 
          | 
          | 
                         b = next == '-' || next == '<' // <- or <<
  | 
      
      
         | 761 | 
          | 
          | 
                 case token.AND:
  | 
      
      
         | 762 | 
          | 
          | 
                         b = next == '&' || next == '^' // && or &^
  | 
      
      
         | 763 | 
          | 
          | 
                 }
  | 
      
      
         | 764 | 
          | 
          | 
                 return
  | 
      
      
         | 765 | 
          | 
          | 
         }
  | 
      
      
         | 766 | 
          | 
          | 
          
  | 
      
      
         | 767 | 
          | 
          | 
         // print prints a list of "items" (roughly corresponding to syntactic
  | 
      
      
         | 768 | 
          | 
          | 
         // tokens, but also including whitespace and formatting information).
  | 
      
      
         | 769 | 
          | 
          | 
         // It is the only print function that should be called directly from
  | 
      
      
         | 770 | 
          | 
          | 
         // any of the AST printing functions in nodes.go.
  | 
      
      
         | 771 | 
          | 
          | 
         //
  | 
      
      
         | 772 | 
          | 
          | 
         // Whitespace is accumulated until a non-whitespace token appears. Any
  | 
      
      
         | 773 | 
          | 
          | 
         // comments that need to appear before that token are printed first,
  | 
      
      
         | 774 | 
          | 
          | 
         // taking into account the amount and structure of any pending white-
  | 
      
      
         | 775 | 
          | 
          | 
         // space for best comment placement. Then, any leftover whitespace is
  | 
      
      
         | 776 | 
          | 
          | 
         // printed, followed by the actual token.
  | 
      
      
         | 777 | 
          | 
          | 
         //
  | 
      
      
         | 778 | 
          | 
          | 
         func (p *printer) print(args ...interface{}) {
  | 
      
      
         | 779 | 
          | 
          | 
                 for _, arg := range args {
  | 
      
      
         | 780 | 
          | 
          | 
                         // information about the current arg
  | 
      
      
         | 781 | 
          | 
          | 
                         var data string
  | 
      
      
         | 782 | 
          | 
          | 
                         var isLit bool
  | 
      
      
         | 783 | 
          | 
          | 
                         var impliedSemi bool // value for p.impliedSemi after this arg
  | 
      
      
         | 784 | 
          | 
          | 
          
  | 
      
      
         | 785 | 
          | 
          | 
                         switch x := arg.(type) {
  | 
      
      
         | 786 | 
          | 
          | 
                         case pmode:
  | 
      
      
         | 787 | 
          | 
          | 
                                 // toggle printer mode
  | 
      
      
         | 788 | 
          | 
          | 
                                 p.mode ^= x
  | 
      
      
         | 789 | 
          | 
          | 
                                 continue
  | 
      
      
         | 790 | 
          | 
          | 
          
  | 
      
      
         | 791 | 
          | 
          | 
                         case whiteSpace:
  | 
      
      
         | 792 | 
          | 
          | 
                                 if x == ignore {
  | 
      
      
         | 793 | 
          | 
          | 
                                         // don't add ignore's to the buffer; they
  | 
      
      
         | 794 | 
          | 
          | 
                                         // may screw up "correcting" unindents (see
  | 
      
      
         | 795 | 
          | 
          | 
                                         // LabeledStmt)
  | 
      
      
         | 796 | 
          | 
          | 
                                         continue
  | 
      
      
         | 797 | 
          | 
          | 
                                 }
  | 
      
      
         | 798 | 
          | 
          | 
                                 i := len(p.wsbuf)
  | 
      
      
         | 799 | 
          | 
          | 
                                 if i == cap(p.wsbuf) {
  | 
      
      
         | 800 | 
          | 
          | 
                                         // Whitespace sequences are very short so this should
  | 
      
      
         | 801 | 
          | 
          | 
                                         // never happen. Handle gracefully (but possibly with
  | 
      
      
         | 802 | 
          | 
          | 
                                         // bad comment placement) if it does happen.
  | 
      
      
         | 803 | 
          | 
          | 
                                         p.writeWhitespace(i)
  | 
      
      
         | 804 | 
          | 
          | 
                                         i = 0
  | 
      
      
         | 805 | 
          | 
          | 
                                 }
  | 
      
      
         | 806 | 
          | 
          | 
                                 p.wsbuf = p.wsbuf[0 : i+1]
  | 
      
      
         | 807 | 
          | 
          | 
                                 p.wsbuf[i] = x
  | 
      
      
         | 808 | 
          | 
          | 
                                 if x == newline || x == formfeed {
  | 
      
      
         | 809 | 
          | 
          | 
                                         // newlines affect the current state (p.impliedSemi)
  | 
      
      
         | 810 | 
          | 
          | 
                                         // and not the state after printing arg (impliedSemi)
  | 
      
      
         | 811 | 
          | 
          | 
                                         // because comments can be interspersed before the arg
  | 
      
      
         | 812 | 
          | 
          | 
                                         // in this case
  | 
      
      
         | 813 | 
          | 
          | 
                                         p.impliedSemi = false
  | 
      
      
         | 814 | 
          | 
          | 
                                 }
  | 
      
      
         | 815 | 
          | 
          | 
                                 p.lastTok = token.ILLEGAL
  | 
      
      
         | 816 | 
          | 
          | 
                                 continue
  | 
      
      
         | 817 | 
          | 
          | 
          
  | 
      
      
         | 818 | 
          | 
          | 
                         case *ast.Ident:
  | 
      
      
         | 819 | 
          | 
          | 
                                 data = x.Name
  | 
      
      
         | 820 | 
          | 
          | 
                                 impliedSemi = true
  | 
      
      
         | 821 | 
          | 
          | 
                                 p.lastTok = token.IDENT
  | 
      
      
         | 822 | 
          | 
          | 
          
  | 
      
      
         | 823 | 
          | 
          | 
                         case *ast.BasicLit:
  | 
      
      
         | 824 | 
          | 
          | 
                                 data = x.Value
  | 
      
      
         | 825 | 
          | 
          | 
                                 isLit = true
  | 
      
      
         | 826 | 
          | 
          | 
                                 impliedSemi = true
  | 
      
      
         | 827 | 
          | 
          | 
                                 p.lastTok = x.Kind
  | 
      
      
         | 828 | 
          | 
          | 
          
  | 
      
      
         | 829 | 
          | 
          | 
                         case token.Token:
  | 
      
      
         | 830 | 
          | 
          | 
                                 s := x.String()
  | 
      
      
         | 831 | 
          | 
          | 
                                 if mayCombine(p.lastTok, s[0]) {
  | 
      
      
         | 832 | 
          | 
          | 
                                         // the previous and the current token must be
  | 
      
      
         | 833 | 
          | 
          | 
                                         // separated by a blank otherwise they combine
  | 
      
      
         | 834 | 
          | 
          | 
                                         // into a different incorrect token sequence
  | 
      
      
         | 835 | 
          | 
          | 
                                         // (except for token.INT followed by a '.' this
  | 
      
      
         | 836 | 
          | 
          | 
                                         // should never happen because it is taken care
  | 
      
      
         | 837 | 
          | 
          | 
                                         // of via binary expression formatting)
  | 
      
      
         | 838 | 
          | 
          | 
                                         if len(p.wsbuf) != 0 {
  | 
      
      
         | 839 | 
          | 
          | 
                                                 p.internalError("whitespace buffer not empty")
  | 
      
      
         | 840 | 
          | 
          | 
                                         }
  | 
      
      
         | 841 | 
          | 
          | 
                                         p.wsbuf = p.wsbuf[0:1]
  | 
      
      
         | 842 | 
          | 
          | 
                                         p.wsbuf[0] = ' '
  | 
      
      
         | 843 | 
          | 
          | 
                                 }
  | 
      
      
         | 844 | 
          | 
          | 
                                 data = s
  | 
      
      
         | 845 | 
          | 
          | 
                                 // some keywords followed by a newline imply a semicolon
  | 
      
      
         | 846 | 
          | 
          | 
                                 switch x {
  | 
      
      
         | 847 | 
          | 
          | 
                                 case token.BREAK, token.CONTINUE, token.FALLTHROUGH, token.RETURN,
  | 
      
      
         | 848 | 
          | 
          | 
                                         token.INC, token.DEC, token.RPAREN, token.RBRACK, token.RBRACE:
  | 
      
      
         | 849 | 
          | 
          | 
                                         impliedSemi = true
  | 
      
      
         | 850 | 
          | 
          | 
                                 }
  | 
      
      
         | 851 | 
          | 
          | 
                                 p.lastTok = x
  | 
      
      
         | 852 | 
          | 
          | 
          
  | 
      
      
         | 853 | 
          | 
          | 
                         case token.Pos:
  | 
      
      
         | 854 | 
          | 
          | 
                                 if x.IsValid() {
  | 
      
      
         | 855 | 
          | 
          | 
                                         p.pos = p.posFor(x) // accurate position of next item
  | 
      
      
         | 856 | 
          | 
          | 
                                 }
  | 
      
      
         | 857 | 
          | 
          | 
                                 continue
  | 
      
      
         | 858 | 
          | 
          | 
          
  | 
      
      
         | 859 | 
          | 
          | 
                         case string:
  | 
      
      
         | 860 | 
          | 
          | 
                                 // incorrect AST - print error message
  | 
      
      
         | 861 | 
          | 
          | 
                                 data = x
  | 
      
      
         | 862 | 
          | 
          | 
                                 isLit = true
  | 
      
      
         | 863 | 
          | 
          | 
                                 impliedSemi = true
  | 
      
      
         | 864 | 
          | 
          | 
                                 p.lastTok = token.STRING
  | 
      
      
         | 865 | 
          | 
          | 
          
  | 
      
      
         | 866 | 
          | 
          | 
                         default:
  | 
      
      
         | 867 | 
          | 
          | 
                                 fmt.Fprintf(os.Stderr, "print: unsupported argument %v (%T)\n", arg, arg)
  | 
      
      
         | 868 | 
          | 
          | 
                                 panic("go/printer type")
  | 
      
      
         | 869 | 
          | 
          | 
                         }
  | 
      
      
         | 870 | 
          | 
          | 
                         // data != ""
  | 
      
      
         | 871 | 
          | 
          | 
          
  | 
      
      
         | 872 | 
          | 
          | 
                         next := p.pos // estimated/accurate position of next item
  | 
      
      
         | 873 | 
          | 
          | 
                         wroteNewline, droppedFF := p.flush(next, p.lastTok)
  | 
      
      
         | 874 | 
          | 
          | 
          
  | 
      
      
         | 875 | 
          | 
          | 
                         // intersperse extra newlines if present in the source and
  | 
      
      
         | 876 | 
          | 
          | 
                         // if they don't cause extra semicolons (don't do this in
  | 
      
      
         | 877 | 
          | 
          | 
                         // flush as it will cause extra newlines at the end of a file)
  | 
      
      
         | 878 | 
          | 
          | 
                         if !p.impliedSemi {
  | 
      
      
         | 879 | 
          | 
          | 
                                 n := nlimit(next.Line - p.pos.Line)
  | 
      
      
         | 880 | 
          | 
          | 
                                 // don't exceed maxNewlines if we already wrote one
  | 
      
      
         | 881 | 
          | 
          | 
                                 if wroteNewline && n == maxNewlines {
  | 
      
      
         | 882 | 
          | 
          | 
                                         n = maxNewlines - 1
  | 
      
      
         | 883 | 
          | 
          | 
                                 }
  | 
      
      
         | 884 | 
          | 
          | 
                                 if n > 0 {
  | 
      
      
         | 885 | 
          | 
          | 
                                         ch := byte('\n')
  | 
      
      
         | 886 | 
          | 
          | 
                                         if droppedFF {
  | 
      
      
         | 887 | 
          | 
          | 
                                                 ch = '\f' // use formfeed since we dropped one before
  | 
      
      
         | 888 | 
          | 
          | 
                                         }
  | 
      
      
         | 889 | 
          | 
          | 
                                         p.writeByteN(ch, n)
  | 
      
      
         | 890 | 
          | 
          | 
                                         impliedSemi = false
  | 
      
      
         | 891 | 
          | 
          | 
                                 }
  | 
      
      
         | 892 | 
          | 
          | 
                         }
  | 
      
      
         | 893 | 
          | 
          | 
          
  | 
      
      
         | 894 | 
          | 
          | 
                         p.writeItem(next, data, isLit)
  | 
      
      
         | 895 | 
          | 
          | 
                         p.impliedSemi = impliedSemi
  | 
      
      
         | 896 | 
          | 
          | 
                 }
  | 
      
      
         | 897 | 
          | 
          | 
         }
  | 
      
      
         | 898 | 
          | 
          | 
          
  | 
      
      
         | 899 | 
          | 
          | 
         // commentBefore returns true iff the current comment group occurs
  | 
      
      
         | 900 | 
          | 
          | 
         // before the next position in the source code and printing it does
  | 
      
      
         | 901 | 
          | 
          | 
         // not introduce implicit semicolons.
  | 
      
      
         | 902 | 
          | 
          | 
         //
  | 
      
      
         | 903 | 
          | 
          | 
         func (p *printer) commentBefore(next token.Position) (result bool) {
  | 
      
      
         | 904 | 
          | 
          | 
                 return p.commentOffset < next.Offset && (!p.impliedSemi || !p.commentNewline)
  | 
      
      
         | 905 | 
          | 
          | 
         }
  | 
      
      
         | 906 | 
          | 
          | 
          
  | 
      
      
         | 907 | 
          | 
          | 
         // flush prints any pending comments and whitespace occurring textually
  | 
      
      
         | 908 | 
          | 
          | 
         // before the position of the next token tok. The flush result indicates
  | 
      
      
         | 909 | 
          | 
          | 
         // if a newline was written or if a formfeed was dropped from the whitespace
  | 
      
      
         | 910 | 
          | 
          | 
         // buffer.
  | 
      
      
         | 911 | 
          | 
          | 
         //
  | 
      
      
         | 912 | 
          | 
          | 
         func (p *printer) flush(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) {
  | 
      
      
         | 913 | 
          | 
          | 
                 if p.commentBefore(next) {
  | 
      
      
         | 914 | 
          | 
          | 
                         // if there are comments before the next item, intersperse them
  | 
      
      
         | 915 | 
          | 
          | 
                         wroteNewline, droppedFF = p.intersperseComments(next, tok)
  | 
      
      
         | 916 | 
          | 
          | 
                 } else {
  | 
      
      
         | 917 | 
          | 
          | 
                         // otherwise, write any leftover whitespace
  | 
      
      
         | 918 | 
          | 
          | 
                         p.writeWhitespace(len(p.wsbuf))
  | 
      
      
         | 919 | 
          | 
          | 
                 }
  | 
      
      
         | 920 | 
          | 
          | 
                 return
  | 
      
      
         | 921 | 
          | 
          | 
         }
  | 
      
      
         | 922 | 
          | 
          | 
          
  | 
      
      
         | 923 | 
          | 
          | 
         // getNode returns the ast.CommentGroup associated with n, if any.
  | 
      
      
         | 924 | 
          | 
          | 
         func getDoc(n ast.Node) *ast.CommentGroup {
  | 
      
      
         | 925 | 
          | 
          | 
                 switch n := n.(type) {
  | 
      
      
         | 926 | 
          | 
          | 
                 case *ast.Field:
  | 
      
      
         | 927 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 928 | 
          | 
          | 
                 case *ast.ImportSpec:
  | 
      
      
         | 929 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 930 | 
          | 
          | 
                 case *ast.ValueSpec:
  | 
      
      
         | 931 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 932 | 
          | 
          | 
                 case *ast.TypeSpec:
  | 
      
      
         | 933 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 934 | 
          | 
          | 
                 case *ast.GenDecl:
  | 
      
      
         | 935 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 936 | 
          | 
          | 
                 case *ast.FuncDecl:
  | 
      
      
         | 937 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 938 | 
          | 
          | 
                 case *ast.File:
  | 
      
      
         | 939 | 
          | 
          | 
                         return n.Doc
  | 
      
      
         | 940 | 
          | 
          | 
                 }
  | 
      
      
         | 941 | 
          | 
          | 
                 return nil
  | 
      
      
         | 942 | 
          | 
          | 
         }
  | 
      
      
         | 943 | 
          | 
          | 
          
  | 
      
      
         | 944 | 
          | 
          | 
         func (p *printer) printNode(node interface{}) error {
  | 
      
      
         | 945 | 
          | 
          | 
                 // unpack *CommentedNode, if any
  | 
      
      
         | 946 | 
          | 
          | 
                 var comments []*ast.CommentGroup
  | 
      
      
         | 947 | 
          | 
          | 
                 if cnode, ok := node.(*CommentedNode); ok {
  | 
      
      
         | 948 | 
          | 
          | 
                         node = cnode.Node
  | 
      
      
         | 949 | 
          | 
          | 
                         comments = cnode.Comments
  | 
      
      
         | 950 | 
          | 
          | 
                 }
  | 
      
      
         | 951 | 
          | 
          | 
          
  | 
      
      
         | 952 | 
          | 
          | 
                 if comments != nil {
  | 
      
      
         | 953 | 
          | 
          | 
                         // commented node - restrict comment list to relevant range
  | 
      
      
         | 954 | 
          | 
          | 
                         n, ok := node.(ast.Node)
  | 
      
      
         | 955 | 
          | 
          | 
                         if !ok {
  | 
      
      
         | 956 | 
          | 
          | 
                                 goto unsupported
  | 
      
      
         | 957 | 
          | 
          | 
                         }
  | 
      
      
         | 958 | 
          | 
          | 
                         beg := n.Pos()
  | 
      
      
         | 959 | 
          | 
          | 
                         end := n.End()
  | 
      
      
         | 960 | 
          | 
          | 
                         // if the node has associated documentation,
  | 
      
      
         | 961 | 
          | 
          | 
                         // include that commentgroup in the range
  | 
      
      
         | 962 | 
          | 
          | 
                         // (the comment list is sorted in the order
  | 
      
      
         | 963 | 
          | 
          | 
                         // of the comment appearance in the source code)
  | 
      
      
         | 964 | 
          | 
          | 
                         if doc := getDoc(n); doc != nil {
  | 
      
      
         | 965 | 
          | 
          | 
                                 beg = doc.Pos()
  | 
      
      
         | 966 | 
          | 
          | 
                         }
  | 
      
      
         | 967 | 
          | 
          | 
                         // token.Pos values are global offsets, we can
  | 
      
      
         | 968 | 
          | 
          | 
                         // compare them directly
  | 
      
      
         | 969 | 
          | 
          | 
                         i := 0
  | 
      
      
         | 970 | 
          | 
          | 
                         for i < len(comments) && comments[i].End() < beg {
  | 
      
      
         | 971 | 
          | 
          | 
                                 i++
  | 
      
      
         | 972 | 
          | 
          | 
                         }
  | 
      
      
         | 973 | 
          | 
          | 
                         j := i
  | 
      
      
         | 974 | 
          | 
          | 
                         for j < len(comments) && comments[j].Pos() < end {
  | 
      
      
         | 975 | 
          | 
          | 
                                 j++
  | 
      
      
         | 976 | 
          | 
          | 
                         }
  | 
      
      
         | 977 | 
          | 
          | 
                         if i < j {
  | 
      
      
         | 978 | 
          | 
          | 
                                 p.comments = comments[i:j]
  | 
      
      
         | 979 | 
          | 
          | 
                         }
  | 
      
      
         | 980 | 
          | 
          | 
                 } else if n, ok := node.(*ast.File); ok {
  | 
      
      
         | 981 | 
          | 
          | 
                         // use ast.File comments, if any
  | 
      
      
         | 982 | 
          | 
          | 
                         p.comments = n.Comments
  | 
      
      
         | 983 | 
          | 
          | 
                 }
  | 
      
      
         | 984 | 
          | 
          | 
          
  | 
      
      
         | 985 | 
          | 
          | 
                 // if there are no comments, use node comments
  | 
      
      
         | 986 | 
          | 
          | 
                 p.useNodeComments = p.comments == nil
  | 
      
      
         | 987 | 
          | 
          | 
          
  | 
      
      
         | 988 | 
          | 
          | 
                 // get comments ready for use
  | 
      
      
         | 989 | 
          | 
          | 
                 p.nextComment()
  | 
      
      
         | 990 | 
          | 
          | 
          
  | 
      
      
         | 991 | 
          | 
          | 
                 // format node
  | 
      
      
         | 992 | 
          | 
          | 
                 switch n := node.(type) {
  | 
      
      
         | 993 | 
          | 
          | 
                 case ast.Expr:
  | 
      
      
         | 994 | 
          | 
          | 
                         p.expr(n, ignoreMultiLine)
  | 
      
      
         | 995 | 
          | 
          | 
                 case ast.Stmt:
  | 
      
      
         | 996 | 
          | 
          | 
                         // A labeled statement will un-indent to position the
  | 
      
      
         | 997 | 
          | 
          | 
                         // label. Set indent to 1 so we don't get indent "underflow".
  | 
      
      
         | 998 | 
          | 
          | 
                         if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt {
  | 
      
      
         | 999 | 
          | 
          | 
                                 p.indent = 1
  | 
      
      
         | 1000 | 
          | 
          | 
                         }
  | 
      
      
         | 1001 | 
          | 
          | 
                         p.stmt(n, false, ignoreMultiLine)
  | 
      
      
         | 1002 | 
          | 
          | 
                 case ast.Decl:
  | 
      
      
         | 1003 | 
          | 
          | 
                         p.decl(n, ignoreMultiLine)
  | 
      
      
         | 1004 | 
          | 
          | 
                 case ast.Spec:
  | 
      
      
         | 1005 | 
          | 
          | 
                         p.spec(n, 1, false, ignoreMultiLine)
  | 
      
      
         | 1006 | 
          | 
          | 
                 case *ast.File:
  | 
      
      
         | 1007 | 
          | 
          | 
                         p.file(n)
  | 
      
      
         | 1008 | 
          | 
          | 
                 default:
  | 
      
      
         | 1009 | 
          | 
          | 
                         goto unsupported
  | 
      
      
         | 1010 | 
          | 
          | 
                 }
  | 
      
      
         | 1011 | 
          | 
          | 
          
  | 
      
      
         | 1012 | 
          | 
          | 
                 return nil
  | 
      
      
         | 1013 | 
          | 
          | 
          
  | 
      
      
         | 1014 | 
          | 
          | 
         unsupported:
  | 
      
      
         | 1015 | 
          | 
          | 
                 return fmt.Errorf("go/printer: unsupported node type %T", node)
  | 
      
      
         | 1016 | 
          | 
          | 
         }
  | 
      
      
         | 1017 | 
          | 
          | 
          
  | 
      
      
         | 1018 | 
          | 
          | 
         // ----------------------------------------------------------------------------
  | 
      
      
         | 1019 | 
          | 
          | 
         // Trimmer
  | 
      
      
         | 1020 | 
          | 
          | 
          
  | 
      
      
         | 1021 | 
          | 
          | 
         // A trimmer is an io.Writer filter for stripping tabwriter.Escape
  | 
      
      
         | 1022 | 
          | 
          | 
         // characters, trailing blanks and tabs, and for converting formfeed
  | 
      
      
         | 1023 | 
          | 
          | 
         // and vtab characters into newlines and htabs (in case no tabwriter
  | 
      
      
         | 1024 | 
          | 
          | 
         // is used). Text bracketed by tabwriter.Escape characters is passed
  | 
      
      
         | 1025 | 
          | 
          | 
         // through unchanged.
  | 
      
      
         | 1026 | 
          | 
          | 
         //
  | 
      
      
         | 1027 | 
          | 
          | 
         type trimmer struct {
  | 
      
      
         | 1028 | 
          | 
          | 
                 output io.Writer
  | 
      
      
         | 1029 | 
          | 
          | 
                 state  int
  | 
      
      
         | 1030 | 
          | 
          | 
                 space  bytes.Buffer
  | 
      
      
         | 1031 | 
          | 
          | 
         }
  | 
      
      
         | 1032 | 
          | 
          | 
          
  | 
      
      
         | 1033 | 
          | 
          | 
         // trimmer is implemented as a state machine.
  | 
      
      
         | 1034 | 
          | 
          | 
         // It can be in one of the following states:
  | 
      
      
         | 1035 | 
          | 
          | 
         const (
  | 
      
      
         | 1036 | 
          | 
          | 
                 inSpace  = iota // inside space
  | 
      
      
         | 1037 | 
          | 
          | 
                 inEscape        // inside text bracketed by tabwriter.Escapes
  | 
      
      
         | 1038 | 
          | 
          | 
                 inText          // inside text
  | 
      
      
         | 1039 | 
          | 
          | 
         )
  | 
      
      
         | 1040 | 
          | 
          | 
          
  | 
      
      
         | 1041 | 
          | 
          | 
         // Design note: It is tempting to eliminate extra blanks occurring in
  | 
      
      
         | 1042 | 
          | 
          | 
         //              whitespace in this function as it could simplify some
  | 
      
      
         | 1043 | 
          | 
          | 
         //              of the blanks logic in the node printing functions.
  | 
      
      
         | 1044 | 
          | 
          | 
         //              However, this would mess up any formatting done by
  | 
      
      
         | 1045 | 
          | 
          | 
         //              the tabwriter.
  | 
      
      
         | 1046 | 
          | 
          | 
          
  | 
      
      
         | 1047 | 
          | 
          | 
         var aNewline = []byte("\n")
  | 
      
      
         | 1048 | 
          | 
          | 
          
  | 
      
      
         | 1049 | 
          | 
          | 
         func (p *trimmer) Write(data []byte) (n int, err error) {
  | 
      
      
         | 1050 | 
          | 
          | 
                 // invariants:
  | 
      
      
         | 1051 | 
          | 
          | 
                 // p.state == inSpace:
  | 
      
      
         | 1052 | 
          | 
          | 
                 //      p.space is unwritten
  | 
      
      
         | 1053 | 
          | 
          | 
                 // p.state == inEscape, inText:
  | 
      
      
         | 1054 | 
          | 
          | 
                 //      data[m:n] is unwritten
  | 
      
      
         | 1055 | 
          | 
          | 
                 m := 0
  | 
      
      
         | 1056 | 
          | 
          | 
                 var b byte
  | 
      
      
         | 1057 | 
          | 
          | 
                 for n, b = range data {
  | 
      
      
         | 1058 | 
          | 
          | 
                         if b == '\v' {
  | 
      
      
         | 1059 | 
          | 
          | 
                                 b = '\t' // convert to htab
  | 
      
      
         | 1060 | 
          | 
          | 
                         }
  | 
      
      
         | 1061 | 
          | 
          | 
                         switch p.state {
  | 
      
      
         | 1062 | 
          | 
          | 
                         case inSpace:
  | 
      
      
         | 1063 | 
          | 
          | 
                                 switch b {
  | 
      
      
         | 1064 | 
          | 
          | 
                                 case '\t', ' ':
  | 
      
      
         | 1065 | 
          | 
          | 
                                         p.space.WriteByte(b) // WriteByte returns no errors
  | 
      
      
         | 1066 | 
          | 
          | 
                                 case '\n', '\f':
  | 
      
      
         | 1067 | 
          | 
          | 
                                         p.space.Reset() // discard trailing space
  | 
      
      
         | 1068 | 
          | 
          | 
                                         _, err = p.output.Write(aNewline)
  | 
      
      
         | 1069 | 
          | 
          | 
                                 case tabwriter.Escape:
  | 
      
      
         | 1070 | 
          | 
          | 
                                         _, err = p.output.Write(p.space.Bytes())
  | 
      
      
         | 1071 | 
          | 
          | 
                                         p.state = inEscape
  | 
      
      
         | 1072 | 
          | 
          | 
                                         m = n + 1 // +1: skip tabwriter.Escape
  | 
      
      
         | 1073 | 
          | 
          | 
                                 default:
  | 
      
      
         | 1074 | 
          | 
          | 
                                         _, err = p.output.Write(p.space.Bytes())
  | 
      
      
         | 1075 | 
          | 
          | 
                                         p.state = inText
  | 
      
      
         | 1076 | 
          | 
          | 
                                         m = n
  | 
      
      
         | 1077 | 
          | 
          | 
                                 }
  | 
      
      
         | 1078 | 
          | 
          | 
                         case inEscape:
  | 
      
      
         | 1079 | 
          | 
          | 
                                 if b == tabwriter.Escape {
  | 
      
      
         | 1080 | 
          | 
          | 
                                         _, err = p.output.Write(data[m:n])
  | 
      
      
         | 1081 | 
          | 
          | 
                                         p.state = inSpace
  | 
      
      
         | 1082 | 
          | 
          | 
                                         p.space.Reset()
  | 
      
      
         | 1083 | 
          | 
          | 
                                 }
  | 
      
      
         | 1084 | 
          | 
          | 
                         case inText:
  | 
      
      
         | 1085 | 
          | 
          | 
                                 switch b {
  | 
      
      
         | 1086 | 
          | 
          | 
                                 case '\t', ' ':
  | 
      
      
         | 1087 | 
          | 
          | 
                                         _, err = p.output.Write(data[m:n])
  | 
      
      
         | 1088 | 
          | 
          | 
                                         p.state = inSpace
  | 
      
      
         | 1089 | 
          | 
          | 
                                         p.space.Reset()
  | 
      
      
         | 1090 | 
          | 
          | 
                                         p.space.WriteByte(b) // WriteByte returns no errors
  | 
      
      
         | 1091 | 
          | 
          | 
                                 case '\n', '\f':
  | 
      
      
         | 1092 | 
          | 
          | 
                                         _, err = p.output.Write(data[m:n])
  | 
      
      
         | 1093 | 
          | 
          | 
                                         p.state = inSpace
  | 
      
      
         | 1094 | 
          | 
          | 
                                         p.space.Reset()
  | 
      
      
         | 1095 | 
          | 
          | 
                                         _, err = p.output.Write(aNewline)
  | 
      
      
         | 1096 | 
          | 
          | 
                                 case tabwriter.Escape:
  | 
      
      
         | 1097 | 
          | 
          | 
                                         _, err = p.output.Write(data[m:n])
  | 
      
      
         | 1098 | 
          | 
          | 
                                         p.state = inEscape
  | 
      
      
         | 1099 | 
          | 
          | 
                                         m = n + 1 // +1: skip tabwriter.Escape
  | 
      
      
         | 1100 | 
          | 
          | 
                                 }
  | 
      
      
         | 1101 | 
          | 
          | 
                         default:
  | 
      
      
         | 1102 | 
          | 
          | 
                                 panic("unreachable")
  | 
      
      
         | 1103 | 
          | 
          | 
                         }
  | 
      
      
         | 1104 | 
          | 
          | 
                         if err != nil {
  | 
      
      
         | 1105 | 
          | 
          | 
                                 return
  | 
      
      
         | 1106 | 
          | 
          | 
                         }
  | 
      
      
         | 1107 | 
          | 
          | 
                 }
  | 
      
      
         | 1108 | 
          | 
          | 
                 n = len(data)
  | 
      
      
         | 1109 | 
          | 
          | 
          
  | 
      
      
         | 1110 | 
          | 
          | 
                 switch p.state {
  | 
      
      
         | 1111 | 
          | 
          | 
                 case inEscape, inText:
  | 
      
      
         | 1112 | 
          | 
          | 
                         _, err = p.output.Write(data[m:n])
  | 
      
      
         | 1113 | 
          | 
          | 
                         p.state = inSpace
  | 
      
      
         | 1114 | 
          | 
          | 
                         p.space.Reset()
  | 
      
      
         | 1115 | 
          | 
          | 
                 }
  | 
      
      
         | 1116 | 
          | 
          | 
          
  | 
      
      
         | 1117 | 
          | 
          | 
                 return
  | 
      
      
         | 1118 | 
          | 
          | 
         }
  | 
      
      
         | 1119 | 
          | 
          | 
          
  | 
      
      
         | 1120 | 
          | 
          | 
         // ----------------------------------------------------------------------------
  | 
      
      
         | 1121 | 
          | 
          | 
         // Public interface
  | 
      
      
         | 1122 | 
          | 
          | 
          
  | 
      
      
         | 1123 | 
          | 
          | 
         // General printing is controlled with these Config.Mode flags.
  | 
      
      
         | 1124 | 
          | 
          | 
         const (
  | 
      
      
         | 1125 | 
          | 
          | 
                 RawFormat uint = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
  | 
      
      
         | 1126 | 
          | 
          | 
                 TabIndent                  // use tabs for indentation independent of UseSpaces
  | 
      
      
         | 1127 | 
          | 
          | 
                 UseSpaces                  // use spaces instead of tabs for alignment
  | 
      
      
         | 1128 | 
          | 
          | 
         )
  | 
      
      
         | 1129 | 
          | 
          | 
          
  | 
      
      
         | 1130 | 
          | 
          | 
         // A Config node controls the output of Fprint.
  | 
      
      
         | 1131 | 
          | 
          | 
         type Config struct {
  | 
      
      
         | 1132 | 
          | 
          | 
                 Mode     uint // default: 0
  | 
      
      
         | 1133 | 
          | 
          | 
                 Tabwidth int  // default: 8
  | 
      
      
         | 1134 | 
          | 
          | 
         }
  | 
      
      
         | 1135 | 
          | 
          | 
          
  | 
      
      
         | 1136 | 
          | 
          | 
         // fprint implements Fprint and takes a nodesSizes map for setting up the printer state.
  | 
      
      
         | 1137 | 
          | 
          | 
         func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (err error) {
  | 
      
      
         | 1138 | 
          | 
          | 
                 // print node
  | 
      
      
         | 1139 | 
          | 
          | 
                 var p printer
  | 
      
      
         | 1140 | 
          | 
          | 
                 p.init(cfg, fset, nodeSizes)
  | 
      
      
         | 1141 | 
          | 
          | 
                 if err = p.printNode(node); err != nil {
  | 
      
      
         | 1142 | 
          | 
          | 
                         return
  | 
      
      
         | 1143 | 
          | 
          | 
                 }
  | 
      
      
         | 1144 | 
          | 
          | 
                 // print outstanding comments
  | 
      
      
         | 1145 | 
          | 
          | 
                 p.impliedSemi = false // EOF acts like a newline
  | 
      
      
         | 1146 | 
          | 
          | 
                 p.flush(token.Position{Offset: infinity, Line: infinity}, token.EOF)
  | 
      
      
         | 1147 | 
          | 
          | 
          
  | 
      
      
         | 1148 | 
          | 
          | 
                 // redirect output through a trimmer to eliminate trailing whitespace
  | 
      
      
         | 1149 | 
          | 
          | 
                 // (Input to a tabwriter must be untrimmed since trailing tabs provide
  | 
      
      
         | 1150 | 
          | 
          | 
                 // formatting information. The tabwriter could provide trimming
  | 
      
      
         | 1151 | 
          | 
          | 
                 // functionality but no tabwriter is used when RawFormat is set.)
  | 
      
      
         | 1152 | 
          | 
          | 
                 output = &trimmer{output: output}
  | 
      
      
         | 1153 | 
          | 
          | 
          
  | 
      
      
         | 1154 | 
          | 
          | 
                 // redirect output through a tabwriter if necessary
  | 
      
      
         | 1155 | 
          | 
          | 
                 if cfg.Mode&RawFormat == 0 {
  | 
      
      
         | 1156 | 
          | 
          | 
                         minwidth := cfg.Tabwidth
  | 
      
      
         | 1157 | 
          | 
          | 
          
  | 
      
      
         | 1158 | 
          | 
          | 
                         padchar := byte('\t')
  | 
      
      
         | 1159 | 
          | 
          | 
                         if cfg.Mode&UseSpaces != 0 {
  | 
      
      
         | 1160 | 
          | 
          | 
                                 padchar = ' '
  | 
      
      
         | 1161 | 
          | 
          | 
                         }
  | 
      
      
         | 1162 | 
          | 
          | 
          
  | 
      
      
         | 1163 | 
          | 
          | 
                         twmode := tabwriter.DiscardEmptyColumns
  | 
      
      
         | 1164 | 
          | 
          | 
                         if cfg.Mode&TabIndent != 0 {
  | 
      
      
         | 1165 | 
          | 
          | 
                                 minwidth = 0
  | 
      
      
         | 1166 | 
          | 
          | 
                                 twmode |= tabwriter.TabIndent
  | 
      
      
         | 1167 | 
          | 
          | 
                         }
  | 
      
      
         | 1168 | 
          | 
          | 
          
  | 
      
      
         | 1169 | 
          | 
          | 
                         output = tabwriter.NewWriter(output, minwidth, cfg.Tabwidth, 1, padchar, twmode)
  | 
      
      
         | 1170 | 
          | 
          | 
                 }
  | 
      
      
         | 1171 | 
          | 
          | 
          
  | 
      
      
         | 1172 | 
          | 
          | 
                 // write printer result via tabwriter/trimmer to output
  | 
      
      
         | 1173 | 
          | 
          | 
                 if _, err = output.Write(p.output.Bytes()); err != nil {
  | 
      
      
         | 1174 | 
          | 
          | 
                         return
  | 
      
      
         | 1175 | 
          | 
          | 
                 }
  | 
      
      
         | 1176 | 
          | 
          | 
          
  | 
      
      
         | 1177 | 
          | 
          | 
                 // flush tabwriter, if any
  | 
      
      
         | 1178 | 
          | 
          | 
                 if tw, _ := (output).(*tabwriter.Writer); tw != nil {
  | 
      
      
         | 1179 | 
          | 
          | 
                         err = tw.Flush()
  | 
      
      
         | 1180 | 
          | 
          | 
                 }
  | 
      
      
         | 1181 | 
          | 
          | 
          
  | 
      
      
         | 1182 | 
          | 
          | 
                 return
  | 
      
      
         | 1183 | 
          | 
          | 
         }
  | 
      
      
         | 1184 | 
          | 
          | 
          
  | 
      
      
         | 1185 | 
          | 
          | 
         // A CommentedNode bundles an AST node and corresponding comments.
  | 
      
      
         | 1186 | 
          | 
          | 
         // It may be provided as argument to any of the Fprint functions.
  | 
      
      
         | 1187 | 
          | 
          | 
         //
  | 
      
      
         | 1188 | 
          | 
          | 
         type CommentedNode struct {
  | 
      
      
         | 1189 | 
          | 
          | 
                 Node     interface{} // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
  | 
      
      
         | 1190 | 
          | 
          | 
                 Comments []*ast.CommentGroup
  | 
      
      
         | 1191 | 
          | 
          | 
         }
  | 
      
      
         | 1192 | 
          | 
          | 
          
  | 
      
      
         | 1193 | 
          | 
          | 
         // Fprint "pretty-prints" an AST node to output for a given configuration cfg.
  | 
      
      
         | 1194 | 
          | 
          | 
         // Position information is interpreted relative to the file set fset.
  | 
      
      
         | 1195 | 
          | 
          | 
         // The node type must be *ast.File, *CommentedNode, or assignment-compatible
  | 
      
      
         | 1196 | 
          | 
          | 
         // to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.
  | 
      
      
         | 1197 | 
          | 
          | 
         //
  | 
      
      
         | 1198 | 
          | 
          | 
         func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) error {
  | 
      
      
         | 1199 | 
          | 
          | 
                 return cfg.fprint(output, fset, node, make(map[ast.Node]int))
  | 
      
      
         | 1200 | 
          | 
          | 
         }
  | 
      
      
         | 1201 | 
          | 
          | 
          
  | 
      
      
         | 1202 | 
          | 
          | 
         // Fprint "pretty-prints" an AST node to output.
  | 
      
      
         | 1203 | 
          | 
          | 
         // It calls Config.Fprint with default settings.
  | 
      
      
         | 1204 | 
          | 
          | 
         //
  | 
      
      
         | 1205 | 
          | 
          | 
         func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error {
  | 
      
      
         | 1206 | 
          | 
          | 
                 return (&Config{Tabwidth: 8}).Fprint(output, fset, node)
  | 
      
      
         | 1207 | 
          | 
          | 
         }
  |