github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/gen/codegen/gen_basic.go (about) 1 package codegen 2 3 import ( 4 "bytes" 5 "fmt" 6 "go/token" 7 "sort" 8 "strconv" 9 "strings" 10 11 "github.com/machinefi/w3bstream/pkg/depends/x/stringsx" 12 ) 13 14 type ( 15 Snippet interface{ Bytes() []byte } 16 17 SnippetSpec interface { 18 Snippet 19 IfSpec 20 } 21 22 SnippetType interface { 23 Snippet 24 IfType 25 } 26 27 SnippetCanAddr interface { 28 Snippet 29 IfCanAddr 30 } 31 32 IfSpec interface{ _spec() } 33 IfCanAddr interface{ _canAddr() } 34 IfCanBeIfMethod interface{ _canBeInterfaceMethod() } 35 IfType interface{ _type() } 36 37 FnAlaise func(string) string 38 ) 39 40 type SnippetLiteral string 41 42 var _ Snippet = SnippetLiteral("") 43 44 func (s SnippetLiteral) Bytes() []byte { return []byte(s) } 45 46 type SnippetLiteralCompose struct { 47 Type SnippetType 48 Elements []Snippet 49 } 50 51 var _ Snippet = (*SnippetLiteralCompose)(nil) 52 53 func (s *SnippetLiteralCompose) Bytes() []byte { 54 buf := bytes.NewBuffer(nil) 55 56 if s.Type != nil { 57 buf.Write(s.Type.Bytes()) 58 } 59 60 buf.WriteRune('{') 61 62 for _, e := range s.Elements { 63 buf.WriteRune('\n') 64 buf.Write(e.Bytes()) 65 buf.WriteRune(',') 66 } 67 68 buf.WriteRune('\n') 69 buf.WriteRune('}') 70 return buf.Bytes() 71 } 72 73 /* 74 SnippetBlock code block, like 75 ```go 76 77 var a = "Hello CodeGen" 78 fmt.Print(a) 79 80 ``` 81 */ 82 type SnippetBlock []Snippet 83 84 var _ Snippet = SnippetBlock(nil) 85 86 func (s SnippetBlock) Bytes() []byte { 87 buf := bytes.NewBuffer(nil) 88 89 for _, sn := range s { 90 if sn == nil { 91 continue 92 } 93 buf.Write(sn.Bytes()) 94 buf.WriteRune('\n') 95 } 96 97 return buf.Bytes() 98 } 99 100 /* 101 SnippetBlockWithBrace code block quote with '{' and '}', like 102 ```go 103 104 { 105 var a = "Hello CodeGen" 106 fmt.Print(a) 107 } 108 109 ``` 110 */ 111 type SnippetBlockWithBrace []Snippet 112 113 var _ Snippet = SnippetBlockWithBrace(nil) 114 115 func (s SnippetBlockWithBrace) Bytes() []byte { 116 buf := bytes.NewBuffer(nil) 117 118 buf.WriteRune('{') 119 buf.WriteRune('\n') 120 for _, sn := range s { 121 if sn == nil { 122 continue 123 } 124 buf.Write(sn.Bytes()) 125 buf.WriteRune('\n') 126 } 127 buf.WriteRune('}') 128 129 return buf.Bytes() 130 } 131 132 // SnippetBuiltIn built-in symbols, like `int`, `println` 133 type SnippetBuiltIn string 134 135 var _ Snippet = SnippetBuiltIn("") 136 137 func (s SnippetBuiltIn) Bytes() []byte { return []byte(s) } 138 139 // SnippetIdent idenifier `a := 0 // a is a identifier` 140 type SnippetIdent string 141 142 var _ SnippetCanAddr = SnippetIdent("") 143 144 func (s SnippetIdent) Bytes() []byte { return []byte(s) } 145 146 func (s SnippetIdent) _canAddr() {} 147 148 func (s SnippetIdent) UpperCamelCase() *SnippetIdent { 149 return Ident(stringsx.UpperCamelCase(string(s))) 150 } 151 152 func (s SnippetIdent) LowerCamelCase() *SnippetIdent { 153 return Ident(stringsx.LowerCamelCase(string(s))) 154 } 155 156 func (s SnippetIdent) UpperSnakeCase() *SnippetIdent { 157 return Ident(stringsx.UpperSnakeCase(string(s))) 158 } 159 160 func (s SnippetIdent) LowerSnakeCase() *SnippetIdent { 161 return Ident(stringsx.LowerSnakeCase(string(s))) 162 } 163 164 // SnippetComments comment code 165 type SnippetComments struct { 166 OneLine bool 167 Comments []string 168 } 169 170 var _ Snippet = (*SnippetComments)(nil) 171 172 func (s *SnippetComments) Bytes() []byte { 173 buf := bytes.NewBuffer(nil) 174 175 if s.IsOneLine() { 176 buf.WriteString("// ") 177 buf.WriteString(s.Comments[0]) 178 } else { 179 for i, c := range s.Comments { 180 if i > 0 { 181 buf.WriteRune('\n') 182 } 183 buf.WriteString("// ") 184 buf.WriteString(c) 185 } 186 } 187 return buf.Bytes() 188 } 189 190 func (s SnippetComments) AsOneLine() *SnippetComments { s.OneLine = true; return &s } 191 192 func (s SnippetComments) IsOneLine() bool { return s.OneLine && len(s.Comments) == 1 } 193 194 func (s SnippetComments) Append(cmt ...string) SnippetComments { 195 for _, c := range cmt { 196 s.Comments = append(s.Comments, strings.Split(c, "\n")...) 197 } 198 return s 199 } 200 201 // SnippetExpr expression `a == 0 // a == 0 is a expression` 202 type SnippetExpr string 203 204 var _ Snippet = SnippetExpr("") 205 206 func (s SnippetExpr) Bytes() []byte { return []byte(s) } 207 208 type SnippetKVExpr struct { 209 Snippet 210 K, V Snippet 211 } 212 213 var _ Snippet = (*SnippetKVExpr)(nil) 214 215 func (s *SnippetKVExpr) Bytes() []byte { 216 buf := bytes.NewBuffer(nil) 217 218 buf.Write(s.K.Bytes()) 219 buf.WriteString(token.COLON.String()) 220 buf.WriteRune(' ') 221 buf.Write(s.V.Bytes()) 222 223 return buf.Bytes() 224 } 225 226 type SnippetTypeDecl struct { 227 Token token.Token 228 Specs []SnippetSpec 229 *SnippetComments 230 } 231 232 var _ Snippet = (*SnippetTypeDecl)(nil) 233 234 func (s *SnippetTypeDecl) Bytes() []byte { 235 buf := bytes.NewBuffer(nil) 236 mul := len(s.Specs) > 1 237 238 if s.SnippetComments != nil { 239 buf.Write(s.SnippetComments.Bytes()) 240 buf.WriteRune('\n') 241 } 242 243 buf.WriteString(s.Token.String()) 244 buf.WriteRune(' ') 245 246 if mul { 247 buf.WriteRune('(') 248 buf.WriteRune('\n') 249 } 250 251 for i, spec := range s.Specs { 252 if i > 0 { 253 buf.WriteRune('\n') 254 } 255 buf.Write(spec.Bytes()) 256 } 257 258 if mul { 259 buf.WriteRune('\n') 260 buf.WriteRune(')') 261 } 262 263 return buf.Bytes() 264 } 265 266 func (s SnippetTypeDecl) WithComments(comments ...string) *SnippetTypeDecl { 267 if len(comments) > 0 { 268 s.SnippetComments = Comments(comments...) 269 } 270 return &s 271 } 272 273 // SnippetField define a field or var 274 // eg: 275 // a int 276 // a, b int 277 // AliasString = string 278 type SnippetField struct { 279 SnippetSpec 280 SnippetCanAddr 281 282 Type SnippetType 283 Names []*SnippetIdent 284 Tag string 285 Alias bool 286 287 *SnippetComments 288 } 289 290 var _ Snippet = (*SnippetField)(nil) 291 292 func (s *SnippetField) Bytes() []byte { 293 buf := bytes.NewBuffer(nil) 294 295 if s.SnippetComments != nil && !s.SnippetComments.IsOneLine() { 296 tmp := s.SnippetComments.Bytes() 297 buf.Write(tmp) 298 if len(tmp) > 0 { 299 buf.WriteRune('\n') 300 } 301 } 302 303 for i := range s.Names { 304 if i > 0 { 305 buf.WriteRune(',') 306 buf.WriteRune(' ') 307 } 308 buf.Write(s.Names[i].Bytes()) 309 } 310 311 if len(s.Names) > 0 { 312 if s.Alias { 313 buf.WriteRune(' ') 314 buf.WriteRune('=') 315 } 316 } 317 318 // type inference 319 if s.Type != nil { 320 if len(s.Names) > 0 { 321 buf.WriteRune(' ') 322 } 323 buf.Write(s.Type.Bytes()) 324 } 325 326 if s.Tag != "" { 327 buf.WriteRune(' ') 328 buf.WriteRune('`') 329 buf.WriteString(s.Tag) 330 buf.WriteRune('`') 331 } 332 333 if s.SnippetComments != nil && s.SnippetComments.IsOneLine() { 334 buf.WriteRune(' ') 335 buf.Write(s.SnippetComments.Bytes()) 336 } 337 338 return buf.Bytes() 339 } 340 341 func (s SnippetField) WithTag(tag string) *SnippetField { 342 s.Tag = tag 343 return &s 344 } 345 346 func (s SnippetField) WithTags(tags map[string][]string) *SnippetField { 347 buf := bytes.NewBuffer(nil) 348 349 names := make([]string, 0) 350 for tag := range tags { 351 names = append(names, tag) 352 } 353 sort.Strings(names) 354 355 for i, tag := range names { 356 if i > 0 { 357 buf.WriteRune(' ') 358 } 359 values := make([]string, 0) 360 for j := range tags[tag] { 361 v := tags[tag][j] 362 if v != "" { 363 values = append(values, v) 364 } 365 } 366 buf.WriteString(tag) 367 buf.WriteRune(':') 368 buf.WriteString(strconv.Quote(strings.Join(values, ","))) 369 } 370 371 s.Tag = buf.String() 372 return &s 373 } 374 375 func (s SnippetField) WithoutTag() *SnippetField { 376 s.Tag = "" 377 return &s 378 } 379 380 func (s SnippetField) WithComments(cmt ...string) *SnippetField { 381 s.SnippetComments = Comments(cmt...) 382 return &s 383 } 384 385 func (s SnippetField) WithOneLineComment(cmt string) *SnippetField { 386 if s.SnippetComments == nil { 387 s.SnippetComments = &SnippetComments{} 388 } 389 s.SnippetComments.OneLine = true 390 s.SnippetComments.Comments = []string{cmt} 391 return &s 392 } 393 394 func (s SnippetField) AsAlias() *SnippetField { 395 s.Alias = true 396 return &s 397 } 398 399 type SnippetCaseClause struct { 400 Case []Snippet 401 Blk SnippetBlock 402 } 403 404 var _ Snippet = (*SnippetCaseClause)(nil) 405 406 func (s *SnippetCaseClause) Bytes() []byte { 407 buf := bytes.NewBuffer(nil) 408 409 if len(s.Case) == 0 { 410 buf.WriteString(token.DEFAULT.String()) 411 } else { 412 buf.WriteString(token.CASE.String() + " ") 413 for i, c := range s.Case { 414 if i > 0 { 415 buf.WriteString(", ") 416 } 417 buf.Write(c.Bytes()) 418 } 419 } 420 421 buf.WriteRune(':') 422 for _, b := range s.Blk { 423 buf.WriteRune('\n') 424 buf.Write(b.Bytes()) 425 } 426 427 return buf.Bytes() 428 } 429 430 func (s SnippetCaseClause) Do(bodies ...Snippet) *SnippetCaseClause { 431 s.Blk = bodies 432 return &s 433 } 434 435 type SnippetTypeAssertExpr struct { 436 Asserter Snippet 437 Type SnippetType 438 } 439 440 var _ Snippet = (*SnippetTypeAssertExpr)(nil) 441 442 func (s *SnippetTypeAssertExpr) Bytes() []byte { 443 buf := bytes.NewBuffer(nil) 444 445 buf.Write(s.Asserter.Bytes()) 446 447 buf.WriteRune('.') 448 buf.WriteRune('(') 449 buf.Write(s.Type.Bytes()) 450 buf.WriteRune(')') 451 452 return buf.Bytes() 453 } 454 455 type SnippetAssignStmt struct { 456 IfSpec 457 Token token.Token 458 Ls []SnippetCanAddr 459 Rs []Snippet 460 } 461 462 var _ Snippet = (*SnippetAssignStmt)(nil) 463 464 func (s *SnippetAssignStmt) Bytes() []byte { 465 buf := bytes.NewBuffer(nil) 466 467 for i, l := range s.Ls { 468 if i > 0 { 469 buf.WriteString(", ") 470 } 471 buf.Write(l.Bytes()) 472 } 473 474 if len(s.Rs) > 0 { 475 buf.WriteRune(' ') 476 buf.WriteString(s.Token.String()) 477 buf.WriteRune(' ') 478 479 for i, r := range s.Rs { 480 if i > 0 { 481 buf.WriteString(", ") 482 } 483 buf.Write(r.Bytes()) 484 } 485 } 486 487 return buf.Bytes() 488 } 489 490 func (s SnippetAssignStmt) By(rs ...Snippet) *SnippetAssignStmt { 491 s.Rs = rs 492 return &s 493 } 494 495 type SnippetReturnStmt struct { 496 Res []Snippet 497 } 498 499 var _ Snippet = (*SnippetReturnStmt)(nil) 500 501 func (s *SnippetReturnStmt) Bytes() []byte { 502 buf := bytes.NewBuffer(nil) 503 504 buf.WriteString("return") 505 506 for i, r := range s.Res { 507 if i > 0 { 508 buf.WriteRune(',') 509 } 510 buf.WriteRune(' ') 511 buf.Write(r.Bytes()) 512 } 513 514 return buf.Bytes() 515 } 516 517 type SnippetSelectStmt struct { 518 Clauses []*SnippetCaseClause 519 } 520 521 var _ Snippet = (*SnippetSelectStmt)(nil) 522 523 func (s *SnippetSelectStmt) Bytes() []byte { 524 buf := bytes.NewBuffer(nil) 525 526 buf.WriteString(token.SELECT.String()) 527 buf.WriteRune(' ') 528 buf.WriteRune('{') 529 530 for _, c := range s.Clauses { 531 buf.WriteRune('\n') 532 buf.Write(c.Bytes()) 533 } 534 535 buf.WriteRune('\n') 536 buf.WriteRune('}') 537 538 return buf.Bytes() 539 } 540 541 type SnippetSwitchStmt struct { 542 Init, Cond Snippet 543 Clauses []*SnippetCaseClause 544 } 545 546 var _ Snippet = (*SnippetSwitchStmt)(nil) 547 548 func (s *SnippetSwitchStmt) Bytes() []byte { 549 buf := bytes.NewBuffer(nil) 550 551 buf.WriteString(token.SWITCH.String()) 552 if s.Cond != nil { 553 if s.Init != nil { 554 buf.WriteRune(' ') 555 buf.Write(s.Init.Bytes()) 556 buf.WriteRune(';') 557 } 558 buf.WriteRune(' ') 559 buf.Write(s.Cond.Bytes()) 560 } 561 562 buf.WriteRune(' ') 563 buf.WriteRune('{') 564 565 for _, c := range s.Clauses { 566 buf.WriteRune('\n') 567 buf.Write(c.Bytes()) 568 } 569 570 buf.WriteRune('\n') 571 buf.WriteRune('}') 572 573 return buf.Bytes() 574 } 575 576 func (s SnippetSwitchStmt) InitWith(init Snippet) *SnippetSwitchStmt { 577 s.Init = init 578 return &s 579 } 580 581 func (s SnippetSwitchStmt) When(clauses ...*SnippetCaseClause) *SnippetSwitchStmt { 582 s.Clauses = append(s.Clauses, clauses...) 583 return &s 584 } 585 586 type SnippetForRangeStmt struct { 587 K, V SnippetIdent 588 Ranger Snippet 589 Blk SnippetBlockWithBrace 590 } 591 592 var _ Snippet = (*SnippetForRangeStmt)(nil) 593 594 func (s *SnippetForRangeStmt) Bytes() []byte { 595 buf := bytes.NewBuffer(nil) 596 597 buf.WriteString(token.FOR.String()) 598 buf.WriteRune(' ') 599 600 if s.K != AnonymousIdent || s.V != AnonymousIdent { 601 if s.K != AnonymousIdent && s.V == AnonymousIdent { 602 buf.Write(s.K.Bytes()) 603 } else if s.V != AnonymousIdent { 604 buf.Write(s.K.Bytes()) 605 buf.WriteRune(',') 606 buf.WriteRune(' ') 607 buf.Write(s.V.Bytes()) 608 } 609 buf.WriteRune(' ') 610 buf.WriteString(token.DEFINE.String()) 611 buf.WriteRune(' ') 612 } 613 614 buf.WriteString(token.RANGE.String()) 615 buf.WriteRune(' ') 616 buf.Write(s.Ranger.Bytes()) 617 buf.WriteRune(' ') 618 buf.Write(s.Blk.Bytes()) 619 620 return buf.Bytes() 621 } 622 623 func (s SnippetForRangeStmt) Do(blk ...Snippet) *SnippetForRangeStmt { 624 s.Blk = blk 625 return &s 626 } 627 628 type SnippetForStmt struct { 629 Init, Cond, Post Snippet 630 Blk SnippetBlockWithBrace 631 } 632 633 var _ Snippet = (*SnippetForStmt)(nil) 634 635 func (s *SnippetForStmt) Bytes() []byte { 636 buf := bytes.NewBuffer(nil) 637 638 buf.WriteString(token.FOR.String()) 639 if s.Init != nil { 640 buf.WriteRune(' ') 641 buf.Write(s.Init.Bytes()) 642 buf.WriteRune(';') 643 } 644 if s.Cond != nil { 645 buf.WriteRune(' ') 646 buf.Write(s.Cond.Bytes()) 647 } 648 if s.Post != nil { 649 buf.WriteRune(';') 650 buf.WriteRune(' ') 651 buf.Write(s.Post.Bytes()) 652 } 653 buf.WriteRune(' ') 654 buf.Write(s.Blk.Bytes()) 655 656 return buf.Bytes() 657 } 658 659 func (s SnippetForStmt) Do(blk ...Snippet) *SnippetForStmt { 660 s.Blk = blk 661 return &s 662 } 663 664 type SnippetIfStmt struct { 665 Init, Cond Snippet 666 Blk SnippetBlockWithBrace 667 ElseList []*SnippetIfStmt 668 } 669 670 var _ Snippet = (*SnippetIfStmt)(nil) 671 672 func (s *SnippetIfStmt) Bytes() []byte { 673 buf := bytes.NewBuffer(nil) 674 675 if s.Cond != nil { 676 buf.WriteString(token.IF.String()) 677 } 678 679 if s.Init != nil { 680 buf.WriteRune(' ') 681 buf.Write(s.Init.Bytes()) 682 buf.WriteRune(';') 683 } 684 685 if s.Cond != nil { 686 buf.WriteRune(' ') 687 buf.Write(s.Cond.Bytes()) 688 } 689 690 buf.WriteRune(' ') 691 buf.Write(s.Blk.Bytes()) 692 693 for _, then := range s.ElseList { 694 buf.WriteRune(' ') 695 buf.WriteString(token.ELSE.String()) 696 if then.Cond != nil { 697 buf.WriteRune(' ') 698 } 699 buf.Write(then.Bytes()) 700 } 701 702 return buf.Bytes() 703 } 704 705 func (s SnippetIfStmt) InitWith(init Snippet) *SnippetIfStmt { 706 s.Init = init 707 return &s 708 } 709 710 func (s SnippetIfStmt) Do(ss ...Snippet) *SnippetIfStmt { 711 s.Blk = append(s.Blk, ss...) 712 return &s 713 } 714 715 func (s SnippetIfStmt) Else(sub *SnippetIfStmt) *SnippetIfStmt { 716 s.ElseList = append(s.ElseList, sub) 717 return &s 718 } 719 720 type SnippetRefExpr struct { 721 Lead Snippet 722 Refs []Snippet 723 } 724 725 var _ Snippet = (*SnippetRefExpr)(nil) 726 727 func (s *SnippetRefExpr) Bytes() []byte { 728 buf := bytes.NewBuffer(nil) 729 730 buf.Write(s.Lead.Bytes()) 731 for _, ref := range s.Refs { 732 buf.WriteRune('.') 733 buf.Write(ref.Bytes()) 734 } 735 736 return buf.Bytes() 737 } 738 739 type SnippetStarExpr struct { 740 SnippetCanAddr 741 SnippetType 742 T SnippetType 743 } 744 745 var _ Snippet = (*SnippetStarExpr)(nil) 746 747 func (s *SnippetStarExpr) Bytes() []byte { 748 buf := bytes.NewBuffer(nil) 749 750 buf.WriteString(token.MUL.String()) 751 buf.Write(s.T.Bytes()) 752 753 return buf.Bytes() 754 } 755 756 type SnippetAccessValueExpr struct { 757 V SnippetCanAddr 758 IfCanAddr 759 } 760 761 func (s *SnippetAccessValueExpr) Bytes() []byte { 762 buf := bytes.NewBuffer(nil) 763 buf.WriteString(token.MUL.String()) 764 buf.WriteRune('(') 765 buf.Write(s.V.Bytes()) 766 buf.WriteRune(')') 767 return buf.Bytes() 768 } 769 770 type SnippetAddrExpr struct { 771 V SnippetCanAddr 772 } 773 774 var _ Snippet = (*SnippetAddrExpr)(nil) 775 776 func (s *SnippetAddrExpr) Bytes() []byte { 777 buf := bytes.NewBuffer(nil) 778 779 buf.WriteString(token.AND.String()) 780 buf.Write(s.V.Bytes()) 781 782 return buf.Bytes() 783 } 784 785 type SnippetParenExpr struct { 786 SnippetCanAddr 787 V Snippet 788 } 789 790 var _ Snippet = (*SnippetParenExpr)(nil) 791 792 func (s *SnippetParenExpr) Bytes() []byte { 793 buf := bytes.NewBuffer(nil) 794 795 buf.WriteRune('(') 796 buf.Write(s.V.Bytes()) 797 buf.WriteRune(')') 798 799 return buf.Bytes() 800 } 801 802 type SnippetIncExpr struct { 803 Value SnippetCanAddr 804 } 805 806 var _ Snippet = (*SnippetIncExpr)(nil) 807 808 func (s *SnippetIncExpr) Bytes() []byte { 809 buf := bytes.NewBuffer(nil) 810 811 buf.Write(s.Value.Bytes()) 812 buf.WriteString(token.INC.String()) 813 814 return buf.Bytes() 815 } 816 817 type SnippetDecExpr struct { 818 Value SnippetCanAddr 819 } 820 821 var _ Snippet = (*SnippetDecExpr)(nil) 822 823 func (s *SnippetDecExpr) Bytes() []byte { 824 buf := bytes.NewBuffer(nil) 825 826 buf.Write(s.Value.Bytes()) 827 buf.WriteString(token.DEC.String()) 828 829 return buf.Bytes() 830 } 831 832 type SnippetArrowExpr struct { 833 SnippetCanAddr 834 Chan Snippet 835 } 836 837 var _ Snippet = (*SnippetArrowExpr)(nil) 838 839 func (s *SnippetArrowExpr) Bytes() []byte { 840 buf := bytes.NewBuffer(nil) 841 842 buf.WriteString(token.ARROW.String()) 843 buf.Write(s.Chan.Bytes()) 844 845 return buf.Bytes() 846 } 847 848 type SnippetAccessExpr struct { 849 V Snippet 850 Index Snippet 851 } 852 853 var _ Snippet = (*SnippetAccessExpr)(nil) 854 855 func (s *SnippetAccessExpr) Bytes() []byte { 856 buf := bytes.NewBuffer(nil) 857 858 buf.Write(s.V.Bytes()) 859 buf.WriteString(token.LBRACK.String()) 860 buf.Write(s.Index.Bytes()) 861 buf.WriteString(token.RBRACK.String()) 862 863 return buf.Bytes() 864 } 865 866 type SnippetCallExpr struct { 867 Callee Snippet 868 Args []Snippet 869 Ellipsis bool 870 Modifier token.Token 871 } 872 873 var _ Snippet = (*SnippetCallExpr)(nil) 874 875 func (s *SnippetCallExpr) Bytes() []byte { 876 buf := bytes.NewBuffer(nil) 877 878 if s.Modifier > token.ILLEGAL { 879 buf.WriteString(s.Modifier.String()) 880 buf.WriteRune(' ') 881 } 882 883 buf.Write(s.Callee.Bytes()) 884 885 buf.WriteRune('(') 886 for i, a := range s.Args { 887 if i > 0 { 888 buf.WriteString(", ") 889 } 890 buf.Write(a.Bytes()) 891 } 892 893 if s.Ellipsis { 894 buf.WriteString(token.ELLIPSIS.String()) 895 } 896 buf.WriteRune(')') 897 898 return buf.Bytes() 899 } 900 901 func (s SnippetCallExpr) AsDefer() *SnippetCallExpr { 902 s.Modifier = token.DEFER 903 return &s 904 } 905 906 func (s SnippetCallExpr) AsRoutine() *SnippetCallExpr { 907 s.Modifier = token.GO 908 return &s 909 } 910 911 func (s SnippetCallExpr) WithEllipsis() *SnippetCallExpr { 912 s.Ellipsis = true 913 return &s 914 } 915 916 type BuiltInType string 917 918 var _ SnippetType = BuiltInType("") 919 920 func (t BuiltInType) Bytes() []byte { return []byte(t) } 921 922 func (t BuiltInType) _type() {} 923 924 type MapType struct { 925 SnippetType 926 Tk, Tv SnippetType 927 } 928 929 var _ SnippetType = (*MapType)(nil) 930 931 func (m *MapType) Bytes() []byte { 932 buf := bytes.NewBuffer(nil) 933 934 buf.WriteString(token.MAP.String()) 935 buf.WriteRune('[') 936 buf.Write(m.Tk.Bytes()) 937 buf.WriteRune(']') 938 buf.Write(m.Tv.Bytes()) 939 940 return buf.Bytes() 941 } 942 943 type ArrayType struct { 944 SnippetType 945 T SnippetType 946 Len int 947 } 948 949 var _ SnippetType = (*ArrayType)(nil) 950 951 func (s *ArrayType) Bytes() []byte { 952 buf := bytes.NewBuffer(nil) 953 954 buf.WriteString(fmt.Sprintf("[%d]", s.Len)) 955 buf.Write(s.T.Bytes()) 956 957 return buf.Bytes() 958 } 959 960 type SliceType struct { 961 SnippetType 962 T SnippetType 963 } 964 965 var _ SnippetType = (*SliceType)(nil) 966 967 func (s *SliceType) Bytes() []byte { 968 buf := bytes.NewBuffer(nil) 969 970 buf.WriteRune('[') 971 buf.WriteRune(']') 972 buf.Write(s.T.Bytes()) 973 974 return buf.Bytes() 975 } 976 977 type ChanMode uint8 978 979 const ( 980 ChanModeRO ChanMode = 0x01 981 ChanModeWO ChanMode = 0x10 982 ChanModeRW ChanMode = 0x11 983 ) 984 985 type ChanType struct { 986 SnippetType 987 T SnippetType 988 Mode ChanMode 989 } 990 991 var _ SnippetType = (*ChanType)(nil) 992 993 func (s *ChanType) Bytes() []byte { 994 buf := bytes.NewBuffer(nil) 995 996 if s.Mode == ChanModeRO { 997 buf.WriteString(token.ARROW.String()) 998 } 999 buf.WriteString(token.CHAN.String()) 1000 if s.Mode == ChanModeWO { 1001 buf.WriteString(token.ARROW.String()) 1002 } 1003 buf.WriteRune(' ') 1004 buf.Write(s.T.Bytes()) 1005 1006 return buf.Bytes() 1007 } 1008 1009 type EllipsisType struct { 1010 SnippetType 1011 T SnippetType 1012 } 1013 1014 var _ SnippetType = (*EllipsisType)(nil) 1015 1016 func (s *EllipsisType) Bytes() []byte { 1017 buf := bytes.NewBuffer(nil) 1018 1019 buf.WriteString(token.ELLIPSIS.String()) 1020 buf.Write(s.T.Bytes()) 1021 1022 return buf.Bytes() 1023 } 1024 1025 type FuncType struct { 1026 SnippetType 1027 IfCanBeIfMethod 1028 Name *SnippetIdent 1029 Recv *SnippetField 1030 Args []*SnippetField 1031 Rets []*SnippetField 1032 Blk SnippetBlockWithBrace 1033 noToken bool 1034 } 1035 1036 var _ SnippetType = (*FuncType)(nil) 1037 1038 func (f *FuncType) Bytes() []byte { 1039 buf := bytes.NewBuffer(nil) 1040 1041 if !f.noToken { 1042 buf.WriteString(token.FUNC.String()) 1043 buf.WriteRune(' ') 1044 } 1045 1046 if f.Recv != nil { 1047 buf.WriteRune('(') 1048 buf.Write(f.Recv.Bytes()) 1049 buf.WriteRune(')') 1050 buf.WriteRune(' ') 1051 } 1052 1053 if f.Name != nil { 1054 buf.Write(f.Name.Bytes()) 1055 } 1056 1057 buf.WriteByte('(') 1058 for i := range f.Args { 1059 if i > 0 { 1060 buf.WriteRune(',') 1061 buf.WriteRune(' ') 1062 } 1063 buf.Write(f.Args[i].WithoutTag().Bytes()) 1064 } 1065 buf.WriteByte(')') 1066 buf.WriteRune(' ') 1067 1068 quoteRet := len(f.Rets) > 0 && len(f.Rets[0].Names) > 0 || len(f.Rets) > 1 1069 1070 if quoteRet { 1071 buf.WriteRune('(') 1072 } 1073 1074 for i := range f.Rets { 1075 if i > 0 { 1076 buf.WriteRune(',') 1077 buf.WriteRune(' ') 1078 } 1079 buf.Write(f.Rets[i].WithoutTag().Bytes()) 1080 } 1081 1082 if quoteRet { 1083 buf.WriteRune(')') 1084 buf.WriteRune(' ') 1085 } else { 1086 if len(f.Rets) > 0 { 1087 buf.WriteRune(' ') 1088 } 1089 } 1090 1091 if f.Blk != nil { 1092 buf.Write(f.Blk.Bytes()) 1093 } 1094 return buf.Bytes() 1095 } 1096 1097 func (f FuncType) WithoutToken() *FuncType { f.noToken = true; return &f } 1098 1099 func (f FuncType) Named(name string) *FuncType { f.Name = Ident(name); return &f } 1100 1101 func (f FuncType) MethodOf(rcv *SnippetField) *FuncType { f.Recv = rcv; return &f } 1102 1103 func (f FuncType) Return(rets ...*SnippetField) *FuncType { f.Rets = rets; return &f } 1104 1105 func (f FuncType) Do(ss ...Snippet) *FuncType { f.Blk = append([]Snippet{}, ss...); return &f } 1106 1107 type StructType struct { 1108 SnippetType 1109 Fields []*SnippetField 1110 } 1111 1112 var _ SnippetType = (*StructType)(nil) 1113 1114 func (s *StructType) Bytes() []byte { 1115 buf := bytes.NewBuffer(nil) 1116 1117 buf.WriteString(token.STRUCT.String()) 1118 1119 buf.WriteRune(' ') 1120 buf.WriteRune('{') 1121 1122 for i := range s.Fields { 1123 buf.WriteRune('\n') 1124 buf.Write(s.Fields[i].Bytes()) 1125 } 1126 1127 buf.WriteRune('\n') 1128 buf.WriteRune('}') 1129 1130 return buf.Bytes() 1131 } 1132 1133 type InterfaceType struct { 1134 SnippetType 1135 Methods []IfCanBeIfMethod 1136 } 1137 1138 var _ SnippetType = (*InterfaceType)(nil) 1139 1140 func (s *InterfaceType) Bytes() []byte { 1141 buf := bytes.NewBuffer(nil) 1142 1143 buf.WriteString(token.INTERFACE.String()) 1144 if len(s.Methods) == 0 { 1145 buf.WriteRune('{') 1146 buf.WriteRune('}') 1147 return buf.Bytes() 1148 } 1149 buf.WriteRune(' ') 1150 buf.WriteRune('{') 1151 buf.WriteRune('\n') 1152 for i := range s.Methods { 1153 switch method := s.Methods[i].(type) { 1154 case *FuncType: 1155 buf.Write(method.WithoutToken().Bytes()) 1156 case *NamedType: 1157 buf.Write(method.Bytes()) 1158 } 1159 buf.WriteRune('\n') 1160 } 1161 buf.WriteRune('}') 1162 1163 return buf.Bytes() 1164 } 1165 1166 type NamedType struct { 1167 SnippetType 1168 IfCanBeIfMethod 1169 IfCanAddr 1170 Name *SnippetIdent 1171 } 1172 1173 var _ SnippetType = (*NamedType)(nil) 1174 1175 func (t *NamedType) Bytes() []byte { return t.Name.Bytes() }