This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
162 lines (139 loc) · 4.44 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
package gogen
import (
"go/parser"
"go/token"
"go/ast"
"path/filepath"
"fmt"
)
// ParseDir will create a Build from the directory that
// was passed into the function.
func ParseDir(path string) (*Build, error) {
var fileSet token.FileSet
packages, err := parser.ParseDir(&fileSet, path, nil, parser.AllErrors)
if err != nil {
return nil, err
}
// create new build for the file set
build := NewBuild()
// iterate over all packages in the directory
for _, pkg := range packages {
// iterate over all files within the package
for name, astTree := range pkg.Files {
baseName := filepath.Base(name)
// create a comment map from file
commentMap := ast.NewCommentMap(&fileSet, astTree, astTree.Comments)
fileAST, err := ParseFileAST(baseName, astTree, commentMap)
if err != nil {
return nil, err
}
build.AddFile(baseName, fileAST)
}
}
return build, nil
}
// ParseFile will create a Build from the file path that
// was passed. FileSet of the Build will only contain a
// single file.
func ParseFile(path string) (*Build, error) {
var fileSet token.FileSet
astTree, err := parser.ParseFile(&fileSet, path, nil, parser.AllErrors|parser.ParseComments)
if err != nil {
return nil, err
}
fileName := filepath.Base(path)
// create a comment map from file
commentMap := ast.NewCommentMap(&fileSet, astTree, astTree.Comments)
// create new build for the file
build := NewBuild()
fileAST, err := ParseFileAST(fileName, astTree, commentMap)
if err != nil {
return nil, err
}
// add parsed file to the build file set
build.AddFile(fileName, fileAST)
return build, nil
}
// ParseFileAST creates a File parse with all necessary
// structures.
func ParseFileAST(name string, tree *ast.File, commentMap ast.CommentMap) (*File, error) {
f := NewFile(name, tree)
for _, i := range tree.Imports {
f.AddImport(ParseImport(i, commentMap.Filter(i)))
}
for _, declaration := range tree.Decls {
switch decValue := declaration.(type) {
// catch only generic declarations
case *ast.GenDecl:
for _, spec := range decValue.Specs {
switch specValue := spec.(type) {
case *ast.TypeSpec:
// all cases should pass in also specValue as
// it is the underlying spec
switch typeValue := specValue.Type.(type) {
case *ast.StructType:
f.AddStruct(ParseStruct(specValue, typeValue, commentMap.Filter(declaration)))
case *ast.InterfaceType:
f.AddInterface(ParseInterface(specValue, typeValue, commentMap.Filter(declaration)))
case *ast.FuncType:
fmt.Println("Generic value not recognized: ", specValue)
case *ast.ArrayType:
f.AddArray(ParseArray(specValue, typeValue, commentMap.Filter(declaration)))
case *ast.MapType:
fmt.Println("Generic value not recognized: ", specValue)
case *ast.ChanType:
fmt.Println("Generic value not recognized: ", specValue)
default:
f.AddBaseType(ParseBaseType(specValue, typeValue, commentMap.Filter(declaration)))
}
case *ast.ImportSpec:
// just ignore for now
case *ast.ValueSpec:
f.AddConstant(ParseConstant(specValue, commentMap.Filter(declaration)))
default:
fmt.Println("Generic value not recognized: ", specValue)
}
}
// catch function declarations
case *ast.FuncDecl:
fun := ParseFunction(decValue, commentMap.Filter(declaration))
if !fun.IsMethod() {
// add the function to the top level map
f.AddFunction(fun)
} else {
// add the function to the structure it belongs to
if len(fun.parent.Recv.List) <= 0 {
// TODO: no receiver defined report?
break
}
// struct that should be assigned the method
var structType *ast.StructType
switch receiver := fun.parent.Recv.List[0].Type.(type) {
// pointer receiver
case *ast.StarExpr:
// if the receiver is defined append it to it,
// otherwise register it as normal function
if receiver.X.(*ast.Ident).Obj != nil {
structType = receiver.X.(*ast.Ident).Obj.Decl.(*ast.TypeSpec).Type.(*ast.StructType)
} else {
f.AddFunction(fun)
}
// copy receiver
case *ast.Ident:
switch receiver.Obj.Decl.(*ast.TypeSpec).Type.(type) {
case *ast.StructType:
structType = receiver.Obj.Decl.(*ast.TypeSpec).Type.(*ast.StructType)
}
}
// search for the structures that receive the method
// and bind it
for _, st := range f.structures {
if st.parent == structType {
st.AddMethod(fun)
}
}
}
}
}
return f, nil
}