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 // The printer package 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('<')
38 esc = []byte{tabwriter.Escape}
40 htabs = []byte("\t\t\t\t\t\t\t\t")
41 newlines = []byte("\n\n\n\n\n\n\n\n") // more than the max determined by nlines
42 formfeeds = []byte("\f\f\f\f\f\f\f\f") // more than the max determined by nlines
47 var noPos token.Position // use noPos when a position is needed but not known
48 var infinity = 1 << 30
51 // Use ignoreMultiLine if the multiLine information is not important.
52 var ignoreMultiLine = new(bool)
55 // A pmode value represents the current printer mode.
59 inLiteral pmode = 1 << iota
65 // Configuration (does not change after initialization)
72 nesting int // nesting level (0: top-level (package scope), >0: functions/decls.)
73 written int // number of bytes written
74 indent int // current indentation
75 mode pmode // current printer mode
76 lastTok token.Token // the last token printed (token.ILLEGAL if it's whitespace)
79 wsbuf []whiteSpace // delayed white space
80 litbuf bytes.Buffer // for creation of escaped literals and comments
82 // The (possibly estimated) position in the generated output;
83 // in AST space (i.e., pos is set whenever a token position is
84 // known accurately, and updated dependending on what has been
88 // The value of pos immediately after the last item has been
89 // written using writeItem.
92 // The list of all source comments, in order of appearance.
93 comments []*ast.CommentGroup // may be nil
94 cindex int // current comment index
95 useNodeComments bool // if not set, ignore lead and line comments of nodes
97 // Cache of already computed node sizes.
98 nodeSizes map[ast.Node]int
102 func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) {
106 p.errors = make(chan os.Error)
107 p.wsbuf = make([]whiteSpace, 0, 16) // whitespace sequences are short
108 p.nodeSizes = nodeSizes
112 func (p *printer) internalError(msg ...interface{}) {
114 fmt.Print(p.pos.String() + ": ")
121 // escape escapes string s by bracketing it with tabwriter.Escape.
122 // Escaped strings pass through tabwriter unchanged. (Note that
123 // valid Go programs cannot contain tabwriter.Escape bytes since
124 // they do not appear in legal UTF-8 sequences).
126 func (p *printer) escape(s string) string {
128 p.litbuf.WriteByte(tabwriter.Escape)
129 p.litbuf.WriteString(s)
130 p.litbuf.WriteByte(tabwriter.Escape)
131 return p.litbuf.String()
135 // nlines returns the adjusted number of linebreaks given the desired number
136 // of breaks n such that min <= result <= max where max depends on the current
139 func (p *printer) nlines(n, min int) int {
143 max := 3 // max. number of newlines at the top level (p.nesting == 0)
145 max = 2 // max. number of newlines everywhere else
154 // write0 writes raw (uninterpreted) data to p.output and handles errors.
155 // write0 does not indent after newlines, and does not HTML-escape or update p.pos.
157 func (p *printer) write0(data []byte) {
159 n, err := p.output.Write(data)
169 // write interprets data and writes it to p.output. It inserts indentation
170 // after a line break unless in a tabwriter escape sequence.
171 // It updates p.pos as a side-effect.
173 func (p *printer) write(data []byte) {
175 for i, b := range data {
178 // write segment ending in b
179 p.write0(data[i0 : i+1])
182 p.pos.Offset += i + 1 - i0
186 if p.mode&inLiteral == 0 {
188 // use "hard" htabs - indentation columns
189 // must not be discarded by the tabwriter
191 for ; j > len(htabs); j -= len(htabs) {
197 p.pos.Offset += p.indent
198 p.pos.Column += p.indent
201 // next segment start
204 case tabwriter.Escape:
207 // ignore escape chars introduced by printer - they are
208 // invisible and must not affect p.pos (was issue #1089)
214 // write remaining segment
224 func (p *printer) writeNewlines(n int, useFF bool) {
228 p.write(formfeeds[0:n])
230 p.write(newlines[0:n])
236 // writeItem writes data at position pos. data is the text corresponding to
237 // a single lexical token, but may also be comment text. pos is the actual
238 // (or at least very accurately estimated) position of the data in the original
239 // source text. writeItem updates p.last to the position immediately following
242 func (p *printer) writeItem(pos token.Position, data string) {
244 // continue with previous position if we don't have a valid pos
245 if p.last.IsValid() && p.last.Filename != pos.Filename {
246 // the file has changed - reset state
247 // (used when printing merged ASTs of different files
248 // e.g., the result of ast.MergePackageFiles)
251 p.wsbuf = p.wsbuf[0:0]
256 // do not update p.pos - use write0
257 _, filename := filepath.Split(pos.Filename)
258 p.write0([]byte(fmt.Sprintf("[%s:%d:%d]", filename, pos.Line, pos.Column)))
260 p.write([]byte(data))
265 // writeCommentPrefix writes the whitespace before a comment.
266 // If there is any pending whitespace, it consumes as much of
267 // it as is likely to help position the comment nicely.
268 // pos is the comment position, next the position of the item
269 // after all pending comments, prev is the previous comment in
270 // a group of comments (or nil), and isKeyword indicates if the
271 // next item is a keyword.
273 func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment, isKeyword bool) {
275 // the comment is the first item to be printed - don't write any whitespace
279 if pos.IsValid() && pos.Filename != p.last.Filename {
280 // comment in a different file - separate with newlines (writeNewlines will limit the number)
281 p.writeNewlines(10, true)
285 if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') {
286 // comment on the same line as last item:
287 // separate with at least one separator
290 // first comment of a comment group
292 for i, ch := range p.wsbuf {
295 // ignore any blanks before a comment
299 // respect existing tabs - important
300 // for proper formatting of commented structs
304 // apply pending indentation
312 // make sure there is at least one separator
314 if pos.Line == next.Line {
315 // next item is on the same line as the comment
316 // (which must be a /*-style comment): separate
317 // with a blank instead of a tab
325 // comment on a different line:
326 // separate with at least one line break
328 // first comment of a comment group
330 for i, ch := range p.wsbuf {
333 // ignore any horizontal whitespace before line breaks
337 // apply pending indentation
340 // if the next token is a keyword, apply the outdent
341 // if it appears that the comment is aligned with the
342 // keyword; otherwise assume the outdent is part of a
343 // closing block and stop (this scenario appears with
344 // comments before a case label where the comments
345 // apply to the next case instead of the current one)
346 if isKeyword && pos.Column == next.Column {
349 case newline, formfeed:
350 // TODO(gri): may want to keep formfeed info in some cases
358 // use formfeeds to break columns before a comment;
359 // this is analogous to using formfeeds to separate
360 // individual lines of /*-style comments - but make
361 // sure there is at least one line break if the previous
362 // comment was a line comment
363 n := pos.Line - p.last.Line // if !pos.IsValid(), pos.Line == 0, and n will be 0
364 if n <= 0 && prev != nil && prev.Text[1] == '/' {
367 p.writeNewlines(n, true)
372 // TODO(gri): It should be possible to convert the code below from using
373 // []byte to string and in the process eliminate some conversions.
375 // Split comment text into lines
376 func split(text []byte) [][]byte {
377 // count lines (comment text never ends in a newline)
379 for _, c := range text {
386 lines := make([][]byte, n)
389 for j, c := range text {
391 lines[n] = text[i:j] // exclude newline
392 i = j + 1 // discard newline
402 func isBlank(s []byte) bool {
403 for _, b := range s {
412 func commonPrefix(a, b []byte) []byte {
414 for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
421 func stripCommonPrefix(lines [][]byte) {
423 return // at most one line - nothing to do
427 // The heuristic in this function tries to handle a few
428 // common patterns of /*-style comments: Comments where
429 // the opening /* and closing */ are aligned and the
430 // rest of the comment text is aligned and indented with
431 // blanks or tabs, cases with a vertical "line of stars"
432 // on the left, and cases where the closing */ is on the
433 // same line as the last comment text.
435 // Compute maximum common white prefix of all but the first,
436 // last, and blank lines, and replace blank lines with empty
437 // lines (the first line starts with /* and has no prefix).
438 // In case of two-line comments, consider the last line for
439 // the prefix computation since otherwise the prefix would
442 // Note that the first and last line are never empty (they
443 // contain the opening /* and closing */ respectively) and
444 // thus they can be ignored by the blank line check.
447 for i, line := range lines[1 : len(lines)-1] {
450 lines[1+i] = nil // range starts at line 1
452 prefix = commonPrefix(line, line)
454 prefix = commonPrefix(prefix, line)
457 } else { // len(lines) == 2
459 prefix = commonPrefix(line, line)
463 * Check for vertical "line of stars" and correct prefix accordingly.
466 if i := bytes.Index(prefix, []byte{'*'}); i >= 0 {
467 // Line of stars present.
468 if i > 0 && prefix[i-1] == ' ' {
469 i-- // remove trailing blank from prefix so stars remain aligned
474 // No line of stars present.
475 // Determine the white space on the first line after the /*
476 // and before the beginning of the comment text, assume two
477 // blanks instead of the /* unless the first character after
478 // the /* is a tab. If the first comment line is empty but
479 // for the opening /*, assume up to 3 blanks or a tab. This
480 // whitespace may be found as suffix in the common prefix.
482 if isBlank(first[2:]) {
483 // no comment text on the first line:
484 // reduce prefix by up to 3 blanks or a tab
485 // if present - this keeps comment text indented
486 // relative to the /* and */'s if it was indented
487 // in the first place
489 for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ {
492 if i == len(prefix) && i > 0 && prefix[i-1] == '\t' {
497 // comment text on the first line
498 suffix := make([]byte, len(first))
499 n := 2 // start after opening /*
500 for n < len(first) && first[n] <= ' ' {
504 if n > 2 && suffix[2] == '\t' {
505 // assume the '\t' compensates for the /*
508 // otherwise assume two blanks
509 suffix[0], suffix[1] = ' ', ' '
512 // Shorten the computed common prefix by the length of
513 // suffix, if it is found as suffix of the prefix.
514 if bytes.HasSuffix(prefix, suffix) {
515 prefix = prefix[0 : len(prefix)-len(suffix)]
520 // Handle last line: If it only contains a closing */, align it
521 // with the opening /*, otherwise align the text with the other
523 last := lines[len(lines)-1]
524 closing := []byte("*/")
525 i := bytes.Index(last, closing)
526 if isBlank(last[0:i]) {
527 // last line only contains closing */
530 // insert an aligning blank
533 lines[len(lines)-1] = bytes.Join([][]byte{prefix, closing}, sep)
535 // last line contains more comment text - assume
536 // it is aligned like the other lines
537 prefix = commonPrefix(prefix, last)
540 // Remove the common prefix from all but the first and empty lines.
541 for i, line := range lines[1:] {
543 lines[1+i] = line[len(prefix):] // range starts at line 1
549 func (p *printer) writeComment(comment *ast.Comment) {
552 // shortcut common case of //-style comments
554 p.writeItem(p.fset.Position(comment.Pos()), p.escape(text))
558 // for /*-style comments, print line by line and let the
559 // write function take care of the proper indentation
560 lines := split([]byte(text))
561 stripCommonPrefix(lines)
563 // write comment lines, separated by formfeed,
564 // without a line break after the last line
565 linebreak := formfeeds[0:1]
566 pos := p.fset.Position(comment.Pos())
567 for i, line := range lines {
573 p.writeItem(pos, p.escape(string(line)))
579 // writeCommentSuffix writes a line break after a comment if indicated
580 // and processes any leftover indentation information. If a line break
581 // is needed, the kind of break (newline vs formfeed) depends on the
582 // pending whitespace. writeCommentSuffix returns true if a pending
583 // formfeed was dropped from the whitespace buffer.
585 func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) {
586 for i, ch := range p.wsbuf {
589 // ignore trailing whitespace
591 case indent, unindent:
592 // don't loose indentation information
593 case newline, formfeed:
594 // if we need a line break, keep exactly one
595 // but remember if we dropped any formfeeds
597 needsLinebreak = false
606 p.writeWhitespace(len(p.wsbuf))
608 // make sure we have a line break
610 p.write([]byte{'\n'})
617 // intersperseComments consumes all comments that appear before the next token
618 // tok and prints it together with the buffered whitespace (i.e., the whitespace
619 // that needs to be written before the next token). A heuristic is used to mix
620 // the comments and whitespace. intersperseComments returns true if a pending
621 // formfeed was dropped from the whitespace buffer.
623 func (p *printer) intersperseComments(next token.Position, tok token.Token) (droppedFF bool) {
624 var last *ast.Comment
625 for ; p.commentBefore(next); p.cindex++ {
626 for _, c := range p.comments[p.cindex].List {
627 p.writeCommentPrefix(p.fset.Position(c.Pos()), next, last, tok.IsKeyword())
634 if last.Text[1] == '*' && p.fset.Position(last.Pos()).Line == next.Line {
635 // the last comment is a /*-style comment and the next item
636 // follows on the same line: separate with an extra blank
639 // ensure that there is a line break after a //-style comment,
640 // before a closing '}' unless explicitly disabled, or at eof
642 last.Text[1] == '/' ||
643 tok == token.RBRACE && p.mode&noExtraLinebreak == 0 ||
645 return p.writeCommentSuffix(needsLinebreak)
648 // no comment was written - we should never reach here since
649 // intersperseComments should not be called in that case
650 p.internalError("intersperseComments called without pending comments")
655 // whiteWhitespace writes the first n whitespace entries.
656 func (p *printer) writeWhitespace(n int) {
659 for i := 0; i < n; i++ {
660 switch ch := p.wsbuf[i]; ch {
668 p.internalError("negative indentation:", p.indent)
671 case newline, formfeed:
672 // A line break immediately followed by a "correcting"
673 // unindent is swapped with the unindent - this permits
674 // proper label positioning. If a comment is between
675 // the line break and the label, the unindent is not
676 // part of the comment whitespace prefix and the comment
677 // will be positioned correctly indented.
678 if i+1 < n && p.wsbuf[i+1] == unindent {
679 // Use a formfeed to terminate the current section.
680 // Otherwise, a long label name on the next line leading
681 // to a wide column may increase the indentation column
682 // of lines before the label; effectively leading to wrong
684 p.wsbuf[i], p.wsbuf[i+1] = unindent, formfeed
695 // shift remaining entries down
697 for ; n < len(p.wsbuf); n++ {
698 p.wsbuf[i] = p.wsbuf[n]
701 p.wsbuf = p.wsbuf[0:i]
705 // ----------------------------------------------------------------------------
706 // Printing interface
709 func mayCombine(prev token.Token, next byte) (b bool) {
712 b = next == '.' // 1.
714 b = next == '+' // ++
716 b = next == '-' // --
718 b = next == '*' // /*
720 b = next == '-' || next == '<' // <- or <<
722 b = next == '&' || next == '^' // && or &^
728 // print prints a list of "items" (roughly corresponding to syntactic
729 // tokens, but also including whitespace and formatting information).
730 // It is the only print function that should be called directly from
731 // any of the AST printing functions in nodes.go.
733 // Whitespace is accumulated until a non-whitespace token appears. Any
734 // comments that need to appear before that token are printed first,
735 // taking into account the amount and structure of any pending white-
736 // space for best comment placement. Then, any leftover whitespace is
737 // printed, followed by the actual token.
739 func (p *printer) print(args ...interface{}) {
740 for _, f := range args {
741 next := p.pos // estimated position of next item
745 switch x := f.(type) {
747 // toggle printer mode
751 // don't add ignore's to the buffer; they
752 // may screw up "correcting" unindents (see
757 if i == cap(p.wsbuf) {
758 // Whitespace sequences are very short so this should
759 // never happen. Handle gracefully (but possibly with
760 // bad comment placement) if it does happen.
764 p.wsbuf = p.wsbuf[0 : i+1]
770 data = p.escape(x.Value)
774 if mayCombine(p.lastTok, s[0]) {
775 // the previous and the current token must be
776 // separated by a blank otherwise they combine
777 // into a different incorrect token sequence
778 // (except for token.INT followed by a '.' this
779 // should never happen because it is taken care
780 // of via binary expression formatting)
781 if len(p.wsbuf) != 0 {
782 p.internalError("whitespace buffer not empty")
784 p.wsbuf = p.wsbuf[0:1]
791 next = p.fset.Position(x) // accurate position of next item
795 fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f)
796 panic("go/printer type")
802 droppedFF := p.flush(next, tok)
804 // intersperse extra newlines if present in the source
805 // (don't do this in flush as it will cause extra newlines
806 // at the end of a file) - use formfeeds if we dropped one
808 p.writeNewlines(next.Line-p.pos.Line, droppedFF)
810 p.writeItem(next, data)
816 // commentBefore returns true iff the current comment occurs
817 // before the next position in the source code.
819 func (p *printer) commentBefore(next token.Position) bool {
820 return p.cindex < len(p.comments) && p.fset.Position(p.comments[p.cindex].List[0].Pos()).Offset < next.Offset
824 // Flush prints any pending comments and whitespace occurring
825 // textually before the position of the next token tok. Flush
826 // returns true if a pending formfeed character was dropped
827 // from the whitespace buffer as a result of interspersing
830 func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) {
831 if p.commentBefore(next) {
832 // if there are comments before the next item, intersperse them
833 droppedFF = p.intersperseComments(next, tok)
835 // otherwise, write any leftover whitespace
836 p.writeWhitespace(len(p.wsbuf))
842 // ----------------------------------------------------------------------------
845 // A trimmer is an io.Writer filter for stripping tabwriter.Escape
846 // characters, trailing blanks and tabs, and for converting formfeed
847 // and vtab characters into newlines and htabs (in case no tabwriter
848 // is used). Text bracketed by tabwriter.Escape characters is passed
849 // through unchanged.
851 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
867 // Design note: It is tempting to eliminate extra blanks occurring in
868 // whitespace in this function as it could simplify some
869 // of the blanks logic in the node printing functions.
870 // However, this would mess up any formatting done by
873 func (p *trimmer) Write(data []byte) (n int, err os.Error) {
875 // p.state == inSpace:
876 // p.space is unwritten
877 // p.state == inEscape, inText:
878 // data[m:n] is unwritten
881 for n, b = range data {
883 b = '\t' // convert to htab
889 p.space.WriteByte(b) // WriteByte returns no errors
891 p.space.Reset() // discard trailing space
892 _, err = p.output.Write(newlines[0:1]) // write newline
893 case tabwriter.Escape:
894 _, err = p.output.Write(p.space.Bytes())
896 m = n + 1 // +1: skip tabwriter.Escape
898 _, err = p.output.Write(p.space.Bytes())
903 if b == tabwriter.Escape {
904 _, err = p.output.Write(data[m:n])
911 _, err = p.output.Write(data[m:n])
914 p.space.WriteByte(b) // WriteByte returns no errors
916 _, err = p.output.Write(data[m:n])
919 _, err = p.output.Write(newlines[0:1]) // write newline
920 case tabwriter.Escape:
921 _, err = p.output.Write(data[m:n])
923 m = n + 1 // +1: skip tabwriter.Escape
935 case inEscape, inText:
936 _, err = p.output.Write(data[m:n])
945 // ----------------------------------------------------------------------------
948 // General printing is controlled with these Config.Mode flags.
950 RawFormat uint = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
951 TabIndent // use tabs for indentation independent of UseSpaces
952 UseSpaces // use spaces instead of tabs for alignment
956 // A Config node controls the output of Fprint.
958 Mode uint // default: 0
959 Tabwidth int // default: 8
963 // fprint implements Fprint and takes a nodesSizes map for setting up the printer state.
964 func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (int, os.Error) {
965 // redirect output through a trimmer to eliminate trailing whitespace
966 // (Input to a tabwriter must be untrimmed since trailing tabs provide
967 // formatting information. The tabwriter could provide trimming
968 // functionality but no tabwriter is used when RawFormat is set.)
969 output = &trimmer{output: output}
971 // setup tabwriter if needed and redirect output
972 var tw *tabwriter.Writer
973 if cfg.Mode&RawFormat == 0 {
974 minwidth := cfg.Tabwidth
976 padchar := byte('\t')
977 if cfg.Mode&UseSpaces != 0 {
981 twmode := tabwriter.DiscardEmptyColumns
982 if cfg.Mode&TabIndent != 0 {
984 twmode |= tabwriter.TabIndent
987 tw = tabwriter.NewWriter(output, minwidth, cfg.Tabwidth, 1, padchar, twmode)
991 // setup printer and print node
993 p.init(output, cfg, fset, nodeSizes)
995 switch n := node.(type) {
998 p.useNodeComments = true
999 p.expr(n, ignoreMultiLine)
1002 p.useNodeComments = true
1003 // A labeled statement will un-indent to position the
1004 // label. Set indent to 1 so we don't get indent "underflow".
1005 if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt {
1008 p.stmt(n, false, ignoreMultiLine)
1011 p.useNodeComments = true
1012 p.decl(n, ignoreMultiLine)
1015 p.useNodeComments = true
1016 p.spec(n, 1, false, ignoreMultiLine)
1019 p.comments = n.Comments
1020 p.useNodeComments = n.Comments == nil
1023 p.errors <- fmt.Errorf("printer.Fprint: unsupported node type %T", n)
1026 p.flush(token.Position{Offset: infinity, Line: infinity}, token.EOF)
1027 p.errors <- nil // no errors
1029 err := <-p.errors // wait for completion of goroutine
1031 // flush tabwriter, if any
1033 tw.Flush() // ignore errors
1036 return p.written, err
1040 // Fprint "pretty-prints" an AST node to output and returns the number
1041 // of bytes written and an error (if any) for a given configuration cfg.
1042 // Position information is interpreted relative to the file set fset.
1043 // The node type must be *ast.File, or assignment-compatible to ast.Expr,
1044 // ast.Decl, ast.Spec, or ast.Stmt.
1046 func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, os.Error) {
1047 return cfg.fprint(output, fset, node, make(map[ast.Node]int))
1051 // Fprint "pretty-prints" an AST node to output.
1052 // It calls Config.Fprint with default settings.
1054 func Fprint(output io.Writer, fset *token.FileSet, node interface{}) os.Error {
1055 _, err := (&Config{Tabwidth: 8}).Fprint(output, fset, node) // don't care about number of bytes written