<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/read.go
blob: bedb856ba1e10105c05a926a17e13bff47a27f0e (plain)
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
package walk

import (
	"bufio"
	"math"
	"strings"
	"strconv"
	"fmt"
)

type ReadAction int
const (
	ActionReadValue ReadAction = iota
	ActionAppendPath
	ActionPopPath
	ActionIncrementPath
	ActionAppendPathNull
)

type JSONInStructure int
const (
	JSONInMap JSONInStructure = iota
	JSONInArray
)

type JSONInState int
const (
	JSONInValueEnd JSONInState = iota
	JSONInValue
	JSONInValueStart
	JSONInString
	JSONInKey
)

type JSONIn struct {
	path []Atom
	reader *bufio.Reader
	structure []JSONInStructure
	state JSONInState
	readBuffer []Atom
	readIndex int
	readBufferCapacity int
	actionBuffer []ReadAction
	actionIndex int
}

func NewJSONIn(reader *bufio.Reader) JSONIn {
	return JSONIn {
		path: make([]Atom, 0, 256),
		reader: reader,
		structure: []JSONInStructure{},
		state: JSONInValueStart,
		readBuffer: make([]Atom, 0, 256),
		readIndex: 0,
		readBufferCapacity: 256,
		actionBuffer: make([]ReadAction, 0, 256),
		actionIndex: 0,
	}
}

func isWhitespace(r rune) bool {
	for _, ws := range " \t\r\n" {
		if r == ws {
			return true
		}
	}
	return false
}

func isNumberRune(r rune) bool {
	return '0' <= r && r <= '9' || r == '.'
}

func (in *JSONIn) popPath() {
	if len(in.path) == 0 {
		panic("Tried to pop from empty path")
	}
	finalAtom := in.path[len(in.path) - 1]
	if finalAtom.Typ != AtomStringTerminal {
		in.path = in.path[:len(in.path) - 1]
		return
	}
	i := len(in.path) - 2
	for {
		if i < 0 {
			panic("Missing string begin in path")
		}
		if in.path[i].Typ == AtomStringTerminal {
			break
		}
		i--
	}
	in.path = in.path[:i]
}

func (in *JSONIn) nextNonWsRune() (rune, error) {
	for {
		r, _, err := in.reader.ReadRune()
		if err != nil {
			return 0, err
		}
		if !isWhitespace(r) {
			return r, nil
		}
	}
}

func (in *JSONIn) requireString(criteria string) {
	for _, r := range criteria {
		in.require(r)
	}
}

func (in *JSONIn) require(criterion rune) {
	r, _, err := in.reader.ReadRune()
	if err != nil {
		panic("Error while reading required rune: " + err.Error())
	}
	if r != criterion {
		panic("Required rune not read")
	}
}

// Returns the first full value of a list of atoms and also a boolean to indicate if there isn't a value at the beginning
func firstValue(atoms []Atom) ([]Atom, bool) {
	if len(atoms) == 0 {
		return nil, true
	}
	if atoms[0].Typ != AtomStringTerminal {
		return atoms[0:1], false
	}
	i := 1
	for {
		if i == len(atoms) {
			return nil, true
		}
		if atoms[i].Typ == AtomStringTerminal {
			return atoms[0:i+1], false
		}
		i++
	}
}

func (in *JSONIn) readValue() []Atom {
	try:
	value, incomplete := firstValue(in.readBuffer[in.readIndex:])
	if incomplete {
		if in.readIndex == 0 {
			newReadBuffer := make([]Atom, len(in.readBuffer), in.readBufferCapacity * 2)
			in.readBufferCapacity *= 2
			copy(newReadBuffer, in.readBuffer)
			in.readBuffer = newReadBuffer
			in.fillReadBuffer()
			goto try
		}
		copy(in.readBuffer, in.readBuffer[in.readIndex:])
		in.readBuffer = in.readBuffer[:len(in.readBuffer) - in.readIndex]
		in.readIndex = 0
		copy(in.actionBuffer, in.actionBuffer[in.actionIndex:])
		in.actionBuffer = in.actionBuffer[:len(in.actionBuffer) - in.actionIndex]
		in.actionIndex = 0
		in.fillReadBuffer()
		goto try
	}
	in.readIndex += len(value)
	return value
}

