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

import (
	"io"
	"encoding/json"
	"fmt"
	"strings"
	"math"
	"unicode/utf8"
)

// int or string
type PathSegment interface {}
type Path []PathSegment
func (path Path) ToWalkValues() []WalkValue {
	var values []WalkValue
	for _, segment := range path {
		switch s := segment.(type) {
			case int:
				values = append(values, ValueNumber(s))
			case string:
				values = append(values, ValueString(s))
			default:
				panic("Invalid PathSegment")
		}
	}
	return values
}

func PathFromWalkValues(values []WalkValue) Path {
	var segments []PathSegment
	for _, value := range values {
		switch v := value.(type) {
			case ValueNumber:
				segments = append(segments, int(math.Round(float64(v))))
			case ValueString:
				segments = append(segments, string(v))
			default:
				panic("Invalid value in path")
		}
	}
	return segments
}

type TerminalValue int
const (
	ArrayBegin TerminalValue = iota
	ArrayEnd
	MapBegin
	MapEnd
)
func (value TerminalValue) Atomise(in []Atom) []Atom {
	return append(in, value)
}
func (value TerminalValue) String() string {
	switch value {
		case ArrayBegin:
			return "["
		case ArrayEnd:
			return "]"
		case MapBegin:
			return "{"
		case MapEnd:
			return "}"
		default:
			panic("Unknown TerminalValue")
	}
}

type ValueNull struct {}
func (value ValueNull) Atomise(in []Atom) []Atom {
	return append(in, value)
}
func (value ValueNull) String() string {
	return "null"
}

type ValueBool bool
func (value ValueBool) Atomise(in []Atom) []Atom {
	return append(in, value)
}
func (value ValueBool) String() string {
	if value {
		return "true"
	} else {
		return "false"
	}
}

type ValueNumber float64
func (value ValueNumber) Atomise(in []Atom) []Atom {
	return append(in, value)
}
func (value ValueNumber) String() string {
	v := float64(value)
	return fmt.Sprintf("%f", v)
}

type StringTerminal struct {}
func (value StringTerminal) String() string {
	return "\""
}

type StringAtom rune
func (value StringAtom) String() string {
	return string(value)
}

type ValueString string
func (value ValueString) Atomise(in []Atom) []Atom {
	in = append(in, StringTerminal{})
	for _, char := range value {
		in = append(in, StringAtom(char))
	}
	in = append(in, StringTerminal{})
	return in
}
func (value ValueString) String() string {
	return fmt.Sprintf("\"%s\"", string(value))
}

type Atom interface {
	String() string
}

type WalkValue interface {
	// Append this values atoms to the input
	Atomise(in []Atom) []Atom
	String() string
}

type WalkItem struct {
	Value WalkValue
	Path Path
}

type WalkItemStream struct {
	channel chan WalkItem
	rewinds []WalkItem
}

func (stream *WalkItemStream) next() (WalkItem, bool) {
	if len(stream.rewinds) == 0 {
		item, hasItem := <- stream.channel
		return item, hasItem
	}
	item := stream.rewinds[len(stream.rewinds)-1]
	stream.rewinds = stream.rewinds[0:len(stream.rewinds)-1]
	return item, true
}

func (stream *WalkItemStream) rewind(item WalkItem) {
	stream.rewinds = append(stream.rewinds, item)
}

func (stream *WalkItemStream) peek() (WalkItem, bool) {
	item, hasItem := stream.next()
	if !hasItem {
		return item, false
	}
	stream.rewind(item)
	return item, true
}

func tokenToValue(token json.Token) WalkValue {
	switch token.(type) {
		case nil:
			return ValueNull {}
		case bool:
			return ValueBool(token.(bool))
		case float64:
			return ValueNumber(token.(float64))
		case string:
			return ValueString(token.(string))
		default:
			panic("Can't convert JSON token to value")
	}
}

