Move code generator into its own package.
This commit is contained in:
-157
@@ -1,157 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type subspec struct {
|
||||
r *regexp.Regexp
|
||||
s string
|
||||
}
|
||||
|
||||
type Generator struct{
|
||||
firstone bool
|
||||
fset *token.FileSet
|
||||
fspec, tspec, nspec subspec
|
||||
nspecs []subspec
|
||||
nodes *ast.File
|
||||
name string
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) Visit(node ast.Node) ast.Visitor {
|
||||
switch n := node.(type) {
|
||||
case *ast.Ident:
|
||||
if n.Obj != nil && n.Obj.Kind == ast.Typ {
|
||||
subs(&n.Name,g.nspecs)
|
||||
subs(&n.Obj.Name,g.nspecs)
|
||||
}
|
||||
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 }
|
||||
subs(&ci.Name,g.nspecs)
|
||||
}
|
||||
}
|
||||
case *ast.TypeAssertExpr:
|
||||
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.Ellipsis:
|
||||
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.Field:
|
||||
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.StarExpr:
|
||||
nt, ok := n.X.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.ValueSpec:
|
||||
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.ArrayType:
|
||||
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
|
||||
subs(&nt.Name,g.nspecs)
|
||||
case *ast.MapType:
|
||||
nt, ok := n.Key.(*ast.Ident); if ok {
|
||||
subs(&nt.Name,g.nspecs)
|
||||
}
|
||||
nt, ok = n.Value.(*ast.Ident); if ok {
|
||||
subs(&nt.Name,g.nspecs)
|
||||
}
|
||||
case *ast.FuncDecl:
|
||||
sub(&n.Name.Name,g.fspec)
|
||||
//ast.Print(g.fset,n)
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
var (
|
||||
qexp *regexp.Regexp
|
||||
)
|
||||
|
||||
func init() {
|
||||
qexp = regexp.MustCompile(`"`)
|
||||
}
|
||||
|
||||
func NewGenerator(ns ...string) *Generator {
|
||||
g := &Generator{}
|
||||
g.firstone = true
|
||||
g.fset = token.NewFileSet()
|
||||
if len(ns) > 0 {
|
||||
g.name = ns[0]
|
||||
} else {
|
||||
g.name = "main"
|
||||
}
|
||||
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}}
|
||||
|
||||
f, err := parser.ParseFile(g.fset, "", template, parser.ParseComments)
|
||||
if err != nil {
|
||||
log.Fatal("Parsing persist/template.T:",err)
|
||||
}
|
||||
ast.Walk(g,f)
|
||||
|
||||
if g.firstone {
|
||||
g.nodes = f
|
||||
} else {
|
||||
for _,decl := range(f.Decls) {
|
||||
imp, ok := decl.(*ast.GenDecl); if ok {
|
||||
if imp.Tok == token.IMPORT {
|
||||
continue // skip imports
|
||||
}
|
||||
}
|
||||
g.nodes.Decls = append(g.nodes.Decls,decl)
|
||||
}
|
||||
}
|
||||
g.firstone = false
|
||||
}
|
||||
|
||||
func (g *Generator) Import(name string) {
|
||||
if g.nodes == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// strip quotation marks from imports
|
||||
name = qexp.ReplaceAllString(name,"")
|
||||
astutil.AddImport(g.fset, g.nodes, name)
|
||||
}
|
||||
|
||||
func (g *Generator) Save(of io.Writer) {
|
||||
if g.nodes == nil {
|
||||
return
|
||||
}
|
||||
if g.name != "main" {
|
||||
g.nodes.Name = ast.NewIdent(g.name)
|
||||
}
|
||||
|
||||
err := format.Node(of,g.fset,g.nodes)
|
||||
if err != nil {
|
||||
log.Fatal("Generate error:",err)
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -11,6 +11,8 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gitlab.wow.st/gmp/persist/generate"
|
||||
)
|
||||
|
||||
type visitor struct{}
|
||||
@@ -133,7 +135,7 @@ func main() {
|
||||
if len(ts) == 0 {
|
||||
continue
|
||||
}
|
||||
g := NewGenerator(pkg.Name)
|
||||
g := generate.NewGenerator(pkg.Name)
|
||||
for _,v := range ts {
|
||||
g.Add(v.fun, v.name, v.typ)
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package main
|
||||
const template string = `//Generated by gitlab.wow.st/persist/cmd/pgen.
|
||||
//Do not edit
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"time"
|
||||
"unsafe"
|
||||
"gitlab.wow.st/gmp/persist"
|
||||
)
|
||||
|
||||
type Var_N persist.Var
|
||||
|
||||
func New(name string, x _T, opt ...persist.Option) *Var_N {
|
||||
gob.Register(x)
|
||||
ptr := persist.New(name, x, opt...)
|
||||
ret := (*Var_N)(unsafe.Pointer(ptr))
|
||||
return ret
|
||||
}
|
||||
|
||||
func (v *Var_N) Set(x _T) error {
|
||||
ptr := (*persist.Var)(unsafe.Pointer(v))
|
||||
return ptr.Set(x)
|
||||
}
|
||||
|
||||
func (v *Var_N) Get(ts ...time.Time) _T {
|
||||
ptr := (*persist.Var)(unsafe.Pointer(v))
|
||||
return ptr.Get(ts...).(_T)
|
||||
}
|
||||
|
||||
func (v *Var_N) History() ([]persist.TVar, error) {
|
||||
ptr := (*persist.Var)(unsafe.Pointer(v))
|
||||
return ptr.History()
|
||||
}
|
||||
`
|
||||
Reference in New Issue
Block a user