-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonParser.go
More file actions
381 lines (356 loc) · 9.13 KB
/
jsonParser.go
File metadata and controls
381 lines (356 loc) · 9.13 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
)
type Token int
const (
WHITESPACE Token = iota // 0
LEFTCURLY // 1
RIGHTCURLY // 2
LEFTSQUARE // 3
RIGHTSQUARE // 4
COLON // 5
COMMA // 6
BOOL // 7
STRING // 8
INT // 9
)
type TokenList struct {
token Token
char string
}
var indentLevel = 0
var squareOne = 0
func scanFile(input []byte) []TokenList {
tokens := []TokenList{}
var strBuffer bytes.Buffer
var intBuffer bytes.Buffer
var boolBuffer bytes.Buffer
strBlocker := false
intBlocker := false
boolBlocker := false
previousByte := ""
for _, byte := range input {
str := string(byte)
// is int
if _, err := strconv.Atoi(str); err == nil {
if !strBlocker {
if intBuffer.Len() > 0 {
intBuffer.WriteString(str)
previousByte = str
} else {
intBuffer.WriteString(str)
previousByte = str
intBlocker = true
}
}
} else { // not int
if intBuffer.Len() > 0 {
// check if == . || e || E || + || -
if str == "." || str == "e" || str == "E" || str == "+" || str == "-" {
intBuffer.WriteString(str)
previousByte = str
} else {
tokens = append(tokens, TokenList{INT, intBuffer.String()})
intBuffer.Reset()
intBlocker = false
}
} else {
if !strBlocker && !intBlocker && !boolBlocker {
if str == "." || str == "e" || str == "E" || str == "+" || str == "-" {
intBuffer.WriteString(str)
previousByte = str
}
}
}
}
// check for bool
if str == "t" || str == "f" || str == "n" {
if !strBlocker && !intBlocker {
boolBuffer.WriteString(str)
previousByte = str
boolBlocker = true
}
} else if boolBlocker {
if boolBuffer.String() == "true" || boolBuffer.String() == "false" || boolBuffer.String() == "null" {
tokens = append(tokens, TokenList{BOOL, boolBuffer.String()})
boolBuffer.Reset()
boolBlocker = false
} else {
boolBuffer.WriteString(str)
previousByte = str
}
}
if !intBlocker && !boolBlocker {
switch str {
case " ", "\t", "\n":
if !strBlocker {
tokens = append(tokens, TokenList{WHITESPACE, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case "{":
if !strBlocker {
tokens = append(tokens, TokenList{LEFTCURLY, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case "}":
if !strBlocker {
tokens = append(tokens, TokenList{RIGHTCURLY, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case "[":
if !strBlocker {
tokens = append(tokens, TokenList{LEFTSQUARE, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case "]":
if !strBlocker {
tokens = append(tokens, TokenList{RIGHTSQUARE, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case ":":
if !strBlocker {
tokens = append(tokens, TokenList{COLON, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case ",":
if !strBlocker {
tokens = append(tokens, TokenList{COMMA, str})
} else {
strBuffer.WriteString(str)
previousByte = str
}
case "\"":
if strBuffer.Len() > 0 && previousByte != "\\" { // opening double quote already in buffer
strBuffer.WriteString(str)
tokens = append(tokens, TokenList{STRING, strBuffer.String()})
strBuffer.Reset()
strBlocker = false
} else {
strBuffer.WriteString(str)
previousByte = str
strBlocker = true
}
default:
if strBlocker && strBuffer.Len() > 0 {
strBuffer.WriteString(str)
previousByte = str
}
}
}
}
return tokens
}
func formatBracket(indent int, toIndent bool, wasSquare bool, square int, str string) {
var indentBuffer bytes.Buffer
for i := 0; i < indent; i++ {
indentBuffer.WriteString("\t")
}
if str == "{" {
if toIndent && indent == 0 {
fmt.Printf(indentBuffer.String() + "<span style='color:blue'>" + str + "</span>" + "\n")
indentBuffer.Reset()
} else if wasSquare {
if square == 0 {
fmt.Printf(indentBuffer.String() + "<span style='color:blue'>" + str + "</span>" + "\n")
indentBuffer.Reset()
} else {
fmt.Printf("\n" + indentBuffer.String() + "<span style='color:blue'>" + str + "</span>" + "\n")
indentBuffer.Reset()
}
} else {
fmt.Printf("\n\t" + indentBuffer.String() + "<span style='color:blue'>" + str + "</span>" + "\n")
indentLevel++
indentBuffer.Reset()
}
indentBuffer.Reset()
} else if str == "[" {
fmt.Printf("<span style='color:limegreen'>" + str + "</span>" + "\n")
indentBuffer.Reset()
} else if str == "}" {
fmt.Printf("\n" + indentBuffer.String() + "<span style='color:blue'>" + str + "</span>")
indentLevel--
indentBuffer.Reset()
} else if str == "]" {
fmt.Printf("\n" + indentBuffer.String() + "<span style='color:limegreen'>" + str + "</span>")
indentBuffer.Reset()
}
}
func formatString(indent int, toIndent bool, str string) {
var indentBuffer bytes.Buffer
var stringBuffer bytes.Buffer
var escapeBuffer bytes.Buffer
escapeBlocker := false
unicodeBlocker := false
unicodeCounter := 0
for i := 0; i < indent; i++ {
indentBuffer.WriteString("\t")
}
for _, char := range str {
if escapeBlocker {
if string(char) == "u" {
escapeBuffer.WriteString(string(char))
unicodeBlocker = true
unicodeCounter = unicodeCounter + 2
} else {
if unicodeBlocker {
if unicodeCounter == 5 {
escapeBuffer.WriteString(string(char) + "</span>")
stringBuffer.WriteString(escapeBuffer.String())
escapeBuffer.Reset()
escapeBlocker = false
unicodeBlocker = false
unicodeCounter = 0
} else {
escapeBuffer.WriteString(string(char))
unicodeCounter++
}
} else {
escapeBuffer.WriteString(string(char) + "</span>")
stringBuffer.WriteString(escapeBuffer.String())
escapeBuffer.Reset()
escapeBlocker = false
}
}
} else {
switch string(char) {
case "\\":
escapeBuffer.WriteString("<span style='color:black'>" + string(char))
escapeBlocker = true
case "<":
stringBuffer.WriteString("<")
case ">":
stringBuffer.WriteString(">")
case "&":
stringBuffer.WriteString("&")
case "\"":
stringBuffer.WriteString(""")
case "'":
stringBuffer.WriteString("'")
default:
stringBuffer.WriteString(string(char))
}
}
}
if toIndent {
fmt.Printf("%s", indentBuffer.String()+"<span style='color:red'>"+stringBuffer.String()+"</span>")
stringBuffer.Reset()
} else {
fmt.Printf("%s", "<span style='color:red'>"+stringBuffer.String()+"</span>")
stringBuffer.Reset()
}
}
func formatInt(indent int, toIndent bool, str string) {
var indentBuffer bytes.Buffer
for i := 0; i < indent; i++ {
indentBuffer.WriteString("\t")
}
if toIndent {
fmt.Printf(indentBuffer.String() + "<span style='color:mediumpurple'>" + str + "</span>")
} else {
fmt.Printf("<span style='color:mediumpurple'>" + str + "</span>")
}
}
func formatBool(indent int, toIndent bool, str string) {
var indentBuffer bytes.Buffer
for i := 0; i < indent; i++ {
indentBuffer.WriteString("\t")
}
if toIndent {
fmt.Printf(indentBuffer.String() + "<span style='color:cyan'>" + str + "</span>")
} else {
fmt.Printf("<span style='color:cyan'>" + str + "</span>")
}
}
func formatFile(tokens []TokenList) {
toIndent := false
wasSquare := false
if len(tokens) < 3 { // input is {}
fmt.Printf("{\n}")
} else {
for _, token := range tokens {
switch token.token {
case 0:
// ignore all whitespace
case 1:
// Left curly bracket
toIndent = true
formatBracket(indentLevel, toIndent, wasSquare, squareOne, token.char)
squareOne++
indentLevel++
case 2:
// Right curly bracket
indentLevel--
toIndent = false
formatBracket(indentLevel, toIndent, wasSquare, squareOne, token.char)
if wasSquare {
indentLevel++
}
case 3:
// Left square bracket
toIndent = true
wasSquare = true
squareOne = 0
formatBracket(indentLevel, toIndent, wasSquare, squareOne, token.char)
indentLevel++
case 4:
// Right square bracket
indentLevel--
toIndent = false
wasSquare = false
squareOne = 0
formatBracket(indentLevel, toIndent, wasSquare, squareOne, token.char)
case 5:
// Colon
toIndent = false
fmt.Printf("<span style='color:darkgray'>:</span> ")
case 6:
// Comma
toIndent = true
fmt.Printf("<span style='color:darkorange'>,</span> \n")
case 7:
// Bool
formatBool(indentLevel, toIndent, token.char)
case 8:
// String
formatString(indentLevel, toIndent, token.char)
case 9:
// Int
formatInt(indentLevel, toIndent, token.char)
}
}
}
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Filename not provided.")
}
content, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
} else {
fmt.Printf("<span style='font-family:monospace; white-space:pre'>\n")
// Tokenize
tokenArr := scanFile(content)
// Format
formatFile(tokenArr)
fmt.Printf("</span>")
}
}