From 0e0610fedbf809805148dc6a1f0dc5f6314c513c Mon Sep 17 00:00:00 2001 From: "arcanfe@nospam" Date: Sun, 28 Mar 2021 09:42:29 +0000 Subject: [PATCH] stand-alone color import helper + more schemes This just takes the json output from https://terminal.sexy FossilOrigin-Name: b17f8ec1cd0f50610a39557652556853c37670dd62e098b404045f5166b29a38 --- durden/devmaps/colorschemes/dracula.lua | 34 +- durden/devmaps/colorschemes/gruvbox-light.lua | 2 +- .../devmaps/colorschemes/solarized-light.lua | 36 ++ durden/devmaps/colorschemes/solarized.lua | 36 ++ durden/devmaps/colorschemes/srcery.lua | 34 ++ durden/devmaps/colorschemes/twilight.lua | 34 ++ durden/extevh.lua | 15 +- durden/menus/global/colors.lua | 3 +- durden/menus/global/terminal.lua | 1 + durden/suppl.lua | 2 + util/color_conv.lua | 99 +++++ util/json.lua | 400 ++++++++++++++++++ 12 files changed, 673 insertions(+), 23 deletions(-) create mode 100644 durden/devmaps/colorschemes/solarized-light.lua create mode 100644 durden/devmaps/colorschemes/solarized.lua create mode 100644 durden/devmaps/colorschemes/srcery.lua create mode 100644 durden/devmaps/colorschemes/twilight.lua create mode 100644 util/color_conv.lua create mode 100644 util/json.lua diff --git a/durden/devmaps/colorschemes/dracula.lua b/durden/devmaps/colorschemes/dracula.lua index b97bf9f1..0af1c310 100644 --- a/durden/devmaps/colorschemes/dracula.lua +++ b/durden/devmaps/colorschemes/dracula.lua @@ -7,7 +7,7 @@ return { {}, -- secondary (alternate base color) {0x28, 0x2a, 0x36}, -- background {0xf8, 0xf8, 0xf2, 0x28, 0x2a, 0x36}, -- text (foreground+background) - {0x97, 0x1a}, -- cursor (normal state) + {0x97, 0x1a, 0x00}, -- cursor (normal state) {0xd7, 0x99, 0x21}, -- alt-cursor (scrollback/special state) {0x44, 0x47, 0x5a}, -- highlight selection, active-word (foreground+background) {}, -- label : prompts, data labels (foreground+background) @@ -19,22 +19,22 @@ return { {0xfb, 0xf1, 0xc7, 0x07, 0x66, 0x78}, -- ui : generic UI elements like menubar -- terminal clients below -- - {0x21, 0x22, 0x2c}, -- black - {0xff, 0x55, 0x55}, -- red - {0x50, 0xfa, 0x7b}, -- green - {0xf1, 0xfa, 0x8c}, -- yellow - {0xbd, 0x93, 0xf9}, -- blue - {0xff, 0x79, 0xc6}, -- magenta - {0x88, 0xe9, 0xfd}, -- cyan - {0xf8, 0xf8, 0xf2}, -- light-grey - {0x62, 0x72, 0xa4}, -- dark-grey - {0xff, 0x6e, 0x6e}, -- light-red - {0x69, 0xff, 0x94}, -- light-green - {0xff, 0xff, 0xa5}, -- light-yellow - {0xd6, 0xac, 0xff}, -- light-blue - {0xff, 0x92, 0xdf}, -- light-magenta - {0xa4, 0xff, 0xff}, -- light-cyan - {0xff, 0xff, 0xff}, -- white + {0x21, 0x22, 0x2c}, -- black 1 + {0xff, 0x55, 0x55}, -- red 2 + {0x50, 0xfa, 0x7b}, -- green 3 + {0xf1, 0xfa, 0x8c}, -- yellow 4 + {0xbd, 0x93, 0xf9}, -- blue 5 + {0xff, 0x79, 0xc6}, -- magenta 6 + {0x88, 0xe9, 0xfd}, -- cyan 7 + {0xf8, 0xf8, 0xf2}, -- light-grey 8 + {0x62, 0x72, 0xa4}, -- dark-grey 9 + {0xff, 0x6e, 0x6e}, -- light-red 10 + {0x69, 0xff, 0x94}, -- light-green 11 + {0xff, 0xff, 0xa5}, -- light-yellow 12 + {0xd6, 0xac, 0xff}, -- light-blue 13 + {0xff, 0x92, 0xdf}, -- light-magenta 14 + {0xa4, 0xff, 0xff}, -- light-cyan 15 + {0xff, 0xff, 0xff}, -- white 16 {0xf8, 0xf8, 0xf2}, -- foreground {0x28, 0x2a, 0x36}, -- background diff --git a/durden/devmaps/colorschemes/gruvbox-light.lua b/durden/devmaps/colorschemes/gruvbox-light.lua index c9f69fed..c45d4298 100644 --- a/durden/devmaps/colorschemes/gruvbox-light.lua +++ b/durden/devmaps/colorschemes/gruvbox-light.lua @@ -37,5 +37,5 @@ return { {0xeb, 0xdb, 0xb2}, -- white {0x3c, 0x38, 0x36}, -- foreground - {0xf9, 0xf5, 0xd7}, {0xf9, 0xf5, 0xd7}-- background + {0xf9, 0xf5, 0xd7} -- background } diff --git a/durden/devmaps/colorschemes/solarized-light.lua b/durden/devmaps/colorschemes/solarized-light.lua new file mode 100644 index 00000000..1cdf4261 --- /dev/null +++ b/durden/devmaps/colorschemes/solarized-light.lua @@ -0,0 +1,36 @@ +-- scheme: +-- by: +return { +{88, 110, 117}, +{}, -- secondary +{253, 246, 227}, +{88, 110, 227, 253, 246, 227}, +{88, 110, 117}, +{}, -- alt-cursor +{}, -- highlight +{}, -- label +{}, -- warning +{}, -- error +{}, -- alert +{}, -- reference +{}, -- inactive +{}, -- ui +{0, 43, 54}, +{220, 50, 47}, +{133, 153, 0}, +{181, 137, 0}, +{38, 139, 210}, +{108, 113, 196}, +{42, 161, 152}, +{147, 161, 161}, +{101, 123, 131}, +{220, 50, 47}, +{133, 153, 0}, +{181, 137, 0}, +{38, 139, 210}, +{108, 113, 196}, +{42, 161, 152}, +{253, 246, 227}, +{88, 110, 117}, +{253, 246, 227}, +} \ No newline at end of file diff --git a/durden/devmaps/colorschemes/solarized.lua b/durden/devmaps/colorschemes/solarized.lua new file mode 100644 index 00000000..ab0174d8 --- /dev/null +++ b/durden/devmaps/colorschemes/solarized.lua @@ -0,0 +1,36 @@ +-- scheme: +-- by: +return { +{147, 161, 161}, +{}, -- secondary +{0, 43, 54}, +{147, 161, 54, 0, 43, 54}, +{147, 161, 161}, +{}, -- alt-cursor +{}, -- highlight +{}, -- label +{}, -- warning +{}, -- error +{}, -- alert +{}, -- reference +{}, -- inactive +{}, -- ui +{0, 43, 54}, +{220, 50, 47}, +{133, 153, 0}, +{181, 137, 0}, +{38, 139, 210}, +{108, 113, 196}, +{42, 161, 152}, +{147, 161, 161}, +{101, 123, 131}, +{220, 50, 47}, +{133, 153, 0}, +{181, 137, 0}, +{38, 139, 210}, +{108, 113, 196}, +{42, 161, 152}, +{253, 246, 227}, +{147, 161, 161}, +{0, 43, 54}, +} \ No newline at end of file diff --git a/durden/devmaps/colorschemes/srcery.lua b/durden/devmaps/colorschemes/srcery.lua new file mode 100644 index 00000000..040ff634 --- /dev/null +++ b/durden/devmaps/colorschemes/srcery.lua @@ -0,0 +1,34 @@ +return { +{252, 232, 195}, +{}, -- secondary +{28, 27, 25}, +{252, 232, 25, 28, 27, 25}, +{251, 184, 41}, +{4, 71, 214}, +{}, -- highlight +{}, -- label +{}, -- warning +{}, -- error +{}, -- alert +{}, -- reference +{}, -- inactive +{}, -- ui +{28, 27, 25}, +{239, 47, 39}, +{81, 159, 80}, +{251, 184, 41}, +{44, 120, 191}, +{224, 44, 109}, +{10, 174, 179}, +{208, 191, 161}, +{145, 129, 117}, +{247, 83, 65}, +{152, 188, 55}, +{254, 208, 110}, +{104, 168, 228}, +{255, 92, 143}, +{83, 253, 233}, +{252, 232, 195}, +{252, 232, 195}, +{28, 27, 25}, +} \ No newline at end of file diff --git a/durden/devmaps/colorschemes/twilight.lua b/durden/devmaps/colorschemes/twilight.lua new file mode 100644 index 00000000..a0851b6a --- /dev/null +++ b/durden/devmaps/colorschemes/twilight.lua @@ -0,0 +1,34 @@ +return { +{167, 167, 167}, +{30, 30, 30}, +{}, -- background +{167, 167, 30, 30, 30, 30}, +{}, -- cursor +{}, -- alt-cursor +{}, -- highlight +{}, -- label +{}, -- warning +{}, -- error +{}, -- alert +{}, -- reference +{}, -- inactive +{}, -- ui +{30, 30, 30}, +{207, 106, 76}, +{143, 157, 106}, +{249, 238, 152}, +{117, 135, 166}, +{155, 133, 157}, +{175, 196, 219}, +{167, 167, 167}, +{95, 90, 96}, +{207, 106, 76}, +{143, 157, 106}, +{249, 238, 152}, +{117, 135, 166}, +{155, 133, 157}, +{175, 196, 219}, +{255, 255, 255}, +{167, 167, 167}, +{30, 30, 30}, +} diff --git a/durden/extevh.lua b/durden/extevh.lua index 925502d6..431467a5 100644 --- a/durden/extevh.lua +++ b/durden/extevh.lua @@ -468,11 +468,12 @@ function extevh_apply_atype(wnd, atype, source, stat) if (atbl.init) then atbl:init(wnd, source); end +end --- very rarely needed - for k,v in ipairs(wnd.handlers.register) do - v(wnd, stat.segkind, stat); - end +-- this is not overridable so we need to check if we should chain into the +-- dispatch for the type +defhtbl["preroll"] = +function(wnd, source, stat) end defhtbl["registered"] = @@ -501,8 +502,14 @@ function(wnd, source, stat) "registered:name=%s:kind=%s", wnd.name, stat.segkind)); end extevh_apply_atype(wnd, stat.segkind, source, stat); + wnd:set_title(stat.title); wnd:set_guid(stat.guid); + +-- very rarely needed + for k,v in ipairs(wnd.handlers.register) do + v(wnd, stat.segkind, stat); + end end -- stateinf is used in the builtin/shared diff --git a/durden/menus/global/colors.lua b/durden/menus/global/colors.lua index e13fb2df..a5e8c5fe 100644 --- a/durden/menus/global/colors.lua +++ b/durden/menus/global/colors.lua @@ -46,8 +46,9 @@ return { }, { name = "colorscheme", - label = "Scheme", + label = "Tui/Terminal Scheme", kind = "value", + description = "Change the current and default set of colors used for terminal/tui clients", set = function() return suppl_colorschemes() end, diff --git a/durden/menus/global/terminal.lua b/durden/menus/global/terminal.lua index 60f2ae65..a6011ac3 100644 --- a/durden/menus/global/terminal.lua +++ b/durden/menus/global/terminal.lua @@ -130,6 +130,7 @@ return { name = "colorscheme", label = "Scheme", kind = "value", + description = "Change the current and default set of colors used for terminal/tui clients", set = function() return suppl_colorschemes() end, diff --git a/durden/suppl.lua b/durden/suppl.lua index 665cfae3..893dffd4 100644 --- a/durden/suppl.lua +++ b/durden/suppl.lua @@ -1766,6 +1766,8 @@ function suppl_tgt_color(vid, cmap) end end tbl = color_cache[cmap] + else + tbl = cmap end if not tbl then diff --git a/util/color_conv.lua b/util/color_conv.lua new file mode 100644 index 00000000..6d5eba9e --- /dev/null +++ b/util/color_conv.lua @@ -0,0 +1,99 @@ +-- This is a simple stand-alone script for converting json export output +-- from https://terminal.sexy into color-schemes that can be used as part +-- of target/video/color/schemes and global/settings/visual/colors/... + +local json = dofile('./json.lua') + +if not arg[1] or not arg[2] then + error("use: color-in.json color-out.lua") +end + +file = io.open(arg[1]) + +local inp = file:read("*all") +local res = json.decode(inp) + +local rt = +{ + "{}, -- primary", + "{}, -- secondary", + "{}, -- background", + "{}, -- text", + "{}, -- cursor", + "{}, -- alt-cursor", + "{}, -- highlight", + "{}, -- label", + "{}, -- warning", + "{}, -- error", + "{}, -- alert", + "{}, -- reference", + "{}, -- inactive", + "{}, -- ui" +} + +local term_i = 15 +local function getcol(v) + local r = string.sub(v, 2, 3) + local g = string.sub(v, 4, 5) + local b = string.sub(v, 6, 7) + return { + tonumber(r, 16), + tonumber(g, 16), + tonumber(b, 16) + } +end + +-- 1..16 + foreground and background +for k,v in pairs(res.color) do + local col = getcol(v) + table.insert(rt, string.format("{%d, %d, %d},", col[1], col[2], col[3])) +end + +if res.foreground then + local col = getcol(res.foreground) + table.insert(rt, string.format("{%d, %d, %d},", col[1], col[2], col[3])) + rt[1] = rt[#rt] + + if res.background then + local col2 = getcol(res.background) + table.insert(rt, string.format("{%d, %d, %d},", col2[1], col2[2], col2[3])) + rt[3] = rt[#rt] + rt[4] = string.format("{%d, %d, %d, %d, %d, %d},", + col[1], col[2], col2[3], col2[1], col2[2], col2[3]) + end +end + +if res.cursor then + local col = getcol(res.cursor) + rt[5] = string.format("{%d, %d, %d},", col[1], col[2], col[3]) + rt[6] = string.format("{%d, %d, %d},", 255 - col[1], 255 - col[2], 255 - col[3]) +else + rt[5] = rt[1] +end + +-- auto-assign into labeled slots is worse, some base assumptions might be possible, +-- e.g. error tends to be terminal +-- +-- 'red', warning terminal 'yellow', inactive 'dark-grey' and sanity check against +-- fg/bg values. but might as well do this somewhere else +-- +-- label (8), warning (9), error (10), alert (11), ref (12), inactive (13), ui (14) +-- +local out = io.output(arg[2]) + +if res.name then + out:write("-- scheme: " .. res.name .. "\n") +end + +if res.author then + out:write("-- by: " .. res.author .. "\n") +end + +out:write("return {\n") + +for _,v in ipairs(rt) do + out:write(v .. "\n") +end + +out:write("}") +out:close() diff --git a/util/json.lua b/util/json.lua new file mode 100644 index 00000000..d008bf70 --- /dev/null +++ b/util/json.lua @@ -0,0 +1,400 @@ +-- +-- json.lua +-- +-- Copyright (c) 2019 rxi +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy of +-- this software and associated documentation files (the "Software"), to deal in +-- the Software without restriction, including without limitation the rights to +-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +-- of the Software, and to permit persons to whom the Software is furnished to do +-- so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. +-- + +local json = { _version = "0.1.1" } + +------------------------------------------------------------------------------- +-- Encode +------------------------------------------------------------------------------- + +local encode + +local escape_char_map = { + [ "\\" ] = "\\\\", + [ "\"" ] = "\\\"", + [ "\b" ] = "\\b", + [ "\f" ] = "\\f", + [ "\n" ] = "\\n", + [ "\r" ] = "\\r", + [ "\t" ] = "\\t", +} + +local escape_char_map_inv = { [ "\\/" ] = "/" } +for k, v in pairs(escape_char_map) do + escape_char_map_inv[v] = k +end + + +local function escape_char(c) + return escape_char_map[c] or string.format("\\u%04x", c:byte()) +end + + +local function encode_nil(val) + return "null" +end + + +local function encode_table(val, stack) + local res = {} + stack = stack or {} + + -- Circular reference? + if stack[val] then error("circular reference") end + + stack[val] = true + + if rawget(val, 1) ~= nil or next(val) == nil then + -- Treat as array -- check keys are valid and it is not sparse + local n = 0 + for k in pairs(val) do + if type(k) ~= "number" then + error("invalid table: mixed or invalid key types") + end + n = n + 1 + end + if n ~= #val then + error("invalid table: sparse array") + end + -- Encode + for i, v in ipairs(val) do + table.insert(res, encode(v, stack)) + end + stack[val] = nil + return "[" .. table.concat(res, ",") .. "]" + + else + -- Treat as an object + for k, v in pairs(val) do + if type(k) ~= "string" then + error("invalid table: mixed or invalid key types") + end + table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) + end + stack[val] = nil + return "{" .. table.concat(res, ",") .. "}" + end +end + + +local function encode_string(val) + return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"' +end + + +local function encode_number(val) + -- Check for NaN, -inf and inf + if val ~= val or val <= -math.huge or val >= math.huge then + error("unexpected number value '" .. tostring(val) .. "'") + end + return string.format("%.14g", val) +end + + +local type_func_map = { + [ "nil" ] = encode_nil, + [ "table" ] = encode_table, + [ "string" ] = encode_string, + [ "number" ] = encode_number, + [ "boolean" ] = tostring, +} + + +encode = function(val, stack) + local t = type(val) + local f = type_func_map[t] + if f then + return f(val, stack) + end + error("unexpected type '" .. t .. "'") +end + + +function json.encode(val) + return ( encode(val) ) +end + + +------------------------------------------------------------------------------- +-- Decode +------------------------------------------------------------------------------- + +local parse + +local function create_set(...) + local res = {} + for i = 1, select("#", ...) do + res[ select(i, ...) ] = true + end + return res +end + +local space_chars = create_set(" ", "\t", "\r", "\n") +local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") +local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") +local literals = create_set("true", "false", "null") + +local literal_map = { + [ "true" ] = true, + [ "false" ] = false, + [ "null" ] = nil, +} + + +local function next_char(str, idx, set, negate) + for i = idx, #str do + if set[str:sub(i, i)] ~= negate then + return i + end + end + return #str + 1 +end + + +local function decode_error(str, idx, msg) + local line_count = 1 + local col_count = 1 + for i = 1, idx - 1 do + col_count = col_count + 1 + if str:sub(i, i) == "\n" then + line_count = line_count + 1 + col_count = 1 + end + end + error( string.format("%s at line %d col %d", msg, line_count, col_count) ) +end + + +local function codepoint_to_utf8(n) + -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa + local f = math.floor + if n <= 0x7f then + return string.char(n) + elseif n <= 0x7ff then + return string.char(f(n / 64) + 192, n % 64 + 128) + elseif n <= 0xffff then + return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) + elseif n <= 0x10ffff then + return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, + f(n % 4096 / 64) + 128, n % 64 + 128) + end + error( string.format("invalid unicode codepoint '%x'", n) ) +end + + +local function parse_unicode_escape(s) + local n1 = tonumber( s:sub(3, 6), 16 ) + local n2 = tonumber( s:sub(9, 12), 16 ) + -- Surrogate pair? + if n2 then + return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) + else + return codepoint_to_utf8(n1) + end +end + + +local function parse_string(str, i) + local has_unicode_escape = false + local has_surrogate_escape = false + local has_escape = false + local last + for j = i + 1, #str do + local x = str:byte(j) + + if x < 32 then + decode_error(str, j, "control character in string") + end + + if last == 92 then -- "\\" (escape char) + if x == 117 then -- "u" (unicode escape sequence) + local hex = str:sub(j + 1, j + 5) + if not hex:find("%x%x%x%x") then + decode_error(str, j, "invalid unicode escape in string") + end + if hex:find("^[dD][89aAbB]") then + has_surrogate_escape = true + else + has_unicode_escape = true + end + else + local c = string.char(x) + if not escape_chars[c] then + decode_error(str, j, "invalid escape char '" .. c .. "' in string") + end + has_escape = true + end + last = nil + + elseif x == 34 then -- '"' (end of string) + local s = str:sub(i + 1, j - 1) + if has_surrogate_escape then + s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) + end + if has_unicode_escape then + s = s:gsub("\\u....", parse_unicode_escape) + end + if has_escape then + s = s:gsub("\\.", escape_char_map_inv) + end + return s, j + 1 + + else + last = x + end + end + decode_error(str, i, "expected closing quote for string") +end + + +local function parse_number(str, i) + local x = next_char(str, i, delim_chars) + local s = str:sub(i, x - 1) + local n = tonumber(s) + if not n then + decode_error(str, i, "invalid number '" .. s .. "'") + end + return n, x +end + + +local function parse_literal(str, i) + local x = next_char(str, i, delim_chars) + local word = str:sub(i, x - 1) + if not literals[word] then + decode_error(str, i, "invalid literal '" .. word .. "'") + end + return literal_map[word], x +end + + +local function parse_array(str, i) + local res = {} + local n = 1 + i = i + 1 + while 1 do + local x + i = next_char(str, i, space_chars, true) + -- Empty / end of array? + if str:sub(i, i) == "]" then + i = i + 1 + break + end + -- Read token + x, i = parse(str, i) + res[n] = x + n = n + 1 + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "]" then break end + if chr ~= "," then decode_error(str, i, "expected ']' or ','") end + end + return res, i +end + + +local function parse_object(str, i) + local res = {} + i = i + 1 + while 1 do + local key, val + i = next_char(str, i, space_chars, true) + -- Empty / end of object? + if str:sub(i, i) == "}" then + i = i + 1 + break + end + -- Read key + if str:sub(i, i) ~= '"' then + decode_error(str, i, "expected string for key") + end + key, i = parse(str, i) + -- Read ':' delimiter + i = next_char(str, i, space_chars, true) + if str:sub(i, i) ~= ":" then + decode_error(str, i, "expected ':' after key") + end + i = next_char(str, i + 1, space_chars, true) + -- Read value + val, i = parse(str, i) + -- Set + res[key] = val + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "}" then break end + if chr ~= "," then decode_error(str, i, "expected '}' or ','") end + end + return res, i +end + + +local char_func_map = { + [ '"' ] = parse_string, + [ "0" ] = parse_number, + [ "1" ] = parse_number, + [ "2" ] = parse_number, + [ "3" ] = parse_number, + [ "4" ] = parse_number, + [ "5" ] = parse_number, + [ "6" ] = parse_number, + [ "7" ] = parse_number, + [ "8" ] = parse_number, + [ "9" ] = parse_number, + [ "-" ] = parse_number, + [ "t" ] = parse_literal, + [ "f" ] = parse_literal, + [ "n" ] = parse_literal, + [ "[" ] = parse_array, + [ "{" ] = parse_object, +} + + +parse = function(str, idx) + local chr = str:sub(idx, idx) + local f = char_func_map[chr] + if f then + return f(str, idx) + end + decode_error(str, idx, "unexpected character '" .. chr .. "'") +end + + +function json.decode(str) + if type(str) ~= "string" then + error("expected argument of type string, got " .. type(str)) + end + local res, idx = parse(str, next_char(str, 1, space_chars, true)) + idx = next_char(str, idx, space_chars, true) + if idx <= #str then + decode_error(str, idx, "trailing garbage") + end + return res +end + + +return json