-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastree.go
More file actions
255 lines (232 loc) · 7.03 KB
/
astree.go
File metadata and controls
255 lines (232 loc) · 7.03 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package astree
import (
"fmt"
"go/ast"
"go/token"
"io"
)
// Config holds configuration for tree drawing characters
type Config struct {
MiddlePrefix string
TailPrefix string
MiddleLine string
TailLine string
}
const (
middleLine = "│ "
tailLine = " "
)
var (
middlePrefixes = []string{"├── ", "│ "}
tailPrefixes = []string{"└── ", " "}
currentConfig *Config
)
// File prints AST of one file
func File(w io.Writer, fs *token.FileSet, node *ast.File) error {
if fs == nil {
return fmt.Errorf("*token.FileSet is nil")
}
tree(w, fs, "", []string{"", ""}, node)
return nil
}
// FileWithConfig prints AST of one file with custom tree characters
func FileWithConfig(w io.Writer, fs *token.FileSet, node *ast.File, config Config) error {
if fs == nil {
return fmt.Errorf("*token.FileSet is nil")
}
treeWithConfig(w, fs, "", []string{"", ""}, node, config)
return nil
}
// Packages prints AST of result from go/parser.ParseDir
func Packages(w io.Writer, fs *token.FileSet, pkgs map[string]*ast.Package) error {
if fs == nil {
return fmt.Errorf("*token.FileSet is nil")
}
count := 0
for k, v := range pkgs {
if count < len(pkgs)-1 {
tree(w, fs, "", []string{getMiddlePrefixes()[0] + k + ":", getMiddlePrefixes()[1]}, v)
} else {
tree(w, fs, "", []string{getTailPrefixes()[0] + k + ":", getTailPrefixes()[1]}, v)
}
count++
}
return nil
}
// PackagesWithConfig prints AST of result from go/parser.ParseDir with custom tree characters
func PackagesWithConfig(w io.Writer, fs *token.FileSet, pkgs map[string]*ast.Package, config Config) error {
if fs == nil {
return fmt.Errorf("*token.FileSet is nil")
}
count := 0
for k, v := range pkgs {
if count < len(pkgs)-1 {
treeWithConfig(w, fs, "", []string{config.MiddlePrefix + k + ":", config.MiddleLine}, v, config)
} else {
treeWithConfig(w, fs, "", []string{config.TailPrefix + k + ":", config.TailLine}, v, config)
}
count++
}
return nil
}
// helper functions to get current prefixes based on config
func getMiddlePrefixes() []string {
if currentConfig != nil {
return []string{currentConfig.MiddlePrefix, currentConfig.MiddleLine}
}
return middlePrefixes
}
func getTailPrefixes() []string {
if currentConfig != nil {
return []string{currentConfig.TailPrefix, currentConfig.TailLine}
}
return tailPrefixes
}
// helper function to get the middle prefix character
func getMiddlePrefix() string {
if currentConfig != nil {
return currentConfig.MiddlePrefix
}
return "├── "
}
// helper function to get the tail prefix character
func getTailPrefix() string {
if currentConfig != nil {
return currentConfig.TailPrefix
}
return "└── "
}
// treeWithConfig is the same as tree but sets the currentConfig
func treeWithConfig(w io.Writer, fs *token.FileSet, parentPrefix string, prefixes []string, node ast.Node, config Config) {
oldConfig := currentConfig
currentConfig = &config
tree(w, fs, parentPrefix, prefixes, node)
currentConfig = oldConfig
}
// Node prints AST node
func Node(w io.Writer, fs *token.FileSet, node ast.Node) {
tree(w, fs, "", []string{"", ""}, node)
}
// Tree desplays ast nodes like tree
func tree(w io.Writer, fs *token.FileSet, parentPrefix string, prefixes []string, node ast.Node) {
switch n := node.(type) {
case *ast.File:
file(w, fs, parentPrefix, prefixes, n)
// Spec
case *ast.ImportSpec:
importSpec(w, fs, parentPrefix, prefixes, n)
case *ast.ValueSpec:
valueSpec(w, fs, parentPrefix, prefixes, n)
case *ast.TypeSpec:
typeSpec(w, fs, parentPrefix, prefixes, n)
case *ast.Ident:
ident(w, fs, parentPrefix, prefixes, n)
case *ast.BasicLit:
basicLit(w, fs, parentPrefix, prefixes, n)
case *ast.CompositeLit:
compositeLit(w, fs, parentPrefix, prefixes, n)
case *ast.FuncLit:
funcLit(w, fs, parentPrefix, prefixes, n)
// comment
case *ast.CommentGroup:
commentGroup(w, fs, parentPrefix, prefixes, n)
case *ast.Comment:
comment(w, fs, parentPrefix, prefixes, n)
// Decls
case *ast.GenDecl:
genDecl(w, fs, parentPrefix, prefixes, n)
case *ast.BadDecl:
badDecl(w, fs, parentPrefix, prefixes, n)
case *ast.FuncDecl:
funcDecl(w, fs, parentPrefix, prefixes, n)
// Expr
case *ast.BadExpr:
badExpr(w, fs, parentPrefix, prefixes, n)
case *ast.BinaryExpr:
binaryExpr(w, fs, parentPrefix, prefixes, n)
case *ast.CallExpr:
callExpr(w, fs, parentPrefix, prefixes, n)
case *ast.IndexExpr:
indexExpr(w, fs, parentPrefix, prefixes, n)
case *ast.KeyValueExpr:
keyValueExpr(w, fs, parentPrefix, prefixes, n)
case *ast.ParenExpr:
parenExpr(w, fs, parentPrefix, prefixes, n)
case *ast.SelectorExpr:
selectorExpr(w, fs, parentPrefix, prefixes, n)
case *ast.SliceExpr:
sliceExpr(w, fs, parentPrefix, prefixes, n)
case *ast.StarExpr:
starExpr(w, fs, parentPrefix, prefixes, n)
case *ast.TypeAssertExpr:
typeAssertExpr(w, fs, parentPrefix, prefixes, n)
case *ast.UnaryExpr:
unaryExpr(w, fs, parentPrefix, prefixes, n)
case *ast.IndexListExpr:
indexListExpr(w, fs, parentPrefix, prefixes, n)
// Statement
case *ast.AssignStmt:
assignStmt(w, fs, parentPrefix, prefixes, n)
case *ast.BadStmt:
badStmt(w, fs, parentPrefix, prefixes, n)
case *ast.BlockStmt:
blockStmt(w, fs, parentPrefix, prefixes, n)
case *ast.BranchStmt:
branchStmt(w, fs, parentPrefix, prefixes, n)
case *ast.DeclStmt:
declStmt(w, fs, parentPrefix, prefixes, n)
case *ast.DeferStmt:
deferStmt(w, fs, parentPrefix, prefixes, n)
case *ast.EmptyStmt:
emptyStmt(w, fs, parentPrefix, prefixes, n)
case *ast.ExprStmt:
exprStmt(w, fs, parentPrefix, prefixes, n)
case *ast.ForStmt:
forStmt(w, fs, parentPrefix, prefixes, n)
case *ast.GoStmt:
goStmt(w, fs, parentPrefix, prefixes, n)
case *ast.IfStmt:
ifStmt(w, fs, parentPrefix, prefixes, n)
case *ast.IncDecStmt:
incDecStmt(w, fs, parentPrefix, prefixes, n)
case *ast.LabeledStmt:
labeledStmt(w, fs, parentPrefix, prefixes, n)
case *ast.RangeStmt:
rangeStmt(w, fs, parentPrefix, prefixes, n)
case *ast.ReturnStmt:
returnStmt(w, fs, parentPrefix, prefixes, n)
case *ast.SelectStmt:
selectStmt(w, fs, parentPrefix, prefixes, n)
case *ast.SendStmt:
sendStmt(w, fs, parentPrefix, prefixes, n)
case *ast.SwitchStmt:
switchStmt(w, fs, parentPrefix, prefixes, n)
case *ast.TypeSwitchStmt:
typeSwitchStmt(w, fs, parentPrefix, prefixes, n)
// Type
case *ast.ArrayType:
arrayType(w, fs, parentPrefix, prefixes, n)
case *ast.ChanType:
chanType(w, fs, parentPrefix, prefixes, n)
case *ast.FuncType:
funcType(w, fs, parentPrefix, prefixes, n)
case *ast.InterfaceType:
interfaceType(w, fs, parentPrefix, prefixes, n)
case *ast.MapType:
mapType(w, fs, parentPrefix, prefixes, n)
case *ast.StructType:
structType(w, fs, parentPrefix, prefixes, n)
case *ast.CaseClause:
caseClause(w, fs, parentPrefix, prefixes, n)
case *ast.CommClause:
commClause(w, fs, parentPrefix, prefixes, n)
case *ast.Ellipsis:
ellipsis(w, fs, parentPrefix, prefixes, n)
case *ast.Field:
field(w, fs, parentPrefix, prefixes, n)
case *ast.FieldList:
fieldList(w, fs, parentPrefix, prefixes, n)
case *ast.Package:
package2(w, fs, parentPrefix, prefixes, n)
}
}