func (in *JSONIn) Read() (WalkItem, error) {
	for {
		if in.actionIndex == len(in.actionBuffer) {
			in.actionIndex = 0
			in.readIndex = 0
			in.actionBuffer = in.actionBuffer[:0]
			in.readBuffer = in.readBuffer[:0]
			err := in.fillReadBuffer()
			if len(in.actionBuffer) == 0 {
				return WalkItem{}, err
			}
		}
		action := in.actionBuffer[in.actionIndex]
		in.actionIndex++
		switch action {
			case ActionReadValue:
				value := in.readValue()
				return WalkItem {
					Value: value,
					Path: in.path,
				}, nil
			case ActionAppendPath:
				value := in.readValue()
				in.path = append(in.path, value...)
			case ActionAppendPathNull:
				in.path = append(in.path, NewAtomNull())
			case ActionPopPath:
				in.popPath()
			case ActionIncrementPath:
				prevIndex := in.path[len(in.path) - 1]
				if prevIndex.Typ == AtomNull {
					prevIndex.Typ = AtomNumber
					prevIndex.data = math.Float64bits(0)
				} else if prevIndex.Typ == AtomNumber {
					prevIndex.data = math.Float64bits(math.Float64frombits(prevIndex.data) + 1)
				} else {
					panic("Invalid index in array input. Type: " + fmt.Sprintf("%v", prevIndex.Typ))
				}
				in.path[len(in.path) - 1] = prevIndex
			default:
				panic("Invalid ReadAction")
		}
	}
}

func (in *JSONIn) AssertDone() {
	if len(in.structure) != 0 || in.state != JSONInValueEnd || in.readIndex < len(in.readBuffer) {
		panic("Input ended on incomplete JSON root")
	}
}

func (in *JSONIn) pushReadBuffer(atom Atom) bool {
	in.readBuffer = append(in.readBuffer, atom)
	return len(in.readBuffer) == in.readBufferCapacity
}

func (in *JSONIn) pushActionBuffer(action ReadAction) {
	in.actionBuffer = append(in.actionBuffer, action)
}

