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
|
package walk
import (
"bufio"
"math"
"strings"
"strconv"
)
type JSONInStructure int
const (
JSONInRoot JSONInStructure = iota
JSONInMap
JSONInArray
JSONInValueEnd
)
type JSONIn struct {
path []Atom
reader *bufio.Reader
structure []JSONInStructure
}
func NewJSONIn(reader *bufio.Reader) JSONIn {
return JSONIn {
path: make([]Atom, 0, 256),
reader: reader,
structure: []JSONInStructure{JSONInRoot},
}
}
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")
}
}
func (in *JSONIn) readString(out []Atom) []Atom {
// TODO: improve
out = append(out, NewAtomStringTerminal())
for {
r, _, err := in.reader.ReadRune()
if err != nil {
panic("Missing closing terminal in string input: " + err.Error())
}
if r == '"' {
break
}
if r == '\\' {
r, _, err = in.reader.ReadRune()
if err != nil {
panic("Missing rune after \\")
}
if len(out) == cap(out) {
newOut := make([]Atom, len(out), cap(out) * 2)
copy(newOut, out)
out = newOut
}
out = append(out, NewAtomStringRune(r))
continue
}
if len(out) == cap(out) {
newOut := make([]Atom, len(out), cap(out) * 2)
copy(newOut, out)
out = newOut
}
out = append(out, NewAtomStringRune(r))
}
out = append(out, NewAtomStringTerminal())
return out
}
func (in *JSONIn) Read() (WalkItem, error) {
restart:
// TODO: Escaping
// TODO: Don't allow trailing commas
// TODO: Proper float parsing with e and stuff
r, err := in.nextNonWsRune()
if err != nil {
return WalkItem {}, err
}
state := in.structure[len(in.structure) - 1]
switch state {
case JSONInMap:
in.popPath()
if r == '}' {
in.structure[len(in.structure) - 1] = JSONInValueEnd
return WalkItem {
Value: []Atom{NewAtomTerminal(MapEnd)},
Path: in.path,
}, nil
}
if r != '"' {
panic("Expected key, found something else")
}
in.path = in.readString(in.path)
r, err = in.nextNonWsRune()
if err != nil {
panic("Expected : got: " + err.Error())
}
if r != ':' {
panic("Expected : after key")
}
r, err = in.nextNonWsRune()
if err != nil {
panic("Missing map value after key: " + err.Error())
}
case JSONInArray:
if r == ']' {
in.structure[len(in.structure) - 1] = JSONInValueEnd
in.popPath()
return WalkItem {
Value: []Atom{NewAtomTerminal(ArrayEnd)},
Path: in.path,
}, nil
}
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")
}
in.path[len(in.path) - 1] = prevIndex
case JSONInRoot:
case JSONInValueEnd:
in.structure = in.structure[:len(in.structure) - 1]
underState := in.structure[len(in.structure) - 1]
if underState == JSONInRoot {
panic("More input after root JSON object ends")
} else if underState == JSONInMap && r == '}' {
in.structure[len(in.structure) - 1] = JSONInValueEnd
in.popPath()
return WalkItem {
Value: []Atom{NewAtomTerminal(MapEnd)},
Path: in.path,
}, nil
} else if underState == JSONInArray && r == ']' {
in.structure[len(in.structure) - 1] = JSONInValueEnd
in.popPath()
return WalkItem {
Value: []Atom{NewAtomTerminal(ArrayEnd)},
Path: in.path,
}, nil
}
if r != ',' {
panic("Expected , after JSON value, found: \"" + string(r) + "\"")
}
goto restart
default:
panic("Invalid JSONIn state")
}
switch r {
case 'n':
in.requireString("ull")
in.structure = append(in.structure, JSONInValueEnd)
return WalkItem {
Value: []Atom{NewAtomNull()},
Path: in.path,
}, nil
case 'f':
in.requireString("alse")
in.structure = append(in.structure, JSONInValueEnd)
return WalkItem {
Value: []Atom{NewAtomBool(false)},
Path: in.path,
}, nil
case 't':
in.requireString("rue")
in.structure = append(in.structure, JSONInValueEnd)
return WalkItem {
Value: []Atom{NewAtomBool(true)},
Path: in.path,
}, nil
case '"':
value := make([]Atom, 0, 64)
value = in.readString(value)
in.structure = append(in.structure, JSONInValueEnd)
return WalkItem {
Value: value,
Path: in.path,
}, nil
case '{':
in.structure = append(in.structure, JSONInMap)
in.path = append(in.path, NewAtomNull())
return WalkItem {
Value: []Atom{NewAtomTerminal(MapBegin)},
Path: in.path[:len(in.path) - 1],
}, nil
case '[':
in.structure = append(in.structure, JSONInArray)
in.path = append(in.path, NewAtomNull())
return WalkItem {
Value: []Atom{NewAtomTerminal(ArrayBegin)},
Path: in.path[:len(in.path) - 1],
}, nil
}
if isNumberRune(r) {
var builder strings.Builder
builder.WriteRune(r)
for {
r, _, err = in.reader.ReadRune()
if err != nil || !isNumberRune(r) {
break
}
builder.WriteRune(r)
}
in.reader.UnreadRune()
number, parseError := strconv.ParseFloat(builder.String(), 64)
if parseError != nil {
panic("Invalid number")
}
in.structure = append(in.structure, JSONInValueEnd)
return WalkItem {
Value: []Atom{NewAtomNumber(number)},
Path: in.path,
}, nil
}
panic("Invalid JSON value")
}
func (in *JSONIn) AssertDone() {
if len(in.structure) != 2 || in.structure[0] != JSONInRoot || in.structure[1] != JSONInValueEnd {
panic("Input ended on incomplete JSON root")
}
}
|