/
scan.go
108 lines (96 loc) · 2.5 KB
/
scan.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
package scan
import (
"encoding/json"
"go/token"
)
type Project struct {
Imports []string `json:"imports,omitempty"`
Packages []Package `json:"packages,omitempty"`
}
type Package struct {
Name string `json:"name,omitempty"`
Files []GoFile `json:"goFiles,omitempty"`
}
type GoFile struct {
IsMain bool `json:"isMain,omitempty"`
PackageName string `json:"package,omitempty"`
Imports []*Import `json:"imports,omitempty"`
LogicBlocks []*LogicBlock `json:"logicBlocks,omitempty"`
Path string `json:"path,omitempty"`
bytes []byte
typeCount int
}
type Import struct {
Package string `json:"package,omitempty"`
URI string `json:"uri,omitempty"`
}
type LogicBlock struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Kind BlockType `json:"kind,omitempty"`
Generics []*LogicBlock `json:"typeConstraints,omitempty"`
InputParams []*LogicBlock `json:"inputs,omitempty"`
ReturnParams []*LogicBlock `json:"returns,omitempty"`
BlockParams []*LogicBlock `json:"elements,omitempty"`
Receivers *LogicBlock `json:"receiver,omitempty"`
Calls []string `json:"calls,omitempty"`
Package string `json:"pacakage,omitempty"`
isFunc bool
parent *GoFile
}
type BlockType uint
const (
TypeUndefined BlockType = iota
TypeFunction
TypeMethod
TypeStruct
TypeInterface
TypeFuncParam
TypeFuncReturn
TypeDeferFunc
TypeGoFunc
TypeVariableDecl
TypeConstantDecl
TypeGenericParam
)
var (
blockTypeVals = map[BlockType]string{
0: "",
1: token.FUNC.String(),
2: "method",
3: token.STRUCT.String(),
4: token.INTERFACE.String(),
5: "funcParam",
6: "funcReturn",
7: token.DEFER.String(),
8: token.GO.String(),
9: token.VAR.String(),
10: token.CONST.String(),
11: "genericParam",
}
blockTypeKeys = map[string]BlockType{
"": 0,
token.FUNC.String(): 1,
"method": 2,
token.STRUCT.String(): 3,
token.INTERFACE.String(): 4,
"funcParam": 5,
"funcReturn": 6,
token.DEFER.String(): 7,
token.GO.String(): 8,
token.VAR.String(): 9,
token.CONST.String(): 10,
"genericParam": 11,
}
)
func (b BlockType) String() string {
return blockTypeVals[b]
}
func (f *GoFile) String() string {
b, _ := json.Marshal(f)
return string(b)
}
type Extractor interface {
Do(pos token.Pos, tok token.Token, lit string) Extractor
Done() bool
}