-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
488 lines (422 loc) · 12.4 KB
/
parse.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright (c) 2023 Maple Wu <justmaplewu@gmail.com>
* National Electronics and Computer Technology Center, Thailand
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zcore
import (
"bytes"
"go/ast"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"
)
var (
// SkipDirs contains some directory name would skip in walk
SkipDirs = map[string]struct{}{
"vendor": {},
"node_modules": {},
"testdata": {},
}
// declParsedStore to cached parsed AnnotatedDecls from *ast.File
// same *ast.File always has same parsed results
declParsedStore = new(VersionStore)
)
// Types of annotated declaration
const (
DeclTypeInterface = iota + 1 // type T interface{}
DeclTypeStruct // type T struct{}
DeclTypeMap // type T map[string]string
DeclTypeArray // type T []string
DeclTypeFunc // type T func()
DeclTypeRefer // type T T2 or type T = T2
DeclFunc // func Fn()
DeclValue // var variable = 1 or var v Type or const constant = 1
)
type (
AnnotatedDecl struct {
File *File
Type int
FuncDecl *ast.FuncDecl
TypeSpec *ast.TypeSpec
ValueSpec *ast.ValueSpec
Docs []string
Annotations []string
Fields []*AnnotatedField
}
AnnotatedField struct {
Decl *AnnotatedDecl
Field *ast.Field
Docs []string
Annotations []string
}
AnnotatedDecls []*AnnotatedDecl
)
// Name return name from different decl
func (decl *AnnotatedDecl) Name() string {
if decl.TypeSpec != nil && decl.TypeSpec.Name != nil {
return decl.TypeSpec.Name.Name
}
if decl.FuncDecl != nil && decl.FuncDecl.Name != nil {
return decl.FuncDecl.Name.Name
}
if decl.ValueSpec != nil && len(decl.ValueSpec.Names) == 1 {
return decl.ValueSpec.Names[0].Name
}
return ""
}
// Filename return base filename from file ast
func (decl *AnnotatedDecl) Filename() string { return filepath.Base(decl.File.Path) }
// Package return package name from file ast
func (decl *AnnotatedDecl) Package() string { return decl.File.Ast.Name.Name }
// RelFilename return relative format filename from decl info and mod file
// if filename is absolute. filename would be related to mod file
// else filename would be related to declaration file
// if filename does not have ".go" suffix.
// defaultName provided would be added as base name and origin filename as directory name
func (decl *AnnotatedDecl) RelFilename(filename string, defaultName string) (ret string) {
if strings.Contains(filename, "{{") && strings.Contains(filename, "}}") {
TryExecuteTemplate(decl, filename, &filename)
}
if !strings.HasSuffix(filename, ".go") {
defaultName = strings.TrimSuffix(defaultName, ".go") + ".go"
filename = filepath.Join(filename, defaultName)
}
if dir := filepath.Dir(decl.File.Path); filepath.IsAbs(filename) {
ret = filepath.Join(filepath.Dir(GetModFile(dir)), filename)
} else {
ret = filepath.Join(dir, filename)
}
return
}
// Parse parses declarations by plugin's name and args count. returns declaration entities with parsed args and options
func (decls AnnotatedDecls) Parse(plugin Plugin, extOptions map[string]string) (entities DeclEntities) {
name := plugin.Name()
args, _ := plugin.Args()
for _, decl := range decls {
entities = append(entities, decl.parse(name, len(args), extOptions)...)
}
return
}
// parse analysis annotated declarations annotations matched with name and args count. and convert into args and options.
func (decl *AnnotatedDecl) parse(name string, argsCount int, extOptions map[string]string) (entities DeclEntities) {
for _, annotation := range decl.Annotations {
args, opts, ok := parseAnnotation(annotation, name, argsCount, extOptions)
if !ok {
continue
}
entities = append(entities, DeclEntity{
AnnotatedDecl: decl,
Plugin: name,
Args: args,
Options: opts,
})
}
return
}
// Parse analysis annotated fields annotations matched with name and args count. and convert into args and options.
func (field *AnnotatedField) Parse(name string, argsCount int, extOptions map[string]string) (entities FieldEntities) {
for _, annotation := range field.Annotations {
args, opts, ok := parseAnnotation(annotation, name, argsCount, extOptions)
if !ok {
continue
}
entities = append(entities, FieldEntity{
AnnotatedField: field,
Args: args,
Options: opts,
})
}
return
}
// ParseFileOrDirectory try parse provided path annotated declarations with annotations prefix
// if directory provided. walks file tree from provided path as root and returns all parsed
func ParseFileOrDirectory(path string, prefix string) (decls AnnotatedDecls, err error) {
stat, err := os.Stat(path)
if err != nil {
return
}
if !stat.IsDir() {
// single file
return ParseFileDecls(path, prefix)
}
// directory
// walk all child directories and files
// use error group and pre alloc slots to collect parsed results
slots := make([]*AnnotatedDecls, 0)
if err = filepath.Walk(path, func(filename string, info fs.FileInfo, e error) (err error) {
if e != nil {
return e
}
if name := info.Name(); info.IsDir() {
// some specific skip name or dirs starts with .
if _, skip := SkipDirs[name]; skip || strings.HasPrefix(name, ".") {
return filepath.SkipDir
}
return
}
// parse files with goroutine error group
// results would be placed in slot
index := len(slots)
slots = append(slots, new(AnnotatedDecls))
*slots[index], err = ParseFileDecls(filename, prefix)
return
}); err != nil {
return
}
// expand results from slots
for _, slot := range slots {
decls = append(decls, *slot...)
}
return
}
// ParseFileDecls parse provided file into ast and analysis declarations annotations
// return annotated declarations list or error while reading file or parsing ast
func ParseFileDecls(filename string, prefix string) (decls AnnotatedDecls, err error) {
filename, err = filepath.Abs(filename)
if err != nil {
return
}
if !IsGoFile(filename) {
return
}
// read file data
data, version, err := ReadFile(filename)
if err != nil {
return
}
// check data contains annotations prefix or return
if !bytes.Contains(data, []byte(prefix)) {
return
}
// parse file ast
f, err := ParseFile(filename)
if err != nil {
return
}
// parse annotated decls
ret, _ := declParsedStore.Load(f.Ast, version, func() (interface{}, error) {
return parseFileDecls(f, prefix), nil
})
decls = ret.(AnnotatedDecls)
return
}
func parseFileDecls(file *File, prefix string) (decls AnnotatedDecls) {
for _, astDecl := range file.Ast.Decls {
for _, decl := range ParseDecls(astDecl, prefix) {
decl.File = file
decls = append(decls, decl)
}
}
return
}
// ParseGenericDecl parse generic declaration to match annotation prefix
func ParseGenericDecl(gen *ast.GenDecl, prefix string) (decls AnnotatedDecls) {
genDocs, genAnnotations := ParseCommentGroup(prefix, gen.Doc)
single := !gen.Lparen.IsValid() || len(gen.Specs) == 1
switch gen.Tok {
case token.CONST, token.VAR:
/*
merged type declaration for variable or constant
// +zz:annotation:args:key=value
var (
variableA = 1
variableB = 2
)
// +zz:annotation:args:key=value
var variableC = 4
// +zz:annotation:args:key=value
const (
constantA = 3
constantB = 4
)
// +zz:annotation:args:key=value
const constantC = 4
*/
for _, spec := range gen.Specs {
vs, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
docs, annotations := ParseCommentGroup(prefix, vs.Doc, vs.Comment)
// generic annotations would be appended to each element in merged declaration
if annotations = append(genAnnotations, annotations...); len(annotations) == 0 {
continue
}
if single {
docs = append(genDocs, docs...)
}
decls = append(decls, &AnnotatedDecl{
ValueSpec: vs,
Docs: docs,
Annotations: annotations,
Type: DeclValue,
})
}
case token.TYPE:
/*
separated struct or interface type declaration
// +zz:annotation:args:key=value
type structA struct{
Field0 int
Field1 int
}
// +zz:annotation:args:key=value
type structB struct{
Field0 int
Field1 int
}
// +zz:annotation:args:key=value
type interfaceC interface{
Foo()
}
same annotation for grouped types can use
merged type declaration
would be same effect as upper
// +zz:annotation:args:key=value
type (
structA struct{
Field0 int
Field1 int
}
structB struct{
Field0 int
Field1 int
}
interfaceC interface{
Foo()
}
)
*/
for _, s := range gen.Specs {
spec, ok := s.(*ast.TypeSpec)
if !ok {
continue
}
docs, annotations := ParseCommentGroup(prefix, spec.Doc, spec.Comment)
// generic annotations would be appended to each element in merged declaration
if annotations = append(genAnnotations, annotations...); len(annotations) == 0 {
continue
}
if single {
docs = append(genDocs, docs...)
}
decl := &AnnotatedDecl{
TypeSpec: spec,
Docs: docs,
Annotations: annotations,
}
// check type spec type
switch typ := spec.Type.(type) {
case *ast.InterfaceType:
decl.Type = DeclTypeInterface
decl.parseAnnotatedFields(typ.Methods, prefix)
case *ast.StructType:
decl.Type = DeclTypeStruct
decl.parseAnnotatedFields(typ.Fields, prefix)
case *ast.MapType:
decl.Type = DeclTypeMap
case *ast.ArrayType:
decl.Type = DeclTypeArray
case *ast.FuncType:
decl.Type = DeclTypeFunc
case *ast.Ident, *ast.SelectorExpr, *ast.StarExpr:
decl.Type = DeclTypeRefer
default:
continue
}
decls = append(decls, decl)
}
}
return
}
// parseAnnotatedFields parse fields docs and comments to match annotations prefix
// fields match annotations will be collect as AnnotatedField
func (decl *AnnotatedDecl) parseAnnotatedFields(fl *ast.FieldList, prefix string) {
for _, field := range fl.List {
if len(field.Names) == 0 {
continue
}
if docs, annotations := ParseCommentGroup(prefix, field.Doc, field.Comment); len(annotations) > 0 {
decl.Fields = append(decl.Fields, &AnnotatedField{
Docs: docs,
Annotations: annotations,
Field: field,
Decl: decl,
})
}
}
}
// ParseFuncDecl parse function declaration docs to match annotations prefix
//
// Example:
//
// // +zz:annotation:args:key=value
// func Foo() {
// }
func ParseFuncDecl(decl *ast.FuncDecl, prefix string) (d *AnnotatedDecl) {
docs, annotations := ParseCommentGroup(prefix, decl.Doc)
if len(annotations) == 0 {
return nil
}
return &AnnotatedDecl{
FuncDecl: decl,
Docs: docs,
Annotations: annotations,
Type: DeclFunc,
}
}
// ParseDecls check declaration type
// parse generic declaration or function declaration and get annotated declarations
func ParseDecls(d ast.Decl, prefix string) (items AnnotatedDecls) {
switch decl := d.(type) {
case *ast.GenDecl:
items = append(items, ParseGenericDecl(decl, prefix)...)
case *ast.FuncDecl:
if item := ParseFuncDecl(decl, prefix); item != nil {
items = append(items, item)
}
}
return
}
// ParseCommentGroup extract comment group text and split by lines
// if line match annotation prefix then append line to annotations
// else append line to docs
func ParseCommentGroup(prefix string, cg ...*ast.CommentGroup) (docs, annotations []string) {
for _, g := range cg {
if g == nil {
continue
}
docs = append(docs, strings.Split(strings.TrimSpace(g.Text()), "\n")...)
}
// no prefix provided. return all comment lines as doc
if len(prefix) == 0 {
return docs, nil
}
// comments matched annotation prefix would be appended as annotations
// or appended as docs in same slice memory
offset := 0
for _, doc := range docs {
if annotation, exist := TrimPrefix(strings.TrimSpace(doc), prefix); exist {
annotations = append(annotations, annotation)
} else {
docs[offset] = doc
offset++
}
}
docs = docs[:offset]
return
}