@@ -10,13 +10,13 @@ import (
10
10
"text/template"
11
11
)
12
12
13
- type Data struct {
14
- Name string
15
- ExtSuffix string
13
+ type Generator struct {
14
+ ExtSuffix string
15
+ HeaderName string
16
16
* Yml
17
17
}
18
18
19
- func (d * Data ) GenCHeader (dst io.Writer ) error {
19
+ func (g * Generator ) Gen (dst io.Writer ) error {
20
20
t := template .
21
21
New ("" ).
22
22
Funcs (template.FuncMap {
@@ -27,43 +27,43 @@ func (d *Data) GenCHeader(dst io.Writer) error {
27
27
"ConstantCase" : ConstantCase ,
28
28
"PascalCase" : PascalCase ,
29
29
"CamelCase" : CamelCase ,
30
- "CType" : d .CType ,
31
- "CValue" : d .CValue ,
32
- "EnumValue" : d .EnumValue ,
33
- "BitflagValue" : d .BitflagValue ,
30
+ "CType" : g .CType ,
31
+ "CValue" : g .CValue ,
32
+ "EnumValue" : g .EnumValue ,
33
+ "BitflagValue" : g .BitflagValue ,
34
34
"IsArray" : func (typ string ) bool {
35
35
return arrayTypeRegexp .Match ([]byte (typ ))
36
36
},
37
37
"ArrayType" : func (typ string , pointer PointerType ) string {
38
38
matches := arrayTypeRegexp .FindStringSubmatch (typ )
39
39
if len (matches ) == 2 {
40
- return d .CType (matches [1 ], pointer , "" )
40
+ return g .CType (matches [1 ], pointer , "" )
41
41
}
42
42
return ""
43
43
},
44
44
"Singularize" : Singularize ,
45
45
"IsLast" : func (i int , s any ) bool { return i == reflect .ValueOf (s ).Len ()- 1 },
46
46
"FunctionReturns" : func (f Function ) string {
47
47
if f .Returns != nil {
48
- return d .CType (f .Returns .Type , f .Returns .Pointer , "" )
48
+ return g .CType (f .Returns .Type , f .Returns .Pointer , "" )
49
49
}
50
50
return "void"
51
51
},
52
- "FunctionArgs" : d .FunctionArgs ,
53
- "CallbackArgs" : d .CallbackArgs ,
54
- "StructMember" : d .StructMember ,
52
+ "FunctionArgs" : g .FunctionArgs ,
53
+ "CallbackArgs" : g .CallbackArgs ,
54
+ "StructMember" : g .StructMember ,
55
55
})
56
56
t , err := t .Parse (tmpl )
57
57
if err != nil {
58
58
return fmt .Errorf ("GenCHeader: failed to parse template: %w" , err )
59
59
}
60
- if err := t .Execute (dst , d ); err != nil {
60
+ if err := t .Execute (dst , g ); err != nil {
61
61
return fmt .Errorf ("GenCHeader: failed to execute template: %w" , err )
62
62
}
63
63
return nil
64
64
}
65
65
66
- func (d * Data ) CValue (s string ) (string , error ) {
66
+ func (g * Generator ) CValue (s string ) (string , error ) {
67
67
switch s {
68
68
case "usize_max" :
69
69
return "SIZE_MAX" , nil
@@ -95,7 +95,7 @@ func (d *Data) CValue(s string) (string, error) {
95
95
}
96
96
}
97
97
98
- func (d * Data ) CType (typ string , pointerType PointerType , suffix string ) string {
98
+ func (g * Generator ) CType (typ string , pointerType PointerType , suffix string ) string {
99
99
appendModifiers := func (s string , pointerType PointerType ) string {
100
100
var sb strings.Builder
101
101
sb .WriteString (s )
@@ -153,12 +153,12 @@ func (d *Data) CType(typ string, pointerType PointerType, suffix string) string
153
153
}
154
154
}
155
155
156
- func (d * Data ) FunctionArgs (f Function , o * Object ) string {
156
+ func (g * Generator ) FunctionArgs (f Function , o * Object ) string {
157
157
sb := & strings.Builder {}
158
158
if o != nil {
159
159
var typeSuffix string
160
160
if o .Namespace == "" {
161
- typeSuffix = ConstantCase (d .ExtSuffix )
161
+ typeSuffix = ConstantCase (g .ExtSuffix )
162
162
} else if o .Namespace != "webgpu" {
163
163
typeSuffix = ConstantCase (o .Namespace )
164
164
}
@@ -177,16 +177,16 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
177
177
}
178
178
var typeSuffix string
179
179
if arg .Namespace == "" {
180
- typeSuffix = ConstantCase (d .ExtSuffix )
180
+ typeSuffix = ConstantCase (g .ExtSuffix )
181
181
} else if arg .Namespace != "webgpu" {
182
182
typeSuffix = ConstantCase (arg .Namespace )
183
183
}
184
184
matches := arrayTypeRegexp .FindStringSubmatch (arg .Type )
185
185
if len (matches ) == 2 {
186
186
fmt .Fprintf (sb , "size_t %sCount, " , CamelCase (Singularize (arg .Name )))
187
- fmt .Fprintf (sb , "%s %s" , d .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
187
+ fmt .Fprintf (sb , "%s %s" , g .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
188
188
} else {
189
- fmt .Fprintf (sb , "%s %s" , d .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
189
+ fmt .Fprintf (sb , "%s %s" , g .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
190
190
}
191
191
if i != len (f .Args )- 1 {
192
192
sb .WriteString (", " )
@@ -204,15 +204,15 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
204
204
return sb .String ()
205
205
}
206
206
207
- func (d * Data ) CallbackArgs (f Function ) string {
207
+ func (g * Generator ) CallbackArgs (f Function ) string {
208
208
sb := & strings.Builder {}
209
209
for _ , arg := range f .ReturnsAsync {
210
210
if arg .Optional {
211
211
sb .WriteString ("WGPU_NULLABLE " )
212
212
}
213
213
var typeSuffix string
214
214
if arg .Namespace == "" {
215
- typeSuffix = ConstantCase (d .ExtSuffix )
215
+ typeSuffix = ConstantCase (g .ExtSuffix )
216
216
} else if arg .Namespace != "webgpu" {
217
217
typeSuffix = ConstantCase (arg .Namespace )
218
218
}
@@ -223,16 +223,16 @@ func (d *Data) CallbackArgs(f Function) string {
223
223
matches := arrayTypeRegexp .FindStringSubmatch (arg .Type )
224
224
if len (matches ) == 2 {
225
225
fmt .Fprintf (sb , "size_t %sCount, " , CamelCase (Singularize (arg .Name )))
226
- fmt .Fprintf (sb , "%s%s %s, " , structPrefix , d .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
226
+ fmt .Fprintf (sb , "%s%s %s, " , structPrefix , g .CType (matches [1 ], arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
227
227
} else {
228
- fmt .Fprintf (sb , "%s%s %s, " , structPrefix , d .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
228
+ fmt .Fprintf (sb , "%s%s %s, " , structPrefix , g .CType (arg .Type , arg .Pointer , typeSuffix ), CamelCase (arg .Name ))
229
229
}
230
230
}
231
231
sb .WriteString ("WGPU_NULLABLE void * userdata" )
232
232
return sb .String ()
233
233
}
234
234
235
- func (d * Data ) EnumValue (prefix string , e Enum , entryIndex int ) (string , error ) {
235
+ func (g * Generator ) EnumValue (prefix string , e Enum , entryIndex int ) (string , error ) {
236
236
var entryValue uint16
237
237
entry := e .Entries [entryIndex ]
238
238
if entry .Value == "" {
@@ -256,14 +256,14 @@ func (d *Data) EnumValue(prefix string, e Enum, entryIndex int) (string, error)
256
256
return fmt .Sprintf ("%s%.4X" , prefix , entryValue ), nil
257
257
}
258
258
259
- func (d * Data ) BitflagValue (b Bitflag , entryIndex int ) (string , error ) {
259
+ func (g * Generator ) BitflagValue (b Bitflag , entryIndex int ) (string , error ) {
260
260
entry := b .Entries [entryIndex ]
261
261
var entryValue string
262
262
if len (entry .ValueCombination ) > 0 {
263
263
for valueIndex , v := range entry .ValueCombination {
264
264
entryValue += "WGPU" + PascalCase (b .Name ) + "_" + PascalCase (v )
265
- if d .ExtSuffix != "" {
266
- entryValue += "_" + d .ExtSuffix
265
+ if g .ExtSuffix != "" {
266
+ entryValue += "_" + g .ExtSuffix
267
267
}
268
268
if valueIndex != len (entry .ValueCombination )- 1 {
269
269
entryValue += " | "
@@ -293,24 +293,24 @@ func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
293
293
return entryValue , nil
294
294
}
295
295
296
- func (d * Data ) StructMember (s Struct , memberIndex int ) (string , error ) {
296
+ func (g * Generator ) StructMember (s Struct , memberIndex int ) (string , error ) {
297
297
member := s .Members [memberIndex ]
298
298
sb := & strings.Builder {}
299
299
if member .Optional {
300
300
sb .WriteString ("WGPU_NULLABLE " )
301
301
}
302
302
var typeSuffix string
303
303
if member .Namespace == "" {
304
- typeSuffix = ConstantCase (d .ExtSuffix )
304
+ typeSuffix = ConstantCase (g .ExtSuffix )
305
305
} else if member .Namespace != "webgpu" {
306
306
typeSuffix = ConstantCase (member .Namespace )
307
307
}
308
308
matches := arrayTypeRegexp .FindStringSubmatch (member .Type )
309
309
if len (matches ) == 2 {
310
310
fmt .Fprintf (sb , "size_t %sCount;\n " , CamelCase (Singularize (member .Name )))
311
- fmt .Fprintf (sb , " %s %s;" , d .CType (matches [1 ], member .Pointer , typeSuffix ), CamelCase (member .Name ))
311
+ fmt .Fprintf (sb , " %s %s;" , g .CType (matches [1 ], member .Pointer , typeSuffix ), CamelCase (member .Name ))
312
312
} else {
313
- fmt .Fprintf (sb , "%s %s;" , d .CType (member .Type , member .Pointer , typeSuffix ), CamelCase (member .Name ))
313
+ fmt .Fprintf (sb , "%s %s;" , g .CType (member .Type , member .Pointer , typeSuffix ), CamelCase (member .Name ))
314
314
}
315
315
return sb .String (), nil
316
316
}
0 commit comments