This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
pretty.go
183 lines (145 loc) · 3.62 KB
/
pretty.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
175
176
177
178
179
180
181
182
183
package uast
import (
"fmt"
"io"
"sort"
"strings"
)
// Pretty writes a pretty string representation of the *Node to a writer.
func Pretty(n *Node, w io.Writer, includes IncludeFlag) error {
if n == nil {
return nil
}
return printNode(w, 0, n, includes)
}
func printNode(w io.Writer, indent int, n *Node, includes IncludeFlag) error {
nodeType := n.InternalType
if !includes.Is(IncludeInternalType) {
nodeType = "*"
}
if _, err := fmt.Fprintf(w, "%s {\n", nodeType); err != nil {
return err
}
istr := strings.Repeat(". ", indent+1)
istrPrev := strings.Repeat(". ", indent)
if includes.Is(IncludeAnnotations) && len(n.Roles) > 0 {
_, err := fmt.Fprintf(w, "%sRoles: %s\n",
istr,
rolesToString(n.Roles...),
)
if err != nil {
return err
}
}
if includes.Is(IncludeTokens) && n.Token != "" {
if _, err := fmt.Fprintf(w, "%sTOKEN \"%s\"\n",
istr, n.Token); err != nil {
return err
}
}
if includes.Is(IncludePositions) && n.StartPosition != nil {
if _, err := fmt.Fprintf(w, "%sStartPosition: {\n", istr); err != nil {
return err
}
if err := printPosition(w, indent+2, n.StartPosition); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%s}\n", istr); err != nil {
return err
}
}
if includes.Is(IncludePositions) && n.EndPosition != nil {
if _, err := fmt.Fprintf(w, "%sEndPosition: {\n", istr); err != nil {
return err
}
if err := printPosition(w, indent+2, n.EndPosition); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%s}\n", istr); err != nil {
return err
}
}
if includes.Is(IncludeProperties) && len(n.Properties) > 0 {
if _, err := fmt.Fprintf(w, "%sProperties: {\n", istr); err != nil {
return err
}
if err := printProperties(w, indent+2, n.Properties); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%s}\n", istr); err != nil {
return err
}
}
if includes.Is(IncludeChildren) && len(n.Children) > 0 {
if _, err := fmt.Fprintf(w, "%sChildren: {\n", istr); err != nil {
return err
}
if err := printChildren(w, indent+2, n.Children, includes); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%s}\n", istr); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s}\n", istrPrev); err != nil {
return err
}
return nil
}
func printChildren(w io.Writer, indent int, children []*Node, includes IncludeFlag) error {
istr := strings.Repeat(". ", indent)
for idx, child := range children {
_, err := fmt.Fprintf(w, "%s%d: ",
istr,
idx,
)
if err != nil {
return err
}
if err := printNode(w, indent, child, includes); err != nil {
return err
}
}
return nil
}
func printProperties(w io.Writer, indent int, props map[string]string) error {
istr := strings.Repeat(". ", indent)
keys := sortedKeys(props)
for _, k := range keys {
v := props[k]
_, err := fmt.Fprintf(w, "%s%s: %s\n", istr, k, v)
if err != nil {
return err
}
}
return nil
}
func sortedKeys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func printPosition(w io.Writer, indent int, pos *Position) error {
if pos == nil {
return nil
}
istr := strings.Repeat(". ", indent)
if _, err := fmt.Fprintf(w, "%sOffset: %d\n", istr, pos.Offset); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%sLine: %d\n", istr, pos.Line); err != nil {
return err
}
_, err := fmt.Fprintf(w, "%sCol: %d\n", istr, pos.Col)
return err
}
func rolesToString(roles ...Role) string {
var strs []string
for _, r := range roles {
strs = append(strs, r.String())
}
return strings.Join(strs, ",")
}