forked from yuroyoro/goast-viewer
/
ast.go
174 lines (141 loc) · 3.4 KB
/
ast.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
"strings"
)
type Ast struct {
Label string `json:"label"`
Pos int `json:"pos"`
End int `json:"end"`
Attrs map[string]string `json:"attrs"`
Children []*Ast `json:"children"`
}
type AstConverter interface {
ToAst() *Ast
}
func Parse(filename string, source string) (a *Ast, dump string, err error) {
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, filename, source, 0)
// Print the AST.
var bf bytes.Buffer
ast.Fprint(&bf, fset, f, ast.NotNilFilter)
a, err = BuildAst("", f)
if err != nil {
return nil, "", err
}
return a, string(bf.Bytes()), nil
}
func BuildAst(prefix string, n interface{}) (astobj *Ast, err error) {
v := reflect.ValueOf(n)
t := v.Type()
a := Ast{Label: Label(prefix, n), Attrs: map[string]string{}, Children: []*Ast{}}
if node, ok := n.(ast.Node); ok {
a.Pos = int(node.Pos())
a.End = int(node.End())
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
t = v.Type()
}
if v.IsValid() == false {
return nil, nil
}
switch v.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < v.Len(); i++ {
f := v.Index(i)
child, err := BuildAst(fmt.Sprintf("%d", i), f.Interface())
if err != nil {
return nil, err
}
a.Children = append(a.Children, child)
}
case reflect.Map:
for _, kv := range v.MapKeys() {
f := v.MapIndex(kv)
child, err := BuildAst(fmt.Sprintf("%v", kv.Interface()), f.Interface())
if err != nil {
return nil, err
}
a.Children = append(a.Children, child)
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
fo := f
name := t.Field(i).Name
if f.Kind() == reflect.Ptr {
f = f.Elem()
}
if f.IsValid() == false {
continue
}
if _, ok := v.Interface().(ast.Object); !ok && f.Kind() == reflect.Interface {
switch f.Interface().(type) {
case ast.Decl, ast.Expr, ast.Node, ast.Spec, ast.Stmt:
child, err := BuildAst(name, f.Interface())
if err != nil {
return nil, err
}
a.Children = append(a.Children, child)
continue
}
}
switch f.Kind() {
case reflect.Struct, reflect.Array, reflect.Slice, reflect.Map:
child, err := BuildAst(name, fo.Interface())
if err != nil {
return nil, err
}
a.Children = append(a.Children, child)
default:
a.Attrs[name] = fmt.Sprintf("%v", f.Interface())
}
}
}
return &a, nil
}
func Label(prefix string, n interface{}) string {
var bf bytes.Buffer
if prefix != "" {
fmt.Fprintf(&bf, "%s : ", prefix)
}
fmt.Fprintf(&bf, "%T", n)
v := reflect.ValueOf(n)
t := v.Type()
if v.Kind() == reflect.Ptr {
v = v.Elem()
t = v.Type()
}
if v.IsValid() == false {
return ""
}
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan:
fmt.Fprintf(&bf, "(len = %d)", v.Len())
case reflect.Struct:
if v.Kind() == reflect.Struct {
fs := []string{}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
name := t.Field(i).Name
switch name {
case "Name", "Kind", "Tok", "Op":
fs = append(fs, fmt.Sprintf("%s: %v", name, f.Interface()))
}
}
if len(fs) > 0 {
fmt.Fprintf(&bf, " (%s)", strings.Join(fs, ", "))
}
}
default:
fmt.Fprintf(&bf, " : %s", n)
}
return string(bf.Bytes())
}