Skip to content

Commit

Permalink
specify namespace in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya committed Mar 3, 2024
1 parent e68f5a4 commit 71a5a22
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 55 deletions.
6 changes: 3 additions & 3 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- MCommentN .Copyright 0}}

#ifndef {{.Name | ConstantCase}}_H_
#define {{.Name | ConstantCase}}_H_
#ifndef {{.HeaderName | ConstantCase}}_H_
#define {{.HeaderName | ConstantCase}}_H_

#if defined(WGPU_SHARED_LIBRARY)
# if defined(_WIN32)
Expand Down Expand Up @@ -222,4 +222,4 @@ WGPU_EXPORT void wgpu{{.Name | PascalCase}}Release{{$.ExtSuffix}}(WGPU{{.Name |
} // extern "C"
#endif

#endif // {{.Name | ConstantCase}}_H_
#endif // {{.HeaderName | ConstantCase}}_H_
66 changes: 33 additions & 33 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"text/template"
)

type Data struct {
Name string
ExtSuffix string
type Generator struct {
ExtSuffix string
HeaderName string
*Yml
}

func (d *Data) GenCHeader(dst io.Writer) error {
func (g *Generator) Gen(dst io.Writer) error {
t := template.
New("").
Funcs(template.FuncMap{
Expand All @@ -27,43 +27,43 @@ func (d *Data) GenCHeader(dst io.Writer) error {
"ConstantCase": ConstantCase,
"PascalCase": PascalCase,
"CamelCase": CamelCase,
"CType": d.CType,
"CValue": d.CValue,
"EnumValue": d.EnumValue,
"BitflagValue": d.BitflagValue,
"CType": g.CType,
"CValue": g.CValue,
"EnumValue": g.EnumValue,
"BitflagValue": g.BitflagValue,
"IsArray": func(typ string) bool {
return arrayTypeRegexp.Match([]byte(typ))
},
"ArrayType": func(typ string, pointer PointerType) string {
matches := arrayTypeRegexp.FindStringSubmatch(typ)
if len(matches) == 2 {
return d.CType(matches[1], pointer, "")
return g.CType(matches[1], pointer, "")
}
return ""
},
"Singularize": Singularize,
"IsLast": func(i int, s any) bool { return i == reflect.ValueOf(s).Len()-1 },
"FunctionReturns": func(f Function) string {
if f.Returns != nil {
return d.CType(f.Returns.Type, f.Returns.Pointer, "")
return g.CType(f.Returns.Type, f.Returns.Pointer, "")
}
return "void"
},
"FunctionArgs": d.FunctionArgs,
"CallbackArgs": d.CallbackArgs,
"StructMember": d.StructMember,
"FunctionArgs": g.FunctionArgs,
"CallbackArgs": g.CallbackArgs,
"StructMember": g.StructMember,
})
t, err := t.Parse(tmpl)
if err != nil {
return fmt.Errorf("GenCHeader: failed to parse template: %w", err)
}
if err := t.Execute(dst, d); err != nil {
if err := t.Execute(dst, g); err != nil {
return fmt.Errorf("GenCHeader: failed to execute template: %w", err)
}
return nil
}

func (d *Data) CValue(s string) (string, error) {
func (g *Generator) CValue(s string) (string, error) {
switch s {
case "usize_max":
return "SIZE_MAX", nil
Expand Down Expand Up @@ -95,7 +95,7 @@ func (d *Data) CValue(s string) (string, error) {
}
}

func (d *Data) CType(typ string, pointerType PointerType, suffix string) string {
func (g *Generator) CType(typ string, pointerType PointerType, suffix string) string {
appendModifiers := func(s string, pointerType PointerType) string {
var sb strings.Builder
sb.WriteString(s)
Expand Down Expand Up @@ -153,12 +153,12 @@ func (d *Data) CType(typ string, pointerType PointerType, suffix string) string
}
}

func (d *Data) FunctionArgs(f Function, o *Object) string {
func (g *Generator) FunctionArgs(f Function, o *Object) string {
sb := &strings.Builder{}
if o != nil {
var typeSuffix string
if o.Namespace == "" {
typeSuffix = ConstantCase(d.ExtSuffix)
typeSuffix = ConstantCase(g.ExtSuffix)
} else if o.Namespace != "webgpu" {
typeSuffix = ConstantCase(o.Namespace)
}
Expand All @@ -177,16 +177,16 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
}
var typeSuffix string
if arg.Namespace == "" {
typeSuffix = ConstantCase(d.ExtSuffix)
typeSuffix = ConstantCase(g.ExtSuffix)
} else if arg.Namespace != "webgpu" {
typeSuffix = ConstantCase(arg.Namespace)
}
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name)))
fmt.Fprintf(sb, "%s %s", d.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s %s", g.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
} else {
fmt.Fprintf(sb, "%s %s", d.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s %s", g.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
}
if i != len(f.Args)-1 {
sb.WriteString(", ")
Expand All @@ -204,15 +204,15 @@ func (d *Data) FunctionArgs(f Function, o *Object) string {
return sb.String()
}

func (d *Data) CallbackArgs(f Function) string {
func (g *Generator) CallbackArgs(f Function) string {
sb := &strings.Builder{}
for _, arg := range f.ReturnsAsync {
if arg.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
if arg.Namespace == "" {
typeSuffix = ConstantCase(d.ExtSuffix)
typeSuffix = ConstantCase(g.ExtSuffix)
} else if arg.Namespace != "webgpu" {
typeSuffix = ConstantCase(arg.Namespace)
}
Expand All @@ -223,16 +223,16 @@ func (d *Data) CallbackArgs(f Function) string {
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name)))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, d.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
} else {
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, d.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
}
}
sb.WriteString("WGPU_NULLABLE void * userdata")
return sb.String()
}

func (d *Data) EnumValue(prefix string, e Enum, entryIndex int) (string, error) {
func (g *Generator) EnumValue(prefix string, e Enum, entryIndex int) (string, error) {
var entryValue uint16
entry := e.Entries[entryIndex]
if entry.Value == "" {
Expand All @@ -256,14 +256,14 @@ func (d *Data) EnumValue(prefix string, e Enum, entryIndex int) (string, error)
return fmt.Sprintf("%s%.4X", prefix, entryValue), nil
}

func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
func (g *Generator) BitflagValue(b Bitflag, entryIndex int) (string, error) {
entry := b.Entries[entryIndex]
var entryValue string
if len(entry.ValueCombination) > 0 {
for valueIndex, v := range entry.ValueCombination {
entryValue += "WGPU" + PascalCase(b.Name) + "_" + PascalCase(v)
if d.ExtSuffix != "" {
entryValue += "_" + d.ExtSuffix
if g.ExtSuffix != "" {
entryValue += "_" + g.ExtSuffix
}
if valueIndex != len(entry.ValueCombination)-1 {
entryValue += " | "
Expand Down Expand Up @@ -293,24 +293,24 @@ func (d *Data) BitflagValue(b Bitflag, entryIndex int) (string, error) {
return entryValue, nil
}

func (d *Data) StructMember(s Struct, memberIndex int) (string, error) {
func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]
sb := &strings.Builder{}
if member.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
if member.Namespace == "" {
typeSuffix = ConstantCase(d.ExtSuffix)
typeSuffix = ConstantCase(g.ExtSuffix)
} else if member.Namespace != "webgpu" {
typeSuffix = ConstantCase(member.Namespace)
}
matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount;\n", CamelCase(Singularize(member.Name)))
fmt.Fprintf(sb, " %s %s;", d.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
fmt.Fprintf(sb, " %s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", d.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
}
return sb.String(), nil
}
38 changes: 19 additions & 19 deletions gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,40 @@ func main() {

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

var yml Yml
if err := yaml.Unmarshal(src, &yml); err != nil {
src, err := os.ReadFile(yamlPath)
if err != nil {
panic(err)
}

SortAndTransform(&yml)

headerPath := headerPaths[i]
dst, err := os.Create(headerPath)
if err != nil {
panic(err)
}

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

SortAndTransform(&yml)

suffix := ""
if fileNameSplit[0] != "webgpu" && extSuffix {
suffix = strings.ToUpper(fileNameSplit[0])
if yml.Name != "webgpu" && extSuffix {
suffix = strings.ToUpper(yml.Name)
}
d := &Data{
Yml: &yml,
Name: fileNameSplit[0],
ExtSuffix: suffix,
g := &Generator{
Yml: &yml,
HeaderName: headerFileNameSplit[0],
ExtSuffix: suffix,
}
if err := d.GenCHeader(dst); err != nil {
if err := g.Gen(dst); err != nil {
panic(err)
}
}
Expand Down
1 change: 1 addition & 0 deletions gen/yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Yml struct {
Copyright string `yaml:"copyright"`
Name string `yaml:"name"`
EnumPrefix string `yaml:"enum_prefix"`

Constants []Constant `yaml:"constants"`
Expand Down
5 changes: 5 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
"type": "string",
"description": "The license string to include at the top of the generated header"
},
"name": {
"$ref": "#/definitions/Name",
"description": "The name/namespace of the specification"
},
"enum_prefix": {
"type": "string",
"pattern": "^0x[0-9]{4}$",
Expand Down Expand Up @@ -382,6 +386,7 @@
},
"required": [
"copyright",
"name",
"enum_prefix",
"constants",
"enums",
Expand Down
1 change: 1 addition & 0 deletions webgpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ copyright: |
SPDX-License-Identifier: BSD-3-Clause
name: webgpu
enum_prefix: '0x0000'

constants:
Expand Down

0 comments on commit 71a5a22

Please sign in to comment.