-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.go
330 lines (278 loc) · 9.01 KB
/
parser.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
package parser
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
)
// ParseFiles parses files at the same time
func ParseFiles(paths []string) ([]*GoFile, error) {
files := make([]*ast.File, len(paths))
fsets := make([]*token.FileSet, len(paths))
for i, p := range paths {
// File: A File node represents a Go source file: https://golang.org/pkg/go/ast/#File
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, p, nil, 0)
if err != nil {
return nil, err
}
files[i] = file
fsets[i] = fset
}
goFiles := make([]*GoFile, len(paths))
for i, p := range paths {
goFile, err := parseFile(p, files[i], fsets[i], files)
if err != nil {
return nil, err
}
goFiles[i] = goFile
}
return goFiles, nil
}
// ParseSingleFile parses a single file at the same time
func ParseSingleFile(path string) (*GoFile, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return nil, err
}
return parseFile(path, file, fset, []*ast.File{file})
}
func parseFile(path string, file *ast.File, fset *token.FileSet, files []*ast.File) (*GoFile, error) {
source, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
// To import sources from vendor, we use "source" compile
// https://github.com/golang/go/issues/11415#issuecomment-283445198
conf := types.Config{Importer: importer.For("source", nil)}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
if _, err = conf.Check(file.Name.Name, fset, files, info); err != nil {
return nil, err
}
goFile := &GoFile{
Path: path,
Package: file.Name.Name,
Structs: []*GoStruct{},
}
// File.Decls: A list of the declarations in the file: https://golang.org/pkg/go/ast/#Decl
for _, decl := range file.Decls {
switch declType := decl.(type) {
// GenDecl: represents an import, constant, type or variable declaration: https://golang.org/pkg/go/ast/#GenDecl
case *ast.GenDecl:
genDecl := declType
// Specs: the Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec: https://golang.org/pkg/go/ast/#Spec
for _, genSpec := range genDecl.Specs {
switch genSpecType := genSpec.(type) {
// TypeSpec: A TypeSpec node represents a type declaration: https://golang.org/pkg/go/ast/#TypeSpec
case *ast.TypeSpec:
typeSpec := genSpecType
// typeSpec.Type: an Expr (expression) node: https://golang.org/pkg/go/ast/#Expr
switch typeSpecType := typeSpec.Type.(type) {
// StructType: A StructType node represents a struct type: https://golang.org/pkg/go/ast/#StructType
case (*ast.StructType):
structType := typeSpecType
goStruct := buildGoStruct(source, goFile, info, typeSpec, structType)
goFile.Structs = append(goFile.Structs, goStruct)
// InterfaceType: An InterfaceType node represents an interface type. https://golang.org/pkg/go/ast/#InterfaceType
case (*ast.InterfaceType):
interfaceType := typeSpecType
goInterface := buildGoInterface(source, goFile, info, typeSpec, interfaceType)
goFile.Interfaces = append(goFile.Interfaces, goInterface)
default:
// a not-implemented typeSpec.Type.(type), ignore
}
// ImportSpec: An ImportSpec node represents a single package import. https://golang.org/pkg/go/ast/#ImportSpec
case *ast.ImportSpec:
importSpec := genSpec.(*ast.ImportSpec)
goImport := buildGoImport(importSpec, goFile)
goFile.Imports = append(goFile.Imports, goImport)
default:
// a not-implemented genSpec.(type), ignore
}
}
case *ast.FuncDecl:
funcDecl := declType
goStructMethod := buildStructMethod(info, funcDecl, source)
goFile.StructMethods = append(goFile.StructMethods, goStructMethod)
default:
// a not-implemented decl.(type), ignore
}
}
return goFile, nil
}
func buildGoImport(spec *ast.ImportSpec, file *GoFile) *GoImport {
name := ""
if spec.Name != nil {
name = spec.Name.Name
}
path := ""
if spec.Path != nil {
path = spec.Path.Value
}
return &GoImport{
Name: name,
Path: path,
File: file,
}
}
func buildGoInterface(source []byte, file *GoFile, info *types.Info, typeSpec *ast.TypeSpec, interfaceType *ast.InterfaceType) *GoInterface {
goInterface := &GoInterface{
File: file,
Name: typeSpec.Name.Name,
Methods: buildMethodList(info, interfaceType.Methods.List, source),
}
return goInterface
}
func buildMethodList(info *types.Info, fieldList []*ast.Field, source []byte) []*GoMethod {
methods := []*GoMethod{}
for _, field := range fieldList {
name := getNames(field)[0]
fType, ok := field.Type.(*ast.FuncType)
if !ok {
// method was not a function
continue
}
goMethod := &GoMethod{
Name: name,
Params: buildTypeList(info, fType.Params, source),
Results: buildTypeList(info, fType.Results, source),
}
methods = append(methods, goMethod)
}
return methods
}
func buildStructMethod(info *types.Info, funcDecl *ast.FuncDecl, source []byte) *GoStructMethod {
return &GoStructMethod{
Receivers: buildReceiverList(info, funcDecl.Recv, source),
GoMethod: GoMethod{
Name: funcDecl.Name.Name,
Params: buildTypeList(info, funcDecl.Type.Params, source),
Results: buildTypeList(info, funcDecl.Type.Results, source),
},
}
}
func buildReceiverList(info *types.Info, fieldList *ast.FieldList, source []byte) []string {
receivers := []string{}
if fieldList != nil {
for _, t := range fieldList.List {
receivers = append(receivers, getTypeString(t.Type, source))
}
}
return receivers
}
func buildTypeList(info *types.Info, fieldList *ast.FieldList, source []byte) []*GoType {
types := []*GoType{}
if fieldList != nil {
for _, t := range fieldList.List {
goType := buildType(info, t.Type, source)
for _, n := range getNames(t) {
copyType := copyType(goType)
copyType.Name = n
types = append(types, copyType)
}
}
}
return types
}
func getNames(field *ast.Field) []string {
if field.Names == nil || len(field.Names) == 0 {
return []string{""}
}
result := []string{}
for _, name := range field.Names {
result = append(result, name.String())
}
return result
}
func getTypeString(expr ast.Expr, source []byte) string {
return string(source[expr.Pos()-1 : expr.End()-1])
}
func getUnderlyingTypeString(info *types.Info, expr ast.Expr) string {
if typeInfo := info.TypeOf(expr); typeInfo != nil {
if underlying := typeInfo.Underlying(); underlying != nil {
return underlying.String()
}
}
return ""
}
func copyType(goType *GoType) *GoType {
return &GoType{
Type: goType.Type,
Inner: goType.Inner,
Name: goType.Name,
Underlying: goType.Underlying,
}
}
func buildType(info *types.Info, expr ast.Expr, source []byte) *GoType {
innerTypes := []*GoType{}
typeString := getTypeString(expr, source)
underlyingString := getUnderlyingTypeString(info, expr)
switch specType := expr.(type) {
case *ast.FuncType:
innerTypes = append(innerTypes, buildTypeList(info, specType.Params, source)...)
innerTypes = append(innerTypes, buildTypeList(info, specType.Results, source)...)
case *ast.ArrayType:
innerTypes = append(innerTypes, buildType(info, specType.Elt, source))
case *ast.StructType:
innerTypes = append(innerTypes, buildTypeList(info, specType.Fields, source)...)
case *ast.MapType:
innerTypes = append(innerTypes, buildType(info, specType.Key, source))
innerTypes = append(innerTypes, buildType(info, specType.Value, source))
case *ast.ChanType:
innerTypes = append(innerTypes, buildType(info, specType.Value, source))
case *ast.StarExpr:
innerTypes = append(innerTypes, buildType(info, specType.X, source))
case *ast.Ellipsis:
innerTypes = append(innerTypes, buildType(info, specType.Elt, source))
case *ast.InterfaceType:
methods := buildMethodList(info, specType.Methods.List, source)
for _, m := range methods {
innerTypes = append(innerTypes, m.Params...)
innerTypes = append(innerTypes, m.Results...)
}
case *ast.Ident:
case *ast.SelectorExpr:
default:
fmt.Printf("Unexpected field type: `%s`,\n %#v\n", typeString, specType)
}
return &GoType{
Type: typeString,
Underlying: underlyingString,
Inner: innerTypes,
}
}
func buildGoStruct(source []byte, file *GoFile, info *types.Info, typeSpec *ast.TypeSpec, structType *ast.StructType) *GoStruct {
goStruct := &GoStruct{
File: file,
Name: typeSpec.Name.Name,
Fields: []*GoField{},
}
// Field: A Field declaration list in a struct type, a method list in an interface type,
// or a parameter/result declaration in a signature: https://golang.org/pkg/go/ast/#Field
for _, field := range structType.Fields.List {
for _, name := range field.Names {
goField := &GoField{
Struct: goStruct,
Name: name.String(),
Type: string(source[field.Type.Pos()-1 : field.Type.End()-1]),
}
if field.Tag != nil {
goTag := &GoTag{
Field: goField,
Value: field.Tag.Value,
}
goField.Tag = goTag
}
goStruct.Fields = append(goStruct.Fields, goField)
}
}
return goStruct
}