-
Notifications
You must be signed in to change notification settings - Fork 0
/
decl.go
51 lines (47 loc) · 2.08 KB
/
decl.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
package astree
import (
"fmt"
"go/ast"
"go/token"
"io"
)
func genDecl(w io.Writer, fs *token.FileSet, parentPrefix string, prefixes []string, node *ast.GenDecl) {
fmt.Fprintf(w, "%s%sGenDecl\n", parentPrefix, prefixes[0])
fmt.Fprintf(w, "%s%s├── Doc\n", parentPrefix, prefixes[1])
if node.Doc != nil {
tree(w, fs, parentPrefix+prefixes[1]+middleLine, tailPrefixes, node.Doc)
}
fmt.Fprintf(w, "%s%s├── Tok = %s\n", parentPrefix, prefixes[1], node.Tok)
fmt.Fprintf(w, "%s%s└── Specs (length=%d)\n", parentPrefix, prefixes[1], len(node.Specs))
for i := range node.Specs {
if i < len(node.Specs)-1 {
tree(w, fs, parentPrefix+prefixes[1]+tailLine, middlePrefixes, node.Specs[i])
} else {
tree(w, fs, parentPrefix+prefixes[1]+tailLine, tailPrefixes, node.Specs[i])
}
}
}
func badDecl(w io.Writer, fs *token.FileSet, parentPrefix string, prefixes []string, node *ast.BadDecl) {
fmt.Fprintf(w, "%s%sBadDecl\n", parentPrefix, prefixes[0])
fmt.Fprintf(w, "%s%s├── From = %s\n", parentPrefix, prefixes[1], fs.Position(node.From))
fmt.Fprintf(w, "%s%s└── To = %s\n", parentPrefix, prefixes[1], fs.Position(node.To))
}
func funcDecl(w io.Writer, fs *token.FileSet, parentPrefix string, prefixes []string, node *ast.FuncDecl) {
fmt.Fprintf(w, "%s%sFuncDecl\n", parentPrefix, prefixes[0])
fmt.Fprintf(w, "%s%s├── Doc\n", parentPrefix, prefixes[1])
if node.Doc != nil {
tree(w, fs, parentPrefix+prefixes[1]+middleLine, tailPrefixes, node.Doc)
}
fmt.Fprintf(w, "%s%s├── Recv\n", parentPrefix, prefixes[1])
if node.Recv != nil {
tree(w, fs, parentPrefix+prefixes[1]+middleLine, tailPrefixes, node.Recv)
}
fmt.Fprintf(w, "%s%s├── Name\n", parentPrefix, prefixes[1])
tree(w, fs, parentPrefix+prefixes[1]+middleLine, tailPrefixes, node.Name)
fmt.Fprintf(w, "%s%s├── Type\n", parentPrefix, prefixes[1])
tree(w, fs, parentPrefix+prefixes[1]+middleLine, tailPrefixes, node.Type)
fmt.Fprintf(w, "%s%s└── Body\n", parentPrefix, prefixes[1])
if node.Body != nil {
tree(w, fs, parentPrefix+prefixes[1]+tailLine, tailPrefixes, node.Body)
}
}