forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsontokenizer.go
488 lines (428 loc) · 9.76 KB
/
jsontokenizer.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package gojsonsm
import (
"errors"
"fmt"
)
type tokenType int
const (
tknUnknown tokenType = iota
tknObjectStart
tknObjectEnd
tknObjectKeyDelim
tknArrayStart
tknArrayEnd
tknListDelim
tknString
tknEscString
tknInteger
tknNumber
tknNull
tknTrue
tknFalse
tknEnd
)
func isLiteralToken(token tokenType) bool {
return token >= tknString && token <= tknFalse
}
func tokenToText(token tokenType) string {
switch token {
case tknUnknown:
return "unknown"
case tknObjectStart:
return "object_start"
case tknObjectEnd:
return "object_end"
case tknObjectKeyDelim:
return "object_key_delim"
case tknArrayStart:
return "array_start"
case tknArrayEnd:
return "array_end"
case tknListDelim:
return "list_delim"
case tknString:
return "string"
case tknEscString:
return "escaped_string"
case tknInteger:
return "integer"
case tknNumber:
return "number"
case tknNull:
return "null"
case tknTrue:
return "true"
case tknFalse:
return "false"
case tknEnd:
return "end"
}
return "??ERROR??"
}
type tokenizerState int
const (
toksBeginValueOrEmpty tokenizerState = iota
toksBeginValue
toksBeginStringOrEmpty
toksBeginString
toksInString
toksInStringEsc
toksInStringEscU
toksInStringEscU1
toksInStringEscU12
toksInStringEscU123
toksNeg
toks0
toksT
toksTr
toksTru
toksF
toksFa
toksFal
toksFals
toksN
toksNu
toksNul
toks1
toksDot
toksDot0
toksE
toksESign
toksE0
)
func tokIsSpaceChar(c byte) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'
}
type jsonTokenizer struct {
data []byte
dataLen int
pos int
}
func (tkn *jsonTokenizer) Reset(data []byte) {
tkn.data = data
tkn.dataLen = len(data)
tkn.pos = 0
}
func (tkn *jsonTokenizer) Position() int {
return tkn.pos
}
func (tkn *jsonTokenizer) Seek(pos int) {
tkn.pos = pos
}
func (tkn *jsonTokenizer) Step() (tokenType, []byte, int, error) {
// Bring everying local for optimization purposes
dataSlice := tkn.data
dataLen := tkn.dataLen
dataPos := tkn.pos
// Check that we aren't out of bounds...
if dataPos >= dataLen {
return tknEnd, nil, 0, nil
}
// Keep track of where we started, so we can return the tokens data
startPos := dataPos
tokenType := tknUnknown
state := toksBeginValue
strHasEscapes := false
numberIsNonInteger := false
var c byte
DataLoop:
for {
if dataPos >= dataLen {
// Due to the fact that numbers just kind of... end... during JSON parsing
// we need some special logic to handle the cases where numbers are involved
// and they are the only input to the parser...
switch state {
case toks1, toks0, toksDot0, toksE0:
tokenType = tknNumber
break DataLoop
case toksBeginValue:
tokenType = tknEnd
break DataLoop
default:
// We couldn't have expected this... time to fail :/
return tknUnknown, nil, 0, errors.New("unexpected end of input")
}
}
c = dataSlice[dataPos]
dataPos++
switch state {
case toksBeginValueOrEmpty:
if c <= ' ' && tokIsSpaceChar(c) {
startPos = dataPos
continue DataLoop
}
if c == ']' {
tokenType = tknArrayEnd
break DataLoop
}
fallthrough
case toksBeginValue:
if c <= ' ' && tokIsSpaceChar(c) {
startPos = dataPos
continue DataLoop
}
switch c {
case '{':
tokenType = tknObjectStart
break DataLoop
case '}':
tokenType = tknObjectEnd
break DataLoop
case ':':
tokenType = tknObjectKeyDelim
break DataLoop
case '[':
tokenType = tknArrayStart
break DataLoop
case ']':
tokenType = tknArrayEnd
break DataLoop
case ',':
tokenType = tknListDelim
break DataLoop
case '"':
state = toksInString
continue DataLoop
case '-':
state = toksNeg
continue DataLoop
case '0': // beginning of 0.123
state = toks0
continue DataLoop
case 't': // beginning of true
state = toksT
continue DataLoop
case 'f': // beginning of false
state = toksF
continue DataLoop
case 'n': // beginning of null
state = toksN
continue DataLoop
default:
if '1' <= c && c <= '9' { // beginning of 1234.5
state = toks1
continue DataLoop
}
return tknUnknown, nil, 0, fmt.Errorf("looking for beginning of value but found `%c`", c)
}
case toksBeginStringOrEmpty:
if c <= ' ' && tokIsSpaceChar(c) {
startPos = dataPos
continue DataLoop
}
if c == '}' {
tokenType = tknObjectEnd
break DataLoop
}
fallthrough
case toksBeginString:
if c <= ' ' && tokIsSpaceChar(c) {
startPos = dataPos
continue DataLoop
}
if c == '"' {
state = toksInString
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("looking for beginning of object key string")
case toksInString:
if c == '"' {
tokenType = tknEscString
break DataLoop
}
if c == '\\' {
state = toksInStringEsc
continue DataLoop
}
if c < 0x20 {
return tknUnknown, nil, 0, errors.New("in string literal")
}
// continue with current state
continue DataLoop
case toksInStringEsc:
strHasEscapes = true
switch c {
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
state = toksInString
continue DataLoop
case 'u':
state = toksInStringEscU
continue DataLoop
default:
return tknUnknown, nil, 0, errors.New("in string escape code")
}
case toksInStringEscU:
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
state = toksInStringEscU1
continue DataLoop
}
// numbers
return tknUnknown, nil, 0, errors.New("in \\u hexadecimal character escape")
case toksInStringEscU1:
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
state = toksInStringEscU12
continue DataLoop
}
// numbers
return tknUnknown, nil, 0, errors.New("in \\u hexadecimal character escape")
case toksInStringEscU12:
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
state = toksInStringEscU123
continue DataLoop
}
// numbers
return tknUnknown, nil, 0, errors.New("in \\u hexadecimal character escape")
case toksInStringEscU123:
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
state = toksInString
continue DataLoop
}
// numbers
return tknUnknown, nil, 0, errors.New("in \\u hexadecimal character escape")
case toksNeg:
if c == '0' {
state = toks0
continue DataLoop
}
if '1' <= c && c <= '9' {
state = toks1
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in numeric literal")
case toks1:
if '0' <= c && c <= '9' {
state = toks1
continue DataLoop
}
fallthrough
case toks0:
if c == '.' {
state = toksDot
continue DataLoop
}
if c == 'e' || c == 'E' {
state = toksE
continue DataLoop
}
// need to rewind one character since this was non-numeric
dataPos--
tokenType = tknNumber
break DataLoop
case toksDot:
numberIsNonInteger = true
if '0' <= c && c <= '9' {
state = toksDot0
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("after decimal point in numeric literal")
case toksDot0:
if '0' <= c && c <= '9' {
state = toksDot0
continue DataLoop
}
if c == 'e' || c == 'E' {
state = toksE
continue DataLoop
}
// need to rewind one character since this was non-numeric
dataPos--
tokenType = tknNumber
break DataLoop
case toksE:
numberIsNonInteger = true
if c == '+' || c == '-' {
state = toksESign
continue DataLoop
}
fallthrough
case toksESign:
if '0' <= c && c <= '9' {
state = toksE0
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in exponent of numeric literal")
case toksE0:
if '0' <= c && c <= '9' {
// continue parsing numbers...
continue DataLoop
}
// need to rewind one character since this was non-numeric
dataPos--
tokenType = tknNumber
break DataLoop
case toksT:
if c == 'r' {
state = toksTr
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal true (expecting 'r')")
case toksTr:
if c == 'u' {
state = toksTru
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal true (expecting 'u')")
case toksTru:
if c == 'e' {
tokenType = tknTrue
break DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal true (expecting 'e')")
case toksF:
if c == 'a' {
state = toksFa
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal false (expecting 'a')")
case toksFa:
if c == 'l' {
state = toksFal
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal false (expecting 'l')")
case toksFal:
if c == 's' {
state = toksFals
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal false (expecting 's')")
case toksFals:
if c == 'e' {
tokenType = tknFalse
break DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal false (expecting 'e')")
case toksN:
if c == 'u' {
state = toksNu
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal null (expecting 'u')")
case toksNu:
if c == 'l' {
state = toksNul
continue DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal null (expecting 'l')")
case toksNul:
if c == 'l' {
tokenType = tknNull
break DataLoop
}
return tknUnknown, nil, 0, errors.New("in literal null (expecting 'l')")
}
}
// Enhance some of the tokens with additional information
if tokenType == tknNumber && !numberIsNonInteger {
tokenType = tknInteger
}
if tokenType == tknEscString && !strHasEscapes {
tokenType = tknString
}
endPos := dataPos
tokenData := tkn.data[startPos:endPos]
tokenDataLen := endPos - startPos
// Update the scanners state
tkn.pos = dataPos
return tokenType, tokenData, tokenDataLen, nil
}