This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
goify.go
336 lines (309 loc) · 6.53 KB
/
goify.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"regexp"
"strconv"
"strings"
"text/scanner"
"github.com/iancoleman/strcase"
)
var typeMapping = map[string]string{
"u1": "uint8",
"u2": "uint16",
"u4": "uint32",
"u8": "uint64",
"u2le": "uint16",
"u4le": "uint32",
"u8le": "uint64",
"u2be": "uint16",
"u4be": "uint32",
"u8be": "uint64",
"s1": "int8",
"s2": "int16",
"s4": "int32",
"s8": "int64",
"s2le": "int16",
"s4le": "int32",
"s8le": "int64",
"s2be": "int16",
"s4be": "int32",
"s8be": "int64",
"f4": "float32",
"f8": "float64",
"f4le": "float32",
"f8le": "float64",
"f4be": "float32",
"f8be": "float64",
"str": "[]byte",
"strz": "[]byte",
"": "[]byte",
}
func bitString(s string) string {
num, err := strconv.ParseInt(s[2:], 2, 64)
if err != nil {
panic(err)
}
return fmt.Sprintf("%d", num)
}
func isInt(expr string) bool {
return !strings.Contains(goExpr(expr, ""), "k.")
}
func getExprType(expr ast.Expr) (s string, r bool) {
// fmt.Printf("%#v\n", expr)
ast.Inspect(expr, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.BasicLit:
s = expressionTypes[x.Kind]
return false
case *ast.Ident:
if x.Name == "true" || x.Name == "false" {
s = "bool"
} else {
if isNative(x.Name) {
s = x.Name
} else {
s = getKaitaiType(x.Name)
}
}
return false
case *ast.UnaryExpr:
if x.Op == token.NOT {
s = "bool"
} else {
s, r = getExprType(x.X)
}
return false
case *ast.BinaryExpr:
s = expressionTypes[x.Op]
return x.Op == token.ADD || x.Op == token.ADD_ASSIGN
case *ast.CallExpr:
s, r = getExprType(x.Fun)
return r
case *ast.SelectorExpr:
s, r = getExprType(x.Sel)
return r
case *ast.FuncType:
s, r = getExprType(x.Results.List[0].Type)
return r
default:
return true
}
})
return
}
func getType(expr string) (t string) {
var re = regexp.MustCompile(`\*k.*\(\)`)
goExpr := re.ReplaceAllString(goExpr(expr, ""), `"x"`)
// fmt.Println()
// fmt.Println(goExpr)
exprx, _ := parser.ParseExpr(goExpr)
var s string
if exprx != nil {
s, _ = getExprType(exprx)
}
// fmt.Println("s", s)
switch s {
case "int":
return "int64"
case "string":
return "[]byte"
case "bool":
return "bool"
case "":
return "[]byte"
case "float":
return "float64"
default:
return s
}
}
func goenum(s string, cast string) string {
// cast
if strings.HasSuffix(s, ".to_i") {
s = s[:len(s)-5]
cast = "int64"
}
parts := strings.SplitN(s, "::", 2)
if len(parts) < 2 {
return s
}
s = strcase.ToCamel(parts[0]) + "." + strcase.ToCamel(parts[1])
if cast != "" {
return cast + "(" + s + ")"
}
return s
}
func isIdentifierPart(tok rune, casting bool) bool {
// handle greater and lower than
if tok == '<' || tok == '>' {
if !casting {
return false
} else {
return true
}
}
return tok == scanner.Ident || tok == '.' || tok == '"' || tok == '_' || tok == '[' || tok == ']' || tok == '\''
}
func goExprIdent(expr, casttype, current_attr string) string {
ret := "k."
var s scanner.Scanner
s.Init(strings.NewReader(expr))
s.Filename = "example"
cast := false
start := true
// fmt.Println(expr)
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
// fmt.Println("inner", s.TokenText())
switch s.TokenText() {
case "not":
ret = "!"
case "true", "false":
ret = s.TokenText()
case "_":
ret = ret[:len(ret)-1] + "." + current_attr
case "\"":
ret += "\""
case ".":
ret += "."
case "<":
ret += "("
case ">":
ret += ")"
cast = false
case "[":
if start {
ret = "[]byte{"
} else {
ret += s.TokenText()
}
case "]":
if start {
ret = "}"
} else {
ret += s.TokenText()
}
case "_parent":
ret += "Parent()"
case "_root":
ret += "Root()"
case "_index":
ret += "index"
case "to_i":
ret = "int64(" + ret[:len(ret)-1] + ")"
case "to_s":
ret = "strconv.Itoa(int(" + ret[:len(ret)-1] + "))"
case "as":
cast = true
case "first":
if expr == "first" {
ret += strcase.ToCamel(s.TokenText())
if !cast {
ret += "()"
}
} else {
ret = ret[:len(ret)-1] + "[0]"
}
case "last":
if expr == "last" {
ret += strcase.ToCamel(s.TokenText())
if !cast {
ret += "()"
}
} else {
ret = ret[:len(ret)-1] + "[:len(" + ret[:len(ret)-1] + ")-1]"
}
case "length":
if expr == "length" {
ret += strcase.ToCamel(s.TokenText())
if !cast {
ret += "()"
}
} else {
ret = "len(" + ret[:len(ret)-1] + ")"
}
case "size":
if expr == "size" {
ret += strcase.ToCamel(s.TokenText())
if !cast {
ret += "()"
}
} else {
ret = "len(" + ret[:len(ret)-1] + ")"
}
default:
ret += strcase.ToCamel(s.TokenText())
if !cast {
ret += "()"
}
}
start = false
}
if casttype != "" {
return casttype + "(" + ret + ")"
}
return ret
}
func goExpr(expr, casttype string) string {
return goExprAttr(expr, casttype, "")
}
func goExprAttr(expr, casttype, current_attr string) string {
// replace binary values
re := regexp.MustCompile("0[bB][0-9]+")
expr = re.ReplaceAllStringFunc(expr, bitString)
re = regexp.MustCompile("'")
expr = re.ReplaceAllString(expr, "\"")
var s scanner.Scanner
s.Init(strings.NewReader(expr))
s.Whitespace = 0
s.Filename = "example"
identifier := ""
casting := false
ret := ""
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
// fmt.Println(tok, s.TokenText())
// handle identifier chain
if !isIdentifierPart(tok, casting) && identifier != "" {
ret += " " + goExprIdent(identifier, casttype, current_attr)
identifier = ""
}
switch {
case isIdentifierPart(tok, casting):
identifierPart := s.TokenText()
// identify casting start
if identifierPart == "as" {
casting = true
}
// identify casting end
if tok == '>' {
casting = false
}
identifier += " " + identifierPart
case tok == '?':
parts := strings.SplitN(expr, "?", 2)
check := goExpr(parts[0], "")
cases := strings.SplitN(parts[1], ":", 2)
ifvalue := goExpr(cases[0], "")
elsevalue := goExpr(cases[1], "")
return fmt.Sprintf("func()"+getType(ifvalue)+"{if %s{return %s}else{return %s}}()", check, ifvalue, elsevalue)
default:
ret += s.TokenText()
}
}
// handle identifier chain
if identifier != "" {
ret += " " + goExprIdent(identifier, casttype, current_attr)
}
// remove whitespace and format code
ret = strings.TrimSpace(ret)
formated, err := format.Source([]byte(ret))
if err != nil {
log.Println(ret, err)
return ret
}
return string(formated)
}