persist/cmd/pgen/gen.go

179 lines
3.5 KiB
Go
Raw Normal View History

2018-08-03 15:08:30 -04:00
package main
import (
"path/filepath"
2018-08-03 15:08:30 -04:00
"log"
"go/ast"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"os"
"regexp"
"strings"
2018-08-03 15:08:30 -04:00
)
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
name string
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
}
var (
qexp *regexp.Regexp
)
2018-08-03 15:08:30 -04:00
func init() {
fs,err := filepath.Glob("pgen*.go")
if err != nil {
log.Fatal("Glob error")
}
for _,f := range(fs) {
err := os.Remove(f)
if err != nil && os.IsExist(err) {
log.Fatal("Removing ",f,err)
}
2018-08-03 15:08:30 -04:00
}
qexp = regexp.MustCompile(`"`)
2018-08-03 15:08:30 -04:00
}
func NewGenerator(ns ...string) *Generator {
2018-08-06 10:01:40 -04:00
g := &Generator{}
g.firstone = true
g.fset = token.NewFileSet()
if len(ns) > 0 {
g.name = ns[0]
} else {
g.name = "main"
}
2018-08-06 10:01:40 -04:00
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
f, err := parser.ParseFile(g.fset, "", template, parser.ParseComments)
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
}
func (g *Generator) Import(name string) {
2018-08-06 10:01:40 -04:00
if g.nodes == nil {
return
}
// strip quotation marks from imports
name = qexp.ReplaceAllString(name,"")
astutil.AddImport(g.fset, g.nodes, name)
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
}
var of *os.File
var err error
if g.name == "main" {
of, err = os.Create("pgen.go")
} else {
oname := strings.Join([]string{"pgen_",g.name,".go"},"")
of, err = os.Create(oname)
g.nodes.Name = ast.NewIdent(g.name)
}
2018-08-03 15:08:30 -04:00
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)
}
}