-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
89 lines (80 loc) · 2.1 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
package main
import (
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"strings"
"github.com/nextzhou/goderive/plugin"
"github.com/nextzhou/goderive/utils"
)
type TypeInfo struct {
Name string
Assigned string
Plugins *plugin.Entries
Ast ast.Expr
Env plugin.Env
}
func ExtractTypes(src []byte) ([]TypeInfo, error) {
var types []TypeInfo
// parse source code
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
return nil, err
}
pkg, _ := ast.NewPackage(fset, map[string]*ast.File{"": file}, nil, nil)
d := doc.New(pkg, file.Name.String(), doc.AllDecls)
env := plugin.MakeEnv(pkg.Name)
for _, i := range file.Imports {
env.Imports.Append(plugin.MakeImportFromAst(i))
}
// select types with 'derive' marker
for _, typ := range d.Types {
var typeInfo TypeInfo
cmts := strings.Split(typ.Doc, "\n")
for _, cmt := range cmts {
dc, err := utils.MatchDeriveComment(cmt)
if err != nil {
return nil, fmt.Errorf("type %s: %v", typ.Name, err)
}
if dc == nil {
continue
}
if typeInfo.Name == "" {
typeInfo.Name = typ.Name
typeInfo.Plugins = plugin.NewEntries(0)
spec := typ.Decl.Specs[0].(*ast.TypeSpec)
typeInfo.Ast = spec.Name.Obj.Decl.(*ast.TypeSpec).Type
if spec.Assign.IsValid() {
switch assigned := typeInfo.Ast.(type) {
case *ast.Ident:
typeInfo.Assigned = assigned.Name
case *ast.SelectorExpr:
typeInfo.Assigned = utils.SelectorExprString(assigned)
}
}
}
opts, err := plugin.ParseOptions(dc.OptionsStr)
if err != nil {
return nil, fmt.Errorf("type %s: %v", typ.Name, err)
}
// merge options
idx := typeInfo.Plugins.FindBy(func(e plugin.Entry) bool { return e.Plugin == dc.Plugin })
if idx == -1 {
typeInfo.Plugins.Append(plugin.MakeEntry(dc.Plugin, opts))
} else {
err := typeInfo.Plugins.Index(idx).Opts.Merge(opts)
if err != nil {
return nil, fmt.Errorf("type %s: %v", typ.Name, err)
}
}
}
if typeInfo.Name != "" {
typeInfo.Env = env
types = append(types, typeInfo)
}
}
return types, nil
}