Skip to content

Commit 90a0314

Browse files
authored
Merge pull request #2130 from xushiwei/q
gopbuiltingen: docForMethod support exargs
2 parents 230fb7b + 5e5a90d commit 90a0314

File tree

4 files changed

+64
-42
lines changed

4 files changed

+64
-42
lines changed

builtin/doc/builtin.gop

+32-15
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func Create(name string) (*os.File, error)
8383
func Lines(r io.Reader) iox.LineReader
8484
func (r io.Reader) Gop_Enum() iox.LineIter
8585

86-
// String converts the floating-point number f to a string,
86+
// String is equivalent to strconv.FormatFloat(f, 'g', -1, 64)
87+
//
88+
// FormatFloat converts the floating-point number f to a string,
8789
// according to the format fmt and precision prec. It rounds the
8890
// result assuming that the original was obtained from a floating-point
8991
// value of bitSize bits (32 for float32, 64 for float64).
@@ -110,12 +112,16 @@ func (f float64) String() string
110112
// String is equivalent to FormatInt(int64(i), 10).
111113
func (i int) String() string
112114

113-
// String returns the string representation of i in the given base,
115+
// String is equivalent to strconv.FormatInt(i, 10)
116+
//
117+
// FormatInt returns the string representation of i in the given base,
114118
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
115119
// for digit values >= 10.
116120
func (i int64) String() string
117121

118-
// String returns the string representation of i in the given base,
122+
// String is equivalent to strconv.FormatUint(u, 10)
123+
//
124+
// FormatUint returns the string representation of i in the given base,
119125
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
120126
// for digit values >= 10.
121127
func (u uint64) String() string
@@ -141,7 +147,9 @@ func (s string) Count(substr string) int
141147
// Int is equivalent to ParseInt(s, 10, 0), converted to type int.
142148
func (s string) Int() (int, error)
143149

144-
// Int64 interprets a string s in the given base (0, 2 to 36) and
150+
// Int64 is equivalent to strconv.ParseInt(s, 10, 64)
151+
//
152+
// ParseInt interprets a string s in the given base (0, 2 to 36) and
145153
// bit size (0 to 64) and returns the corresponding value i.
146154
//
147155
// The string may begin with a leading sign: "+" or "-".
@@ -157,7 +165,7 @@ func (s string) Int() (int, error)
157165
// correspond to int, int8, int16, int32, and int64.
158166
// If bitSize is below 0 or above 64, an error is returned.
159167
//
160-
// The errors that Int64 returns have concrete type *NumError
168+
// The errors that ParseInt returns have concrete type *NumError
161169
// and include err.Num = s. If s is empty or contains invalid
162170
// digits, err.Err = ErrSyntax and the returned value is 0;
163171
// if the value corresponding to s cannot be represented by a
@@ -168,35 +176,39 @@ func (s string) Int() (int, error)
168176
// [integer literals]: https://go.dev/ref/spec#Integer_literals
169177
func (s string) Int64() (i int64, err error)
170178

171-
// Uint64 is like ParseInt but for unsigned numbers.
179+
// Uint64 is equivalent to strconv.ParseUint(s, 10, 64)
180+
//
181+
// ParseUint is like ParseInt but for unsigned numbers.
172182
//
173183
// A sign prefix is not permitted.
174184
func (s string) Uint64() (uint64, error)
175185

176-
// Float converts the string s to a floating-point number
186+
// Float is equivalent to strconv.ParseFloat(s, 64)
187+
//
188+
// ParseFloat converts the string s to a floating-point number
177189
// with the precision specified by bitSize: 32 for float32, or 64 for float64.
178190
// When bitSize=32, the result still has type float64, but it will be
179191
// convertible to float32 without changing its value.
180192
//
181-
// Float accepts decimal and hexadecimal floating-point numbers
193+
// ParseFloat accepts decimal and hexadecimal floating-point numbers
182194
// as defined by the Go syntax for [floating-point literals].
183195
// If s is well-formed and near a valid floating-point number,
184-
// Float returns the nearest floating-point number rounded
196+
// ParseFloat returns the nearest floating-point number rounded
185197
// using IEEE754 unbiased rounding.
186198
// (Parsing a hexadecimal floating-point value only rounds when
187199
// there are more bits in the hexadecimal representation than
188200
// will fit in the mantissa.)
189201
//
190-
// The errors that Float returns have concrete type *NumError
202+
// The errors that ParseFloat returns have concrete type *NumError
191203
// and include err.Num = s.
192204
//
193-
// If s is not syntactically well-formed, Float returns err.Err = ErrSyntax.
205+
// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
194206
//
195207
// If s is syntactically well-formed but is more than 1/2 ULP
196208
// away from the largest floating point number of the given size,
197-
// Float returns f = ±Inf, err.Err = ErrRange.
209+
// ParseFloat returns f = ±Inf, err.Err = ErrRange.
198210
//
199-
// Float recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity"
211+
// ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity"
200212
// as their respective special floating point values. It ignores case when matching.
201213
//
202214
// [floating-point literals]: https://go.dev/ref/spec#Floating-point_literals
@@ -345,8 +357,13 @@ func (s string) SplitN(sep string, n int) []string
345357
// as described in the documentation for SplitAfter.
346358
func (s string) SplitAfterN(sep string, n int) []string
347359

