-
Notifications
You must be signed in to change notification settings - Fork 24
/
parser.go
155 lines (145 loc) · 3.87 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
package main
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
)
type Node struct {
Name string
Type string // "func", "method", "type", "interface", "int", or "import"
Calls []string // list of called functions/methods
Code string // source code of the node
Position string // file position of the node
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: ./parser <path_to_go_file>")
os.Exit(1)
}
filePath := os.Args[1]
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
nodes := make(map[string]*Node)
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
funcName := x.Name.Name
funcType := "func"
if x.Recv != nil {
recvType := fmt.Sprintf("%s", x.Recv.List[0].Type)
if len(recvType) > 1 && recvType[0] == '*' {
recvType = recvType[1:]
}
funcType = "method"
funcName = fmt.Sprintf("%s.%s", cleanType(recvType), funcName)
}
pos := fset.Position(x.Pos())
code := getNodeCode(fset, x.Pos(), x.End(), filePath)
nodes[funcName] = &Node{
Name: funcName,
Type: funcType,
Calls: []string{},
Code: code,
Position: pos.String(),
}
case *ast.GenDecl:
if x.Tok == token.TYPE {
for _, spec := range x.Specs {
typeSpec := spec.(*ast.TypeSpec)
typeName := typeSpec.Name.Name
pos := fset.Position(typeSpec.Pos())
code := getNodeCode(fset, typeSpec.Pos(), typeSpec.End(), filePath)
nodes[typeName] = &Node{
Name: typeName,
Type: "type",
Calls: []string{},
Code: code,
Position: pos.String(),
}
}
} else if x.Tok == token.IMPORT {
for _, spec := range x.Specs {
importSpec := spec.(*ast.ImportSpec)
importPath := importSpec.Path.Value
pos := fset.Position(importSpec.Pos())
code := importSpec.Path.Value
nodes[importPath] = &Node{
Name: importPath,
Type: "import",
Calls: []string{},
Code: code,
Position: pos.String(),
}
}
}
}
return true
})
// Collect function and method calls
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
caller := ""
if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
if ident, ok := sel.X.(*ast.Ident); ok {
caller = fmt.Sprintf("%s.%s", ident.Name, sel.Sel.Name)
}
} else if ident, ok := x.Fun.(*ast.Ident); ok {
caller = ident.Name
}
if caller != "" {
if parentFunc := getParentFunc(node, x.Pos(), fset); parentFunc != "" {
if _, ok := nodes[parentFunc]; ok {
nodes[parentFunc].Calls = append(nodes[parentFunc].Calls, caller)
}
}
}
}
return true
})
jsonOutput, err := json.Marshal(nodes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(jsonOutput))
}
func getNodeCode(fset *token.FileSet, start, end token.Pos, filePath string) string {
startOffset := fset.Position(start).Offset
endOffset := fset.Position(end).Offset
code, _ := os.ReadFile(filePath)
return string(code[startOffset:endOffset])
}
func getParentFunc(node *ast.File, pos token.Pos, fset *token.FileSet) string {
var parentFunc string
ast.Inspect(node, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok {
if fd.Pos() < pos && fd.End() > pos {
if fd.Recv != nil {
recvType := fmt.Sprintf("%s", fd.Recv.List[0].Type)
if len(recvType) > 1 && recvType[0] == '*' {
recvType = recvType[1:]
}
parentFunc = fmt.Sprintf("%s.%s", cleanType(recvType), fd.Name.Name)
} else {
parentFunc = fd.Name.Name
}
return false
}
}
return true
})
return parentFunc
}
func cleanType(typeName string) string {
// Remove generic type parameters if present
return strings.TrimSpace(strings.Split(typeName, "[")[0])
}