fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799More config file work (exporting/importing/defaults) Properly cleanup orphaned panels and "removed" panels SetZPos will now reorder the parents child panels MakePopup will take the place of the highest Z positioned panel29daa42

gui
9.9 KiB417 linesraw
1--
2-- json.lua
3--
4-- Copyright (c) 2019 rxi
5--
6-- Permission is hereby granted, free of charge, to any person obtaining a copy of
7-- this software and associated documentation files (the "Software"), to deal in
8-- the Software without restriction, including without limitation the rights to
9-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10-- of the Software, and to permit persons to whom the Software is furnished to do
11-- so, subject to the following conditions:
12--
13-- The above copyright notice and this permission notice shall be included in all
14-- copies or substantial portions of the Software.
15--
16-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-- SOFTWARE.
23--
24
25local json = { _version = "0.1.1" }
26
27-------------------------------------------------------------------------------
28-- Encode
29-------------------------------------------------------------------------------
30
31local encode
32
33local escape_char_map = {
34	[ "\\" ] = "\\\\",
35	[ "\"" ] = "\\\"",
36	[ "\b" ] = "\\b",
37	[ "\f" ] = "\\f",
38	[ "\n" ] = "\\n",
39	[ "\r" ] = "\\r",
40	[ "\t" ] = "\\t",
41}
42
43local escape_char_map_inv = { [ "\\/" ] = "/" }
44for k, v in pairs(escape_char_map) do
45	escape_char_map_inv[v] = k
46end
47
48
49local function escape_char(c)
50	return escape_char_map[c] or string.format("\\u%04x", c:byte())
51end
52
53
54local function encode_nil(val)
55	return "null"
56end
57
58
59local function encode_table(val, pretty, stack, scope)
60	local res = {}
61	stack = stack or {}
62	scope = scope or 1
63
64	-- Circular reference?
65	if stack[val] then error("circular reference") end
66
67	stack[val] = true
68
69	if rawget(val, 1) ~= nil or next(val) == nil then
70		-- Treat as array -- check keys are valid and it is not sparse
71		local n = 0
72		for k in pairs(val) do
73			if type(k) ~= "number" then
74				error("invalid table: mixed or invalid key types")
75			end
76			n = n + 1
77		end
78		if n ~= #val then
79			error("invalid table: sparse array")
80		end
81		-- Encode
82		for i, v in ipairs(val) do
83			table.insert(res, encode(v, pretty, stack, scope + 1))
84		end
85		stack[val] = nil
86
87		if pretty then
88			local tabs = string.rep("\t", scope)
89			local tabs_end = string.rep("\t", scope-1)
90			if #res > 0 then
91				return "[\n" .. tabs .. table.concat(res, ",\n" .. tabs) .. "\n" .. tabs_end .. "]"
92			else
93				return "[]"
94			end
95		else
96			return "[" .. table.concat(res, ",") .. "]"
97		end
98	else
99		-- Treat as an object
100		for k, v in pairs(val) do
101			if type(k) ~= "string" then
102				error("invalid table: mixed or invalid key types")
103			end
104			table.insert(res, encode(k, pretty, stack, scope + 1) .. (pretty and ": " or ":") .. encode(v, pretty, stack, scope + 1))
105		end
106		stack[val] = nil
107		if pretty then
108			local tabs = string.rep("\t", scope)
109			local tabs_end = string.rep("\t", scope-1)
110			return "{\n" .. tabs .. table.concat(res, ",\n" .. tabs) .. "\n" .. tabs_end .. "}"
111		else
112			return "{" .. table.concat(res, ",") .. "}"
113		end
114	end
115end
116
117
118local function encode_string(val)
119	return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
120end
121
122
123local function encode_number(val)
124	-- Check for NaN, -inf and inf
125	if val ~= val or val <= -math.huge or val >= math.huge then
126		error("unexpected number value '" .. tostring(val) .. "'")
127	end
128	return string.format("%.14g", val)
129end
130
131
132local type_func_map = {
133	[ "nil"     ] = encode_nil,
134	[ "table"   ] = encode_table,
135	[ "string"  ] = encode_string,
136	[ "number"  ] = encode_number,
137	[ "boolean" ] = tostring,
138}
139
140
141encode = function(val, pretty, stack, scope)
142	local t = type(val)
143	local f = type_func_map[t]
144	if f then
145		return f(val, pretty, stack, scope)
146	end
147	error("unexpected type '" .. t .. "'")
148end
149
150
151function json.encode(val, pretty)
152	return encode(val, pretty)
153end
154
155
156-------------------------------------------------------------------------------
157-- Decode
158-------------------------------------------------------------------------------
159
160local parse
161
162local function create_set(...)
163	local res = {}
164	for i = 1, select("#", ...) do
165		res[ select(i, ...) ] = true
166	end
167	return res
168end
169
170local space_chars   = create_set(" ", "\t", "\r", "\n")
171local delim_chars   = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
172local escape_chars  = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
173local literals      = create_set("true", "false", "null")
174
175local literal_map = {
176	[ "true"  ] = true,
177	[ "false" ] = false,
178	[ "null"  ] = nil,
179}
180
181
182local function next_char(str, idx, set, negate)
183	for i = idx, #str do
184		if set[str:sub(i, i)] ~= negate then
185			return i
186		end
187	end
188	return #str + 1
189end
190
191
192local function decode_error(str, idx, msg)
193	local line_count = 1
194	local col_count = 1
195	for i = 1, idx - 1 do
196		col_count = col_count + 1
197		if str:sub(i, i) == "\n" then
198			line_count = line_count + 1
199			col_count = 1
200		end
201	end
202	error( string.format("%s at line %d col %d", msg, line_count, col_count) )
203end
204
205
206local function codepoint_to_utf8(n)
207	-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
208	local f = math.floor
209	if n <= 0x7f then
210		return string.char(n)
211	elseif n <= 0x7ff then
212		return string.char(f(n / 64) + 192, n % 64 + 128)
213	elseif n <= 0xffff then
214		return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
215	elseif n <= 0x10ffff then
216		return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
217											 f(n % 4096 / 64) + 128, n % 64 + 128)
218	end
219	error( string.format("invalid unicode codepoint '%x'", n) )
220end
221
222
223local function parse_unicode_escape(s)
224	local n1 = tonumber( s:sub(3, 6),  16 )
225	local n2 = tonumber( s:sub(9, 12), 16 )
226	-- Surrogate pair?
227	if n2 then
228		return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
229	else
230		return codepoint_to_utf8(n1)
231	end
232end
233
234
235local function parse_string(str, i)
236	local has_unicode_escape = false
237	local has_surrogate_escape = false
238	local has_escape = false
239	local last
240	for j = i + 1, #str do
241		local x = str:byte(j)
242
243		if x < 32 then
244			decode_error(str, j, "control character in string")
245		end
246
247		if last == 92 then -- "\\" (escape char)
248			if x == 117 then -- "u" (unicode escape sequence)
249				local hex = str:sub(j + 1, j + 5)
250				if not hex:find("%x%x%x%x") then
251					decode_error(str, j, "invalid unicode escape in string")
252				end
253				if hex:find("^[dD][89aAbB]") then
254					has_surrogate_escape = true
255				else
256					has_unicode_escape = true
257				end
258			else
259				local c = string.char(x)
260				if not escape_chars[c] then
261					decode_error(str, j, "invalid escape char '" .. c .. "' in string")
262				end
263				has_escape = true
264			end
265			last = nil
266
267		elseif x == 34 then -- '"' (end of string)
268			local s = str:sub(i + 1, j - 1)
269			if has_surrogate_escape then
270				s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)
271			end
272			if has_unicode_escape then
273				s = s:gsub("\\u....", parse_unicode_escape)
274			end
275			if has_escape then
276				s = s:gsub("\\.", escape_char_map_inv)
277			end
278			return s, j + 1
279
280		else
281			last = x
282		end
283	end
284	decode_error(str, i, "expected closing quote for string")
285end
286
287
288local function parse_number(str, i)
289	local x = next_char(str, i, delim_chars)
290	local s = str:sub(i, x - 1)
291	local n = tonumber(s)
292	if not n then
293		decode_error(str, i, "invalid number '" .. s .. "'")
294	end
295	return n, x
296end
297
298
299local function parse_literal(str, i)
300	local x = next_char(str, i, delim_chars)
301	local word = str:sub(i, x - 1)
302	if not literals[word] then
303		decode_error(str, i, "invalid literal '" .. word .. "'")
304	end
305	return literal_map[word], x
306end
307
308
309local function parse_array(str, i)
310	local res = {}
311	local n = 1
312	i = i + 1
313	while 1 do
314		local x
315		i = next_char(str, i, space_chars, true)
316		-- Empty / end of array?
317		if str:sub(i, i) == "]" then
318			i = i + 1
319			break
320		end
321		-- Read token
322		x, i = parse(str, i)
323		res[n] = x
324		n = n + 1
325		-- Next token
326		i = next_char(str, i, space_chars, true)
327		local chr = str:sub(i, i)
328		i = i + 1
329		if chr == "]" then break end
330		if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
331	end
332	return res, i
333end
334
335
336local function parse_object(str, i)
337	local res = {}
338	i = i + 1
339	while 1 do
340		local key, val
341		i = next_char(str, i, space_chars, true)
342		-- Empty / end of object?
343		if str:sub(i, i) == "}" then
344			i = i + 1
345			break
346		end
347		-- Read key
348		if str:sub(i, i) ~= '"' then
349			decode_error(str, i, "expected string for key")
350		end
351		key, i = parse(str, i)
352		-- Read ':' delimiter
353		i = next_char(str, i, space_chars, true)
354		if str:sub(i, i) ~= ":" then
355			decode_error(str, i, "expected ':' after key")
356		end
357		i = next_char(str, i + 1, space_chars, true)
358		-- Read value
359		val, i = parse(str, i)
360		-- Set
361		res[key] = val
362		-- Next token
363		i = next_char(str, i, space_chars, true)
364		local chr = str:sub(i, i)
365		i = i + 1
366		if chr == "}" then break end
367		if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
368	end
369	return res, i
370end
371
372
373local char_func_map = {
374	[ '"' ] = parse_string,
375	[ "0" ] = parse_number,
376	[ "1" ] = parse_number,
377	[ "2" ] = parse_number,
378	[ "3" ] = parse_number,
379	[ "4" ] = parse_number,
380	[ "5" ] = parse_number,
381	[ "6" ] = parse_number,
382	[ "7" ] = parse_number,
383	[ "8" ] = parse_number,
384	[ "9" ] = parse_number,
385	[ "-" ] = parse_number,
386	[ "t" ] = parse_literal,
387	[ "f" ] = parse_literal,
388	[ "n" ] = parse_literal,
389	[ "[" ] = parse_array,
390	[ "{" ] = parse_object,
391}
392
393
394parse = function(str, idx)
395	local chr = str:sub(idx, idx)
396	local f = char_func_map[chr]
397	if f then
398		return f(str, idx)
399	end
400	decode_error(str, idx, "unexpected character '" .. chr .. "'")
401end
402
403
404function json.decode(str)
405	if type(str) ~= "string" then
406		error("expected argument of type string, got " .. type(str))
407	end
408	local res, idx = parse(str, next_char(str, 1, space_chars, true))
409	idx = next_char(str, idx, space_chars, true)
410	if idx <= #str then
411		decode_error(str, idx, "trailing garbage")
412	end
413	return res
414end
415
416
417return json