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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package parser
import (
"fmt"
"strconv"
"strings"
"github.com/jaqx0r/mtail/internal/metrics"
"github.com/jaqx0r/mtail/internal/runtime/compiler/ast"
)
// Unparser is for converting program syntax trees back to program text.
type Unparser struct {
pos int
output strings.Builder
line strings.Builder
emitTypes bool
}
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.WriteString(s)
}
func (u *Unparser) newline() {
u.output.WriteString(u.prefix())
u.output.WriteString(u.line.String())
u.output.WriteString("\n")
u.line.Reset()
}
// VisitBefore implements the ast.Visitor interface.
func (u *Unparser) VisitBefore(n ast.Node) (ast.Visitor, ast.Node) {
if u.emitTypes {
u.emit(fmt.Sprintf("<%s>(", n.Type()))
}
switch v := n.(type) {
case *ast.LogFilter:
u.emit("log_filter ")
if len(v.Filters) > 0 {
u.emit("\"" + strings.Join(v.Filters, "\", \"") + "\"")
}
u.newline()
case *ast.StmtList:
for _, child := range v.Children {
ast.Walk(u, child)
u.newline()
}
case *ast.ExprList:
if len(v.Children) > 0 {
ast.Walk(u, v.Children[0])
for _, child := range v.Children[1:] {
u.emit(", ")
ast.Walk(u, child)
}
}
case *ast.CondStmt:
if v.Cond != nil {
ast.Walk(u, v.Cond)
}
u.emit(" {")
u.newline()
u.indent()
ast.Walk(u, v.Truth)
if v.Else != nil {
u.outdent()
u.emit("} else {")
u.newline()
u.indent()
ast.Walk(u, v.Else)
}
u.outdent()
u.emit("}")
case *ast.PatternFragment:
u.emit("const ")
ast.Walk(u, v.ID)
u.emit(" ")
ast.Walk(u, v.Expr)
case *ast.PatternLit:
u.emit("/" + strings.ReplaceAll(v.Pattern, "/", "\\/") + "/")
case *ast.BinaryExpr:
ast.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 BITAND:
u.emit(" & ")
case BITOR:
u.emit(" | ")
case XOR:
u.emit(" ^ ")
case NOT:
u.emit(" ~ ")
case AND:
u.emit(" && ")
case OR:
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 ADD_ASSIGN:
u.emit(" += ")
case MOD:
u.emit(" % ")
case MATCH:
u.emit(" =~ ")
case NOT_MATCH:
u.emit(" !~ ")
default:
u.emit(fmt.Sprintf("Unexpected op: %v", v.Op))
}
ast.Walk(u, v.RHS)
case *ast.IDTerm:
u.emit(v.Name)
case *ast.CaprefTerm:
u.emit("$" + v.Name)
case *ast.BuiltinExpr:
u.emit(v.Name + "(")
if v.Args != nil {
ast.Walk(u, v.Args)
}
u.emit(")")
case *ast.IndexedExpr:
ast.Walk(u, v.LHS)
if len(v.Index.(*ast.ExprList).Children) > 0 {
u.emit("[")
ast.Walk(u, v.Index)
u.emit("]")
}
case *ast.VarDecl:
switch v.Kind {
case metrics.Counter:
u.emit("counter ")
case metrics.Gauge:
u.emit("gauge ")
case metrics.Timer:
u.emit("timer ")
case metrics.Text:
u.emit("text ")
case metrics.Histogram:
u.emit("histogram ")
}
u.emit(v.Name)
if len(v.Keys) > 0 {
u.emit(" by " + strings.Join(v.Keys, ", "))
}
if v.Limit > 0 {
u.emit(fmt.Sprintf(" limit %d", v.Limit))
}
if len(v.Buckets) > 0 {
buckets := strings.Builder{}
buckets.WriteString(" buckets ")
for _, f := range v.Buckets {
buckets.WriteString(fmt.Sprintf("%f, ", f))
}
u.emit(buckets.String()[:buckets.Len()-2])
}
case *ast.UnaryExpr:
switch v.Op {
case INC:
ast.Walk(u, v.Expr)
u.emit("++")
case DEC:
ast.Walk(u, v.Expr)
u.emit("--")
case NOT:
u.emit(" ~")
ast.Walk(u, v.Expr)
case MATCH:
ast.Walk(u, v.Expr)
default:
u.emit(fmt.Sprintf("Unexpected op: %s", Kind(v.Op)))
}
case *ast.StringLit:
u.emit("\"" + v.Text + "\"")
case *ast.IntLit:
u.emit(strconv.FormatInt(v.I, 10))
case *ast.FloatLit:
u.emit(strconv.FormatFloat(v.F, 'g', -1, 64))
case *ast.DecoDecl:
u.emit(fmt.Sprintf("def %s {", v.Name))
u.newline()
u.indent()
ast.Walk(u, v.Block)
u.outdent()
u.emit("}")
case *ast.DecoStmt:
u.emit(fmt.Sprintf("@%s {", v.Name))
u.newline()
u.indent()
ast.Walk(u, v.Block)
u.outdent()
u.emit("}")
case *ast.NextStmt:
u.emit("next")
case *ast.OtherwiseStmt:
u.emit("otherwise")
case *ast.DelStmt:
u.emit("del ")
ast.Walk(u, v.N)
if v.Expiry > 0 {
u.emit(fmt.Sprintf(" after %s", v.Expiry))
}
u.newline()
case *ast.ConvExpr:
ast.Walk(u, v.N)
case *ast.PatternExpr:
ast.Walk(u, v.Expr)
case *ast.Error:
u.emit("// error")
u.newline()
u.emit(v.Spelling)
case *ast.StopStmt:
u.emit("stop")
default:
panic(fmt.Sprintf("unfound undefined type %T", n))
}
if u.emitTypes {
u.emit(")")
}
return nil, n
}
// VisitAfter implements the ast.Visitor interface.
func (u *Unparser) VisitAfter(n ast.Node) ast.Node {
return n
}
// Unparse begins the unparsing of the syntax tree, returning the program text as a single string.
func (u *Unparser) Unparse(n ast.Node) string {
ast.Walk(u, n)
return u.output.String()
}
|