-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_token.go
354 lines (340 loc) · 6.79 KB
/
json_token.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package xjson
import "errors"
type Token string
const (
Init Token = "Init"
BeginObject = "BeginObject"
EndObject = "EndObject"
BeginArray = "BeginArray"
EndArray = "EndArray"
Null = "Null"
Null1 = "Null1"
Null2 = "Null2"
Null3 = "Null3"
Number = "Number"
Float = "Float"
BeginString = "BeginString"
EndString = "EndString"
Escape = "Escape"
String = "String"
True = "True"
True1 = "True1"
True2 = "True2"
True3 = "True3"
False = "False"
False1 = "False1"
False2 = "False2"
False3 = "False3"
False4 = "False4"
// SepColon :
SepColon = "SepColon"
// SepComma ,
SepComma = "SepComma"
EndJson = "EndJson"
)
type TokenType struct {
T Token
Value string
}
func Tokenize(str string) ([]*TokenType, error) {
//bytes := []byte(str)
var result []*TokenType
var values []byte
status := Init
for i := 0; i < len(str); i++ {
b := str[i]
switch status {
case Init:
status, values = InitStatus(b, values)
break
case BeginObject:
t := &TokenType{
T: BeginObject,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
case EndObject:
t := &TokenType{
T: EndObject,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
case BeginString:
if b == '"' && str[i-1] != '\\' {
//values = append(values, b)
status = EndString
} else if b == '\\' {
// skip escape
continue
} else {
values = append(values, b)
}
case EndString:
t := &TokenType{
T: String,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case Number:
if b == '.' {
values = append(values, b)
status = Float
break
}
if isDigit(b) {
values = append(values, b)
} else {
t := &TokenType{
T: Number,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
}
case Float:
if b == '.' {
return nil, errors.New("invalid float")
}
if isDigit(b) {
values = append(values, b)
} else {
t := &TokenType{
T: Float,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
}
case SepColon:
t := &TokenType{
T: SepColon,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case SepComma:
t := &TokenType{
T: SepComma,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case BeginArray:
t := &TokenType{
T: BeginArray,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case EndArray:
t := &TokenType{
T: EndArray,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case True1:
if b == 'r' {
values = append(values, b)
status = True2
break
} else {
return nil, errors.New("invalid bool true")
}
case True2:
if b == 'u' {
values = append(values, b)
status = True3
break
} else {
return nil, errors.New("invalid bool true")
}
case True3:
if b == 'e' {
values = append(values, b)
status = True
break
} else {
return nil, errors.New("invalid bool true")
}
case True:
t := &TokenType{
T: True,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case Null1:
if b == 'u' {
values = append(values, b)
status = Null2
break
} else {
return nil, errors.New("invalid null")
}
case Null2:
if b == 'l' {
values = append(values, b)
status = Null3
break
} else {
return nil, errors.New("invalid null")
}
case Null3:
if b == 'l' {
values = append(values, b)
status = Null
break
} else {
return nil, errors.New("invalid null")
}
case Null:
t := &TokenType{
T: Null,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
case False1:
if b == 'a' {
values = append(values, b)
status = False2
break
} else {
return nil, errors.New("invalid bool false")
}
case False2:
if b == 'l' {
values = append(values, b)
status = False3
break
} else {
return nil, errors.New("invalid bool false")
}
case False3:
if b == 's' {
values = append(values, b)
status = False4
break
} else {
return nil, errors.New("invalid bool false")
}
case False4:
if b == 'e' {
values = append(values, b)
status = False
break
} else {
return nil, errors.New("invalid bool false")
}
case False:
t := &TokenType{
T: False,
Value: string(values),
}
result = append(result, t)
values = nil
status, values = InitStatus(b, values)
break
}
}
// 解析最后一个
if len(values) > 0 {
t := &TokenType{
T: status,
Value: string(values),
}
result = append(result, t)
}
return result, nil
}
func InitStatus(b byte, values []byte) (Token, []byte) {
if b == '{' {
values = append(values, b)
return BeginObject, values
}
if b == '}' {
values = append(values, b)
return EndObject, values
}
if b == '"' {
//values = append(values, b)
return BeginString, values
}
if b == ':' {
values = append(values, b)
return SepColon, values
}
if b == ',' {
values = append(values, b)
return SepComma, values
}
if b == '[' {
values = append(values, b)
return BeginArray, values
}
if b == ']' {
values = append(values, b)
return EndArray, values
}
if isDigit(b) {
values = append(values, b)
return Number, values
}
if b == 't' {
values = append(values, b)
return True1, values
}
if b == 'f' {
values = append(values, b)
return False1, values
}
if b == 'n' {
values = append(values, b)
return Null1, values
}
return Init, values
}
func isDigit(b byte) bool {
return b >= 48 && b <= 57
}
type TokenReader struct {
tokens []*TokenType
pos uint64
}
func NewTokenReader(tokens []*TokenType) *TokenReader {
return &TokenReader{tokens: tokens, pos: 0}
}
func (t *TokenReader) Read() *TokenType {
if int(t.pos) >= len(t.tokens) {
return &TokenType{
T: EndJson,
}
}
tokenType := t.tokens[t.pos]
t.pos += 1
return tokenType
}