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