348-
// Replace returns a copy of s with all replacements performed.
349-
func (s string) Replace() string
360+
// Replace returns a copy of the string s with the first n
361+
// non-overlapping instances of old replaced by new.
362+
// If old is empty, it matches at the beginning of the string
363+
// and after each UTF-8 sequence, yielding up to k+1 replacements
364+
// for a k-rune string.
365+
// If n < 0, there is no limit on the number of replacements.
366+
func (s string) Replace(old, new string, n int) string
350367

351368
// ReplaceAll returns a copy of the string s with all
352369
// non-overlapping instances of old replaced by new.

cmd/chore/gopbuiltingen/builtin.gox

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func genAST() *ast.FuncDecl {
1212
ref := f.getAST()
1313
newName := Name.capitalize
1414
return {
15-
Doc: reference.docForFunc(ref.Doc, f.Name, newName),
15+
Doc: docForFunc(ref.Doc, f.Name, newName),
1616
Name: ast.newIdent(newName),
1717
Type: reference.toFuncType(ref.Type, f.Pkg),
1818
}
@@ -22,13 +22,14 @@ func genMethodAST(methods []builtin) *ast.FuncDecl {
2222
f := Fn
2323
at := f.Pkg
2424
ref := f.getAST()
25-
mt, recvType := reference.toMethodType(ref.Type, f.Exargs, at)
25+
ex := f.Exargs
26+
mt, recvType := reference.toMethodType(ref.Type, ex, at)
2627
if at == "" { // builtin
2728
recvType = methods[methods.len-1].genAST().Type.Params.List[0].Type
2829
}
2930
recvName := recvNameOf(recvType)
3031
return {
31-
Doc: reference.docForMethod(ref.Doc, f.Name, Name),
32+
Doc: docForMethod(ref.Doc, at, f.Name, Name, recvName, ex),
3233
Name: ast.newIdent(Name),
3334
Type: mt,
3435
Recv: {List: {{

cmd/chore/gopbuiltingen/helper.gop

+28
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,31 @@ func toParams(params *ast.FieldList, at string) *ast.FieldList {
8181
}
8282
return {List: list}
8383
}
84+
85+
func docForFunc(doc *ast.CommentGroup, oldName, newName string) *ast.CommentGroup {
86+
if doc == nil {
87+
return nil
88+
}
89+
list := make([]*ast.Comment, len(doc.List))
90+
for i, c := range doc.List {
91+
text := c.Text.replaceAll(oldName, newName)
92+
list[i] = {Text: text}
93+
}
94+
return {List: list}
95+
}
96+
97+
func docForMethod(doc *ast.CommentGroup, at, oldName, newName, recvName string, ex *exargs) *ast.CommentGroup {
98+
if doc == nil {
99+
return nil
100+
}
101+
if ex == nil {
102+
return docForFunc(doc, oldName, newName)
103+
}
104+
list := make([]*ast.Comment, 0, len(doc.List)+2)
105+
list <- &ast.Comment{Text: "// ${newName} is equivalent to ${at}.${oldName}(${recvName}, ${ex.Code})"}
106+
list <- &ast.Comment{Text: "//"}
107+
for _, c := range doc.List {
108+
list <- &ast.Comment{Text: c.Text}
109+
}
110+
return {List: list}
111+
}

cmd/chore/gopbuiltingen/reference.gox

-24
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ func .toMethodType(t *ast.FuncType, ex *exargs, at string) (mt *ast.FuncType, re
4545
return
4646
}
4747

48-
func .docForFunc(doc *ast.CommentGroup, oldName, newName string) *ast.CommentGroup {
49-
if doc == nil {
50-
return nil
51-
}
52-
list := make([]*ast.Comment, len(doc.List))
53-
for i, c := range doc.List {
54-
text := c.Text.replaceAll(oldName, newName)
55-
list[i] = {Text: text}
56-
}
57-
return {List: list}
58-
}
59-
60-
func .docForMethod(doc *ast.CommentGroup, oldName, newName string) *ast.CommentGroup {
61-
if doc == nil {
62-
return nil
63-
}
64-
list := make([]*ast.Comment, len(doc.List))
65-
for i, c := range doc.List {
66-
text := c.Text.replaceAll(oldName, newName)
67-
list[i] = {Text: text}
68-
}
69-
return {List: list}
70-
}
71-
7248
var (
7349
fset = token.newFileSet
7450
pkgs = map[string]*ast.Package{}

0 commit comments

Comments
 (0)