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