php-parser/pkg/ast/node_funcs_gen.go

88 lines
1.6 KiB
Go
Raw Normal View History

//go:build ignore
// +build ignore
package main
import (
"fmt"
"os"
"regexp"
"text/template"
2023-12-09 21:36:19 +00:00
"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
2023-12-09 21:36:19 +00:00
import "git.maride.cc/maride/php-parser/pkg/position"
2023-03-26 19:31:37 +00:00
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
}
2023-03-26 19:31:37 +00:00
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})
}