Path: blob/main/src/resources/pandoc/datadir/_json.lua
12922 views
--1-- json.lua2--3-- Copyright (c) 2020 rxi4-- https://github.com/rxi/json.lua5--6-- includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua7-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/518-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/529--10-- Permission is hereby granted, free of charge, to any person obtaining a copy of11-- this software and associated documentation files (the "Software"), to deal in12-- the Software without restriction, including without limitation the rights to13-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies14-- of the Software, and to permit persons to whom the Software is furnished to do15-- so, subject to the following conditions:16--17-- The above copyright notice and this permission notice shall be included in all18-- copies or substantial portions of the Software.19--20-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR21-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,22-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE23-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER24-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,25-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE26-- SOFTWARE.27--2829local json = { _version = "0.1.2-quarto" }3031-- taken from https://www.lua.org/pil/19.3.html32function pairsByKeys (t, f)33local a = {}34for n in pairs(t) do table.insert(a, n) end35table.sort(a, f)36local i = 0 -- iterator variable37local iter = function () -- iterator function38i = i + 139if a[i] == nil then return nil40else return a[i], t[a[i]]41end42end43return iter44end4546-------------------------------------------------------------------------------47-- Encode48-------------------------------------------------------------------------------4950local encode5152local escape_char_map = {53[ "\\" ] = "\\",54[ "\"" ] = "\"",55[ "\b" ] = "b",56[ "\f" ] = "f",57[ "\n" ] = "n",58[ "\r" ] = "r",59[ "\t" ] = "t",60}6162local escape_char_map_inv = { [ "/" ] = "/" }63for k, v in pairs(escape_char_map) do64escape_char_map_inv[v] = k65end666768local function escape_char(c)69return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))70end717273local function encode_nil(val)74return "null"75end7677local function encode_string(val)78return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'79end8081local function encode_table(val, stack)82local res = {}83stack = stack or {}8485-- Circular reference?86if stack[val] then error("circular reference") end8788stack[val] = true8990if next(val) == nil then91return '[]'92end9394local types = {}9596for k in pairs(val) do97types[type(k)] = true98end99100if #types > 1 then101error("invalid table: mixed or invalid key types")102elseif types["number"] then103-- Treat as array104local max_key = 0105for k in pairs(val) do106if k > max_key then107max_key = k108end109end110for i = 1, max_key do111if val[i] == nil then112table.insert(res, "null")113else114local v = encode(val[i], stack)115table.insert(res, v)116end117end118stack[val] = nil119return "[" .. table.concat(res, ",") .. "]"120elseif types["string"] then121-- Treat as object122for k, v in pairsByKeys(val) do123table.insert(res, encode_string(k) .. ":" .. encode(v, stack))124end125stack[val] = nil126return "{" .. table.concat(res, ",") .. "}"127else128error( string.format("invalid table: unsupported key type %s", types[1]) )129end130end131132local function encode_number(val)133-- Check for NaN, -inf and inf134if val ~= val or val <= -math.huge or val >= math.huge then135error("unexpected number value '" .. tostring(val) .. "'")136end137return string.format("%.14g", val)138end139140141local type_func_map = {142[ "nil" ] = encode_nil,143[ "table" ] = encode_table,144[ "string" ] = encode_string,145[ "number" ] = encode_number,146[ "boolean" ] = tostring,147}148149150encode = function(val, stack)151local t = type(val)152local f = type_func_map[t]153if f then154return f(val, stack)155end156error("unexpected type '" .. t .. "'")157end158159160function json.encode(val)161return ( encode(val) )162end163164165-------------------------------------------------------------------------------166-- Decode167-------------------------------------------------------------------------------168169local parse170171local function create_set(...)172local res = {}173for i = 1, select("#", ...) do174res[ select(i, ...) ] = true175end176return res177end178179local space_chars = create_set(" ", "\t", "\r", "\n")180local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")181local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")182local literals = create_set("true", "false", "null")183184local literal_map = {185[ "true" ] = true,186[ "false" ] = false,187[ "null" ] = nil,188}189190191local function next_char(str, idx, set, negate)192for i = idx, #str do193if set[str:sub(i, i)] ~= negate then194return i195end196end197return #str + 1198end199200201local function decode_error(str, idx, msg)202local line_count = 1203local col_count = 1204for i = 1, idx - 1 do205col_count = col_count + 1206if str:sub(i, i) == "\n" then207line_count = line_count + 1208col_count = 1209end210end211error( string.format("%s at line %d col %d", msg, line_count, col_count) )212end213214215local function codepoint_to_utf8(n)216-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa217local f = math.floor218if n <= 0x7f then219return string.char(n)220elseif n <= 0x7ff then221return string.char(f(n / 64) + 192, n % 64 + 128)222elseif n <= 0xffff then223return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)224elseif n <= 0x10ffff then225return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,226f(n % 4096 / 64) + 128, n % 64 + 128)227end228error( string.format("invalid unicode codepoint '%x'", n) )229end230231232local function parse_unicode_escape(s)233local n1 = tonumber( s:sub(1, 4), 16 )234local n2 = tonumber( s:sub(7, 10), 16 )235-- Surrogate pair?236if n2 then237return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)238else239return codepoint_to_utf8(n1)240end241end242243244local function parse_string(str, i)245local res = ""246local j = i + 1247local k = j248249while j <= #str do250local x = str:byte(j)251252if x < 32 then253decode_error(str, j, "control character in string")254255elseif x == 92 then -- `\`: Escape256res = res .. str:sub(k, j - 1)257j = j + 1258local c = str:sub(j, j)259if c == "u" then260local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)261or str:match("^%x%x%x%x", j + 1)262or decode_error(str, j - 1, "invalid unicode escape in string")263res = res .. parse_unicode_escape(hex)264j = j + #hex265else266if not escape_chars[c] then267decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")268end269res = res .. escape_char_map_inv[c]270end271k = j + 1272273elseif x == 34 then -- `"`: End of string274res = res .. str:sub(k, j - 1)275return res, j + 1276end277278j = j + 1279end280281decode_error(str, i, "expected closing quote for string")282end283284285local function parse_number(str, i)286local x = next_char(str, i, delim_chars)287local s = str:sub(i, x - 1)288local n = tonumber(s)289if not n then290decode_error(str, i, "invalid number '" .. s .. "'")291end292return n, x293end294295296local function parse_literal(str, i)297local x = next_char(str, i, delim_chars)298local word = str:sub(i, x - 1)299if not literals[word] then300decode_error(str, i, "invalid literal '" .. word .. "'")301end302return literal_map[word], x303end304305306local function parse_array(str, i)307local res = {}308local n = 1309i = i + 1310while 1 do311local x312i = next_char(str, i, space_chars, true)313-- Empty / end of array?314if str:sub(i, i) == "]" then315i = i + 1316break317end318-- Read token319x, i = parse(str, i)320res[n] = x321n = n + 1322-- Next token323i = next_char(str, i, space_chars, true)324local chr = str:sub(i, i)325i = i + 1326if chr == "]" then break end327if chr ~= "," then decode_error(str, i, "expected ']' or ','") end328end329return res, i330end331332333local function parse_object(str, i)334local res = {}335i = i + 1336while 1 do337local key, val338i = next_char(str, i, space_chars, true)339-- Empty / end of object?340if str:sub(i, i) == "}" then341i = i + 1342break343end344-- Read key345if str:sub(i, i) ~= '"' then346decode_error(str, i, "expected string for key")347end348key, i = parse(str, i)349-- Read ':' delimiter350i = next_char(str, i, space_chars, true)351if str:sub(i, i) ~= ":" then352decode_error(str, i, "expected ':' after key")353end354i = next_char(str, i + 1, space_chars, true)355-- Read value356val, i = parse(str, i)357-- Set358res[key] = val359-- Next token360i = next_char(str, i, space_chars, true)361local chr = str:sub(i, i)362i = i + 1363if chr == "}" then break end364if chr ~= "," then decode_error(str, i, "expected '}' or ','") end365end366return res, i367end368369370local char_func_map = {371[ '"' ] = parse_string,372[ "0" ] = parse_number,373[ "1" ] = parse_number,374[ "2" ] = parse_number,375[ "3" ] = parse_number,376[ "4" ] = parse_number,377[ "5" ] = parse_number,378[ "6" ] = parse_number,379[ "7" ] = parse_number,380[ "8" ] = parse_number,381[ "9" ] = parse_number,382[ "-" ] = parse_number,383[ "t" ] = parse_literal,384[ "f" ] = parse_literal,385[ "n" ] = parse_literal,386[ "[" ] = parse_array,387[ "{" ] = parse_object,388}389390391parse = function(str, idx)392local chr = str:sub(idx, idx)393local f = char_func_map[chr]394if f then395return f(str, idx)396end397decode_error(str, idx, "unexpected character '" .. chr .. "'")398end399400401function json.decode(str)402if type(str) ~= "string" then403error("expected argument of type string, got " .. type(str))404end405local res, idx = parse(str, next_char(str, 1, space_chars, true))406idx = next_char(str, idx, space_chars, true)407if idx <= #str then408decode_error(str, idx, "trailing garbage")409end410return res411end412413414return json415416417