-
Notifications
You must be signed in to change notification settings - Fork 6
/
resultparser.go
233 lines (191 loc) · 4.91 KB
/
resultparser.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
// Copyright 2013 Chris McGee <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gdblib
import (
//"fmt"
)
type gdbResultNode struct {
children []gdbResultNode
key string
value interface{}
nodeType string
}
func createObjectNode(input string) (gdbResultNode, int) {
// fmt.Printf("OBJECT\n")
node := gdbResultNode{nodeType: "object"}
i := 1
for ; i < len(input); i++ {
c := input[i]
if c == '}' {
break
} else if c == ',' {
// skip and continue to the next key value node
} else if c == ' ' || c == '\t' || c == '\r' || c == '\n' {
// Whitespace, skip and proceed to the next character
} else {
value, size := createKeyValueNode(input[i:])
i = i + size - 1
node.children = append(node.children, value)
}
}
// fmt.Printf("OBJECT : %v %v\n", input[:i+1], i+1)
return node, i + 1
}
func createStringNode(input string) (string, int) {
// fmt.Printf("STRING\n")
i := 1
buffer := `"`
for ; i < len(input); i++ {
c := input[i]
// This is the beginning of a C string escape sequence
// Figure out if there is a way to map it. Otherwise, skip
// over it.
if c == '\\' && i < len(input)-1 {
c2 := input[i+1]
switch {
case c2 == 'n':
// Newline, leave as-is
case c2 >= '0' && c2 <= '9':
// Octal sequence (skip the slash and provide the octal numbers as-is for now)
continue
case c2 == 'x':
// Hex sequence (skip the slash and provide the hex numbers as-is for now)
continue
case c2 == 'r':
// Carriage return, skip
i = i + 1
continue
case c2 == 't':
// Tab, skip
i = i + 1
continue
case c2 == '\\':
// Double-backslash, pass the escaped backslash on to the interpretation
buffer = buffer + `\\`
i = i + 1
continue
default:
}
}
buffer = buffer + string(c)
if c == '"' && (i <= 1 || input[i-1] != '\\') {
break
}
}
// fmt.Printf("STRING : %v %v\n", input[:i+1], i+1)
return buffer, i + 1
}
func createArrayNode(input string) (gdbResultNode, int) {
// fmt.Printf("ARRAY\n")
node := gdbResultNode{nodeType: "array"}
i := 1
for ; i < len(input); i++ {
c := input[i]
if c == '"' {
str, size := createStringNode(input[i:])
i = i + size - 1
childNode := gdbResultNode{nodeType: "keyvalue"}
childNode.value = str
node.children = append(node.children, childNode)
} else if c == '{' {
objectNode, size := createObjectNode(input[i:])
i = i + size - 1
childNode := gdbResultNode{nodeType: "keyvalue"}
childNode.value = objectNode
node.children = append(node.children, childNode)
} else if c == '\t' || c == '\n' || c == ' ' || c == '\r' {
// Whitespace, ignore it
} else if c == ',' {
// A new array element is beginning
} else if c == ']' {
break
} else {
valueNode, size := createKeyValueNode(input[i:])
valueNode.key = ""
i = i + size - 1
childNode := gdbResultNode{nodeType: "keyvalue"}
childNode.value = valueNode
node.children = append(node.children, childNode)
}
}
// fmt.Printf("ARRAY : %v %v\n", input[:i+1], i+1)
return node, i + 1
}
func createKeyValueNode(input string) (gdbResultNode, int) {
// fmt.Printf("KEYVALUE\n")
node := gdbResultNode{nodeType: "keyvalue"}
buffer := ""
i := 0
for ; i < len(input); i++ {
c := input[i]
if c == '=' {
node.key = buffer
buffer = ""
} else if c == '{' {
// Beginning of an object
objectNode, size := createObjectNode(input[i:])
i = i + size - 1
node.value = objectNode
break
} else if c == '[' {
// Beginning of an array
arrayNode, size := createArrayNode(input[i:])
i = i + size - 1
node.value = arrayNode
break
} else if c == '"' {
// Beginning of a string
str, size := createStringNode(input[i:])
i = i + size - 1
node.value = str
break
} else if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
// Whitespace, ignore it
} else {
buffer = buffer + string(c)
}
}
// if buffer != "" {
// node.value = buffer
// }
// fmt.Printf("KEYVALUE : %v %v\n", input[:i+1], i+1)
return node, i + 1
}
func (node *gdbResultNode) toJSON() string {
buffer := ""
if node.nodeType == "array" {
buffer = buffer + "["
for idx, child := range node.children {
if idx > 0 {
buffer = buffer + ","
}
buffer = buffer + child.toJSON()
}
buffer = buffer + "]"
} else if node.nodeType == "object" {
buffer = buffer + "{"
for idx, child := range node.children {
if idx > 0 {
buffer = buffer + ","
}
buffer = buffer + child.toJSON()
}
buffer = buffer + "}"
} else if node.nodeType == "keyvalue" {
key := node.key
if key != "" {
buffer = buffer + "\"" + key + "\":"
}
stringValue, ok := node.value.(string)
if ok {
buffer = buffer + stringValue
} else {
valueNode, ok := node.value.(gdbResultNode)
if ok {
buffer = buffer + valueNode.toJSON()
}
}
}
return buffer
}