// Appends to the readBuffer until it has reached capacity
// Also appends to the actionBuffer as needed
func (in *JSONIn) fillReadBuffer() error {
	switch in.state {
		case JSONInValueStart:
			goto valueStart
		case JSONInValue:
			goto value
		case JSONInValueEnd:
			goto valueEnd
		case JSONInString:
			goto string
		case JSONInKey:
			goto key
		default:
			panic("Invalid JSONInState")
	}
	valueStart: {
		if len(in.structure) == 0 {
			goto value
		}
		innermost := in.structure[len(in.structure) - 1]
		switch innermost {
			case JSONInMap:
				goto mapValue
			case JSONInArray:
				goto arrayValue
			default:
				panic("Invalid JSONInStructure")
		}
	}
	value: {
		r, err := in.nextNonWsRune()
		if err != nil {
			panic("Missing value in JSON")
		}
		switch r {
			case 'n':
				in.requireString("ull")
				in.pushActionBuffer(ActionReadValue)
				if in.pushReadBuffer(NewAtomNull()) {
					in.state = JSONInValueEnd
					return nil
				}
				goto valueEnd
			case 'f':
				in.requireString("alse")
				in.pushActionBuffer(ActionReadValue)
				if in.pushReadBuffer(NewAtomBool(false)) {
					in.state = JSONInValueEnd
					return nil
				}
				goto valueEnd
			case 't':
				in.requireString("rue")
				in.pushActionBuffer(ActionReadValue)
				if in.pushReadBuffer(NewAtomBool(true)) {
					in.state = JSONInValueEnd
					return nil
				}
				goto valueEnd
			case '"':
				in.pushActionBuffer(ActionReadValue)
				if in.pushReadBuffer(NewAtomStringTerminal()) {
					in.state = JSONInString
					return nil
				}
				goto string
			case '{':
				in.structure = append(in.structure, JSONInMap)
				in.pushActionBuffer(ActionReadValue)
				in.pushActionBuffer(ActionAppendPathNull)
				if in.pushReadBuffer(NewAtomTerminal(MapBegin)) {
					in.state = JSONInValueStart
					return nil
				}
				goto mapValue
			case '[':
				in.structure = append(in.structure, JSONInArray)
				in.pushActionBuffer(ActionReadValue)
				in.pushActionBuffer(ActionAppendPathNull)
				if in.pushReadBuffer(NewAtomTerminal(ArrayBegin)) {
					in.state = JSONInValueStart
					return nil
				}
				goto arrayValue
		}
		if isNumberRune(r) {
			var builder strings.Builder
			builder.WriteRune(r)
			for {
				r, _, err = in.reader.ReadRune()
				if err != nil {
					break
				}
				if !isNumberRune(r) {
					in.reader.UnreadRune()
					break
				}
				builder.WriteRune(r)
			}
			number, parseError := strconv.ParseFloat(builder.String(), 64)
			if parseError != nil {
				panic("Invalid number")
			}
			in.pushActionBuffer(ActionReadValue)
			if in.pushReadBuffer(NewAtomNumber(number)) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		}
		panic("Invalid JSON value starting with: " + string(r))
	}
	string: {
		r, _, err := in.reader.ReadRune()
		if err != nil {
			panic("Missing closing terminal in string input: " + err.Error())
		}
		if r == '"' {
			if in.pushReadBuffer(NewAtomStringTerminal()) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		}
		if r == '\\' {
			r, _, err = in.reader.ReadRune()
			if err != nil {
				panic("Missing rune after \\")
			}
			if in.pushReadBuffer(NewAtomStringRune(r)) {
				in.state = JSONInString
				return nil
			}
			goto string
		}
		if in.pushReadBuffer(NewAtomStringRune(r)) {
			in.state = JSONInString
			return nil
		}
		goto string
	}
	key: {
		var full bool
		for {
			r, _, err := in.reader.ReadRune()
			if err != nil {
				panic("Missing closing terminal in string input: " + err.Error())
			}
			if r == '"' {
				full = in.pushReadBuffer(NewAtomStringTerminal())
				break
			}
			if r == '\\' {
				r, _, err = in.reader.ReadRune()
				if err != nil {
					panic("Missing rune after \\")
				}
				if in.pushReadBuffer(NewAtomStringRune(r)) {
					in.state = JSONInKey
					return nil
				}
				continue
			}
			if in.pushReadBuffer(NewAtomStringRune(r)) {
				in.state = JSONInKey
				return nil
			}
			continue
		}
		r, err := in.nextNonWsRune()
		if err != nil {
			panic("Expected : got: " + err.Error())
		}
		if r != ':' {
			panic("Expected : after key")
		}
		if full {
			in.state = JSONInValue
			return nil
		}
		goto value
	}
	valueEnd: {
		r, err := in.nextNonWsRune()
		if err != nil {
			in.state = JSONInValueEnd
			return err
		}
		if len(in.structure) == 0 {
			panic("More input after root JSON object ends")
		}
		innermost := in.structure[len(in.structure) - 1]
		if innermost == JSONInMap && r == '}' {
			in.structure = in.structure[:len(in.structure) - 1]
			in.pushActionBuffer(ActionPopPath)
			in.pushActionBuffer(ActionReadValue)
			if in.pushReadBuffer(NewAtomTerminal(MapEnd)) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		} else if innermost == JSONInArray && r == ']' {
			in.structure = in.structure[:len(in.structure) - 1]
			in.pushActionBuffer(ActionPopPath)
			in.pushActionBuffer(ActionReadValue)
			if in.pushReadBuffer(NewAtomTerminal(ArrayEnd)) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		}
		if r != ',' {
			panic("Expected , after JSON value, found: \"" + string(r) + "\"")
		}
		goto valueStart
	}
	mapValue: {
		in.pushActionBuffer(ActionPopPath)
		r, err := in.nextNonWsRune()
		if err != nil {
			panic("Missing value inside object")
		}
		if r == '}' {
			in.structure = in.structure[:len(in.structure) - 1]
			in.pushActionBuffer(ActionReadValue)
			if in.pushReadBuffer(NewAtomTerminal(MapEnd)) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		}
		if r != '"' {
			panic("Expected key found something else")
		}
		in.pushActionBuffer(ActionAppendPath)
		if in.pushReadBuffer(NewAtomStringTerminal()) {
			in.state = JSONInKey
			return nil
		}
		goto key
	}
	arrayValue: {
		r, err := in.nextNonWsRune()
		if err != nil {
			panic("Missing value inside array")
		}
		if r == ']' {
			in.structure = in.structure[:len(in.structure) - 1]
			in.pushActionBuffer(ActionPopPath)
			in.pushActionBuffer(ActionReadValue)
			if in.pushReadBuffer(NewAtomTerminal(ArrayEnd)) {
				in.state = JSONInValueEnd
				return nil
			}
			goto valueEnd
		}
		in.reader.UnreadRune()
		in.pushActionBuffer(ActionIncrementPath)
		goto value
	}
}