-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.go
139 lines (123 loc) · 2.96 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
)
type Declaration struct {
Label string `json:"label"`
Type string `json:"type"`
ReceiverType string `json:"receiverType,omitempty"`
Start token.Pos `json:"start"`
End token.Pos `json:"end"`
Children []Declaration `json:"children,omitempty"`
}
var (
file = flag.String("f", "", "the path to the file to outline")
importsOnly = flag.Bool("imports-only", false, "parse imports only")
src = flag.String("src", "", "source code of the file to outline")
)
func main() {
flag.Parse()
fset := token.NewFileSet()
parserMode := parser.ParseComments
if *importsOnly == true {
parserMode = parser.ImportsOnly
}
var fileAst *ast.File
var err error
if len(*src) > 0 {
fileAst, err = parser.ParseFile(fset, *file, *src, parserMode)
} else {
fileAst, err = parser.ParseFile(fset, *file, nil, parserMode)
}
if err != nil {
reportError(fmt.Errorf("Could not parse file %s", *file))
}
declarations := []Declaration{}
for _, decl := range fileAst.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
receiverType, err := getReceiverType(fset, decl)
if err != nil {
reportError(fmt.Errorf("Failed to parse receiver type: %v", err))
}
declarations = append(declarations, Declaration{
decl.Name.String(),
"function",
receiverType,
decl.Pos(),
decl.End(),
[]Declaration{},
})
case *ast.GenDecl:
for _, spec := range decl.Specs {
switch spec := spec.(type) {
case *ast.ImportSpec:
declarations = append(declarations, Declaration{
spec.Path.Value,
"import",
"",
spec.Pos(),
spec.End(),
[]Declaration{},
})
case *ast.TypeSpec:
//TODO: Members if it's a struct or interface type?
declarations = append(declarations, Declaration{
spec.Name.String(),
"type",
"",
spec.Pos(),
spec.End(),
[]Declaration{},
})
case *ast.ValueSpec:
for _, id := range spec.Names {
declarations = append(declarations, Declaration{
id.Name,
"variable",
"",
id.Pos(),
id.End(),
[]Declaration{},
})
}
default:
reportError(fmt.Errorf("Unknown token type: %s", decl.Tok))
}
}
default:
reportError(fmt.Errorf("Unknown declaration @", decl.Pos()))
}
}
pkg := []*Declaration{&Declaration{
fileAst.Name.String(),
"package",
"",
fileAst.Pos(),
fileAst.End(),
declarations,
}}
str, _ := json.Marshal(pkg)
fmt.Println(string(str))
}
func getReceiverType(fset *token.FileSet, decl *ast.FuncDecl) (string, error) {
if decl.Recv == nil {
return "", nil
}
buf := &bytes.Buffer{}
if err := format.Node(buf, fset, decl.Recv.List[0].Type); err != nil {
return "", err
}
return buf.String(), nil
}
func reportError(err error) {
fmt.Fprintln(os.Stderr, "error:", err)
}