-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.go
260 lines (243 loc) · 7.35 KB
/
parse.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
package xjson
import (
"errors"
"strconv"
)
type status int
const (
StatusObjectKey status = 0x0002
StatusColon status = 0x0004 //;
StatusObjectValue status = 0x0008
StatusComma status = 0x0010 //,
StatusBeginObject status = 0x0020
StatusEndObject status = 0x0040
StatusBeginArray status = 0x0080
StatusEndArray status = 0x0100
StatusArrayValue status = 0x0200
)
func Parse(reader *TokenReader) (interface{}, error) {
s := &Stack{}
status := StatusBeginObject | StatusBeginArray
for {
tokenType := reader.Read()
switch tokenType.T {
case BeginObject:
if !includeTokenStatus(StatusBeginObject, status) {
return nil, errors.New("invalid '{'")
}
root := make(map[string]interface{})
stackValue := NewObjectValue(root)
s.Push(stackValue)
status = StatusObjectKey | StatusBeginObject | StatusEndObject
case String:
if includeTokenStatus(StatusObjectKey, status) {
stackValue := NewObjectKey(tokenType.Value)
s.Push(stackValue)
status = StatusColon
continue
}
if includeTokenStatus(StatusObjectValue, status) {
// ObjectValue 后跟的可能是 , 和 }
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = tokenType.Value
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
arrayValue := s.Peek().ArrayValuePoint()
*arrayValue = append(*arrayValue, tokenType.Value)
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid string '" + tokenType.Value + "'")
case Number:
// todo crossoverJie 优雅转为整形
if includeTokenStatus(StatusObjectValue, status) {
i, _ := strconv.Atoi(tokenType.Value)
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = i
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
i, _ := strconv.Atoi(tokenType.Value)
arrayValue := s.Peek().ArrayValuePoint()
//arrayValue := s.Pop().ArrayValue()
*arrayValue = append(*arrayValue, i)
//s.Push(NewArray(arrayValue))
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid int")
case Float:
// todo crossoverJie 优雅转为整形
if includeTokenStatus(StatusObjectValue, status) {
i, _ := strconv.ParseFloat(tokenType.Value, 64)
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = i
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
i, _ := strconv.ParseFloat(tokenType.Value, 64)
arrayValue := s.Peek().ArrayValuePoint()
*arrayValue = append(*arrayValue, i)
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid int")
case True:
value := tokenType.Value
if includeTokenStatus(StatusObjectValue, status) {
b, _ := strconv.ParseBool(value)
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = b
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
b, _ := strconv.ParseBool(value)
arrayValue := s.Peek().ArrayValuePoint()
*arrayValue = append(*arrayValue, b)
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid bool true")
case False:
value := tokenType.Value
if includeTokenStatus(StatusObjectValue, status) {
b, _ := strconv.ParseBool(value)
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = b
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
b, _ := strconv.ParseBool(value)
arrayValue := s.Peek().ArrayValuePoint()
*arrayValue = append(*arrayValue, b)
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid bool false")
case Null:
if includeTokenStatus(StatusObjectValue, status) {
objectKey := s.Pop().ObjectKeyValue()
rootMap := s.Peek().ObjectValue()
rootMap[objectKey] = ""
status = StatusComma | StatusEndObject
continue
}
if includeTokenStatus(StatusArrayValue, status) {
arrayValue := s.Peek().ArrayValuePoint()
*arrayValue = append(*arrayValue, "")
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("invalid null")
case SepComma:
//,
if !includeTokenStatus(StatusComma, status) {
return nil, errors.New("missing ':'")
} else {
// 逗号之前可能是 '}',下一个状态则是 StatusObjectKey
if includeTokenStatus(StatusEndObject, status) {
status = StatusObjectKey
continue
}
// 逗号之前可能是 ']',下一个状态则可能是
if includeTokenStatus(StatusEndArray, status) {
status = StatusArrayValue | StatusBeginArray | StatusBeginObject
continue
}
}
case SepColon:
//:
if !includeTokenStatus(StatusColon, status) {
return nil, errors.New("missing ','")
} else {
status = StatusObjectValue | StatusBeginObject | StatusBeginArray
}
case BeginArray:
if includeTokenStatus(StatusBeginArray, status) {
arr := make([]interface{}, 0)
stackValue := NewArrayPoint(&arr)
s.Push(stackValue)
status = StatusArrayValue | StatusBeginArray | StatusBeginObject | StatusEndArray
continue
}
case EndArray:
if !includeTokenStatus(StatusEndArray, status) {
return nil, errors.New("invalid ']'")
}
root := s.Pop().ArrayValuePoint()
if s.IsEmpty() {
array := NewArrayPoint(root)
s.Push(array)
continue
}
stackType := s.Peek().StackType()
// 如果栈顶还有一个 key
if stackType == ObjectKey {
keyValue := s.Pop().ObjectKeyValue()
objectValue := s.Peek().ObjectValue()
objectValue[keyValue] = root
status = StatusComma | StatusEndObject
continue
}
// 如果栈顶还有一个 [
if stackType == Array {
arrayValuePoint := s.Peek().ArrayValuePoint()
*arrayValuePoint = append(*arrayValuePoint, root)
status = StatusComma | StatusEndArray
continue
}
return nil, errors.New("miss ']'")
case EndObject:
if !includeTokenStatus(StatusEndObject, status) {
return nil, errors.New("invalid '}'")
}
root := s.Pop().ObjectValue()
if s.IsEmpty() {
value := NewObjectValue(root)
s.Push(value)
// 此时栈已经读完,表名所有 token 解析完毕
continue
}
stackType := s.Peek().StackType()
// 如果栈顶还有一个 key
// {"name":"cj", "age":{"a":"a","b":"b","c":true}, "e":"e"}
if stackType == ObjectKey {
keyValue := s.Pop().ObjectKeyValue()
objectValue := s.Peek().ObjectValue()
objectValue[keyValue] = root
// 下一个可能是 ',' 和 '}'
status = StatusComma | StatusEndObject
continue
}
// 如果栈顶还有一个 [
if stackType == Array {
arrayValuePoint := s.Peek().ArrayValuePoint()
*arrayValuePoint = append(*arrayValuePoint, root)
status = StatusComma | StatusEndArray
continue
}
case EndJson:
// token 读不到数据了,EOF
root := s.Pop().Raw()
if s.IsEmpty() {
return root, nil
} else {
return nil, errors.New("invalid EOF")
}
}
}
}
func includeTokenStatus(current, target status) bool {
return (current & target) > 0
}