func readValue(dec *json.Decoder, path Path, out chan WalkItem) bool {
	if !dec.More() {
		return true
	}
	t, err := dec.Token()
	if err == io.EOF {
		return true
	} else if err != nil {
		panic("Invalid JSON")
	}
	switch t.(type) {
		case nil, string, float64, bool:
			v := tokenToValue(t)
			out <- WalkItem {v, path}
			return false
		case json.Delim:
			switch rune(t.(json.Delim)) {
				case '[':
					out <- WalkItem {ArrayBegin, path}
					index := 0
					for dec.More() {
						empty := readValue(dec, append(path, index), out)
						if empty {
							break
						}
						index += 1
					}
					t, err := dec.Token()
					if err != nil {
						panic("Invalid JSON")
					}
					delim, isDelim := t.(json.Delim)
					if !isDelim || delim != ']' {
						panic("Expected ] in JSON")
					}
					out <- WalkItem{ArrayEnd, path}
					return false
				case '{':
					out <- WalkItem {MapBegin, path}
					for dec.More() {
						t, _ := dec.Token()
						key, keyIsString := t.(string)
						if !keyIsString {
							panic("Invalid JSON")
						}
						empty := readValue(dec, append(path, key), out)
						if empty {
							panic("Invalid JSON")
						}
					}
					t, err := dec.Token()
					if err != nil {
						panic("Invalid JSON")
					}
					delim, isDelim := t.(json.Delim)
					if !isDelim || delim != '}' {
						panic("Expected } in JSON")
					}
					out <- WalkItem {MapEnd, path}
					return false
				default:
					panic("Error parsing JSON")
			}
		default:
			panic("Invalid JSON token")
	}
}

func startWalk(dec *json.Decoder, out chan WalkItem) {
	isEmpty := readValue(dec, nil, out)
	if isEmpty {
		panic("Missing JSON input")
	}
	close(out)
}

func Json(r io.Reader) chan WalkItem {
	dec := json.NewDecoder(r)
	out := make(chan WalkItem)
	go startWalk(dec, out)
	return out
}

func printIndent(indent int) {
	for i := 0; i < indent; i += 1 {
		fmt.Print("\t")
	}
}

func jsonOutArray(in *WalkItemStream, indent int) {
	fmt.Println("[")
	token, hasToken := in.next()
	if !hasToken {
		panic("Missing ] in output JSON")
	}
	terminal, isTerminal := token.Value.(TerminalValue)
	if isTerminal && terminal == ArrayEnd {
		fmt.Print("\n")
		printIndent(indent)
		fmt.Print("]")
		return
	}
	in.rewind(token)
	for {
		valueToken := jsonOutValue(in, indent + 1, true)
		if valueToken != nil {
			panic("Missing value in output JSON array")
		}
		token, hasToken := in.next()
		if !hasToken {
			panic("Missing ] in output JSON")
		}
		terminal, isTerminal := token.Value.(TerminalValue)
		if isTerminal && terminal == ArrayEnd {
			fmt.Print("\n")
			printIndent(indent)
			fmt.Print("]")
			return
		}
		in.rewind(token)
		fmt.Println(",")
	}
}

func jsonOutMap(in *WalkItemStream, indent int) {
	fmt.Println("{")
	token, hasToken := in.next()
	if !hasToken {
		panic("Missing } in output JSON")
	}
	terminal, isTerminal := token.Value.(TerminalValue)
	if isTerminal && terminal == MapEnd {
		fmt.Print("\n")
		printIndent(indent)
		fmt.Print("}")
		return
	}
	in.rewind(token)
	for {
		keyToken, hasKeyToken := in.peek()
		if !hasKeyToken {
			panic("Missing map element")
		}
		printIndent(indent + 1)
		if len(keyToken.Path) == 0 {
			panic("Map element missing key")
		}
		key := keyToken.Path[len(keyToken.Path)-1]
		switch key.(type) {
			case int:
				fmt.Print(key.(int))
			case string:
				fmt.Printf("%q", key.(string))
			default:
				panic("Invalid path segment")
		}
		fmt.Print(": ")
		valueToken := jsonOutValue(in, indent + 1, false)
		if valueToken != nil {
			panic("Missing value int output JSON map")
		}
		token, hasToken := in.next()
		if !hasToken {
			panic("Missing } in output JSON")
		}
		terminal, isTerminal := token.Value.(TerminalValue)
		if isTerminal && terminal == MapEnd {
			fmt.Print("\n")
			printIndent(indent)
			fmt.Print("}")
			return
		}
		in.rewind(token)
		fmt.Println(",")
	}
}

