Debian Codegraph @

tools/gen-jscore/main.go

raw symbols SCIP
1 // Command gen-jscore generates go representations of JavaScript core types file.
2 package main
3
4 import (
5 "embed"
6 "flag"
7 "fmt"
8 "log"
9 "os"
10 "os/exec"
11 "strings"
12 "text/template"
13
14 "gopkg.in/yaml.v3"
15 )
16
17 //go:embed .gen-jscore.yaml
18 var configData []byte
19
20 //go:embed templates/*
21 var templates embed.FS
22
23 // jsType represents JavaScript type to generate.
24 type jsType struct {
25 Prototype *prototype `yaml:"prototype"`
26 Name string `yaml:"name"`
27 ObjectClass string `yaml:"objectClass"`
28 ObjectPrototype string `yaml:"objectPrototype"`
29 Class string `yaml:"class"`
30 Value string `yaml:"value"`
31 Properties []property `yaml:"properties"`
32 Core bool `yaml:"core"`
33 }
34
35 // BlankConstructor is a default fallback returning false for templates.
36 func (t jsType) BlankConstructor() bool {
37 return false
38 }
39
40 // prototype represents a JavaScript prototype to generate.
41 type prototype struct {
42 Value string `yaml:"value"`
43 ObjectClass string `yaml:"objectClass"`
44 Prototype string `yaml:"prototype"`
45 Properties []property `yaml:"properties"`
46 }
47
48 // Property returns the property with the given name.
49 func (p prototype) Property(name string) (*property, error) {
50 for _, prop := range p.Properties {
51 if prop.Name == name {
52 return &prop, nil
53 }
54 }
55
56 return nil, fmt.Errorf("missing property %q", name)
57 }
58
59 // property represents a JavaScript property to generate.
60 type property struct {
61 Name string `yaml:"name"`
62 Call string `yaml:"call"`
63 Mode string `yaml:"mode"`
64 Value string `yaml:"value"`
65 Kind string `yaml:"kind"`
66 Function int `yaml:"function"`
67 }
68
69 // value represents a JavaScript value to generate a Value creator for.
70 type value struct {
71 Name string `yaml:"name"`
72 Type string `yaml:"type"`
73 }
74
75 // config represents our configuration.
76 type config struct {
77 Types []jsType `yaml:"types"`
78 Values []value `yaml:"values"`
79 Log jsType `yaml:"log"`
80 }
81
82 // Type returns the type for name.
83 func (c config) Type(name string) (*jsType, error) {
84 for _, t := range c.Types {
85 if t.Name == name {
86 return &t, nil
87 }
88 }
89
90 return nil, fmt.Errorf("missing type %q", name)
91 }
92
93 // generate generates the context file writing the output to filename.
94 func generate(filename string) (err error) {
95 var cfg config
96 if err = yaml.Unmarshal(configData, &cfg); err != nil {
97 return fmt.Errorf("decode config: %w", err)
98 }
99
100 tmpl := template.New("base").Funcs(template.FuncMap{
101 "ucfirst": ucfirst,
102 "dict": dict,
103 "contains": strings.Contains,
104 })
105
106 tmpl, err = tmpl.ParseFS(templates, "templates/*.tmpl")
107 if err != nil {
108 return fmt.Errorf("parse templates: %w", err)
109 }
110
111 output, err := os.Create(filename) //nolint:gosec
112 if err != nil {
113 return fmt.Errorf("open output: %w", err)
114 }
115
116 defer func() {
117 if errc := output.Close(); err == nil && errc != nil {
118 err = errc
119 }
120 }()
121
122 if err = tmpl.ExecuteTemplate(output, "root.tmpl", cfg); err != nil {
123 return fmt.Errorf("execute template: %w", err)
124 }
125
126 cmd := exec.Command("gofmt", "-w", filename)
127 buf, err := cmd.CombinedOutput()
128 if err != nil {
129 return fmt.Errorf("format output %q: %w", string(buf), err)
130 }
131
132 return nil
133 }
134
135 func main() {
136 var filename string
137 flag.StringVar(&filename, "output", "inline.go", "the filename to write the generated code to")
138 if err := generate(filename); err != nil {
139 log.Fatal(err)
140 }
141 }