...
Run Format

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

View as plain text