github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/go/printer/testdata/comments.input (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  // This is a package for testing comment placement by go/printer.
     6  //
     7  package main
     8  
     9  import "fmt"  // fmt
    10  
    11  const c0 = 0  // zero
    12  const (
    13  	c1 = iota  // c1
    14  	c2  // c2
    15  )
    16  
    17  // Alignment of comments in declarations>
    18  const (
    19  	_ T = iota  // comment
    20  	_  // comment
    21  	_  // comment
    22  	_ = iota+10
    23  	_  // comments
    24  
    25  	_ = 10  // comment
    26  	_ T = 20  // comment
    27  )
    28  
    29  const (
    30  	_____ = iota // foo
    31  	_ // bar
    32  	_  = 0    // bal
    33  	_ // bat
    34  )
    35  
    36  const (
    37  	_ T = iota // comment
    38  	_ // comment
    39  	_ // comment
    40  	_ = iota + 10
    41  	_ // comment
    42  	_ = 10
    43  	_ = 20 // comment
    44  	_ T = 0 // comment
    45  )
    46  
    47  // The SZ struct; it is empty.
    48  type SZ struct {}
    49  
    50  // The S0 struct; no field is exported.
    51  type S0 struct {
    52  	int
    53  	x, y, z int  // 3 unexported fields
    54  }
    55  
    56  // The S1 struct; some fields are not exported.
    57  type S1 struct {
    58  	S0
    59  	A, B, C float  // 3 exported fields
    60  	D, b, c int  // 2 unexported fields
    61  }
    62  
    63  // The S2 struct; all fields are exported.
    64  type S2 struct {
    65  	S1
    66  	A, B, C float  // 3 exported fields
    67  }
    68  
    69  // The IZ interface; it is empty.
    70  type SZ interface {}
    71  
    72  // The I0 interface; no method is exported.
    73  type I0 interface {
    74  	f(x int) int  // unexported method
    75  }
    76  
    77  // The I1 interface; some methods are not exported.
    78  type I1 interface {
    79  	I0
    80  	F(x float) float  // exported methods
    81  	g(x int) int  // unexported method
    82  }
    83  
    84  // The I2 interface; all methods are exported.
    85  type I2 interface {
    86  	I0
    87  	F(x float) float  // exported method
    88  	G(x float) float  // exported method
    89  }
    90  
    91  // The S3 struct; all comments except for the last one must appear in the export.
    92  type S3 struct {
    93  	// lead comment for F1
    94  	F1 int // line comment for F1
    95  	// lead comment for F2
    96  	F2 int // line comment for F2
    97  	f3 int // f3 is not exported
    98  }
    99  
   100  // This comment group should be separated
   101  // with a newline from the next comment
   102  // group.
   103  
   104  // This comment should NOT be associated with the next declaration.
   105  
   106  var x int  // x
   107  var ()
   108  
   109  
   110  // This comment SHOULD be associated with f0.
   111  func f0() {
   112  	const pi = 3.14  // pi
   113  	var s1 struct {}  /* an empty struct */ /* foo */
   114  	// a struct constructor
   115  	// --------------------
   116  	var s2 struct {} = struct {}{}
   117  	x := pi
   118  }
   119  //
   120  // This comment should be associated with f1, with one blank line before the comment.
   121  //
   122  func f1() {
   123  	f0()
   124  	/* 1 */
   125  	// 2
   126  	/* 3 */
   127  	/* 4 */
   128  	f0()
   129  }
   130  
   131  
   132  func _() {
   133  // this comment should be properly indented
   134  }
   135  
   136  
   137  func _(x int) int {
   138  	if x < 0 {  // the tab printed before this comment's // must not affect the remaining lines
   139  		return -x  // this statement should be properly indented
   140  	}
   141  	if x < 0 {  /* the tab printed before this comment's /* must not affect the remaining lines */
   142  		return -x  // this statement should be properly indented
   143  	}
   144  	return x
   145  }
   146  
   147  
   148  func typeswitch(x interface{}) {
   149  	switch v := x.(type) {
   150  	case bool, int, float:
   151  	case string:
   152  	default:
   153  	}
   154  
   155  	switch x.(type) {
   156  	}
   157  
   158  	switch v0, ok := x.(int); v := x.(type) {
   159  	}
   160  
   161  	switch v0, ok := x.(int); x.(type) {
   162  	case byte:  // this comment should be on the same line as the keyword
   163  		// this comment should be normally indented
   164  		_ = 0
   165  	case bool, int, float:
   166  		// this comment should be indented
   167  	case string:
   168  	default:
   169  		// this comment should be indented
   170  	}
   171  	// this comment should not be indented
   172  }
   173  
   174  //
   175  // Indentation of comments after possibly indented multi-line constructs
   176  // (test cases for issue 3147).
   177  //
   178  
   179  func _() {
   180  	s := 1 +
   181  		2
   182  // should be indented like s
   183  }
   184  
   185  func _() {
   186  	s := 1 +
   187  		2 // comment
   188  		// should be indented like s
   189  }
   190  
   191  func _() {
   192  	s := 1 +
   193  		2 // comment
   194  	// should be indented like s
   195  	_ = 0
   196  }
   197  
   198  func _() {
   199  	s := 1 +
   200  		2
   201  	// should be indented like s
   202  	_ = 0
   203  }
   204  
   205  func _() {
   206  	s := 1 +
   207  		2
   208  
   209  // should be indented like s
   210  }
   211  
   212  func _() {
   213  	s := 1 +
   214  		2 // comment
   215  
   216  		// should be indented like s
   217  }
   218  
   219  func _() {
   220  	s := 1 +
   221  		2 // comment
   222  
   223  	// should be indented like s
   224  	_ = 0
   225  }
   226  
   227  func _() {
   228  	s := 1 +
   229  		2
   230  
   231  	// should be indented like s
   232  	_ = 0
   233  }
   234  
   235  // Test case from issue 3147.
   236  func f() {
   237  	templateText := "a" + // A
   238  		"b" + // B
   239  		"c" // C
   240  
   241  	// should be aligned with f()
   242  	f()
   243  }
   244  
   245  // Modified test case from issue 3147.
   246  func f() {
   247  	templateText := "a" + // A
   248  		"b" + // B
   249  		"c" // C
   250  
   251  		// may not be aligned with f() (source is not aligned)
   252  	f()
   253  }
   254  
   255  //
   256  // Test cases for alignment of lines in general comments.
   257  //
   258  
   259  func _() {
   260  	/* freestanding comment
   261  	   aligned		line
   262  	   aligned line
   263  	*/
   264  }
   265  
   266  func _() {
   267  	/* freestanding comment
   268  	   aligned		line
   269  	   aligned line
   270  	   */
   271  }
   272  
   273  func _() {
   274  	/* freestanding comment
   275  	   aligned		line
   276  	   aligned line */
   277  }
   278  
   279  func _() {
   280  	/*	freestanding comment
   281  		aligned		line
   282  		aligned line
   283  	*/
   284  }
   285  
   286  func _() {
   287  	/*	freestanding comment
   288  		aligned		line
   289  		aligned line
   290  		*/
   291  }
   292  
   293  func _() {
   294  	/*	freestanding comment
   295  		aligned		line
   296  		aligned line */
   297  }
   298  
   299  
   300  func _() {
   301  	/*
   302  	   freestanding comment
   303  	   aligned		line
   304  	   aligned line
   305  	*/
   306  }
   307  
   308  func _() {
   309  	/*
   310  	   freestanding comment
   311  	   aligned		line
   312  	   aligned line
   313  	   */
   314  }
   315  
   316  func _() {
   317  	/*
   318  	   freestanding comment
   319  	   aligned		line
   320  	   aligned line */
   321  }
   322  
   323  func _() {
   324  	/*
   325  		freestanding comment
   326  		aligned		line
   327  		aligned line
   328  	*/
   329  }
   330  
   331  func _() {
   332  	/*
   333  		freestanding comment
   334  		aligned		line
   335  		aligned line
   336  		*/
   337  }
   338  
   339  func _() {
   340  	/*
   341  		freestanding comment
   342  		aligned		line
   343  		aligned line */
   344  }
   345  
   346  func _() {
   347  	/* freestanding comment
   348  	   aligned line
   349  	*/
   350  }
   351  
   352  func _() {
   353  	/* freestanding comment
   354  	   aligned line
   355  	   */
   356  }
   357  
   358  func _() {
   359  	/* freestanding comment
   360  	   aligned line */
   361  }
   362  
   363  func _() {
   364  	/*	freestanding comment
   365  		aligned line
   366  	*/
   367  }
   368  
   369  func _() {
   370  	/*	freestanding comment
   371  		aligned line
   372  		*/
   373  }
   374  
   375  func _() {
   376  	/*	freestanding comment
   377  		aligned line */
   378  }
   379  
   380  
   381  func _() {
   382  	/*
   383  	   freestanding comment
   384  	   aligned line
   385  	*/
   386  }
   387  
   388  func _() {
   389  	/*
   390  	   freestanding comment
   391  	   aligned line
   392  	   */
   393  }
   394  
   395  func _() {
   396  	/*
   397  	   freestanding comment
   398  	   aligned line */
   399  }
   400  
   401  func _() {
   402  	/*
   403  		freestanding comment
   404  		aligned line
   405  	*/
   406  }
   407  
   408  func _() {
   409  	/*
   410  		freestanding comment
   411  		aligned line
   412  		*/
   413  }
   414  
   415  func _() {
   416  	/*
   417  		freestanding comment
   418  		aligned line */
   419  }
   420  
   421  /*
   422   * line
   423   * of
   424   * stars
   425   */
   426  
   427  /* another line
   428   * of
   429   * stars */
   430  
   431  /*	and another line
   432   *	of
   433   *	stars */
   434  
   435  /* a line of
   436   * stars */
   437  
   438  /*	and another line of
   439   *	stars */
   440  
   441  /* a line of stars
   442  */
   443  
   444  /*	and another line of
   445  */
   446  
   447  /* a line of stars
   448   */
   449  
   450  /*	and another line of
   451   */
   452  
   453  /*
   454  aligned in middle
   455  here
   456          not here
   457  */
   458  
   459  /*
   460  blank line in middle:
   461  
   462  with no leading spaces on blank line.
   463  */
   464  
   465  /*
   466     aligned in middle
   467     here
   468             not here
   469  */
   470  
   471  /*
   472  	blank line in middle:
   473  
   474  	with no leading spaces on blank line.
   475  */
   476  
   477  func _() {
   478  	/*
   479  	 * line
   480  	 * of
   481  	 * stars
   482  	 */
   483  
   484  	/*
   485  	aligned in middle
   486  	here
   487  		not here
   488  	*/
   489  
   490  	/*
   491  	blank line in middle:
   492  
   493  	with no leading spaces on blank line.
   494  */
   495  }
   496  
   497  
   498  // Some interesting interspersed comments.
   499  // See below for more common cases.
   500  func _(/* this */x/* is *//* an */ int) {
   501  }
   502  
   503  func _(/* no params */) {}
   504  
   505  func _() {
   506  	f(/* no args */)
   507  }
   508  
   509  func (/* comment1 */ T /* comment2 */) _() {}
   510  
   511  func _() { /* one-line functions with comments are formatted as multi-line functions */ }
   512  
   513  func _() {
   514  	_ = 0
   515  	/* closing curly brace should be on new line */ }
   516  
   517  func _() {
   518  	_ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */}
   519  }
   520  
   521  // Test cases from issue 1542:
   522  // Comments must not be placed before commas and cause invalid programs.
   523  func _() {
   524  	var a = []int{1, 2, /*jasldf*/
   525  	}
   526  	_ = a
   527  }
   528  
   529  func _() {
   530  	var a = []int{1, 2, /*jasldf
   531  						*/
   532  	}
   533  	_ = a
   534  }
   535  
   536  func _() {
   537  	var a = []int{1, 2, // jasldf
   538  	}
   539  	_ = a
   540  }
   541  
   542  // Comments immediately adjacent to punctuation followed by a newline
   543  // remain after the punctuation (looks better and permits alignment of
   544  // comments).
   545  func _() {
   546  	_ = T{
   547  		1,    // comment after comma
   548  		2,    /* comment after comma */
   549  		3  ,  // comment after comma
   550  	}
   551  	_ = T{
   552  		1  ,// comment after comma
   553  		2  ,/* comment after comma */
   554  		3,// comment after comma
   555  	}
   556  	_ = T{
   557  		/* comment before literal */1,
   558  		2/* comment before comma - ok to move after comma */,
   559  		3  /* comment before comma - ok to move after comma */  ,
   560  	}
   561  
   562  	for
   563  		i=0;// comment after semicolon
   564  		i<9;/* comment after semicolon */
   565  		i++{// comment after opening curly brace
   566  	}
   567  
   568  	// TODO(gri) the last comment in this example should be aligned */
   569  	for
   570  		i=0;// comment after semicolon
   571  		i<9/* comment before semicolon - ok to move after semicolon */;
   572  		i++ /* comment before opening curly brace */ {
   573  	}
   574  }
   575  
   576  // If there is no newline following punctuation, commas move before the punctuation.
   577  // This way, commas interspersed in lists stay with the respective expression.
   578  func f(x/* comment */, y int, z int /* comment */, u, v, w int /* comment */) {
   579  	f(x /* comment */, y)
   580  	f(x /* comment */, 
   581  	y)
   582  	f(
   583  		x /* comment */,
   584  	)
   585  }
   586  
   587  func g(
   588  	x int /* comment */,
   589  ) {}
   590  
   591  type _ struct {
   592  	a, b /* comment */, c int
   593  }
   594  
   595  type _ struct { a, b /* comment */, c int }
   596  
   597  func _() {
   598  	for a /* comment */, b := range x {
   599  	}
   600  }
   601  
   602  // Print line directives correctly.
   603  
   604  // The following is a legal line directive.
   605  //line foo:1
   606  func _() {
   607  	_ = 0
   608  // The following is a legal line directive. It must not be indented:
   609  //line foo:2
   610  	_ = 1
   611  
   612  // The following is not a legal line directive (it doesn't start in column 1):
   613  	//line foo:2
   614  	_ = 2
   615  
   616  // The following is not a legal line directive (negative line number):
   617  //line foo:-3
   618  	_ = 3
   619  }
   620  
   621  // Line comments with tabs
   622  func _() {
   623  var	finput		*bufio.Reader			// input file
   624  var	stderr		*bufio.Writer
   625  var	ftable		*bufio.Writer			// y.go file
   626  var	foutput		*bufio.Writer			// y.output file
   627  
   628  var	oflag		string				// -o [y.go]		- y.go file
   629  var	vflag		string				// -v [y.output]	- y.output file
   630  var	lflag		bool				// -l			- disable line directives
   631  }
   632  
   633  // Trailing white space in comments should be trimmed
   634  func _() {
   635  // This comment has 4 blanks following that should be trimmed:    
   636  /* Each line of this comment has blanks or tabs following that should be trimmed:	
   637     line 2:    
   638     line 3:    			
   639  */
   640  }
   641  
   642  /* This comment is the last entry in this file. It must be printed and should be followed by a newline */