Convert between function type strings and lists of types. Using strings makes this a little harder, but it makes a lot of the rest of the code easier. This will all go away when we can use go/typechecker directly. splitFunc splits "func(x,y,z) (a,b,c)" into ["x", "y", "z"] and ["a", "b", "c"].
(s string)
| 590 | |
| 591 | // splitFunc splits "func(x,y,z) (a,b,c)" into ["x", "y", "z"] and ["a", "b", "c"]. |
| 592 | func splitFunc(s string) (in, out []string) { |
| 593 | if !strings.HasPrefix(s, "func(") { |
| 594 | return nil, nil |
| 595 | } |
| 596 | |
| 597 | i := len("func(") // index of beginning of 'in' arguments |
| 598 | nparen := 0 |
| 599 | for j := i; j < len(s); j++ { |
| 600 | switch s[j] { |
| 601 | case '(': |
| 602 | nparen++ |
| 603 | case ')': |
| 604 | nparen-- |
| 605 | if nparen < 0 { |
| 606 | // found end of parameter list |
| 607 | out := strings.TrimSpace(s[j+1:]) |
| 608 | if len(out) >= 2 && out[0] == '(' && out[len(out)-1] == ')' { |
| 609 | out = out[1 : len(out)-1] |
| 610 | } |
| 611 | return split(s[i:j]), split(out) |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | return nil, nil |
| 616 | } |
| 617 | |
| 618 | // joinFunc is the inverse of splitFunc. |
| 619 | func joinFunc(in, out []string) string { |