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