Lines Matching refs:str

143 local function next_char(str, idx, set, negate)
144 for i = idx, #str do
145 if set[str:sub(i, i)] ~= negate then
149 return #str + 1
153 local function decode_error(str, idx, msg)
158 if str:sub(i, i) == "\n" then
196 local function parse_string(str, i)
201 while j <= #str do
202 local x = str:byte(j)
205 decode_error(str, j, "control character in string")
208 res = res .. str:sub(k, j - 1)
210 local c = str:sub(j, j)
212 local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
213 or str:match("^%x%x%x%x", j + 1)
214 or decode_error(str, j - 1, "invalid unicode escape in string")
219 decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
226 res = res .. str:sub(k, j - 1)
233 decode_error(str, i, "expected closing quote for string")
237 local function parse_number(str, i)
238 local x = next_char(str, i, delim_chars)
239 local s = str:sub(i, x - 1)
242 decode_error(str, i, "invalid number '" .. s .. "'")
248 local function parse_literal(str, i)
249 local x = next_char(str, i, delim_chars)
250 local word = str:sub(i, x - 1)
252 decode_error(str, i, "invalid literal '" .. word .. "'")
258 local function parse_array(str, i)
264 i = next_char(str, i, space_chars, true)
266 if str:sub(i, i) == "]" then
271 x, i = parse(str, i)
275 i = next_char(str, i, space_chars, true)
276 local chr = str:sub(i, i)
279 if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
285 local function parse_object(str, i)
290 i = next_char(str, i, space_chars, true)
292 if str:sub(i, i) == "}" then
297 if str:sub(i, i) ~= '"' then
298 decode_error(str, i, "expected string for key")
300 key, i = parse(str, i)
302 i = next_char(str, i, space_chars, true)
303 if str:sub(i, i) ~= ":" then
304 decode_error(str, i, "expected ':' after key")
306 i = next_char(str, i + 1, space_chars, true)
308 val, i = parse(str, i)
312 i = next_char(str, i, space_chars, true)
313 local chr = str:sub(i, i)
316 if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
343 parse = function(str, idx)
344 local chr = str:sub(idx, idx)
347 return f(str, idx)
349 decode_error(str, idx, "unexpected character '" .. chr .. "'")
353 function json.decode(str)
354 if type(str) ~= "string" then
355 error("expected argument of type string, got " .. type(str))
357 local res, idx = parse(str, next_char(str, 1, space_chars, true))
358 idx = next_char(str, idx, space_chars, true)
359 if idx <= #str then
360 decode_error(str, idx, "trailing garbage")