/
ast_helpers.go
209 lines (182 loc) · 4.1 KB
/
ast_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strings"
"unicode"
)
func export(s string) string {
return strings.Title(s)
}
func unexport(s string) string {
first := true
return strings.Map(func(r rune) rune {
if first {
first = false
return unicode.ToLower(r)
}
return r
}, s)
}
func inventName(t ast.Expr, scope *ast.Scope) *ast.Ident {
n := baseName(t)
for try := 0; ; try++ {
nstr := pickName(n, try)
obj := ast.NewObj(ast.Var, nstr)
if alt := scope.Insert(obj); alt == nil {
return ast.NewIdent(nstr)
}
}
}
func baseName(t ast.Expr) string {
switch tt := t.(type) {
default:
panic(fmt.Sprintf("don't know how to choose a base name for %T (%[1]v)", tt))
case *ast.ArrayType:
return "slice"
case *ast.Ident:
return tt.Name
case *ast.SelectorExpr:
return tt.Sel.Name
}
}
func pickName(base string, idx int) string {
if idx == 0 {
switch base {
default:
return strings.Split(base, "")[0]
case "Context":
return "ctx"
case "error":
return "err"
}
}
return fmt.Sprintf("%s%d", base, idx)
}
func scopeWith(names ...string) *ast.Scope {
scope := ast.NewScope(nil)
for _, name := range names {
scope.Insert(ast.NewObj(ast.Var, name))
}
return scope
}
type visitFn func(ast.Node, func(ast.Node))
func (fn visitFn) Visit(node ast.Node, r func(ast.Node)) Visitor {
fn(node, r)
return fn
}
func replaceIdent(src ast.Node, named string, with ast.Node) ast.Node {
r := visitFn(func(node ast.Node, replaceWith func(ast.Node)) {
switch id := node.(type) {
case *ast.Ident:
if id.Name == named {
replaceWith(with)
}
}
})
return WalkReplace(r, src)
}
func replaceLit(src ast.Node, from, to string) ast.Node {
r := visitFn(func(node ast.Node, replaceWith func(ast.Node)) {
switch lit := node.(type) {
case *ast.BasicLit:
if lit.Value == from {
replaceWith(&ast.BasicLit{Value: to})
}
}
})
return WalkReplace(r, src)
}
func fullAST() *ast.File {
full, err := ASTTemplates.Open("full.go")
if err != nil {
panic(err)
}
f, err := parser.ParseFile(token.NewFileSet(), "templates/full.go", full, parser.DeclarationErrors)
if err != nil {
panic(err)
}
return f
}
func fetchImports() []*ast.ImportSpec {
return fullAST().Imports
}
func fetchFuncDecl(name string) *ast.FuncDecl {
f := fullAST()
for _, decl := range f.Decls {
if f, ok := decl.(*ast.FuncDecl); ok && f.Name.Name == name {
return f
}
}
panic(fmt.Errorf("no function called %q in 'templates/full.go'", name))
}
func id(name string) *ast.Ident {
return ast.NewIdent(name)
}
func sel(ids ...*ast.Ident) ast.Expr {
switch len(ids) {
default:
return &ast.SelectorExpr{
X: sel(ids[:len(ids)-1]...),
Sel: ids[len(ids)-1],
}
case 1:
return ids[0]
case 0:
panic("zero ids to sel()")
}
}
func typeField(t ast.Expr) *ast.Field {
return &ast.Field{Type: t}
}
func field(n *ast.Ident, t ast.Expr) *ast.Field {
return &ast.Field{
Names: []*ast.Ident{n},
Type: t,
}
}
func fieldList(list ...*ast.Field) *ast.FieldList {
return &ast.FieldList{List: list}
}
func mappedFieldList(fn func(arg) *ast.Field, args ...arg) *ast.FieldList {
fl := &ast.FieldList{List: []*ast.Field{}}
for _, a := range args {
fl.List = append(fl.List, fn(a))
}
return fl
}
func blockStmt(stmts ...ast.Stmt) *ast.BlockStmt {
return &ast.BlockStmt{
List: stmts,
}
}
func structDecl(name *ast.Ident, fields *ast.FieldList) ast.Decl {
return typeDecl(&ast.TypeSpec{
Name: name,
Type: &ast.StructType{
Fields: fields,
},
})
}
func typeDecl(ts *ast.TypeSpec) ast.Decl {
return &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{ts},
}
}
func pasteStmts(body *ast.BlockStmt, idx int, stmts []ast.Stmt) {
list := body.List
prefix := list[:idx]
suffix := make([]ast.Stmt, len(list)-idx-1)
copy(suffix, list[idx+1:])
body.List = append(append(prefix, stmts...), suffix...)
}
func importFor(is *ast.ImportSpec) *ast.GenDecl {
return &ast.GenDecl{Tok: token.IMPORT, Specs: []ast.Spec{is}}
}
func importSpec(path string) *ast.ImportSpec {
return &ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"` + filepath.ToSlash(path) + `"`}}
}