-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
442 lines (424 loc) · 9.99 KB
/
parser.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
package zord
import (
"errors"
"fmt"
"io"
"github.com/7fffffff/jsonconv"
)
var (
errMaxDepth = errors.New("exceeded max depth")
)
type kv struct {
keyUnquoted string
keyBytes []byte // double-quoted literal
valueBytes []byte // literal form (quoted/with brackets/etc)
}
// parser isn't a fully fledged JSON parser. It's only concerned about parsing
// JSON objects, and even then, only about finding the positions of the top
// level key-value pairs within.
type parser struct {
MaxDepth int // maximum nesting depth. If 0, defaultMaxDepth is used
}
func (p *parser) depthLimitReached(depth int) bool {
maxDepth := p.MaxDepth
if maxDepth == 0 {
maxDepth = defaultMaxDepth
}
return depth >= maxDepth || depth < 0
}
// parse expects buf to contain a valid utf-8 encoded JSON object and
// extracts the top level key-value pairs in the order they appear. parse
// does not deduplicate keys.
//
// parse returns the key-value pairs and the number of bytes read from buf
func (p *parser) parse(buf []byte) (pairs []kv, n int, err error) {
pairs = make([]kv, 0, 16)
n = skipWhitespace(buf, 0)
if n >= len(buf) {
return pairs, len(buf), parseErrorAt(n, fmt.Errorf("parse: %w", io.ErrUnexpectedEOF))
}
if b := buf[n]; b != '{' {
return pairs, n + 1, parseErrorAt(n, fmt.Errorf("parse: unexpected: 0x%X", b))
}
n++
for {
pair := kv{}
n = skipWhitespace(buf, n)
if n >= len(buf) {
return pairs, len(buf), parseErrorAt(n, fmt.Errorf("parse: %w", io.ErrUnexpectedEOF))
}
b := buf[n]
if b == '}' {
return pairs, n + 1, nil
}
if len(pairs) > 0 {
if b != ',' {
return pairs, n + 1, parseErrorAt(n, fmt.Errorf("parse comma: unexpected 0x%X", b))
}
n++
n = skipWhitespace(buf, n)
}
keyStart := n
n, err = p.parseString(buf, keyStart)
if err != nil {
return pairs, n, err
}
pair.keyBytes = buf[keyStart:n]
if keyString, ok := jsonconv.Unquote(pair.keyBytes); ok {
pair.keyUnquoted = keyString
} else {
return pairs, n, parseErrorAt(keyStart, fmt.Errorf("parse: could not unquote key [%d:%d]", keyStart, n))
}
n = skipWhitespace(buf, n)
if n >= len(buf) {
return pairs, len(buf), parseErrorAt(n, fmt.Errorf("parse colon: %w", io.ErrUnexpectedEOF))
}
b = buf[n]
if b != ':' {
return pairs, n + 1, parseErrorAt(n, fmt.Errorf("parse colon: unexpected 0x%X", b))
}
n++
valueStart := skipWhitespace(buf, n)
n, err = p.parseValue(0, buf, valueStart)
if err != nil {
return pairs, n, err
}
pair.valueBytes = buf[valueStart:n]
pairs = append(pairs, pair)
}
}
func (p *parser) parseArray(depth int, buf []byte, initialPos int) (end int, err error) {
i := initialPos
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("array: %w", io.ErrUnexpectedEOF))
}
if p.depthLimitReached(depth) {
return i + 1, parseErrorAt(i, fmt.Errorf("array: %w", errMaxDepth))
}
b := buf[i]
if b != '[' {
return i + 1, parseErrorAt(i, fmt.Errorf("array: unexpected 0x%X", b))
}
i++
numValues := 0
for {
i = skipWhitespace(buf, i)
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("array: %w", io.ErrUnexpectedEOF))
}
b = buf[i]
if b == ']' {
return i + 1, nil
}
if numValues > 0 {
if b != ',' {
return i + 1, parseErrorAt(i, fmt.Errorf("array comma: unexpected 0x%X", b))
}
i++
i = skipWhitespace(buf, i)
}
valueEnd, err := p.parseValue(depth, buf, i)
if err != nil {
return valueEnd, err
}
i = valueEnd
numValues++
}
}
func (p *parser) parseFalse(buf []byte, initialPos int) (end int, err error) {
raw := [5]byte{'f', 'a', 'l', 's', 'e'}
for r := 0; r < len(raw); r++ {
i := initialPos + r
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("false: %w", io.ErrUnexpectedEOF))
}
b := buf[i]
if b != raw[r] {
return i + 1, parseErrorAt(initialPos+r, fmt.Errorf("false: unexpected 0x%X", b))
}
}
return initialPos + len(raw), nil
}
func (p *parser) parseNull(buf []byte, initialPos int) (end int, err error) {
raw := [4]byte{'n', 'u', 'l', 'l'}
for r := 0; r < len(raw); r++ {
i := initialPos + r
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("null: %w", io.ErrUnexpectedEOF))
}
b := buf[i]
if b != raw[r] {
return i + 1, parseErrorAt(initialPos+r, fmt.Errorf("null: unexpected 0x%X", b))
}
}
return initialPos + len(raw), nil
}
func (p *parser) parseNumber(buf []byte, initialPos int) (end int, err error) {
i := initialPos
// NumberMinus
if i < len(buf) && buf[i] == '-' {
i++
}
// NumberInt
leadingZero := false
digits := 0
if i < len(buf) && buf[i] == '0' {
leadingZero = true
digits++
i++
}
for i < len(buf) {
b := buf[i]
if !leadingZero && '0' <= b && b <= '9' {
digits++
i++
continue
}
if digits > 0 {
switch b {
case '.':
i++
goto NumberFrac
case 'e', 'E':
i++
goto NumberExp
}
}
break
}
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("number int: %w", io.ErrUnexpectedEOF))
}
if digits == 0 {
return i + 1, parseErrorAt(i, fmt.Errorf("number int: unexpected 0x%X", buf[i]))
}
return i, nil
NumberFrac:
digits = 0
for i < len(buf) {
b := buf[i]
if '0' <= b && b <= '9' {
digits++
i++
continue
}
if digits > 0 && (b == 'e' || b == 'E') {
i++
goto NumberExp
}
break
}
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("number frac: %w", io.ErrUnexpectedEOF))
}
if digits == 0 {
return i + 1, parseErrorAt(i, fmt.Errorf("number frac: unexpected 0x%X", buf[i]))
}
return i, nil
NumberExp:
// optional leading + or -
if i < len(buf) && (buf[i] == '+' || buf[i] == '-') {
i++
}
digits = 0
for i < len(buf) {
b := buf[i]
if '0' <= b && b <= '9' {
digits++
i++
continue
}
break
}
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("number exp: %w", io.ErrUnexpectedEOF))
}
if digits == 0 {
return i + 1, parseErrorAt(i, fmt.Errorf("number exp: unexpected 0x%X", buf[i]))
}
return i, nil
}
func (p *parser) parseObject(depth int, buf []byte, initialPos int) (end int, err error) {
i := initialPos
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("object: %w", io.ErrUnexpectedEOF))
}
if p.depthLimitReached(depth) {
return i + 1, parseErrorAt(i, fmt.Errorf("object: %w", errMaxDepth))
}
b := buf[i]
if b != '{' {
return i + 1, parseErrorAt(i, fmt.Errorf("object: unexpected 0x%X", b))
}
i++
numPairs := 0
for {
i = skipWhitespace(buf, i)
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("object: %w", io.ErrUnexpectedEOF))
}
b = buf[i]
if b == '}' {
return i + 1, nil
}
if numPairs > 0 {
if b != ',' {
return i + 1, parseErrorAt(i, fmt.Errorf("object comma: unexpected 0x%X", b))
}
i++
i = skipWhitespace(buf, i)
}
keyEnd, err := p.parseString(buf, i)
if err != nil {
return keyEnd, err
}
i = skipWhitespace(buf, keyEnd)
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("object colon: %w", io.ErrUnexpectedEOF))
}
b = buf[i]
if b != ':' {
return i + 1, parseErrorAt(i, fmt.Errorf("object colon: unexpected 0x%X", b))
}
i++
i = skipWhitespace(buf, i)
valueEnd, err := p.parseValue(depth, buf, i)
if err != nil {
return valueEnd, fmt.Errorf("object value: %w", err)
}
i = valueEnd
numPairs++
}
}
func (p *parser) parseString(buf []byte, initialPos int) (end int, err error) {
i := initialPos
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("string: %w", io.ErrUnexpectedEOF))
}
b := buf[i]
if b != '"' {
return i + 1, parseErrorAt(i, fmt.Errorf("string: unexpected 0x%X", b))
}
i++
escapeNext := false
for i < len(buf) {
b = buf[i]
if escapeNext {
escapeNext = false
switch b {
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
i++
case 'u':
i++
h := 0
for i < len(buf) {
b = buf[i]
if !hexDigits[b] {
return i + 1, parseErrorAt(i, fmt.Errorf("string: unexpected 0x%X", b))
}
h++
i++
if h == 4 {
break
}
}
default:
return i + 1, parseErrorAt(i, fmt.Errorf("string: unexpected 0x%X", b))
}
continue
}
switch b {
case '"':
return i + 1, nil
case '\\':
escapeNext = true
default:
if b < ' ' {
return i + 1, parseErrorAt(i, fmt.Errorf("string: unexpected 0x%X", b))
}
}
i++
}
return len(buf), parseErrorAt(i, fmt.Errorf("string: %w", io.ErrUnexpectedEOF))
}
func (p *parser) parseTrue(buf []byte, initialPos int) (end int, err error) {
raw := [4]byte{'t', 'r', 'u', 'e'}
for r := 0; r < len(raw); r++ {
i := initialPos + r
if i >= len(buf) {
return len(buf), parseErrorAt(i, fmt.Errorf("true: %w", io.ErrUnexpectedEOF))
}
b := buf[i]
if b != raw[r] {
return i + 1, parseErrorAt(initialPos+r, fmt.Errorf("true: unexpected 0x%X", b))
}
}
return initialPos + len(raw), nil
}
func (p *parser) parseValue(depth int, buf []byte, initialPos int) (end int, err error) {
i := initialPos
if i >= len(buf) {
return len(buf), parseErrorAt(len(buf), io.ErrUnexpectedEOF)
}
b := buf[i]
switch {
case b == '"':
return p.parseString(buf, i)
case b == '-' || ('0' <= b && b <= '9'):
return p.parseNumber(buf, i)
case b == 't':
return p.parseTrue(buf, i)
case b == 'f':
return p.parseFalse(buf, i)
case b == 'n':
return p.parseNull(buf, i)
case b == '[':
return p.parseArray(depth+1, buf, i)
case b == '{':
return p.parseObject(depth+1, buf, i)
default:
return i + 1, parseErrorAt(i, fmt.Errorf("value: unexpected: 0x%X", b))
}
}
// the fastest method
// https://dave.cheney.net/high-performance-json.html
var whitespace = [256]bool{
' ': true,
'\t': true,
'\n': true,
'\r': true,
}
func skipWhitespace(buf []byte, initialPos int) (pos int) {
pos = initialPos
for pos < len(buf) {
if c := buf[pos]; !whitespace[c] {
break
}
pos++
}
return pos
}
var hexDigits = [256]bool{
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
}