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.
5 // Package printer implements printing of AST nodes.
21 const debug = false // enable for debugging
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('<')
37 esc = []byte{tabwriter.Escape}
39 htabs = []byte("\t\t\t\t\t\t\t\t")
40 newlines = []byte("\n\n\n\n\n\n\n\n") // more than the max determined by nlines
41 formfeeds = []byte("\f\f\f\f\f\f\f\f") // more than the max determined by nlines
45 var noPos token.Position // use noPos when a position is needed but not known
46 var infinity = 1 << 30
48 // Use ignoreMultiLine if the multiLine information is not important.
49 var ignoreMultiLine = new(bool)
51 // A pmode value represents the current printer mode.
55 inLiteral pmode = 1 << iota
59 // local error wrapper so we can distinguish errors we want to return
60 // as errors from genuine panics (which we don't want to return as errors)
66 // Configuration (does not change after initialization)
72 written int // number of bytes written
73 indent int // current indentation
74 mode pmode // current printer mode
75 lastTok token.Token // the last token printed (token.ILLEGAL if it's whitespace)
78 wsbuf []whiteSpace // delayed white space
79 litbuf bytes.Buffer // for creation of escaped literals and comments
81 // The (possibly estimated) position in the generated output;
82 // in AST space (i.e., pos is set whenever a token position is
83 // known accurately, and updated dependending on what has been
87 // The value of pos immediately after the last item has been
88 // written using writeItem.
91 // The list of all source comments, in order of appearance.
92 comments []*ast.CommentGroup // may be nil
93 cindex int // current comment index
94 useNodeComments bool // if not set, ignore lead and line comments of nodes
96 // Cache of already computed node sizes.
97 nodeSizes map[ast.Node]int
100 func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) {
104 p.wsbuf = make([]whiteSpace, 0, 16) // whitespace sequences are short
105 p.nodeSizes = nodeSizes
108 func (p *printer) internalError(msg ...interface{}) {
110 fmt.Print(p.pos.String() + ": ")
116 // escape escapes string s by bracketing it with tabwriter.Escape.
117 // Escaped strings pass through tabwriter unchanged. (Note that
118 // valid Go programs cannot contain tabwriter.Escape bytes since
119 // they do not appear in legal UTF-8 sequences).
121 func (p *printer) escape(s string) string {
123 p.litbuf.WriteByte(tabwriter.Escape)
124 p.litbuf.WriteString(s)
125 p.litbuf.WriteByte(tabwriter.Escape)
126 return p.litbuf.String()
129 // nlines returns the adjusted number of linebreaks given the desired number
130 // of breaks n such that min <= result <= max.
132 func (p *printer) nlines(n, min int) int {
133 const max = 2 // max. number of newlines
143 // write0 writes raw (uninterpreted) data to p.output and handles errors.
144 // write0 does not indent after newlines, and does not HTML-escape or update p.pos.
146 func (p *printer) write0(data []byte) {
148 n, err := p.output.Write(data)
156 // write interprets data and writes it to p.output. It inserts indentation
157 // after a line break unless in a tabwriter escape sequence.
158 // It updates p.pos as a side-effect.
160 func (p *printer) write(data []byte) {
162 for i, b := range data {
165 // write segment ending in b
166 p.write0(data[i0 : i+1])
169 p.pos.Offset += i + 1 - i0
173 if p.mode&inLiteral == 0 {
175 // use "hard" htabs - indentation columns
176 // must not be discarded by the tabwriter
178 for ; j > len(htabs); j -= len(htabs) {
184 p.pos.Offset += p.indent
185 p.pos.Column += p.indent
188 // next segment start
191 case tabwriter.Escape:
194 // ignore escape chars introduced by printer - they are
195 // invisible and must not affect p.pos (was issue #1089)
201 // write remaining segment
210 func (p *printer) writeNewlines(n int, useFF bool) {
214 p.write(formfeeds[0:n])
216 p.write(newlines[0:n])
221 // writeItem writes data at position pos. data is the text corresponding to
222 // a single lexical token, but may also be comment text. pos is the actual
223 // (or at least very accurately estimated) position of the data in the original
224 // source text. writeItem updates p.last to the position immediately following
227 func (p *printer) writeItem(pos token.Position, data string) {
229 // continue with previous position if we don't have a valid pos
230 if p.last.IsValid() && p.last.Filename != pos.Filename {
231 // the file has changed - reset state
232 // (used when printing merged ASTs of different files
233 // e.g., the result of ast.MergePackageFiles)
236 p.wsbuf = p.wsbuf[0:0]
241 // do not update p.pos - use write0
242 _, filename := filepath.Split(pos.Filename)
243 p.write0([]byte(fmt.Sprintf("[%s:%d:%d]", filename, pos.Line, pos.Column)))
245 p.write([]byte(data))
249 const linePrefix = "//line "
251 // writeCommentPrefix writes the whitespace before a comment.
252 // If there is any pending whitespace, it consumes as much of
253 // it as is likely to help position the comment nicely.
254 // pos is the comment position, next the position of the item
255 // after all pending comments, prev is the previous comment in
256 // a group of comments (or nil), and isKeyword indicates if the
257 // next item is a keyword.
259 func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *ast.Comment, isKeyword bool) {
261 // the comment is the first item to be printed - don't write any whitespace
265 if pos.IsValid() && pos.Filename != p.last.Filename {
266 // comment in a different file - separate with newlines (writeNewlines will limit the number)
267 p.writeNewlines(10, true)
271 if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') {
272 // comment on the same line as last item:
273 // separate with at least one separator
276 // first comment of a comment group
278 for i, ch := range p.wsbuf {
281 // ignore any blanks before a comment
285 // respect existing tabs - important
286 // for proper formatting of commented structs
290 // apply pending indentation
298 // make sure there is at least one separator
300 if pos.Line == next.Line {
301 // next item is on the same line as the comment
302 // (which must be a /*-style comment): separate
303 // with a blank instead of a tab
311 // comment on a different line:
312 // separate with at least one line break
314 // first comment of a comment group
316 for i, ch := range p.wsbuf {
319 // ignore any horizontal whitespace before line breaks
323 // apply pending indentation
326 // if the next token is a keyword, apply the outdent
327 // if it appears that the comment is aligned with the
328 // keyword; otherwise assume the outdent is part of a
329 // closing block and stop (this scenario appears with
330 // comments before a case label where the comments
331 // apply to the next case instead of the current one)
332 if isKeyword && pos.Column == next.Column {
335 case newline, formfeed:
336 // TODO(gri): may want to keep formfeed info in some cases
345 // turn off indent if we're about to print a line directive.
347 if strings.HasPrefix(comment.Text, linePrefix) {
351 // use formfeeds to break columns before a comment;
352 // this is analogous to using formfeeds to separate
353 // individual lines of /*-style comments - but make
354 // sure there is at least one line break if the previous
355 // comment was a line comment
356 n := pos.Line - p.last.Line // if !pos.IsValid(), pos.Line == 0, and n will be 0
357 if n <= 0 && prev != nil && prev.Text[1] == '/' {
360 p.writeNewlines(n, true)
365 // TODO(gri): It should be possible to convert the code below from using
366 // []byte to string and in the process eliminate some conversions.
368 // Split comment text into lines
369 func split(text []byte) [][]byte {
370 // count lines (comment text never ends in a newline)
372 for _, c := range text {
379 lines := make([][]byte, n)
382 for j, c := range text {
384 lines[n] = text[i:j] // exclude newline
385 i = j + 1 // discard newline
394 func isBlank(s []byte) bool {
395 for _, b := range s {
403 func commonPrefix(a, b []byte) []byte {
405 for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
411 func stripCommonPrefix(lines [][]byte) {
413 return // at most one line - nothing to do
417 // The heuristic in this function tries to handle a few
418 // common patterns of /*-style comments: Comments where
419 // the opening /* and closing */ are aligned and the
420 // rest of the comment text is aligned and indented with
421 // blanks or tabs, cases with a vertical "line of stars"
422 // on the left, and cases where the closing */ is on the
423 // same line as the last comment text.
425 // Compute maximum common white prefix of all but the first,
426 // last, and blank lines, and replace blank lines with empty
427 // lines (the first line starts with /* and has no prefix).
428 // In case of two-line comments, consider the last line for
429 // the prefix computation since otherwise the prefix would
432 // Note that the first and last line are never empty (they
433 // contain the opening /* and closing */ respectively) and
434 // thus they can be ignored by the blank line check.
437 for i, line := range lines[1 : len(lines)-1] {
440 lines[1+i] = nil // range starts at line 1
442 prefix = commonPrefix(line, line)
444 prefix = commonPrefix(prefix, line)
447 } else { // len(lines) == 2
449 prefix = commonPrefix(line, line)
453 * Check for vertical "line of stars" and correct prefix accordingly.
456 if i := bytes.Index(prefix, []byte{'*'}); i >= 0 {
457 // Line of stars present.
458 if i > 0 && prefix[i-1] == ' ' {
459 i-- // remove trailing blank from prefix so stars remain aligned
464 // No line of stars present.
465 // Determine the white space on the first line after the /*
466 // and before the beginning of the comment text, assume two
467 // blanks instead of the /* unless the first character after
468 // the /* is a tab. If the first comment line is empty but
469 // for the opening /*, assume up to 3 blanks or a tab. This
470 // whitespace may be found as suffix in the common prefix.
472 if isBlank(first[2:]) {
473 // no comment text on the first line:
474 // reduce prefix by up to 3 blanks or a tab
475 // if present - this keeps comment text indented
476 // relative to the /* and */'s if it was indented
477 // in the first place
479 for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ {
482 if i == len(prefix) && i > 0 && prefix[i-1] == '\t' {
487 // comment text on the first line
488 suffix := make([]byte, len(first))
489 n := 2 // start after opening /*
490 for n < len(first) && first[n] <= ' ' {
494 if n > 2 && suffix[2] == '\t' {
495 // assume the '\t' compensates for the /*
498 // otherwise assume two blanks
499 suffix[0], suffix[1] = ' ', ' '
502 // Shorten the computed common prefix by the length of
503 // suffix, if it is found as suffix of the prefix.
504 if bytes.HasSuffix(prefix, suffix) {
505 prefix = prefix[0 : len(prefix)-len(suffix)]
510 // Handle last line: If it only contains a closing */, align it
511 // with the opening /*, otherwise align the text with the other
513 last := lines[len(lines)-1]
514 closing := []byte("*/")
515 i := bytes.Index(last, closing)
516 if isBlank(last[0:i]) {
517 // last line only contains closing */
520 // insert an aligning blank
523 lines[len(lines)-1] = bytes.Join([][]byte{prefix, closing}, sep)
525 // last line contains more comment text - assume
526 // it is aligned like the other lines
527 prefix = commonPrefix(prefix, last)
530 // Remove the common prefix from all but the first and empty lines.
531 for i, line := range lines[1:] {
533 lines[1+i] = line[len(prefix):] // range starts at line 1
538 func (p *printer) writeComment(comment *ast.Comment) {
541 if strings.HasPrefix(text, linePrefix) {
542 pos := strings.TrimSpace(text[len(linePrefix):])
543 i := strings.LastIndex(pos, ":")
545 // The line directive we are about to print changed
546 // the Filename and Line number used by go/token
547 // as it was reading the input originally.
548 // In order to match the original input, we have to
549 // update our own idea of the file and line number
550 // accordingly, after printing the directive.
552 line, _ := strconv.Atoi(string(pos[i+1:]))
554 p.pos.Filename = string(file)
561 // shortcut common case of //-style comments
563 p.writeItem(p.fset.Position(comment.Pos()), p.escape(text))
567 // for /*-style comments, print line by line and let the
568 // write function take care of the proper indentation
569 lines := split([]byte(text))
570 stripCommonPrefix(lines)
572 // write comment lines, separated by formfeed,
573 // without a line break after the last line
574 linebreak := formfeeds[0:1]
575 pos := p.fset.Position(comment.Pos())
576 for i, line := range lines {
582 p.writeItem(pos, p.escape(string(line)))
587 // writeCommentSuffix writes a line break after a comment if indicated
588 // and processes any leftover indentation information. If a line break
589 // is needed, the kind of break (newline vs formfeed) depends on the
590 // pending whitespace. writeCommentSuffix returns true if a pending
591 // formfeed was dropped from the whitespace buffer.
593 func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) {
594 for i, ch := range p.wsbuf {
597 // ignore trailing whitespace
599 case indent, unindent:
600 // don't lose indentation information
601 case newline, formfeed:
602 // if we need a line break, keep exactly one
603 // but remember if we dropped any formfeeds
605 needsLinebreak = false
614 p.writeWhitespace(len(p.wsbuf))
616 // make sure we have a line break
618 p.write([]byte{'\n'})
624 // intersperseComments consumes all comments that appear before the next token
625 // tok and prints it together with the buffered whitespace (i.e., the whitespace
626 // that needs to be written before the next token). A heuristic is used to mix
627 // the comments and whitespace. intersperseComments returns true if a pending
628 // formfeed was dropped from the whitespace buffer.
630 func (p *printer) intersperseComments(next token.Position, tok token.Token) (droppedFF bool) {
631 var last *ast.Comment
632 for ; p.commentBefore(next); p.cindex++ {
633 for _, c := range p.comments[p.cindex].List {
634 p.writeCommentPrefix(p.fset.Position(c.Pos()), next, last, c, tok.IsKeyword())
641 if last.Text[1] == '*' && p.fset.Position(last.Pos()).Line == next.Line {
642 // the last comment is a /*-style comment and the next item
643 // follows on the same line: separate with an extra blank
646 // ensure that there is a line break after a //-style comment,
647 // before a closing '}' unless explicitly disabled, or at eof
649 last.Text[1] == '/' ||
650 tok == token.RBRACE && p.mode&noExtraLinebreak == 0 ||
652 return p.writeCommentSuffix(needsLinebreak)
655 // no comment was written - we should never reach here since
656 // intersperseComments should not be called in that case
657 p.internalError("intersperseComments called without pending comments")
661 // whiteWhitespace writes the first n whitespace entries.
662 func (p *printer) writeWhitespace(n int) {
665 for i := 0; i < n; i++ {
666 switch ch := p.wsbuf[i]; ch {
674 p.internalError("negative indentation:", p.indent)
677 case newline, formfeed:
678 // A line break immediately followed by a "correcting"
679 // unindent is swapped with the unindent - this permits
680 // proper label positioning. If a comment is between
681 // the line break and the label, the unindent is not
682 // part of the comment whitespace prefix and the comment
683 // will be positioned correctly indented.
684 if i+1 < n && p.wsbuf[i+1] == unindent {
685 // Use a formfeed to terminate the current section.
686 // Otherwise, a long label name on the next line leading
687 // to a wide column may increase the indentation column
688 // of lines before the label; effectively leading to wrong
690 p.wsbuf[i], p.wsbuf[i+1] = unindent, formfeed
701 // shift remaining entries down
703 for ; n < len(p.wsbuf); n++ {
704 p.wsbuf[i] = p.wsbuf[n]
707 p.wsbuf = p.wsbuf[0:i]
710 // ----------------------------------------------------------------------------
711 // Printing interface
714 func mayCombine(prev token.Token, next byte) (b bool) {
717 b = next == '.' // 1.
719 b = next == '+' // ++
721 b = next == '-' // --
723 b = next == '*' // /*
725 b = next == '-' || next == '<' // <- or <<
727 b = next == '&' || next == '^' // && or &^
732 // print prints a list of "items" (roughly corresponding to syntactic
733 // tokens, but also including whitespace and formatting information).
734 // It is the only print function that should be called directly from
735 // any of the AST printing functions in nodes.go.
737 // Whitespace is accumulated until a non-whitespace token appears. Any
738 // comments that need to appear before that token are printed first,
739 // taking into account the amount and structure of any pending white-
740 // space for best comment placement. Then, any leftover whitespace is
741 // printed, followed by the actual token.
743 func (p *printer) print(args ...interface{}) {
744 for _, f := range args {
745 next := p.pos // estimated position of next item
749 switch x := f.(type) {
751 // toggle printer mode
755 // don't add ignore's to the buffer; they
756 // may screw up "correcting" unindents (see
761 if i == cap(p.wsbuf) {
762 // Whitespace sequences are very short so this should
763 // never happen. Handle gracefully (but possibly with
764 // bad comment placement) if it does happen.
768 p.wsbuf = p.wsbuf[0 : i+1]
774 data = p.escape(x.Value)
778 if mayCombine(p.lastTok, s[0]) {
779 // the previous and the current token must be
780 // separated by a blank otherwise they combine
781 // into a different incorrect token sequence
782 // (except for token.INT followed by a '.' this
783 // should never happen because it is taken care
784 // of via binary expression formatting)
785 if len(p.wsbuf) != 0 {
786 p.internalError("whitespace buffer not empty")
788 p.wsbuf = p.wsbuf[0:1]
795 next = p.fset.Position(x) // accurate position of next item
799 fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f)
800 panic("go/printer type")
806 droppedFF := p.flush(next, tok)
808 // intersperse extra newlines if present in the source
809 // (don't do this in flush as it will cause extra newlines
810 // at the end of a file) - use formfeeds if we dropped one
812 p.writeNewlines(next.Line-p.pos.Line, droppedFF)
814 p.writeItem(next, data)
819 // commentBefore returns true iff the current comment occurs
820 // before the next position in the source code.
822 func (p *printer) commentBefore(next token.Position) bool {
823 return p.cindex < len(p.comments) && p.fset.Position(p.comments[p.cindex].List[0].Pos()).Offset < next.Offset
826 // Flush prints any pending comments and whitespace occurring
827 // textually before the position of the next token tok. Flush
828 // returns true if a pending formfeed character was dropped
829 // from the whitespace buffer as a result of interspersing
832 func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) {
833 if p.commentBefore(next) {
834 // if there are comments before the next item, intersperse them
835 droppedFF = p.intersperseComments(next, tok)
837 // otherwise, write any leftover whitespace
838 p.writeWhitespace(len(p.wsbuf))
843 // ----------------------------------------------------------------------------
846 // A trimmer is an io.Writer filter for stripping tabwriter.Escape
847 // characters, trailing blanks and tabs, and for converting formfeed
848 // and vtab characters into newlines and htabs (in case no tabwriter
849 // is used). Text bracketed by tabwriter.Escape characters is passed
850 // through unchanged.
852 type trimmer struct {
858 // trimmer is implemented as a state machine.
859 // It can be in one of the following states:
861 inSpace = iota // inside space
862 inEscape // inside text bracketed by tabwriter.Escapes
863 inText // inside text
866 // Design note: It is tempting to eliminate extra blanks occurring in
867 // whitespace in this function as it could simplify some
868 // of the blanks logic in the node printing functions.
869 // However, this would mess up any formatting done by
872 func (p *trimmer) Write(data []byte) (n int, err error) {
874 // p.state == inSpace:
875 // p.space is unwritten
876 // p.state == inEscape, inText:
877 // data[m:n] is unwritten
880 for n, b = range data {
882 b = '\t' // convert to htab
888 p.space.WriteByte(b) // WriteByte returns no errors
890 p.space.Reset() // discard trailing space
891 _, err = p.output.Write(newlines[0:1]) // write newline
892 case tabwriter.Escape:
893 _, err = p.output.Write(p.space.Bytes())
895 m = n + 1 // +1: skip tabwriter.Escape
897 _, err = p.output.Write(p.space.Bytes())
902 if b == tabwriter.Escape {
903 _, err = p.output.Write(data[m:n])
910 _, err = p.output.Write(data[m:n])
913 p.space.WriteByte(b) // WriteByte returns no errors
915 _, err = p.output.Write(data[m:n])
918 _, err = p.output.Write(newlines[0:1]) // write newline
919 case tabwriter.Escape:
920 _, err = p.output.Write(data[m:n])
922 m = n + 1 // +1: skip tabwriter.Escape
934 case inEscape, inText:
935 _, err = p.output.Write(data[m:n])
943 // ----------------------------------------------------------------------------
946 // General printing is controlled with these Config.Mode flags.
948 RawFormat uint = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
949 TabIndent // use tabs for indentation independent of UseSpaces
950 UseSpaces // use spaces instead of tabs for alignment
953 // A Config node controls the output of Fprint.
955 Mode uint // default: 0
956 Tabwidth int // default: 8
959 // fprint implements Fprint and takes a nodesSizes map for setting up the printer state.
960 func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (written int, err error) {
961 // redirect output through a trimmer to eliminate trailing whitespace
962 // (Input to a tabwriter must be untrimmed since trailing tabs provide
963 // formatting information. The tabwriter could provide trimming
964 // functionality but no tabwriter is used when RawFormat is set.)
965 output = &trimmer{output: output}
967 // setup tabwriter if needed and redirect output
968 var tw *tabwriter.Writer
969 if cfg.Mode&RawFormat == 0 {
970 minwidth := cfg.Tabwidth
972 padchar := byte('\t')
973 if cfg.Mode&UseSpaces != 0 {
977 twmode := tabwriter.DiscardEmptyColumns
978 if cfg.Mode&TabIndent != 0 {
980 twmode |= tabwriter.TabIndent
983 tw = tabwriter.NewWriter(output, minwidth, cfg.Tabwidth, 1, padchar, twmode)
989 p.init(output, cfg, fset, nodeSizes)
992 if e := recover(); e != nil {
993 err = e.(osError).err // re-panics if it's not a local osError
998 switch n := node.(type) {
1000 p.useNodeComments = true
1001 p.expr(n, ignoreMultiLine)
1003 p.useNodeComments = true
1004 // A labeled statement will un-indent to position the
1005 // label. Set indent to 1 so we don't get indent "underflow".
1006 if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt {
1009 p.stmt(n, false, ignoreMultiLine)
1011 p.useNodeComments = true
1012 p.decl(n, ignoreMultiLine)
1014 p.useNodeComments = true
1015 p.spec(n, 1, false, ignoreMultiLine)
1017 p.comments = n.Comments
1018 p.useNodeComments = n.Comments == nil
1021 panic(osError{fmt.Errorf("printer.Fprint: unsupported node type %T", n)})
1023 p.flush(token.Position{Offset: infinity, Line: infinity}, token.EOF)
1025 // flush tabwriter, if any
1027 tw.Flush() // ignore errors
1033 // Fprint "pretty-prints" an AST node to output and returns the number
1034 // of bytes written and an error (if any) for a given configuration cfg.
1035 // Position information is interpreted relative to the file set fset.
1036 // The node type must be *ast.File, or assignment-compatible to ast.Expr,
1037 // ast.Decl, ast.Spec, or ast.Stmt.
1039 func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, error) {
1040 return cfg.fprint(output, fset, node, make(map[ast.Node]int))
1043 // Fprint "pretty-prints" an AST node to output.
1044 // It calls Config.Fprint with default settings.
1046 func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error {
1047 _, err := (&Config{Tabwidth: 8}).Fprint(output, fset, node) // don't care about number of bytes written