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
|
package ast
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
func formatBytes(b []byte) string {
if utf8.Valid(b) {
for r, i, size := rune(0), 0, 0; i < len(b); i += size {
r, size = utf8.DecodeRune(b[i:])
if !unicode.IsPrint(r) {
return strconv.Quote(string(b))
}
}
return string(b)
}
return fmt.Sprintf("%v", b)
}
func formatString(s string) string {
if utf8.ValidString(s) {
for _, r := range s {
if !unicode.IsPrint(r) {
return strconv.Quote(s)
}
}
return s
}
return fmt.Sprintf("%v", []byte(s))
}
func printAST(w io.Writer, n interface{}, s []byte, prefix string,
detail bool, depth int) {
indent := strings.Repeat(prefix, depth)
indentLong := strings.Repeat(prefix, depth+1)
if n == nil {
fmt.Fprintf(w, "%snil\n", indent)
return
}
typeOf := reflect.TypeOf(n)
valueOf := reflect.ValueOf(n)
kind := typeOf.Kind()
if kind == reflect.Ptr {
if valueOf.IsNil() {
fmt.Fprintf(w, "%snil\n", indent)
return
}
valueOf = valueOf.Elem()
typeOf = typeOf.Elem()
kind = typeOf.Kind()
}
name := typeOf.Name()
if stringer, ok := n.(fmt.Stringer); ok {
s := stringer.String()
fmt.Fprintf(w, "%s%s\n", indent, formatString(s))
return
}
if s, ok := n.(string); ok {
fmt.Fprintf(w, "%s%s\n", indent, formatString(s))
return
}
if bs, ok := n.([]byte); ok {
fmt.Fprintf(w, "%s%s\n", indent, formatBytes(bs))
return
}
if kind == reflect.Slice {
l := valueOf.Len()
if l == 0 {
fmt.Fprintf(w, "%s[]\n", indent)
return
}
fmt.Fprintf(w, "%s[\n", indent)
for i := 0; i < l; i++ {
v := valueOf.Index(i)
printAST(w, v.Interface(), s, prefix, detail, depth+1)
}
fmt.Fprintf(w, "%s]\n", indent)
return
}
if kind == reflect.Struct {
type field struct {
name string
value interface{}
}
var fields []field
var collect func(reflect.Type, reflect.Value)
collect = func(typeOf reflect.Type, valueOf reflect.Value) {
l := typeOf.NumField()
for i := 0; i < l; i++ {
if !detail && typeOf.Field(i).Tag.Get("print") == "-" {
continue
}
if typeOf.Field(i).Anonymous {
embeddedInterface := valueOf.Field(i).Interface()
embeddedTypeOf := reflect.TypeOf(embeddedInterface)
embeddedValueOf := reflect.ValueOf(embeddedInterface)
collect(embeddedTypeOf, embeddedValueOf)
continue
}
fields = append(fields, field{
name: typeOf.Field(i).Name,
value: valueOf.Field(i).Interface(),
})
}
}
collect(typeOf, valueOf)
var position string
if node, ok := n.(Node); ok {
begin := node.GetPosition()
length := node.GetLength()
if node.HasPosition() {
end := begin + length - 1
token := s[begin : begin+length]
position = fmt.Sprintf("%d-%d %s",
begin, end, strconv.Quote(string(token)))
} else {
position = "no position info"
}
}
fmt.Fprintf(w, "%s%s", indent, name)
if len(fields) == 0 {
fmt.Fprintf(w, " {} // %s\n", position)
return
}
fmt.Fprintf(w, " { // %s\n", position)
for i := 0; i < len(fields); i++ {
fmt.Fprintf(w, "%s%s:\n", indentLong, fields[i].name)
printAST(w, fields[i].value, s, prefix, detail, depth+2)
}
fmt.Fprintf(w, "%s}\n", indent)
return
}
fmt.Fprintf(w, "%s%+v\n", indent, valueOf.Interface())
}
// PrintAST prints AST for debugging.
func PrintAST(output io.Writer, node interface{}, source []byte,
indent string, detail bool) {
printAST(output, node, source, indent, detail, 0)
}
|