<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/json_tokens/write.go
blob: 813f2f3aba70e8e344ebb1c019836e37a64d2b64 (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
package json_tokens

import (
	"fmt"
	"strings"
	"bufio"
	"main/walk"
)

func stringPathSegment(segment walk.PathSegment) string {
	return fmt.Sprintf("%v", segment)
}

type JSONOutStructure int
const (
	JSONOutRoot JSONOutStructure = iota
	JSONOutMap
	JSONOutArray
	JSONOutString
	JSONOutValueEnd
)

type JSONOut struct {
	structure []JSONOutStructure
	writer *bufio.Writer
}

func (out *JSONOut) indent(adjust int) {
	fmt.Fprint(out.writer, strings.Repeat("\t", len(out.structure) - 1 + adjust))
}

func (out *JSONOut) atomOut(key string, atom walk.Atom) {
	state := out.structure[len(out.structure) - 1]
	switch state {
		case JSONOutRoot, JSONOutMap, JSONOutArray:
			switch atom.Typ {
				case walk.AtomNull, walk.AtomBool, walk.AtomNumber:
					out.indent(0)
					if state == JSONOutMap {
						fmt.Fprintf(out.writer, "%q: ", key)
					}
					fmt.Fprint(out.writer, atom.String())
					out.structure = append(out.structure, JSONOutValueEnd)
				case walk.AtomStringTerminal:
					out.indent(0)
					if state == JSONOutMap {
						fmt.Fprintf(out.writer, "%q: ", key)
					}
					fmt.Fprint(out.writer, "\"")
					out.structure = append(out.structure, JSONOutString)
				case walk.AtomTerminal:
					switch atom.Terminal() {
						case walk.MapBegin:
							out.indent(0)
							if state == JSONOutMap {
								fmt.Fprintf(out.writer, "%q: ", key)
							}
							fmt.Fprint(out.writer, "{\n")
							out.structure = append(out.structure, JSONOutMap)
						case walk.ArrayBegin:
							out.indent(0)
							if state == JSONOutMap {
								fmt.Fprintf(out.writer, "%q: ", key)
							}
							fmt.Fprint(out.writer, "[\n")
							out.structure = append(out.structure, JSONOutArray)
						case walk.MapEnd:
							out.indent(-1)
							if state != JSONOutMap {
								panic("Map ended while not inside a map")
							}
							fmt.Fprint(out.writer, "}")
							out.structure[len(out.structure) - 1] = JSONOutValueEnd
						case walk.ArrayEnd:
							out.indent(-1)
							if state != JSONOutArray {
								panic("Array ended while not inside a array")
							}
							fmt.Fprint(out.writer, "]")
							out.structure[len(out.structure) - 1] = JSONOutValueEnd
						default:
							panic("Invalid TerminalValue")
					}
				default:
					panic("Invalid AtomType in root value")
			}
		case JSONOutValueEnd:
			out.structure = out.structure[:len(out.structure) - 1]
			underState := out.structure[len(out.structure) - 1]
			if underState == JSONOutMap && atom.Typ == walk.AtomTerminal && atom.Terminal() == walk.MapEnd {
				fmt.Fprint(out.writer, "\n")
				out.indent(-1)
				fmt.Fprint(out.writer, "}")
				out.structure[len(out.structure) - 1] = JSONOutValueEnd
			} else if underState == JSONOutArray && atom.Typ == walk.AtomTerminal && atom.Terminal() == walk.ArrayEnd {
				fmt.Fprint(out.writer, "\n")
				out.indent(-1)
				fmt.Fprint(out.writer, "]")
				out.structure[len(out.structure) - 1] = JSONOutValueEnd
			} else if underState == JSONOutRoot {
				panic("Tried to output JSON after root value has concluded")
			} else {
				fmt.Fprint(out.writer, ",\n")
				out.atomOut(key, atom)
			}
		case JSONOutString:
			if atom.Typ == walk.AtomStringTerminal {
				fmt.Fprint(out.writer, "\"")
				out.structure[len(out.structure) - 1] = JSONOutValueEnd
			} else {
				fmt.Fprint(out.writer, atom.String())
			}
		default:
			panic("Invalid JSONOutState")
	}
}

func (out *JSONOut) Print(path walk.Path, values []walk.Atom) {
	var segment walk.PathSegment
	if len(path) > 0 {
		segment = path[len(path) - 1]
	}
	segmentString := stringPathSegment(segment)
	for _, atom := range values {
		out.atomOut(segmentString, atom)
	}
}

func (out *JSONOut) Write(item walk.WalkItem) error {
	pathValues, err := walk.Compound(item.Path)
	if err != nil {
		return err
	}
	path := walk.PathFromWalkValues(pathValues)
	out.Print(path, item.Value)
	return nil
}

func (out *JSONOut) AssertDone() {
	out.writer.Flush()
	if len(out.structure) != 2 || out.structure[0] != JSONOutRoot || out.structure[1] != JSONOutValueEnd {
		panic("Program ended with incomplete JSON output")
	}
}

func NewJSONOut(writer *bufio.Writer) *JSONOut {
	return &JSONOut {
		structure: []JSONOutStructure{JSONOutRoot},
		writer: writer,
	}
}