-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
244 lines (231 loc) · 4.88 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
package srcdom
import (
"fmt"
"go/ast"
"go/token"
"strconv"
)
// Parser is a parser for go source files.
type Parser struct {
Package *Package
}
func (p *Parser) readImport(s *ast.ImportSpec) error {
path, err := strconv.Unquote(s.Path.Value)
if err != nil {
return err
}
name := ""
if s.Name != nil {
name = s.Name.Name
}
p.Package.Imports = append(p.Package.Imports, &Import{
Name: name,
Path: path,
})
return nil
}
func (p *Parser) readValue(d *ast.GenDecl) error {
prev := ""
for _, spec := range d.Specs {
s, ok := spec.(*ast.ValueSpec)
if !ok {
warnf("readValue not support: %T", spec)
continue
}
// determine var/const typeName
typeName := ""
if s.Type != nil {
if n, imp := baseTypeName(s.Type); !imp {
typeName = n
prev = n
}
}
if typeName == "" {
typeName = prev
}
// check is const
isConst := d.Tok == token.CONST
// extract basic literal
var lit *ast.BasicLit
if len(s.Values) == 1 {
if v, ok := s.Values[0].(*ast.BasicLit); ok {
lit = v
}
}
for _, n := range s.Names {
p.Package.putValue(&Value{
Name: n.Name,
Type: typeName,
IsConst: isConst,
Literal: lit,
})
}
}
return nil
}
func (p *Parser) readType(spec *ast.TypeSpec) error {
name := spec.Name.Name
typ := p.Package.assureType(name)
typ.Defined = true
return p.readTypeFields(spec.Type, typ)
}
func (p *Parser) readTypeFields(expr ast.Expr, typ *Type) error {
switch t := expr.(type) {
case *ast.StructType:
typ.IsStruct = true
if t.Fields == nil || len(t.Fields.List) == 0 {
break
}
err := p.readStructType(t, typ)
if err != nil {
return err
}
case *ast.InterfaceType:
typ.IsInterface = true
if t.Methods == nil || len(t.Methods.List) == 0 {
break
}
err := p.readInterfaceType(t, typ)
if err != nil {
return err
}
}
return nil
}
func (p *Parser) readStructType(st *ast.StructType, typ *Type) error {
typ.IsStruct = true
for _, astField := range st.Fields.List {
f, err := p.toField(astField)
if err != nil {
return err
}
if f.Name == "" {
typ.putEmbed(f.Type)
break
}
typ.putField(f)
}
return nil
}
func (p *Parser) readInterfaceType(it *ast.InterfaceType, typ *Type) error {
// See also https://go.dev/ref/spec#Interface_types
for _, astField := range it.Methods.List {
switch ft := astField.Type.(type) {
case *ast.FuncType:
// MethodElem
name := firstName(astField.Names)
typ.putMethod(toFunc(name, ft))
case *ast.SelectorExpr, *ast.Ident, *ast.BinaryExpr:
// TypeElem
typ.putEmbed(typeString(ft))
default:
return fmt.Errorf("unsupported interface method type: %T (%s)", ft, typeString(ft))
}
}
return nil
}
func (p *Parser) readFunc(fun *ast.FuncDecl) error {
f := toFunc(fun.Name.Name, fun.Type)
if fun.Recv != nil {
if len(fun.Recv.List) == 0 {
// should not happen (incorrect AST);
return fmt.Errorf("no receivers: %q", fun.Name.Name)
}
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
if imp {
// should not happen (incorrect AST);
return fmt.Errorf("method fro imported receiver: %q", recvTypeName)
}
p.Package.assureType(recvTypeName).putMethod(f)
return nil
}
p.Package.putFunc(f)
return nil
}
func (p *Parser) toField(f *ast.Field) (*Field, error) {
tag, err := p.toTag(f.Tag)
if err != nil {
return nil, err
}
return &Field{
Name: firstName(f.Names),
Type: typeString(f.Type),
Tag: tag,
}, nil
}
func (p *Parser) toTag(x *ast.BasicLit) (*Tag, error) {
if x == nil {
return &Tag{}, nil
}
switch x.Kind {
case token.STRING:
v, err := strconv.Unquote(x.Value)
if err != nil {
return nil, err
}
return parseTag(v), nil
default:
return nil, fmt.Errorf("unsupported token for tag: %s", x.Kind)
}
}
// readGenDecl reads top level GenDecl.
func (p *Parser) readGenDecl(d *ast.GenDecl) error {
switch d.Tok {
case token.IMPORT:
for _, spec := range d.Specs {
if s, ok := spec.(*ast.ImportSpec); ok {
err := p.readImport(s)
if err != nil {
return err
}
}
}
case token.CONST, token.VAR:
err := p.readValue(d)
if err != nil {
return err
}
case token.TYPE:
if len(d.Specs) == 1 && !d.Lparen.IsValid() {
if s, ok := d.Specs[0].(*ast.TypeSpec); ok {
err := p.readType(s)
if err != nil {
return err
}
}
break
}
for _, spec := range d.Specs {
if s, ok := spec.(*ast.TypeSpec); ok {
err := p.readType(s)
if err != nil {
return err
}
}
}
}
return nil
}
// ScanFile scans a ast.File to build Package.
func (p *Parser) ScanFile(file *ast.File) error {
if p.Package == nil || p.Package.Name != file.Name.Name {
p.Package = &Package{
Name: file.Name.Name,
}
}
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.GenDecl:
err := p.readGenDecl(d)
if err != nil {
return err
}
case *ast.FuncDecl:
err := p.readFunc(d)
if err != nil {
return err
}
}
}
return nil
}