-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpath.lua
More file actions
386 lines (324 loc) · 9.28 KB
/
Copy pathjsonpath.lua
File metadata and controls
386 lines (324 loc) · 9.28 KB
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
local utf8 = require('utf8')
local utf8_next = utf8.next
local utf8_isalpha = utf8.isalpha
local utf8_isdigit = utf8.isdigit
local string_byte = string.byte
local string_sub = string.sub
local JSON_TOKEN_NUM = 1
local JSON_TOKEN_STR = 2
local JSON_TOKEN_ANY = 3
local JSON_TOKEN_END = 4
local JSON_TOKEN_TYPE = {
JSON_TOKEN_NUM = JSON_TOKEN_NUM,
JSON_TOKEN_STR = JSON_TOKEN_STR,
JSON_TOKEN_ANY = JSON_TOKEN_ANY,
JSON_TOKEN_END = JSON_TOKEN_END,
}
local ZERO_BYTE = string_byte('0')
local NINE_BYTE = string_byte('9')
local UNDERSCORE_BYTE = string_byte('_')
local OPEN_BRACKET_BYTE = string_byte('[')
local CLOSE_BRACKET_BYTE = string_byte(']')
local SINGLE_QUOTE_BYTE = string_byte("'")
local DOUBLE_QUOTE_BYTE = string_byte('"')
local STAR_BYTE = string_byte('*')
local DOT_BYTE = string_byte('.')
local function is_ascii_digit(codepoint)
return codepoint ~= nil
and ZERO_BYTE <= codepoint
and codepoint <= NINE_BYTE
end
local function json_lexer_is_eof(offset, src_len)
return offset > src_len
end
-- Read one UTF-8 symbol.
-- Error positions are 1-based character positions, as in Tarantool json.c.
local function json_read_symbol(src, src_len, offset, symbol_count)
if json_lexer_is_eof(offset, src_len) then
return offset, symbol_count, nil, symbol_count + 1
end
local next_offset, codepoint = utf8_next(src, offset)
if codepoint == nil then
return offset, symbol_count, nil, symbol_count + 1
end
return next_offset, symbol_count + 1, codepoint
end
local function json_parse_string(
src,
src_len,
offset,
symbol_count,
quote_type,
capture_value)
-- Opening quote is always one-byte ASCII.
offset = offset + 1
symbol_count = symbol_count + 1
local str_offset = offset
while true do
local char, err
offset, symbol_count, char, err =
json_read_symbol(src, src_len, offset, symbol_count)
if err ~= nil then
return offset, symbol_count, nil, nil, err
end
if char == quote_type then
local len = offset - str_offset - 1
if len == 0 then
return offset, symbol_count, nil, nil, symbol_count
end
local value
if capture_value then
value = string_sub(
src,
str_offset,
str_offset + len - 1
)
end
return offset, symbol_count, JSON_TOKEN_STR, value
end
end
end
local function json_parse_integer(
src,
offset,
symbol_count,
index_base
)
local pos = offset
local byte = string_byte(src, pos)
if not is_ascii_digit(byte) then
return offset, symbol_count, nil, nil, symbol_count + 1
end
local value = 0
local len = 0
repeat
value = value * 10 + byte - ZERO_BYTE
len = len + 1
pos = pos + 1
byte = string_byte(src, pos)
until not is_ascii_digit(byte)
if value < index_base then
return offset, symbol_count, nil, nil, symbol_count + 1
end
--
-- This is the important off-by-one fix.
--
-- Tarantool json_parse_integer() advances both offsets by
-- exactly the number of consumed ASCII digits.
--
offset = offset + len
symbol_count = symbol_count + len
return offset, symbol_count, JSON_TOKEN_NUM, value - index_base
end
local function json_is_valid_identifier_symbol(char)
return utf8_isalpha(char)
or char == UNDERSCORE_BYTE
or utf8_isdigit(char)
end
local function json_parse_identifier(
src,
src_len,
offset,
symbol_count,
capture_value
)
local str_offset = offset
local char, err
offset, symbol_count, char, err =
json_read_symbol(src, src_len, offset, symbol_count)
if err ~= nil then
return offset, symbol_count, nil, nil, err
end
-- First symbol cannot be a digit.
if not utf8_isalpha(char) and char ~= UNDERSCORE_BYTE then
return offset, symbol_count, nil, nil, symbol_count
end
local last_offset = offset
while true do
offset, symbol_count, char, err =
json_read_symbol(src, src_len, offset, symbol_count)
if err ~= nil then
break
end
if not json_is_valid_identifier_symbol(char) then
offset = last_offset
symbol_count = symbol_count - 1
break
end
last_offset = offset
end
local value
if capture_value then
value = string_sub(src, str_offset, offset - 1)
end
return offset, symbol_count, JSON_TOKEN_STR, value
end
--
-- Returns:
-- updated byte offset,
-- updated symbol count,
-- token type,
-- token value (only when capture_value is true),
-- syntax error position.
--
local function json_lexer_next_token(
src,
src_len,
offset,
symbol_count,
index_base,
capture_value
)
if json_lexer_is_eof(offset, src_len) then
return offset, symbol_count, JSON_TOKEN_END
end
local last_offset = offset
local char, err
offset, symbol_count, char, err =
json_read_symbol(src, src_len, offset, symbol_count)
if err ~= nil then
return offset, symbol_count, nil, nil, err
end
if char == OPEN_BRACKET_BYTE then
-- Same special case as json.c for a trailing '['.
if json_lexer_is_eof(offset, src_len) then
return offset, symbol_count, nil, nil, symbol_count
end
local token_type
local value
-- Tarantool's json_current_char() reads a byte here.
-- There is no reason to invoke utf8.next() for syntax bytes.
char = string_byte(src, offset)
if char == SINGLE_QUOTE_BYTE
or char == DOUBLE_QUOTE_BYTE
then
offset, symbol_count, token_type, value, err =
json_parse_string(
src,
src_len,
offset,
symbol_count,
char,
capture_value
)
elseif char == STAR_BYTE then
-- '*' is one-byte ASCII.
offset = offset + 1
symbol_count = symbol_count + 1
token_type = JSON_TOKEN_ANY
else
offset, symbol_count, token_type, value, err =
json_parse_integer(
src,
offset,
symbol_count,
index_base
)
end
if err ~= nil then
return offset, symbol_count, nil, nil, err
end
if json_lexer_is_eof(offset, src_len)
or string_byte(src, offset) ~= CLOSE_BRACKET_BYTE
then
return offset, symbol_count, nil, nil, symbol_count + 1
end
-- Closing ']' is one-byte ASCII.
offset = offset + 1
symbol_count = symbol_count + 1
return offset, symbol_count, token_type, value
end
if char == DOT_BYTE then
if json_lexer_is_eof(offset, src_len) then
return offset, symbol_count, nil, nil, symbol_count + 1
end
return json_parse_identifier(
src,
src_len,
offset,
symbol_count,
capture_value
)
end
-- A bare identifier is allowed only as the first path component.
if last_offset ~= 1 then
return offset, symbol_count, nil, nil, symbol_count
end
-- Re-read the first symbol as part of the identifier.
offset = last_offset
symbol_count = symbol_count - 1
return json_parse_identifier(
src,
src_len,
offset,
symbol_count,
capture_value
)
end
local function validate(path, base)
local src_len = #path
local offset = 1
local symbol_count = 0
local index_base = base or 0
while true do
local token_type, err
local _
offset, symbol_count, token_type, _, err =
json_lexer_next_token(
path,
src_len,
offset,
symbol_count,
index_base,
false
)
if err ~= nil then
return false, err
end
if token_type == JSON_TOKEN_END then
return true
end
end
end
local function extract_tokens(path, base)
local src_len = #path
local offset = 1
local symbol_count = 0
local index_base = base or 0
local result = {}
local i = 1
while true do
local token_type, value, err
offset, symbol_count, token_type, value, err =
json_lexer_next_token(
path,
src_len,
offset,
symbol_count,
index_base,
true
)
if err ~= nil then
return nil, err
end
if value == nil then
result[i] = {
type = token_type,
}
else
result[i] = {
type = token_type,
value = value,
}
end
if token_type == JSON_TOKEN_END then
return result
end
i = i + 1
end
end
return {
JSON_TOKEN_TYPE = JSON_TOKEN_TYPE,
validate = validate,
extract_tokens = extract_tokens,
}