fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
29daa42
gui
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 45escape_char_map_inv [ v ] =k 46end 47 48 49local function escape_char ( c ) 50return escape_char_map [ c ] or string . format ( "\\u%04x" , c : byte ()) 51end 52 53 54local function encode_nil ( val ) 55return "null" 56end 57 58 59local function encode_table ( val , pretty , stack , scope ) 60local res ={} 61stack =stack or {} 62scope =scope or 1 63 64-- Circular reference? 65if stack [ val ] then error ( "circular reference" ) end 66 67stack [ val ] =true 68 69if rawget ( val , 1 ) ~= nil or next ( val ) == nil then 70-- Treat as array -- check keys are valid and it is not sparse 71local n =0 72for k in pairs ( val ) do 73if type ( k ) ~= "number" then 74error ( "invalid table: mixed or invalid key types" ) 75end 76n =n + 1 77end 78if n ~= # val then 79error ( "invalid table: sparse array" ) 80end 81-- Encode 82for i , v in ipairs ( val ) do 83table . insert ( res , encode ( v , pretty , stack , scope + 1 )) 84end 85stack [ val ] =nil 86 87if pretty then 88local tabs =string . rep ( "\t" , scope ) 89local tabs_end =string . rep ( "\t" , scope - 1 ) 90if # res > 0 then 91return "[\n" .. tabs .. table . concat ( res , ",\n" .. tabs ) .. "\n" .. tabs_end .. "]" 92else 93return "[]" 94end 95else 96return "[" .. table . concat ( res , "," ) .. "]" 97end 98else 99-- Treat as an object 100for k , v in pairs ( val ) do 101if type ( k ) ~= "string" then 102error ( "invalid table: mixed or invalid key types" ) 103end 104table . insert ( res , encode ( k , pretty , stack , scope + 1 ) .. ( pretty and ": " or ":" ) .. encode ( v , pretty , stack , scope + 1 )) 105end 106stack [ val ] =nil 107if pretty then 108local tabs =string . rep ( "\t" , scope ) 109local tabs_end =string . rep ( "\t" , scope - 1 ) 110return "{\n" .. tabs .. table . concat ( res , ",\n" .. tabs ) .. "\n" .. tabs_end .. "}" 111else 112return "{" .. table . concat ( res , "," ) .. "}" 113end 114end 115end 116 117 118local function encode_string ( val ) 119return '"' .. val : gsub ( '[%z\1-\31\\"]' , escape_char ) .. '"' 120end 121 122 123local function encode_number ( val ) 124-- Check for NaN, -inf and inf 125if val ~= val or val <= - math . huge or val >= math . huge then 126error ( "unexpected number value '" .. tostring ( val ) .. "'" ) 127end 128return 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 ) 142local t =type ( val ) 143local f =type_func_map [ t ] 144if f then 145return f ( val , pretty , stack , scope ) 146end 147error ( "unexpected type '" .. t .. "'" ) 148end 149 150 151function json . encode ( val , pretty ) 152return encode ( val , pretty ) 153end 154 155 156------------------------------------------------------------------------------- 157-- Decode 158------------------------------------------------------------------------------- 159 160local parse 161 162local function create_set ( ... ) 163local res ={} 164for i =1 , select ( "#" , ... ) do 165res [ select ( i , ... ) ] =true 166end 167return 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 ) 183for i =idx , # str do 184if set [ str : sub ( i , i )] ~= negate then 185return i 186end 187end 188return # str + 1 189end 190 191 192local function decode_error ( str , idx , msg ) 193local line_count =1 194local col_count =1 195for i =1 , idx - 1 do 196col_count =col_count + 1 197if str : sub ( i , i ) == "\n" then 198line_count =line_count + 1 199col_count =1 200end 201end 202error ( 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 208local f =math . floor 209if n <= 0x7f then 210return string . char ( n ) 211elseif n <= 0x7ff then 212return string . char ( f ( n / 64 ) + 192 , n % 64 + 128 ) 213elseif n <= 0xffff then 214return string . char ( f ( n / 4096 ) + 224 , f ( n % 4096 / 64 ) + 128 , n % 64 + 128 ) 215elseif n <= 0x10ffff then 216return string . char ( f ( n / 262144 ) + 240 , f ( n % 262144 / 4096 ) + 128 , 217f ( n % 4096 / 64 ) + 128 , n % 64 + 128 ) 218end 219error ( string . format ( "invalid unicode codepoint '%x'" , n ) ) 220end 221 222 223local function parse_unicode_escape ( s ) 224local n1 =tonumber ( s : sub ( 3 , 6 ), 16 ) 225local n2 =tonumber ( s : sub ( 9 , 12 ), 16 ) 226-- Surrogate pair? 227if n2 then 228return codepoint_to_utf8 (( n1 - 0xd800 ) * 0x400 + ( n2 - 0xdc00 ) + 0x10000 ) 229else 230return codepoint_to_utf8 ( n1 ) 231end 232end 233 234 235local function parse_string ( str , i ) 236local has_unicode_escape =false 237local has_surrogate_escape =false 238local has_escape =false 239local last 240for j =i + 1 , # str do 241local x =str : byte ( j ) 242 243if x < 32 then 244decode_error ( str , j , "control character in string" ) 245end 246 247if last == 92 then -- "\\" (escape char) 248if x == 117 then -- "u" (unicode escape sequence) 249local hex =str : sub ( j + 1 , j + 5 ) 250if not hex : find ( "%x%x%x%x" ) then 251decode_error ( str , j , "invalid unicode escape in string" ) 252end 253if hex : find ( "^[dD][89aAbB]" ) then 254has_surrogate_escape =true 255else 256has_unicode_escape =true 257end 258else 259local c =string . char ( x ) 260if not escape_chars [ c ] then 261decode_error ( str , j , "invalid escape char '" .. c .. "' in string" ) 262end 263has_escape =true 264end 265last =nil 266 267elseif x == 34 then -- '"' (end of string) 268local s =str : sub ( i + 1 , j - 1 ) 269if has_surrogate_escape then 270s =s : gsub ( "\\u[dD][89aAbB]..\\u...." , parse_unicode_escape ) 271end 272if has_unicode_escape then 273s =s : gsub ( "\\u...." , parse_unicode_escape ) 274end 275if has_escape then 276s =s : gsub ( "\\." , escape_char_map_inv ) 277end 278return s , j + 1 279 280else 281last =x 282end 283end 284decode_error ( str , i , "expected closing quote for string" ) 285end 286 287 288local function parse_number ( str , i ) 289local x =next_char ( str , i , delim_chars ) 290local s =str : sub ( i , x - 1 ) 291local n =tonumber ( s ) 292if not n then 293decode_error ( str , i , "invalid number '" .. s .. "'" ) 294end 295return n , x 296end 297 298 299local function parse_literal ( str , i ) 300local x =next_char ( str , i , delim_chars ) 301local word =str : sub ( i , x - 1 ) 302if not literals [ word ] then 303decode_error ( str , i , "invalid literal '" .. word .. "'" ) 304end 305return literal_map [ word ], x 306end 307 308 309local function parse_array ( str , i ) 310local res ={} 311local n =1 312i =i + 1 313while 1 do 314local x 315i =next_char ( str , i , space_chars , true ) 316-- Empty / end of array? 317if str : sub ( i , i ) == "]" then 318i =i + 1 319break 320end 321-- Read token 322x , i =parse ( str , i ) 323res [ n ] =x 324n =n + 1 325-- Next token 326i =next_char ( str , i , space_chars , true ) 327local chr =str : sub ( i , i ) 328i =i + 1 329if chr == "]" then break end 330if chr ~= "," then decode_error ( str , i , "expected ']' or ','" ) end 331end 332return res , i 333end 334 335 336local function parse_object ( str , i ) 337local res ={} 338i =i + 1 339while 1 do 340local key , val 341i =next_char ( str , i , space_chars , true ) 342-- Empty / end of object? 343if str : sub ( i , i ) == "}" then 344i =i + 1 345break 346end 347-- Read key 348if str : sub ( i , i ) ~= '"' then 349decode_error ( str , i , "expected string for key" ) 350end 351key , i =parse ( str , i ) 352-- Read ':' delimiter 353i =next_char ( str , i , space_chars , true ) 354if str : sub ( i , i ) ~= ":" then 355decode_error ( str , i , "expected ':' after key" ) 356end 357i =next_char ( str , i + 1 , space_chars , true ) 358-- Read value 359val , i =parse ( str , i ) 360-- Set 361res [ key ] =val 362-- Next token 363i =next_char ( str , i , space_chars , true ) 364local chr =str : sub ( i , i ) 365i =i + 1 366if chr == "}" then break end 367if chr ~= "," then decode_error ( str , i , "expected '}' or ','" ) end 368end 369return 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 ) 395local chr =str : sub ( idx , idx ) 396local f =char_func_map [ chr ] 397if f then 398return f ( str , idx ) 399end 400decode_error ( str , idx , "unexpected character '" .. chr .. "'" ) 401end 402 403 404function json . decode ( str ) 405if type ( str ) ~= "string" then 406error ( "expected argument of type string, got " .. type ( str )) 407end 408local res , idx =parse ( str , next_char ( str , 1 , space_chars , true )) 409idx =next_char ( str , idx , space_chars , true ) 410if idx <= # str then 411decode_error ( str , idx , "trailing garbage" ) 412end 413return res 414end 415 416 417return json