persist/cmd/pgen/gen.go

155 lines
3.1 KiB
Go
Raw Normal View History

2018-08-03 15:08:30 -04:00
package main
import (
"log"
"go/ast"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"os"
"regexp"
)
type subspec struct {
r *regexp.Regexp
s string
}
2018-08-06 10:01:40 -04:00
type Generator struct{
2018-08-03 15:08:30 -04:00
firstone bool
2018-08-06 10:01:40 -04:00
fset *token.FileSet
2018-08-03 15:08:30 -04:00
fspec, tspec, nspec subspec
2018-08-06 10:01:40 -04:00
nspecs []subspec
2018-08-03 15:08:30 -04:00
nodes *ast.File
2018-08-06 10:01:40 -04:00
}
2018-08-03 15:08:30 -04:00
func subs(dst *string, specs []subspec) {
for _,spec := range(specs) {
sub(dst,spec)
}
}
func sub(dst *string,spec subspec) {
if spec.r.MatchString(*dst) {
*dst = spec.r.ReplaceAllString(*dst,spec.s)
}
}
2018-08-06 10:01:40 -04:00
func (g *Generator) Visit(node ast.Node) ast.Visitor {
2018-08-03 15:08:30 -04:00
switch n := node.(type) {
case *ast.Ident:
if n.Obj != nil && n.Obj.Kind == ast.Typ {
2018-08-06 10:01:40 -04:00
subs(&n.Name,g.nspecs)
subs(&n.Obj.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
case *ast.TypeSwitchStmt:
for _,s := range(n.Body.List) {
cs, ok := s.(*ast.CaseClause); if !ok { continue }
for _,c := range(cs.List) {
ci, ok := c.(*ast.Ident); if !ok { continue }
2018-08-06 10:01:40 -04:00
subs(&ci.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
}
case *ast.TypeAssertExpr:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.Ellipsis:
2018-08-06 10:01:40 -04:00
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.Field:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.StarExpr:
2018-08-06 10:01:40 -04:00
nt, ok := n.X.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.ValueSpec:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.ArrayType:
2018-08-06 10:01:40 -04:00
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.MapType:
nt, ok := n.Key.(*ast.Ident); if ok {
2018-08-06 10:01:40 -04:00
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
nt, ok = n.Value.(*ast.Ident); if ok {
2018-08-06 10:01:40 -04:00
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
case *ast.FuncDecl:
2018-08-06 10:01:40 -04:00
sub(&n.Name.Name,g.fspec)
//ast.Print(g.fset,n)
2018-08-03 15:08:30 -04:00
}
2018-08-06 10:01:40 -04:00
return g
2018-08-03 15:08:30 -04:00
}
func init() {
err := os.Remove("pgen.go")
if err != nil && os.IsExist(err) {
log.Fatal("Removing pgen.go:",err)
}
}
2018-08-06 10:01:40 -04:00
func NewGenerator() *Generator {
g := &Generator{}
g.firstone = true
g.fset = token.NewFileSet()
return g
}
func (g *Generator) Add(newName, typName, typ string) {
g.fspec = subspec{regexp.MustCompile("New"), newName}
g.nspecs = []subspec{
subspec{regexp.MustCompile("_N"), typName},
subspec{regexp.MustCompile("_T"), typ}}
2018-08-03 15:08:30 -04:00
2018-08-06 10:01:40 -04:00
f, err := parser.ParseFile(g.fset, "", template, 0)
2018-08-03 15:08:30 -04:00
if err != nil {
log.Fatal("Parsing persist/template.T:",err)
}
2018-08-06 10:01:40 -04:00
ast.Walk(g,f)
2018-08-03 15:08:30 -04:00
2018-08-06 10:01:40 -04:00
if g.firstone {
g.nodes = f
2018-08-03 15:08:30 -04:00
} else {
for _,decl := range(f.Decls) {
imp, ok := decl.(*ast.GenDecl); if ok {
if imp.Tok == token.IMPORT {
continue // skip imports
}
}
2018-08-06 10:01:40 -04:00
g.nodes.Decls = append(g.nodes.Decls,decl)
2018-08-03 15:08:30 -04:00
}
}
2018-08-06 10:01:40 -04:00
g.firstone = false
2018-08-03 15:08:30 -04:00
}
2018-08-06 10:01:40 -04:00
func (g *Generator) Import(imports map[string]bool) {
if g.nodes == nil {
return
}
// strip quotation marks from imports
qexp := regexp.MustCompile(`"`)
for v := range imports {
v = qexp.ReplaceAllString(v,"")
astutil.AddImport(g.fset, g.nodes, v)
}
2018-08-03 15:08:30 -04:00
}
2018-08-06 10:01:40 -04:00
func (g *Generator) Save() {
if g.nodes == nil {
return
}
2018-08-03 15:08:30 -04:00
of, err := os.Create("pgen.go")
if err != nil {
log.Fatal("Cannot open pgen.go")
}
2018-08-06 10:01:40 -04:00
err = format.Node(of,g.fset,g.nodes)
2018-08-03 15:08:30 -04:00
if err != nil {
log.Fatal("Generate error:",err)
}
}