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