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