-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcdom.go
386 lines (333 loc) · 7.18 KB
/
srcdom.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Package srcdom provides utilities to manipulate Go's AST (Abstract Structure
Tree). Using srcdom, you can easily access/extract information of types,
funcions and variables from AST.
*/
package srcdom
import (
"go/ast"
"regexp"
"sort"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
func isPublicName(name string) bool {
r, _ := utf8.DecodeRuneInString(name)
if r == utf8.RuneError {
return false
}
return unicode.IsUpper(r)
}
func sortedNames(m map[string]int) []string {
names := make([]string, 0, len(m))
for k := range m {
names = append(names, k)
}
sort.Strings(names)
return names
}
// Package represents a go package.
type Package struct {
Name string
Imports []*Import
Values []*Value
valIdx map[string]int
Funcs []*Func
funIdx map[string]int
Types []*Type
typIdx map[string]int
}
func (p *Package) putValue(v *Value) {
if p.valIdx == nil {
p.valIdx = make(map[string]int)
}
idx := len(p.Values)
p.valIdx[v.Name] = idx
p.Values = append(p.Values, v)
}
// Value gets a value which matches with name.
func (p *Package) Value(name string) (*Value, bool) {
idx, ok := p.valIdx[name]
if !ok {
return nil, false
}
return p.Values[idx], true
}
// ValueName returns sorted names of value in the package.
func (p *Package) ValueNames() []string {
return sortedNames(p.valIdx)
}
func (p *Package) putFunc(fun *Func) {
if p.funIdx == nil {
p.funIdx = make(map[string]int)
}
idx := len(p.Funcs)
p.funIdx[fun.Name] = idx
p.Funcs = append(p.Funcs, fun)
}
// Func gets a func which matches with name.
func (p *Package) Func(name string) (*Func, bool) {
idx, ok := p.funIdx[name]
if !ok {
return nil, false
}
return p.Funcs[idx], true
}
// FuncNames returns sorted names of function in the package.
func (p *Package) FuncNames() []string {
return sortedNames(p.funIdx)
}
func (p *Package) assureType(name string) *Type {
if typ, ok := p.Type(name); ok {
return typ
}
typ := &Type{Name: name}
p.putType(typ)
return typ
}
func (p *Package) putType(typ *Type) {
if p.typIdx == nil {
p.typIdx = make(map[string]int)
}
idx := len(p.Types)
p.typIdx[typ.Name] = idx
p.Types = append(p.Types, typ)
}
// Type gets a type which matches with name.
func (p *Package) Type(name string) (*Type, bool) {
idx, ok := p.typIdx[name]
if !ok {
return nil, false
}
return p.Types[idx], true
}
// TypeNames returns sorted names of type in the package.
func (p *Package) TypeNames() []string {
return sortedNames(p.typIdx)
}
// Import represents an import.
type Import struct {
Name string
Path string
}
// Var represents a variable.
type Var struct {
Name string
Type string
}
// Field represents a variable.
type Field struct {
Name string
Type string
Tag *Tag
}
// Tag represents a tag for field
type Tag struct {
Raw string
Values []*TagValue
valueIdx map[string]int
}
// TagValue gets a tag value which matches with name.
func (tag *Tag) TagValue(n string) (*TagValue, bool) {
idx, ok := tag.valueIdx[n]
if !ok {
return nil, false
}
return tag.Values[idx], false
}
func (tag *Tag) putTagValue(v *TagValue) {
if tag.valueIdx == nil {
tag.valueIdx = make(map[string]int)
}
idx := len(tag.Values)
tag.valueIdx[v.Name] = idx
tag.Values = append(tag.Values, v)
}
func parseTag(tag string) *Tag {
dst := &Tag{Raw: tag}
// parse tag: partially copied from reflect.StructTag.Lookup()
for tag != "" {
// Skip leading space.
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
break
}
// Scan to colon.
i = 0
for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f {
i++
}
if i == 0 || i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
break
}
name := string(tag[:i])
tag = tag[i+1:]
// Scan quoted string to find value.
i = 1
for i < len(tag) && tag[i] != '"' {
if tag[i] == '\\' {
i++
}
i++
}
if i >= len(tag) {
break
}
qvalue := string(tag[:i+1])
tag = tag[i+1:]
value, err := strconv.Unquote(qvalue)
if err != nil {
break
}
dst.putTagValue(parseTagValue(name, value))
}
return dst
}
func (tag *Tag) match(name string, value *string) bool {
for _, v := range tag.Values {
if v.Name == name {
if value == nil || v.has(*value) {
return true
}
}
}
return false
}
var tagValueRx = regexp.MustCompile(`\s+`)
// TagValue represents content of a tag.
type TagValue struct {
Name string
Raw string
Values []string
}
func parseTagValue(name, s string) *TagValue {
return &TagValue{
Name: name,
Raw: s,
Values: tagValueRx.Split(s, -1),
}
}
func (tv *TagValue) has(value string) bool {
for _, v := range tv.Values {
if v == value {
return true
}
}
return false
}
// Func represents a function.
type Func struct {
Name string
Params []*Var
Results []*Var
}
// IsPublic checks its name is public or not.
func (fn *Func) IsPublic() bool {
return isPublicName(fn.Name)
}
func (fn *Func) writeResults(b *strings.Builder) {
rets := typesString(fn.Results)
switch len(fn.Results) {
case 0:
// nothing to append
case 1:
b.WriteString(" ")
b.WriteString(rets)
default:
b.WriteString(" (")
b.WriteString(rets)
b.WriteString(")")
}
}
// Type represents a function.
type Type struct {
Name string
Defined bool
IsStruct bool
IsInterface bool
Embeds []string
embedIdx map[string]int
Fields []*Field
fieldIdx map[string]int
Methods []*Func
methodIdx map[string]int
}
func (typ *Type) putEmbed(typeName string) {
if typ.embedIdx == nil {
typ.embedIdx = make(map[string]int)
}
idx := len(typ.Embeds)
typ.embedIdx[typeName] = idx
typ.Embeds = append(typ.Embeds, typeName)
}
// IsPublic checks its name is public or not.
func (typ *Type) IsPublic() bool {
return isPublicName(typ.Name)
}
// Embed checks the type has embed type or not.
func (typ *Type) Embed(n string) bool {
_, ok := typ.embedIdx[n]
return ok
}
func (typ *Type) putField(f *Field) {
if typ.fieldIdx == nil {
typ.fieldIdx = make(map[string]int)
}
idx := len(typ.Fields)
typ.fieldIdx[f.Name] = idx
typ.Fields = append(typ.Fields, f)
}
// Field gets a field which matches with name.
func (typ *Type) Field(n string) (*Field, bool) {
idx, ok := typ.fieldIdx[n]
if !ok {
return nil, false
}
return typ.Fields[idx], true
}
func (typ *Type) putMethod(fun *Func) {
if typ.methodIdx == nil {
typ.methodIdx = make(map[string]int)
}
idx := len(typ.Methods)
typ.methodIdx[fun.Name] = idx
typ.Methods = append(typ.Methods, fun)
}
// Method gets a method Func which matches with name.
func (typ *Type) Method(n string) (*Func, bool) {
idx, ok := typ.methodIdx[n]
if !ok {
return nil, false
}
return typ.Methods[idx], true
}
// FieldsByTag collects fields which match with query.
// The query's format is "{tagName}" or "{tagName}:{value}".
func (typ *Type) FieldsByTag(tagQuery string) []*Field {
var hits []*Field
var name string
var value *string
for _, f := range typ.Fields {
if f.Tag != nil && f.Tag.match(name, value) {
hits = append(hits, f)
}
}
return hits
}
// Value represents a value or const
type Value struct {
Name string
Type string
IsConst bool
Literal *ast.BasicLit
}
// IsPublic checks its name is public or not.
func (v *Value) IsPublic() bool {
return isPublicName(v.Name)
}