php-parser/pkg/ast/node_funcs_gen.go
2023-12-10 00:36:22 +01:00

88 lines
1.6 KiB
Go

//go:build ignore
// +build ignore
package main
import (
"fmt"
"os"
"regexp"
"text/template"
"git.maride.cc/maride/php-parser/pkg/ast"
)
type templData struct {
Types []nodeType
}
type nodeType struct {
Name string
FuncName string
}
var fileTempl = template.Must(
template.New("").
Parse(`// Code generated by "go generate go run node_funcs_gen.go"; DO NOT EDIT.
package ast
import "git.maride.cc/maride/php-parser/pkg/position"
type Type int
const (
TypeNone Type = iota
{{- range $i, $typ := .Types }}
Type{{ $typ.Name }}
{{- end }}
TypeCount
)
{{range $typ := .Types}}
var _ Vertex = &{{$typ.Name}}{}
func (n *{{$typ.Name}}) Accept(v Visitor) {
v.{{$typ.FuncName}}(n)
}
func (n *{{$typ.Name}}) GetPosition() *position.Position {
return n.Position
}
func (n *{{$typ.Name}}) GetType() Type {
return Type{{$typ.Name}}
}
{{end}}`),
)
var typesRgx = regexp.MustCompile(`type ([a-zA-Z]+) struct`)
func main() {
content, err := os.ReadFile("node.go")
if err != nil {
panic(fmt.Errorf("reading 'ast.go': %w", err))
}
matches := typesRgx.FindAllSubmatch(content, -1)
types := make([]nodeType, 0, len(matches))
for _, match := range matches {
name := string(match[1])
funcName := name
if overwrite, ok := ast.TypeToVisitorNameMap[name]; ok {
funcName = overwrite
}
types = append(types, nodeType{
Name: name,
FuncName: funcName,
})
}
f, err := os.Create("node_funcs.go")
if err != nil {
panic(fmt.Errorf("creating/opening 'node_funcs.go': %w", err))
}
fileTempl.Execute(f, templData{types})
}