func jsonOutValue(in *WalkItemStream, indent int, doIndent bool) WalkValue {
	token, hasToken := in.next()
	if !hasToken {
		panic("Missing JSON token in output")
	}
	switch v := token.Value.(type) {
		case ValueNull:
			if doIndent {
				printIndent(indent)
			}
			fmt.Printf("null")
			return nil
		case ValueBool:
			if doIndent {
				printIndent(indent)
			}
			if token.Value.(ValueBool) {
				fmt.Print("true")
			} else {
				fmt.Print("false")
			}
			return nil
		case ValueNumber:
			if doIndent {
				printIndent(indent)
			}
			fmt.Printf("%v", token.Value)
			return nil
		case ValueString:
			if doIndent {
				printIndent(indent)
			}
			fmt.Printf("%q", string(v))
			return nil
		case TerminalValue:
			switch token.Value.(TerminalValue) {
				case ArrayBegin:
					if doIndent {
						printIndent(indent)
					}
					jsonOutArray(in, indent)
					return nil
				case MapBegin:
					if doIndent {
						printIndent(indent)
					}
					jsonOutMap(in, indent)
					return nil
				default:
					return token.Value
			}
		default:
			panic("Invalid WalkValue")
	}
}

func JsonOut(in chan WalkItem) {
	stream := WalkItemStream {
		channel: in,
		rewinds: nil,
	}
	if jsonOutValue(&stream, 0, true) != nil {
		panic("Invalid output JSON")
	}
	fmt.Print("\n")
}

func ConcatData(first []Atom, second []Atom) []Atom {
	res := make([]Atom, 0, len(first) + len(second))
	res = append(res, first...)
	res = append(res, second...)
	return res
}

func Atomise(in []WalkValue) (out []Atom) {
	numAtoms := 0
	for _, value := range in {
		switch v := value.(type) {
			case TerminalValue, ValueNull, ValueBool, ValueNumber:
				numAtoms++
			case ValueString:
				numAtoms += utf8.RuneCountInString(string(v)) + 2
			default:
				panic("Invalid WalkValue")
		}
	}
	out = make([]Atom, 0, numAtoms)
	for _, value := range in {
		out = value.Atomise(out)
	}
	return out
}

type CompoundError int

const (
	CompoundRuneOutsideString CompoundError = iota
	CompoundUnknownAtom
	CompoundMissingEnd
	CompoundInvalidStringAtom
)

func (err CompoundError) Error() string {
	switch err {
		case CompoundRuneOutsideString:
			return "Compound Error: Rune Outside String"
		case CompoundUnknownAtom:
			return "Compound Error: Unknown Atom"
		case CompoundMissingEnd:
			return "Compound Error: Missing End"
		case CompoundInvalidStringAtom:
			return "Compound Error: Invalid String Atom"
		default:
			panic("Invalid CompoundError")
	}
}

type CompoundResult struct {
	value WalkValue
	error error
}

func Compound(in []Atom) (out []WalkValue, error error) {
	numValues := 0
	i := 0
	inString := false
	for _, atom := range in {
		switch atom.(type) {
			case TerminalValue, ValueNull, ValueBool, ValueNumber:
				if !inString {
					numValues++
				}
			case StringTerminal:
				if inString {
					numValues++
				}
				inString = !inString
		}
	}
	i = 0
	out = make([]WalkValue, 0, numValues)
	for {
		if i >= len(in) {
			break
		}
		atom := in[i]
		i++
		switch v := atom.(type) {
			case TerminalValue, ValueNull, ValueBool, ValueNumber:
				out = append(out, v.(WalkValue))
				continue
			case StringAtom:
				return nil, CompoundRuneOutsideString
			case StringTerminal:
			default:
				return nil, CompoundUnknownAtom
		}
		// Handle string start
		var builder strings.Builder
		loop: for {
			if i >= len(in) {
				return nil, CompoundMissingEnd
			}
			atom := in[i]
			i++
			switch v := atom.(type) {
				case StringTerminal:
					break loop
				case StringAtom, ValueNull, ValueBool, ValueNumber, TerminalValue:
					builder.WriteString(v.String())
				default:
					return nil, CompoundInvalidStringAtom
			}
		}
		out = append(out, ValueString(builder.String()))
	}
	return out, nil
}