github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/go/printer/testdata/expressions.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  package expressions
     6  
     7  type T struct {
     8  	x, y, z int
     9  }
    10  
    11  var (
    12  	a, b, c, d, e int
    13  	under_bar int
    14  	longIdentifier1, longIdentifier2, longIdentifier3 int
    15  	t0, t1, t2 T
    16  	s string
    17  	p *int
    18  )
    19  
    20  
    21  func _() {
    22  	// no spaces around simple or parenthesized expressions
    23  	_ = (a+0)
    24  	_ = a+b
    25  	_ = a+b+c
    26  	_ = a+b-c
    27  	_ = a-b-c
    28  	_ = a+(b*c)
    29  	_ = a+(b/c)
    30  	_ = a-(b%c)
    31  	_ = 1+a
    32  	_ = a+1
    33  	_ = a+b+1
    34  	_ = s[a]
    35  	_ = s[a:]
    36  	_ = s[:b]
    37  	_ = s[1:2]
    38  	_ = s[a:b]
    39  	_ = s[0:len(s)]
    40  	_ = s[0]<<1
    41  	_ = (s[0]<<1)&0xf
    42  	_ = s[0] << 2 | s[1] >> 4
    43  	_ = "foo"+s
    44  	_ = s+"foo"
    45  	_ = 'a'+'b'
    46  	_ = len(s)/2
    47  	_ = len(t0.x)/a
    48  
    49  	// spaces around expressions of different precedence or expressions containing spaces
    50  	_ = a + -b
    51  	_ = a - ^b
    52  	_ = a / *p
    53  	_ = a + b*c
    54  	_ = 1 + b*c
    55  	_ = a + 2*c
    56  	_ = a + c*2
    57  	_ = 1 + 2*3
    58  	_ = s[1 : 2*3]
    59  	_ = s[a : b-c]
    60  	_ = s[0:]
    61  	_ = s[a+b]
    62  	_ = s[: b-c]
    63  	_ = s[a+b :]
    64  	_ = a[a<<b+1]
    65  	_ = a[a<<b+1 :]
    66  	_ = s[a+b : len(s)]
    67  	_ = s[len(s) : -a]
    68  	_ = s[a : len(s)+1]
    69  	_ = s[a : len(s)+1]+s
    70  
    71  	// spaces around operators with equal or lower precedence than comparisons
    72  	_ = a == b
    73  	_ = a != b
    74  	_ = a > b
    75  	_ = a >= b
    76  	_ = a < b
    77  	_ = a <= b
    78  	_ = a < b && c > d
    79  	_ = a < b || c > d
    80  
    81  	// spaces around "long" operands
    82  	_ = a + longIdentifier1
    83  	_ = longIdentifier1 + a
    84  	_ = longIdentifier1 + longIdentifier2 * longIdentifier3
    85  	_ = s + "a longer string"
    86  
    87  	// some selected cases
    88  	_ = a + t0.x
    89  	_ = a + t0.x + t1.x * t2.x
    90  	_ = a + b + c + d + e + 2*3
    91  	_ = a + b + c + 2*3 + d + e
    92  	_ = (a+b+c)*2
    93  	_ = a - b + c - d + (a+b+c) + d&e
    94  	_ = under_bar-1
    95  	_ = Open(dpath + "/file", O_WRONLY | O_CREAT, 0666)
    96  	_ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
    97  
    98  	// the parser does not restrict expressions that may appear as statements
    99  	true
   100  	42
   101  	"foo"
   102  	x
   103  	(x)
   104  	a+b
   105  	a+b+c
   106  	a+(b*c)
   107  	a+(b/c)
   108  	1+a
   109  	a+1
   110  	s[a]
   111  	x<<1
   112  	(s[0]<<1)&0xf
   113  	"foo"+s
   114  	x == y
   115  	x < y || z > 42
   116  }
   117  
   118  
   119  func _() {
   120  	_ = a+b
   121  	_ = a+b+c
   122  	_ = a+b*c
   123  	_ = a+(b*c)
   124  	_ = (a+b)*c
   125  	_ = a+(b*c*d)
   126  	_ = a+(b*c+d)
   127  
   128  	_ = 1<<x
   129  	_ = -1<<x
   130  	_ = 1<<x-1
   131  	_ = -1<<x-1
   132  
   133  	_ = f(a+b)
   134  	_ = f(a+b+c)
   135  	_ = f(a+b*c)
   136  	_ = f(a+(b*c))
   137  	_ = f(1<<x-1, 1<<x-2)
   138  
   139  	_ = 1<<d.logWindowSize-1
   140  
   141  	buf = make(x, 2*cap(b.buf) + n)
   142  
   143  	dst[i*3+2] = dbuf[0]<<2
   144  	dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4
   145  
   146  	b.buf = b.buf[0:b.off+m+n]
   147  	b.buf = b.buf[0:b.off+m*n]
   148  	f(b.buf[0:b.off+m+n])
   149  
   150  	signed += ' '*8
   151  	tw.octal(header[148:155], chksum)
   152  
   153  	_ = x > 0 && i >= 0
   154  
   155  	x1, x0 := x>>w2, x&m2
   156  	z0 = t1<<w2+t0
   157  	z1 = (t1+t0>>w2)>>w2
   158  	q1, r1 := x1/d1, x1%d1
   159  	r1 = r1*b2 | x0>>w2
   160  	x1 = (x1<<z)|(x0>>(uint(w)-z))
   161  	x1 = x1<<z | x0>>(uint(w)-z)
   162  
   163  	_ = buf[0:len(buf)+1]
   164  	_ = buf[0:n+1]
   165  
   166  	a,b = b,a
   167  	a = b+c
   168  	a = b*c+d
   169  	_ = a*b+c
   170  	_ = a-b-c
   171  	_ = a-(b-c)
   172  	_ = a-b*c
   173  	_ = a-(b*c)
   174  	_ = a*b/c
   175  	_ = a/ *b
   176  	_ = x[a|^b]
   177  	_ = x[a/ *b]
   178  	_ = a& ^b
   179  	_ = a+ +b
   180  	_ = a- -b
   181  	_ = x[a*-b]
   182  	_ = x[a+ +b]
   183  	_ = x^y^z
   184  	_ = b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]
   185  	_ = len(longVariableName)*2
   186  
   187  	_ = token(matchType + xlength<<lengthShift + xoffset)
   188  }
   189  
   190  
   191  func f(x int, args ...int) {
   192  	f(0, args...)
   193  	f(1, args)
   194  	f(2, args[0])
   195  
   196  	// make sure syntactically legal code remains syntactically legal
   197  	f(3, 42 ...) // a blank must remain between 42 and ...
   198  	f(4, 42. ...)
   199  	f(5, 42....)
   200  	f(6, 42.0 ...)
   201  	f(7, 42.0...)
   202  	f(8, .42 ...)
   203  	f(9, .42...)
   204  	f(10, 42e0 ...)
   205  	f(11, 42e0...)
   206  
   207  	_ = 42 .x // a blank must remain between 42 and .x
   208  	_ = 42. .x
   209  	_ = 42..x
   210  	_ = 42.0 .x
   211  	_ = 42.0.x
   212  	_ = .42 .x
   213  	_ = .42.x
   214  	_ = 42e0 .x
   215  	_ = 42e0.x
   216  
   217  	// a blank must remain between the binary operator and the 2nd operand
   218  	_ = x/ *y
   219  	_ = x< -1
   220  	_ = x< <-1
   221  	_ = x+ +1
   222  	_ = x- -1
   223  	_ = x& &x
   224  	_ = x& ^x
   225  
   226  	_ = f(x/ *y, x< -1, x< <-1, x+ +1, x- -1, x& &x, x& ^x)
   227  }
   228  
   229  
   230  func _() {
   231  	_ = T{}
   232  	_ = struct{}{}
   233  	_ = [10]T{}
   234  	_ = [...]T{}
   235  	_ = []T{}
   236  	_ = map[int]T{}
   237  }
   238  
   239  
   240  // one-line structs/interfaces in composite literals (up to a threshold)
   241  func _() {
   242  	_ = struct{}{}
   243  	_ = struct{ x int }{0}
   244  	_ = struct{ x, y, z int }{0, 1, 2}
   245  	_ = struct{ int }{0}
   246  	_ = struct{ s struct { int } }{struct{ int}{0} }
   247  }
   248  
   249  
   250  func _() {
   251  	// do not modify literals
   252  	_ = "tab1	tab2	tab3	end"  // string contains 3 tabs
   253  	_ = "tab1 tab2 tab3 end"  // same string with 3 blanks - may be unaligned because editors see tabs in strings
   254  	_ = ""  // this comment should be aligned with the one on the previous line
   255  	_ = ``
   256  	_ = `
   257  `
   258  _ = `foo
   259  		bar`
   260  	_ = `three spaces before the end of the line starting here:   
   261  they must not be removed`
   262  }
   263  
   264  
   265  func _() {
   266  	// smart handling of indentation for multi-line raw strings
   267  	var _ = ``
   268  	var _ = `foo`
   269  	var _ = `foo
   270  bar`
   271  
   272  
   273  var _ =
   274  	``
   275  var _ =
   276  	`foo`
   277  var _ =
   278  	// the next line should remain indented
   279  	`foo
   280  bar`
   281  
   282  
   283  	var _ = // comment
   284  		``
   285  	var _ = // comment
   286  		`foo`
   287  	var _ = // comment
   288  		// the next line should remain indented
   289  		`foo
   290  bar`
   291  
   292  
   293  var _ = /* comment */ ``
   294  var _ = /* comment */ `foo`
   295  var _ = /* comment */ `foo
   296  bar`
   297  
   298  
   299  	var _ = /* comment */
   300  		``
   301  	var _ = /* comment */
   302  		`foo`
   303  	var _ = /* comment */
   304  		// the next line should remain indented
   305  		`foo
   306  bar`
   307  
   308  
   309  var board = []int(
   310  	`...........
   311  ...........
   312  ....●●●....
   313  ....●●●....
   314  ..●●●●●●●..
   315  ..●●●○●●●..
   316  ..●●●●●●●..
   317  ....●●●....
   318  ....●●●....
   319  ...........
   320  ...........
   321  `)
   322  
   323  
   324  	var state = S{
   325  		"foo",
   326  		// the next line should remain indented
   327  		`...........
   328  ...........
   329  ....●●●....
   330  ....●●●....
   331  ..●●●●●●●..
   332  ..●●●○●●●..
   333  ..●●●●●●●..
   334  ....●●●....
   335  ....●●●....
   336  ...........
   337  ...........
   338  `,
   339  		"bar",
   340  	}
   341  }
   342  
   343  
   344  func _() {
   345  	// one-line function literals (body is on a single line)
   346  	_ = func() {}
   347  	_ = func() int { return 0 }
   348  	_ = func(x, y int) bool { m := (x+y)/2; return m < 0 }
   349  
   350  	// multi-line function literals (body is not on one line)
   351  	_ = func() {
   352  	}
   353  	_ = func() int {
   354  		return 0
   355  	}
   356  	_ = func(x, y int) bool {
   357  		m := (x+y)/2; return x < y }
   358  
   359  	f(func() {
   360  	})
   361  	f(func() int {
   362  		return 0
   363  	})
   364  	f(func(x, y int) bool {
   365  		m := (x+y)/2; return x < y })
   366  }
   367  
   368  
   369  func _() {
   370  	_ = [][]int {
   371  		[]int{1},
   372  		[]int{1, 2},
   373  		[]int{1, 2, 3},
   374  	}
   375  	_ = [][]int {
   376  		{1},
   377  		[]int{1, 2},
   378  		[]int{1, 2, 3},
   379  	}
   380  	_ = [][]int {
   381  		{1},
   382  		{1, 2},
   383  		{1, 2, 3},
   384  	}
   385  	_ = [][]int {{1}, {1, 2}, {1, 2, 3}}
   386  }
   387  
   388  
   389  // various multi-line expressions
   390  func _() {
   391  	// do not add extra indentation to multi-line string lists
   392  	_ = "foo" + "bar"
   393  	_ = "foo" +
   394  	"bar" +
   395  	"bah"
   396  	_ = []string {
   397  		"abc" +
   398  		"def",
   399  		"foo" +
   400  		"bar",
   401  	}
   402  }
   403  
   404  
   405  const _ = F1 +
   406  	`string = "%s";` +
   407  	`ptr = *;` +
   408  	`datafmt.T2 = s ["-" p "-"];`
   409  
   410  
   411  const _ =
   412  	`datafmt "datafmt";` +
   413  	`default = "%v";` +
   414  	`array = *;` +
   415  	`datafmt.T3 = s  {" " a a / ","};`
   416  
   417  
   418  const _ = `datafmt "datafmt";` +
   419  `default = "%v";` +
   420  `array = *;` +
   421  `datafmt.T3 = s  {" " a a / ","};`
   422  
   423  
   424  func _() {
   425  	_ = F1 +
   426  		`string = "%s";` +
   427  		`ptr = *;` +
   428  		`datafmt.T2 = s ["-" p "-"];`
   429  
   430  	_ =
   431  		`datafmt "datafmt";` +
   432  		`default = "%v";` +
   433  		`array = *;` +
   434  		`datafmt.T3 = s  {" " a a / ","};`
   435  
   436  	_ = `datafmt "datafmt";` +
   437  	`default = "%v";` +
   438  	`array = *;` +
   439  	`datafmt.T3 = s  {" " a a / ","};`
   440  }
   441  
   442  
   443  func _() {
   444  	// respect source lines in multi-line expressions
   445  	_ = a+
   446  	b+
   447  	c
   448  	_ = a < b ||
   449  		b < a
   450  	_ = "933262154439441526816992388562667004907159682643816214685929" +
   451  	"638952175999932299156089414639761565182862536979208272237582" +
   452  	"51185210916864000000000000000000000000"  // 100!
   453  	_ = "170141183460469231731687303715884105727"  // prime
   454  }
   455  
   456  
   457  // Alignment after overlong lines
   458  const (
   459  	_ = "991"
   460  	_ = "2432902008176640000"  // 20!
   461  	_ = "933262154439441526816992388562667004907159682643816214685929" +
   462  	"638952175999932299156089414639761565182862536979208272237582" +
   463  	"51185210916864000000000000000000000000"  // 100!
   464  	_ = "170141183460469231731687303715884105727"  // prime
   465  )
   466  
   467  
   468  // Correct placement of operators and comments in multi-line expressions
   469  func _() {
   470  	_ = a +  // comment
   471  		b +  // comment
   472  		c
   473  	_ = "a"	+
   474  		"b" +	// comment
   475  		"c"
   476  	_ = "ba0408" + "7265717569726564"     // field 71, encoding 2, string "required"
   477  }
   478  
   479  
   480  // Correct placement of terminating comma/closing parentheses in multi-line calls.
   481  func _() {
   482  	f(1,
   483  		2,
   484  		3)
   485  	f(1,
   486  		2,
   487  		3,
   488  	)
   489  	f(1,
   490  		2,
   491  		3)  // comment
   492  	f(1,
   493  		2,
   494  		3,  // comment
   495  	)
   496  	f(1,
   497  		2,
   498  		3)// comment
   499  	f(1,
   500  		2,
   501  		3,// comment
   502  	)
   503  }
   504  
   505  
   506  // Align comments in multi-line lists of single-line expressions.
   507  var txpix = [NCOL]draw.Color{
   508  	draw.Yellow, // yellow
   509  	draw.Cyan, // cyan
   510  	draw.Green, // lime green
   511  	draw.GreyBlue, // slate
   512  	draw.Red, /* red */
   513  	draw.GreyGreen, /* olive green */
   514  	draw.Blue, /* blue */
   515  	draw.Color(0xFF55AAFF), /* pink */
   516  	draw.Color(0xFFAAFFFF), /* lavender */
   517  	draw.Color(0xBB005DFF), /* maroon */
   518  }
   519  
   520  
   521  func same(t, u *Time) bool {
   522  	// respect source lines in multi-line expressions
   523  	return t.Year == u.Year &&
   524  		t.Month == u.Month &&
   525  		t.Day == u.Day &&
   526  		t.Hour == u.Hour &&
   527  		t.Minute == u.Minute &&
   528  		t.Second == u.Second &&
   529  		t.Weekday == u.Weekday &&
   530  		t.ZoneOffset == u.ZoneOffset &&
   531  		t.Zone == u.Zone
   532  }
   533  
   534  
   535  func (p *parser) charClass() {
   536  	// respect source lines in multi-line expressions
   537  	if cc.negate && len(cc.ranges) == 2 &&
   538  		cc.ranges[0] == '\n' && cc.ranges[1] == '\n' {
   539  		nl := new(_NotNl)
   540  		p.re.add(nl)
   541  	}
   542  }
   543  
   544  
   545  func addState(s []state, inst instr, match []int) {
   546  	// handle comments correctly in multi-line expressions
   547  	for i := 0; i < l; i++ {
   548  		if s[i].inst.index() == index && // same instruction
   549  		   s[i].match[0] < pos {	// earlier match already going; leftmost wins
   550  		   	return s
   551  		 }
   552  	}
   553  }
   554  
   555  func (self *T) foo(x int) *T { return self }
   556  
   557  func _() { module.Func1().Func2() }
   558  
   559  func _() {
   560  	_ = new(T).
   561  		foo(1).
   562  			foo(2).
   563  		foo(3)
   564  
   565  	_ = new(T).
   566  	foo(1).
   567  	foo(2). // inline comments
   568  	foo(3)
   569  
   570  	_ = new(T).foo(1).foo(2).foo(3)
   571  
   572  	// handle multiline argument list correctly
   573  	_ = new(T).
   574  	foo(
   575  		1).
   576  		foo(2)
   577  
   578  	_ = new(T).foo(
   579  		1).foo(2)
   580  
   581  	_ = Array[3 +
   582  4]
   583  
   584  	_ = Method(1, 2,
   585  		3)
   586  
   587  	_ = new(T).
   588     foo().
   589     bar() . (*Type)
   590  
   591  	_ = new(T).
   592  foo().
   593  bar().(*Type).
   594  baz()
   595  
   596  	_ = new(T).
   597  	foo().
   598  	bar()["idx"]
   599  
   600  	_ = new(T).
   601  	foo().
   602  	bar()["idx"]	.
   603  	baz()
   604  
   605  	_ = new(T).
   606  	foo().
   607  	bar()[1:2]
   608  
   609  	_ = new(T).
   610  	foo().
   611  	bar()[1:2].
   612  	baz()
   613  
   614  	_ = new(T).
   615  		Field.
   616  		Array[3+
   617         		4].
   618  		Table ["foo"].
   619  		Blob. (*Type).
   620  	Slices[1:4].
   621  	Method(1, 2,
   622  	3).
   623  		Thingy
   624  
   625  	_ = a.b.c
   626  	_ = a.
   627  	b.
   628  	c
   629  	_ = a.b().c
   630  	_ = a.
   631  	b().
   632  	c
   633  	_ = a.b[0].c
   634  	_ = a.
   635  	b[0].
   636  	c
   637  	_ = a.b[0:].c
   638  	_ = a.
   639  	b[0:].
   640  	c
   641  	_ = a.b.(T).c
   642  	_ = a.
   643  	b.
   644  	(T).
   645  	c
   646  }
   647  
   648  
   649  // Don't introduce extra newlines in strangely formatted expression lists.
   650  func f() {
   651  	// os.Open parameters should remain on two lines
   652  	if writer, err = os.Open(outfile, s.O_WRONLY|os.O_CREATE|
   653  		os.O_TRUNC, 0666); err != nil {
   654  	    log.Fatal(err)
   655  	}
   656  }
   657  
   658  // Handle multi-line argument lists ending in ... correctly.
   659  // Was issue 3130.
   660  func _() {
   661  	_ = append(s, a...)
   662  	_ = append(
   663  		s, a...)
   664  	_ = append(s,
   665  		a...)
   666  	_ = append(
   667  		s,
   668  		a...)
   669  	_ = append(s, a...,
   670  	)
   671  	_ = append(s,
   672  		a...,
   673  	)
   674  	_ = append(
   675  		s,
   676  		a...,
   677  	)
   678  }
   679  
   680  // Literal function types in conversions must be parenthesized;
   681  // for now go/parser accepts the unparenthesized form where it
   682  // is non-ambiguous.
   683  func _() {
   684  	// these conversions should be rewritten to look
   685  	// the same as the parenthesized conversions below
   686  	_ = func()()(nil)
   687  	_ = func(x int)(float)(nil)
   688  	_ = func() func() func()()(nil)
   689  
   690  	_ = (func()())(nil)
   691  	_ = (func(x int)(float))(nil)
   692  	_ = (func() func() func()())(nil)
   693  }