OSDN Git Service

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