forked from google/mtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unparser.go
218 lines (186 loc) · 3.45 KB
/
unparser.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
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
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package vm
import (
"fmt"
"strconv"
"strings"
"github.com/google/mtail/metrics"
)
// Unparser is for converting program syntax trees back to program text.
type Unparser struct {
pos int
output string
line string
}
func (u *Unparser) indent() {
u.pos += 2
}
func (u *Unparser) outdent() {
u.pos -= 2
}
func (u *Unparser) prefix() (s string) {
for i := 0; i < u.pos; i++ {
s += " "
}
return
}
func (u *Unparser) emit(s string) {
u.line += s
}
func (u *Unparser) newline() {
u.output += u.prefix() + u.line + "\n"
u.line = ""
}
func (u *Unparser) VisitBefore(n node) Visitor {
switch v := n.(type) {
case *stmtlistNode:
for _, child := range v.children {
Walk(u, child)
u.newline()
}
case *exprlistNode:
if len(v.children) > 0 {
Walk(u, v.children[0])
for _, child := range v.children[1:] {
u.emit(", ")
Walk(u, child)
}
}
case *condNode:
if v.cond != nil {
Walk(u, v.cond)
}
u.emit(" {")
u.newline()
u.indent()
Walk(u, v.truthNode)
if v.elseNode != nil {
u.outdent()
u.emit("} else {")
u.indent()
Walk(u, v.elseNode)
}
u.outdent()
u.emit("}")
case *regexNode:
u.emit("/" + strings.Replace(v.pattern, "/", "\\/", -1) + "/")
case *binaryExprNode:
Walk(u, v.lhs)
switch v.op {
case LT:
u.emit(" < ")
case GT:
u.emit(" > ")
case LE:
u.emit(" <= ")
case GE:
u.emit(" >= ")
case EQ:
u.emit(" == ")
case NE:
u.emit(" != ")
case SHL:
u.emit(" << ")
case SHR:
u.emit(" >> ")
case AND:
u.emit(" & ")
case OR:
u.emit(" | ")
case XOR:
u.emit(" ^ ")
case NOT:
u.emit(" ~ ")
case PLUS:
u.emit(" + ")
case MINUS:
u.emit(" - ")
case MUL:
u.emit(" * ")
case DIV:
u.emit(" / ")
case POW:
u.emit(" ** ")
case ASSIGN:
u.emit(" = ")
case MOD:
u.emit(" % ")
}
Walk(u, v.rhs)
case *idNode:
u.emit(v.name)
case *caprefNode:
u.emit("$" + v.name)
case *builtinNode:
u.emit(v.name + "(")
if v.args != nil {
Walk(u, v.args)
}
u.emit(")")
case *indexedExprNode:
Walk(u, v.lhs)
u.emit("[")
Walk(u, v.index)
u.emit("]")
case *declNode:
switch v.kind {
case metrics.Counter:
u.emit("counter ")
case metrics.Gauge:
u.emit("gauge ")
case metrics.Timer:
u.emit("timer ")
}
u.emit(v.name)
if len(v.keys) > 0 {
u.emit(" by " + strings.Join(v.keys, ", "))
}
case *unaryExprNode:
switch v.op {
case INC:
Walk(u, v.expr)
u.emit("++")
case NOT:
u.emit(" ~")
Walk(u, v.expr)
}
case *stringConstNode:
u.emit("\"" + v.text + "\"")
case *intConstNode:
u.emit(strconv.FormatInt(v.i, 10))
case *floatConstNode:
u.emit(strconv.FormatFloat(v.f, 'g', -1, 64))
case *defNode:
u.emit(fmt.Sprintf("def %s {", v.name))
u.newline()
u.indent()
Walk(u, v.block)
u.outdent()
u.emit("}")
case *decoNode:
u.emit(fmt.Sprintf("@%s {", v.name))
u.newline()
u.indent()
Walk(u, v.block)
u.outdent()
u.emit("}")
case *nextNode:
u.emit("next")
case *otherwiseNode:
u.emit("otherwise")
case *delNode:
u.emit("del ")
Walk(u, v.n)
u.newline()
default:
panic(fmt.Sprintf("unparser found undefined type %T", n))
}
return nil
}
func (u *Unparser) VisitAfter(n node) {}
// Unparse begins the unparsing of the syntax tree, returning the program text as a single string.
func (u *Unparser) Unparse(n node) string {
Walk(u, n)
return u.output
}