-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_states.go
297 lines (238 loc) · 5.44 KB
/
json_states.go
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
package jsonpath
const (
jsonError = iota
jsonEOF
jsonBraceLeft
jsonBraceRight
jsonBracketLeft
jsonBracketRight
jsonColon
jsonComma
jsonNumber
jsonString
jsonNull
jsonKey
jsonBool
)
var trueBytes = []byte{'t', 'r', 'u', 'e'}
var falseBytes = []byte{'f', 'a', 'l', 's', 'e'}
var nullBytes = []byte{'n', 'u', 'l', 'l'}
var jsonTokenNames = map[int]string{
jsonEOF: "EOF",
jsonError: "ERROR",
jsonBraceLeft: "{",
jsonBraceRight: "}",
jsonBracketLeft: "[",
jsonBracketRight: "]",
jsonColon: ":",
jsonComma: ",",
jsonNumber: "NUMBER",
jsonString: "STRING",
jsonNull: "NULL",
jsonKey: "KEY",
jsonBool: "BOOL",
}
var JSON = lexJSONRoot
func lexJSONRoot(l lexer, state *intStack) stateFn {
var next stateFn
ignoreSpaceRun(l)
cur := l.peek()
switch cur {
case '{':
next = stateJSONObjectOpen
case '[':
next = stateJSONArrayOpen
default:
next = l.errorf("Expected '{' or '[' at root of JSON instead of %#U", cur)
}
return next
}
func stateJSONObjectOpen(l lexer, state *intStack) stateFn {
cur := l.take()
if cur != '{' {
return l.errorf("Expected '{' as start of object instead of %#U", cur)
}
l.emit(jsonBraceLeft)
state.push(jsonBraceLeft)
return stateJSONObject
}
func stateJSONArrayOpen(l lexer, state *intStack) stateFn {
cur := l.take()
if cur != '[' {
return l.errorf("Expected '[' as start of array instead of %#U", cur)
}
l.emit(jsonBracketLeft)
state.push(jsonBracketLeft)
return stateJSONArray
}
func stateJSONObject(l lexer, state *intStack) stateFn {
var next stateFn
cur := l.peek()
switch cur {
case '}':
if top, ok := state.peek(); ok && top != jsonBraceLeft {
next = l.errorf("Received %#U but has no matching '{'", cur)
break
}
l.take()
l.emit(jsonBraceRight)
state.pop()
next = stateJSONAfterValue
case '"':
next = stateJSONKey
default:
next = l.errorf("Expected '}' or \" within an object instead of %#U", cur)
}
return next
}
func stateJSONArray(l lexer, state *intStack) stateFn {
var next stateFn
cur := l.peek()
switch cur {
case ']':
if top, ok := state.peek(); ok && top != jsonBracketLeft {
next = l.errorf("Received %#U but has no matching '['", cur)
break
}
l.take()
l.emit(jsonBracketRight)
state.pop()
next = stateJSONAfterValue
default:
next = stateJSONValue
}
return next
}
func stateJSONAfterValue(l lexer, state *intStack) stateFn {
cur := l.take()
topVal := noValue
top, ok := state.peek()
if ok {
topVal = top
}
switch cur {
case ',':
l.emit(jsonComma)
switch topVal {
case jsonBraceLeft:
return stateJSONKey
case jsonBracketLeft:
return stateJSONValue
case noValue:
return l.errorf("found %#U outside of array or object", cur)
default:
return l.errorf("unexpected character in lexer stack: %#U", cur)
}
case '}':
l.emit(jsonBraceRight)
state.pop()
switch topVal {
case jsonBraceLeft:
return stateJSONAfterValue
case jsonBracketLeft:
return l.errorf("unexpected %#U in array", cur)
case noValue:
return stateJSONAfterRoot
}
case ']':
l.emit(jsonBracketRight)
state.pop()
switch topVal {
case jsonBraceLeft:
return l.errorf("unexpected %#U in object", cur)
case jsonBracketLeft:
return stateJSONAfterValue
case noValue:
return stateJSONAfterRoot
}
case eof:
if state.len() == 0 {
l.emit(jsonEOF)
return nil
}
return l.errorf("unexpected EOF instead of value")
default:
return l.errorf("unexpected character after json value token: %#U", cur)
}
return nil
}
func stateJSONKey(l lexer, state *intStack) stateFn {
if err := l.takeString(); err != nil {
return l.errorf(err.Error())
}
l.emit(jsonKey)
return stateJSONColon
}
func stateJSONColon(l lexer, state *intStack) stateFn {
cur := l.take()
if cur != ':' {
return l.errorf("expected ':' after key string instead of %#U", cur)
}
l.emit(jsonColon)
return stateJSONValue
}
func stateJSONValue(l lexer, state *intStack) stateFn {
cur := l.peek()
switch cur {
case eof:
return l.errorf("Unexpected EOF instead of value")
case '"':
return stateJSONString
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return stateJSONNumber
case 't', 'f':
return stateJSONBool
case 'n':
return stateJSONNull
case '{':
return stateJSONObjectOpen
case '[':
return stateJSONArrayOpen
default:
return l.errorf("unexpected character as start of value: %#U", cur)
}
}
func stateJSONString(l lexer, state *intStack) stateFn {
if err := l.takeString(); err != nil {
return l.errorf(err.Error())
}
l.emit(jsonString)
return stateJSONAfterValue
}
func stateJSONNumber(l lexer, state *intStack) stateFn {
if err := takeJSONNumeric(l); err != nil {
return l.errorf(err.Error())
}
l.emit(jsonNumber)
return stateJSONAfterValue
}
func stateJSONBool(l lexer, state *intStack) stateFn {
var match []byte
cur := l.peek()
switch cur {
case 't':
match = trueBytes
case 'f':
match = falseBytes
}
if !takeExactSequence(l, match) {
return l.errorf("Could not parse value as 'true' or 'false'")
}
l.emit(jsonBool)
return stateJSONAfterValue
}
func stateJSONNull(l lexer, state *intStack) stateFn {
if !takeExactSequence(l, nullBytes) {
return l.errorf("Could not parse value as 'null'")
}
l.emit(jsonNull)
return stateJSONAfterValue
}
func stateJSONAfterRoot(l lexer, state *intStack) stateFn {
cur := l.take()
if cur != eof {
return l.errorf("Expected EOF instead of %#U", cur)
}
l.emit(jsonEOF)
return nil
}