-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
544 lines (507 loc) · 11.3 KB
/
parser.go
File metadata and controls
544 lines (507 loc) · 11.3 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package json
import (
"github.com/tinywasm/fmt"
)
type parser struct {
data []byte
pos int
}
func (p *parser) skipWhitespace() {
for p.pos < len(p.data) {
c := p.data[p.pos]
if c == ' ' || c == '\t' || c == '\r' || c == '\n' {
p.pos++
} else {
break
}
}
}
func (p *parser) peek() byte {
if p.pos < len(p.data) {
return p.data[p.pos]
}
return 0
}
func (p *parser) next() byte {
if p.pos < len(p.data) {
c := p.data[p.pos]
p.pos++
return c
}
return 0
}
// skipString consumes a JSON string without allocation.
// The opening '"' must already be consumed.
func (p *parser) skipString() error {
for p.pos < len(p.data) {
c := p.data[p.pos]
if c == '"' {
p.pos++
return nil
}
if c == '\\' {
p.pos++
if p.pos >= len(p.data) {
return fmt.Err("json", "decode", "unexpected EOF")
}
esc := p.data[p.pos]
p.pos++
switch esc {
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
// Valid
case 'u':
if p.pos+4 > len(p.data) {
return fmt.Err("json", "decode", "invalid unicode escape")
}
p.pos += 4
default:
return fmt.Err("json", "decode", "invalid escape sequence")
}
continue
}
p.pos++
}
return fmt.Err("json", "decode", "unexpected EOF")
}
// skipValue consumes a JSON value without allocating or returning it.
// Used to discard unknown fields and unresolvable struct pointers.
func (p *parser) skipValue() error {
p.skipWhitespace()
c := p.next()
switch c {
case '"':
return p.skipString()
case '{':
return p.skipObject()
case '[':
return p.skipArray()
case 't', 'f':
p.pos--
_, err := p.parseBool()
return err
case 'n':
p.pos--
return p.parseNull()
default:
if (c >= '0' && c <= '9') || c == '-' {
p.pos--
return p.skipNumber()
}
return fmt.Err("json", "decode", "unexpected character")
}
}
// skipObject consumes a JSON object without allocating map or keys.
func (p *parser) skipObject() error {
p.skipWhitespace()
if p.peek() == '}' {
p.next()
return nil
}
for {
p.skipWhitespace()
if p.next() != '"' {
return fmt.Err("json", "decode", "expected quote")
}
if err := p.skipString(); err != nil {
return err
}
p.skipWhitespace()
if p.next() != ':' {
return fmt.Err("json", "decode", "expected :")
}
if err := p.skipValue(); err != nil {
return err
}
p.skipWhitespace()
c := p.next()
if c == '}' {
return nil
}
if c != ',' {
return fmt.Err("json", "decode", "expected , or }")
}
}
}
// skipArray consumes a JSON array without allocating []any.
func (p *parser) skipArray() error {
p.skipWhitespace()
if p.peek() == ']' {
p.next()
return nil
}
for {
if err := p.skipValue(); err != nil {
return err
}
p.skipWhitespace()
c := p.next()
if c == ']' {
return nil
}
if c != ',' {
return fmt.Err("json", "decode", "expected , or ]")
}
}
}
// skipNumber consumes a JSON number without returning any value.
func (p *parser) skipNumber() error {
for p.pos < len(p.data) {
c := p.data[p.pos]
if (c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E' {
p.pos++
} else {
break
}
}
return nil
}
// parseString parses a JSON string. The opening '"' must already be consumed.
// Fast path: if no escape sequences, returns string(data[start:end]) — 1 alloc.
// Slow path: uses Conv builder for strings with escapes — 2 allocs.
func (p *parser) parseString() (string, error) {
start := p.pos
for p.pos < len(p.data) {
c := p.data[p.pos]
if c == '"' {
s := string(p.data[start:p.pos])
p.pos++ // consume closing '"'
return s, nil
}
if c == '\\' {
// Escape found — fall back to slow path with builder
return p.parseStringEscape(start)
}
p.pos++
}
return "", fmt.Err("json", "decode", "unexpected EOF")
}
// parseStringEscape handles strings with escape sequences.
// Called when parseString encounters '\' at some position.
// start is the position of the first character after the opening '"'.
func (p *parser) parseStringEscape(start int) (string, error) {
b := fmt.GetConv()
defer b.PutConv()
// Write the part before the escape that was already scanned
for i := start; i < p.pos; i++ {
b.WriteByte(p.data[i])
}
// Continue parsing with escape handling
for p.pos < len(p.data) {
c := p.data[p.pos]
p.pos++
if c == '"' {
return b.GetString(fmt.BuffOut), nil
}
if c == '\\' {
if p.pos >= len(p.data) {
return "", fmt.Err("json", "decode", "unexpected EOF")
}
esc := p.data[p.pos]
p.pos++
switch esc {
case '"':
b.WriteByte('"')
case '\\':
b.WriteByte('\\')
case '/':
b.WriteByte('/')
case 'b':
b.WriteByte('\b')
case 'f':
b.WriteByte('\f')
case 'n':
b.WriteByte('\n')
case 'r':
b.WriteByte('\r')
case 't':
b.WriteByte('\t')
case 'u':
if p.pos+4 > len(p.data) {
return "", fmt.Err("json", "decode", "invalid unicode escape")
}
s := string(p.data[p.pos : p.pos+4])
p.pos += 4
val, _ := fmt.Convert(s).Int64(16)
b.WriteByte(byte(val))
default:
return "", fmt.Err("json", "decode", "invalid escape sequence")
}
} else {
b.WriteByte(c)
}
}
return "", fmt.Err("json", "decode", "unexpected EOF")
}
// parseNumberInto parses a JSON number directly into a typed pointer.
// Uses fmt.GetConv + LoadBytes + Int64/Float64 + PutConv — 0 allocations
// (reuses fmt's existing parseIntBase/parseFloatBase via pool).
func (p *parser) parseNumberInto(ptr any, ft fmt.FieldType) error {
p.skipWhitespace()
start := p.pos
isFloat := false
for p.pos < len(p.data) {
c := p.data[p.pos]
if (c >= '0' && c <= '9') || c == '-' || c == '+' {
p.pos++
} else if c == '.' || c == 'e' || c == 'E' {
isFloat = true
p.pos++
} else {
break
}
}
if p.pos == start {
return fmt.Err("json", "decode", "expected number")
}
numBytes := p.data[start:p.pos]
c := fmt.GetConv()
c.LoadBytes(numBytes)
if ft == fmt.FieldFloat || isFloat {
v, err := c.Float64()
c.PutConv()
if err != nil {
return err
}
if ft == fmt.FieldFloat {
switch fp := ptr.(type) {
case *float64:
*fp = v
case *float32:
*fp = float32(v)
}
} else {
// FieldInt but JSON has decimal/exponent — truncate
switch ip := ptr.(type) {
case *int64:
*ip = int64(v)
case *int:
*ip = int(v)
case *int32:
*ip = int32(v)
}
}
} else {
v, err := c.Int64()
c.PutConv()
if err != nil {
return err
}
switch ip := ptr.(type) {
case *int64:
*ip = v
case *int:
*ip = int(v)
case *int32:
*ip = int32(v)
}
}
return nil
}
func (p *parser) parseBool() (bool, error) {
if p.peek() == 't' {
if p.pos+4 <= len(p.data) && string(p.data[p.pos:p.pos+4]) == "true" {
p.pos += 4
return true, nil
}
} else {
if p.pos+5 <= len(p.data) && string(p.data[p.pos:p.pos+5]) == "false" {
p.pos += 5
return false, nil
}
}
return false, fmt.Err("json", "decode", "expected boolean")
}
func (p *parser) parseNull() error {
if p.pos+4 <= len(p.data) && string(p.data[p.pos:p.pos+4]) == "null" {
p.pos += 4
return nil
}
return fmt.Err("json", "decode", "expected null")
}
// parseIntoPtr parses a JSON value directly into a typed pointer.
// Bypasses parseValue()/writeValue() to avoid boxing values into any.
func (p *parser) parseIntoPtr(ptr any, ft fmt.FieldType) error {
p.skipWhitespace()
// Handle JSON null for any type
if p.peek() == 'n' {
return p.parseNull()
}
switch ft {
case fmt.FieldText:
if p.next() != '"' {
return fmt.Err("json", "decode", "expected string")
}
s, err := p.parseString()
if err != nil {
return err
}
if sp, ok := ptr.(*string); ok {
*sp = s
}
return nil
case fmt.FieldInt, fmt.FieldFloat:
return p.parseNumberInto(ptr, ft)
case fmt.FieldBool:
b, err := p.parseBool()
if err != nil {
return err
}
if bp, ok := ptr.(*bool); ok {
*bp = b
}
return nil
case fmt.FieldBlob:
if p.next() != '"' {
return fmt.Err("json", "decode", "expected string for blob")
}
s, err := p.parseString()
if err != nil {
return err
}
if bp, ok := ptr.(*[]byte); ok {
*bp = []byte(s)
}
return nil
case fmt.FieldIntSlice:
if p.peek() != '[' {
return fmt.Err("json", "decode", "expected array for int slice")
}
p.next() // consume '['
p.skipWhitespace()
sp, ok := ptr.(*[]int)
if !ok {
return p.skipArray()
}
if p.peek() == ']' {
p.next()
*sp = []int{}
return nil
}
var result []int
for {
var v int
if err := p.parseNumberInto(&v, fmt.FieldInt); err != nil {
return err
}
result = append(result, v)
p.skipWhitespace()
c := p.next()
if c == ']' {
break
}
if c != ',' {
return fmt.Err("json", "decode", "expected , or ]")
}
}
*sp = result
return nil
}
// Fallback for unknown field types
return p.skipValue()
}
// matchFieldIndex matches the current JSON key (bytes between pos and closing '"')
// against schema field names WITHOUT allocating a string.
// Returns field index or -1 if not found. Advances pos past the closing '"'.
// Falls back to parseString if escape sequences are found.
func (p *parser) matchFieldIndex(schema []fmt.Field) (int, error) {
start := p.pos
for p.pos < len(p.data) {
c := p.data[p.pos]
if c == '"' {
keyBytes := p.data[start:p.pos]
p.pos++ // consume closing '"'
for i, field := range schema {
if len(field.Name) == len(keyBytes) && matchBytesStr(field.Name, keyBytes) {
return i, nil
}
}
return -1, nil // unknown field
}
if c == '\\' {
// Rare: escaped key — fall back to allocating path
key, err := p.parseStringEscape(start)
if err != nil {
return -1, err
}
for i, field := range schema {
if field.Name == key {
return i, nil
}
}
return -1, nil
}
p.pos++
}
return -1, fmt.Err("json", "decode", "unexpected EOF in key")
}
// matchBytesStr compares a string against a byte slice without allocation.
func matchBytesStr(s string, b []byte) bool {
for i := 0; i < len(s); i++ {
if s[i] != b[i] {
return false
}
}
return true
}
// parseIntoFielder parses a JSON object directly into the Fielder.
// Eliminates the need for map[string]any as an intermediary.
func (p *parser) parseIntoFielder(f fmt.Fielder) error {
p.skipWhitespace()
if p.next() != '{' {
return fmt.Err("json", "decode", "expected {")
}
schema := f.Schema()
pointers := f.Pointers()
p.skipWhitespace()
if p.peek() == '}' {
p.next()
return nil
}
for {
p.skipWhitespace()
if p.next() != '"' {
return fmt.Err("json", "decode", "expected quote")
}
fieldIdx, err := p.matchFieldIndex(schema)
if err != nil {
return err
}
p.skipWhitespace()
if p.next() != ':' {
return fmt.Err("json", "decode", "expected :")
}
if fieldIdx < 0 {
// Unknown field: parse and discard
if err := p.skipValue(); err != nil {
return err
}
} else {
field := schema[fieldIdx]
ptr := pointers[fieldIdx]
if field.Type == fmt.FieldStruct {
if nested, ok := ptr.(fmt.Fielder); ok {
if err := p.parseIntoFielder(nested); err != nil {
return err
}
} else {
if err := p.skipValue(); err != nil {
return err
}
}
} else {
if err := p.parseIntoPtr(ptr, field.Type); err != nil {
return err
}
}
}
p.skipWhitespace()
c := p.next()
if c == '}' {
break
}
if c != ',' {
return fmt.Err("json", "decode", "expected , or }")
}
}
return nil
}