Skip to content

Commit 71a5a22

Browse files
specify namespace in yaml
1 parent e68f5a4 commit 71a5a22

File tree

6 files changed

+62
-55
lines changed

6 files changed

+62
-55
lines changed

gen/cheader.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{- MCommentN .Copyright 0}}
22

3-
#ifndef {{.Name | ConstantCase}}_H_
4-
#define {{.Name | ConstantCase}}_H_
3+
#ifndef {{.HeaderName | ConstantCase}}_H_
4+
#define {{.HeaderName | ConstantCase}}_H_
55

66
#if defined(WGPU_SHARED_LIBRARY)
77
# if defined(_WIN32)
@@ -222,4 +222,4 @@ WGPU_EXPORT void wgpu{{.Name | PascalCase}}Release{{$.ExtSuffix}}(WGPU{{.Name |
222222
} // extern "C"
223223
#endif
224224

225-
#endif // {{.Name | ConstantCase}}_H_
225+
#endif // {{.HeaderName | ConstantCase}}_H_

gen/gen.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"text/template"
1111
)
1212

13-
type Data struct {
14-
Name string
15-
ExtSuffix string
13+
type Generator struct {
14+
ExtSuffix string
15+
HeaderName string
1616
*Yml
1717
}
1818

19-
func (d *Data) GenCHeader(dst io.Writer) error {
19+
func (g *Generator) Gen(dst io.Writer) error {
2020
t := template.
2121
New("").
2222
Funcs(template.FuncMap{
@@ -27,43 +27,43 @@ func (d *Data) GenCHeader(dst io.Writer) error {
2727
"ConstantCase": ConstantCase,
2828
"PascalCase": PascalCase,
2929
"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,
3434
"IsArray": func(typ string) bool {
3535
return arrayTypeRegexp.Match([]byte(typ))
3636
},
3737
"ArrayType": func(typ string, pointer PointerType) string {
3838
matches := arrayTypeRegexp.FindStringSubmatch(typ)
3939
if len(matches) == 2 {
40-
return d.CType(matches[1], pointer, "")
40+
return g.CType(matches[1], pointer, "")
4141
}
4242
return ""
4343
},
4444
"Singularize": Singularize,
4545
"IsLast": func(i int, s any) bool { return i == reflect.ValueOf(s).Len()-1 },
4646
"FunctionReturns": func(f Function) string {
4747
if f.Returns != nil {
48-
return d.CType(f.Returns.Type, f.Returns.Pointer, "")
48+
return g.CType(f.Returns.Type, f.Returns.Pointer, "")
4949
}
5050
return "void"
5151
},
52-
"FunctionArgs": d.FunctionArgs,
53-
"CallbackArgs": d.CallbackArgs,
54-
"StructMember": d.StructMember,
52+
"FunctionArgs": g.FunctionArgs,
53+
"CallbackArgs": g.CallbackArgs,
54+
"StructMember": g.StructMember,
5555
})
5656
t, err := t.Parse(tmpl)
5757
if err != nil {
5858
return fmt.Errorf("GenCHeader: failed to parse template: %w", err)
5959
}
60-
if err := t.Execute(dst, d); err != nil {
60+
if err := t.Execute(dst, g); err != nil {
6161
return fmt.Errorf("GenCHeader: failed to execute template: %w", err)
6262
}
6363
return nil
6464
}
6565

66-
func (d *Data) CValue(s string) (string, error) {
66+
func (g *Generator) CValue(s string) (string, error) {
6767
switch s {
6868
case "usize_max":
6969
return "SIZE_MAX", nil
@@ -95,7 +95,7 @@ func (d *Data) CValue(s string) (string, error) {
9595
}
9696
}
9797

98-
func (d *Data) CType(typ string, pointerType PointerType, suffix string) string {
98+
func (g *Generator) CType(typ string, pointerType PointerType, suffix string) string {
9999
appendModifiers := func(s string, pointerType PointerType) string {
100100
var sb strings.Builder
101101
sb.WriteString(s)
@@ -153,12 +153,12 @@ func (d *Data) CType(typ string, pointerType PointerType, suffix string) string
153153
}
154154
}
155155

156-
func (d *Data) FunctionArgs(f Function, o *Object) string {
156+
func (g *Generator) FunctionArgs(f Function, o *Object) string {
157157
sb := &strings.Builder{}
158158
if o != nil {
159159
var typeSuffix string
160160
if o.Namespace == "" {
161-
typeSuffix = ConstantCase(d.ExtSuffix)
161+
typeSuffix = ConstantCase(g.ExtSuffix)
162162
} else if o.Namespace != "webgpu" {
163163
typeSuffix = ConstantCase(o.Namespace)
164164
}
@@ -177,16 +177,16 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
177177
}
178178
var typeSuffix string
179179
if arg.Namespace == "" {
180-
typeSuffix = ConstantCase(d.ExtSuffix)
180+
typeSuffix = ConstantCase(g.ExtSuffix)
181181
} else if arg.Namespace != "webgpu" {
182182
typeSuffix = ConstantCase(arg.Namespace)
183183
}
184184
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
185185
if len(matches) == 2 {
186186
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))
188188
} 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))
190190
}
191191
if i != len(f.Args)-1 {
192192
sb.WriteString(", ")
@@ -204,15 +204,15 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
204204
return sb.String()
205205
}
206206

207-
func (d *Data) CallbackArgs(f Function) string {
207+
func (g *Generator) CallbackArgs(f Function) string {
208208
sb := &strings.Builder{}
209209
for _, arg := range f.ReturnsAsync {
210210
if arg.Optional {
211211
sb.WriteString("WGPU_NULLABLE ")
212212
}
213213
var typeSuffix string
214214
if arg.Namespace == "" {
215-
typeSuffix = ConstantCase(d.ExtSuffix)
215+
typeSuffix = ConstantCase(g.ExtSuffix)
216216
} else if arg.Namespace != "webgpu" {
217217
typeSuffix = ConstantCase(arg.Namespace)
218218
}
@@ -223,16 +223,16 @@ func (d *Data) CallbackArgs(f Function) string {
223223
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
224224
if len(matches) == 2 {
225225
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))
227227
} 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))
229229
}
230230
}
231231
sb.WriteString("WGPU_NULLABLE void * userdata")
232232
return sb.String()
233233
}
234234

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) {
236236
var entryValue uint16
237237
entry := e.Entries[entryIndex]
238238
if entry.Value == "" {
@@ -256,14 +256,14 @@ func (d *Data) EnumValue(prefix string, e Enum, entryIndex int) (string, error)
256256
return fmt.Sprintf("%s%.4X", prefix, entryValue), nil
257257
}
258258

259-
func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
259+
func (g *Generator) BitflagValue(b Bitflag, entryIndex int) (string, error) {
260260
entry := b.Entries[entryIndex]
261261
var entryValue string
262262
if len(entry.ValueCombination) > 0 {
263263
for valueIndex, v := range entry.ValueCombination {
264264
entryValue += "WGPU" + PascalCase(b.Name) + "_" + PascalCase(v)
265-
if d.ExtSuffix != "" {
266-
entryValue += "_" + d.ExtSuffix
265+
if g.ExtSuffix != "" {
266+
entryValue += "_" + g.ExtSuffix
267267
}
268268
if valueIndex != len(entry.ValueCombination)-1 {
269269
entryValue += " | "
@@ -293,24 +293,24 @@ func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
293293
return entryValue, nil
294294
}
295295

296-
func (d *Data) StructMember(s Struct, memberIndex int) (string, error) {
296+
func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
297297
member := s.Members[memberIndex]
298298
sb := &strings.Builder{}
299299
if member.Optional {
300300
sb.WriteString("WGPU_NULLABLE ")
301301
}
302302
var typeSuffix string
303303
if member.Namespace == "" {
304-
typeSuffix = ConstantCase(d.ExtSuffix)
304+
typeSuffix = ConstantCase(g.ExtSuffix)
305305
} else if member.Namespace != "webgpu" {
306306
typeSuffix = ConstantCase(member.Namespace)
307307
}
308308
matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
309309
if len(matches) == 2 {
310310
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))
312312
} 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))
314314
}
315315
return sb.String(), nil
316316
}

gen/main.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,40 @@ func main() {
4545

4646
// Generate the header files
4747
for i, yamlPath := range yamlPaths {
48-
src, err := os.ReadFile(yamlPath)
49-
if err != nil {
50-
panic(err)
48+
headerPath := headerPaths[i]
49+
headerFileName := filepath.Base(headerPath)
50+
headerFileNameSplit := strings.Split(headerFileName, ".")
51+
if len(headerFileNameSplit) != 2 {
52+
panic("got invalid header file name: " + headerFileName)
5153
}
5254

53-
var yml Yml
54-
if err := yaml.Unmarshal(src, &yml); err != nil {
55+
src, err := os.ReadFile(yamlPath)
56+
if err != nil {
5557
panic(err)
5658
}
5759

58-
SortAndTransform(&yml)
59-
60-
headerPath := headerPaths[i]
6160
dst, err := os.Create(headerPath)
6261
if err != nil {
6362
panic(err)
6463
}
6564

66-
fileName := filepath.Base(yamlPath)
67-
fileNameSplit := strings.Split(fileName, ".")
68-
if len(fileNameSplit) != 2 {
69-
panic("got invalid file name: " + fileName)
65+
var yml Yml
66+
if err := yaml.Unmarshal(src, &yml); err != nil {
67+
panic(err)
7068
}
7169

70+
SortAndTransform(&yml)
71+
7272
suffix := ""
73-
if fileNameSplit[0] != "webgpu" && extSuffix {
74-
suffix = strings.ToUpper(fileNameSplit[0])
73+
if yml.Name != "webgpu" && extSuffix {
74+
suffix = strings.ToUpper(yml.Name)
7575
}
76-
d := &Data{
77-
Yml: &yml,
78-
Name: fileNameSplit[0],
79-
ExtSuffix: suffix,
76+
g := &Generator{
77+
Yml: &yml,
78+
HeaderName: headerFileNameSplit[0],
79+
ExtSuffix: suffix,
8080
}
81-
if err := d.GenCHeader(dst); err != nil {
81+
if err := g.Gen(dst); err != nil {
8282
panic(err)
8383
}
8484
}

gen/yml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type Yml struct {
1111
Copyright string `yaml:"copyright"`
12+
Name string `yaml:"name"`
1213
EnumPrefix string `yaml:"enum_prefix"`
1314

1415
Constants []Constant `yaml:"constants"`

schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@
160160
"type": "string",
161161
"description": "The license string to include at the top of the generated header"
162162
},
163+
"name": {
164+
"$ref": "#/definitions/Name",
165+
"description": "The name/namespace of the specification"
166+
},
163167
"enum_prefix": {
164168
"type": "string",
165169
"pattern": "^0x[0-9]{4}$",
@@ -382,6 +386,7 @@
382386
},
383387
"required": [
384388
"copyright",
389+
"name",
385390
"enum_prefix",
386391
"constants",
387392
"enums",

webgpu.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ copyright: |
33
44
SPDX-License-Identifier: BSD-3-Clause
55
6+
name: webgpu
67
enum_prefix: '0x0000'
78

89
constants:

0 commit comments

Comments
 (0)