|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- fbjson.lua: parse json (for fbcache).
-- This file:
-- http://angg.twu.net/fbcache/fbjson.lua
-- http://angg.twu.net/fbcache/fbjson.lua.html
-- (find-angg "fbcache/fbjson.lua")
-- _
-- (_)___ ___ _ __ _ __ __ _ _ __ ___ ___
-- | / __|/ _ \| '_ \ | '_ \ / _` | '__/ __|/ _ \
-- | \__ \ (_) | | | | | |_) | (_| | | \__ \ __/
-- _/ |___/\___/|_| |_|____| .__/ \__,_|_| |___/\___|
-- |__/ |_____|_|
--
-- (find-fbcache "fbcache.lua" "requires")
-- (find-fbcache "fbcache.lua" "json_parse")
-- http://www.json.org/
require "re"
require "posix"
json_grammar0 = [=[
objrest <- obj {} [%s]* {.*}
obj <- bool / num / str / table / list
objp <- obj colon obj
table <- ("{" (objp (comma objp)*)* "}") -> totable
list <- ("[" (obj (comma obj )*)* "]") -> tolist
colon <- [%s]* ":" [%s]*
comma <- [%s]* "," [%s]*
bool <- ("true" / "false") -> tobool
num <- ("-"? [0-9.]+) -> tonum
str <- (ustr / str0)
ustr <- "u" str0
str0 <- ("'" (stritem / {'"'})* -> concat "'") /
('"' (stritem / {"'"})* -> concat '"')
stritem <- strcnormals / strcc / strcx / strcu / strcU / strcother
strcnormals <- {[^'"\]+}
strcc <- "\" {['"\/]}
strcx <- "\x" ({[%x][%x]} -> hextoc)
strcu <- "\u" ({[%x][%x][%x][%x]} -> utoc)
strcU <- "\U" ({[%x][%x][%x][%x][%x][%x][%x][%x]} -> Utoc)
strcother <- "\" . -> otherc
]=]
json_chars_b = {n="\n", r="\r", t="\t", b="\b", ["/"]="/"}
json_chars_u = {["2018"]='"', ["2019"]='"', ["201c"]='"', ["201d"]='"',
["2013"]="---", ["2022"]="*", }
json_chars_U = {}
json_defs = {
tobool = function (s) return s == "True" and true or false end,
tonum = function (s) return tonumber(s) end,
tostr = function (s) return s end,
tolist = function (...) return {...} end,
totable = function (...)
local L, T = {...}, {}
for i=1,#L-1,2 do T[L[i]] = L[i+1] end
return T
end,
hextoc = function (hh) return string.char(tonumber(hh, 16)) end,
utoc = function (h4)
local n = tonumber(h4, 16)
if n < 256 then return string.char(n) end
return json_chars_u[h4] or "\\u"..h4
end,
Utoc = function (h8) return "\\U"..h8 end,
concat = function (...) return table.concat {...} end,
otherc = function (c)
if json_chars_b[c] then return json_chars_b[c] end
print("\\"..c)
return "\\"..c
end,
}
json_grammar = re.compile(json_grammar0, json_defs)
json_parse = function (bigstr, pos) return json_grammar:match(bigstr, pos) end
json_test = function (bigstr, pos) pp(json_parse(bigstr, pos)) end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbjson.lua"
--]]
-- Local Variables:
-- coding: raw-text-unix
-- End: