Types (backward compatible additions in the Data section): Types.object / isObject / set / keys (ordered objects and empty-object marking, shared by every data format), Types.Date (RFC 3339 typed dates for TOML / plist / CBOR / msgpack: new / is / valid / parse / toUnix / fromUnix), XmlNode gains optional text / target / ns fields. tools/bench.sh: also generates the json fixtures. Co-Authored-By: Claude Opus 5.5 <[email protected]>
434 lines
12 KiB
Luau
434 lines
12 KiB
Luau
--!strict
|
|
--!native
|
|
-- codecs/Json/Decode: the RFC 8259 parser behind codecs/Json. Iterative (an explicit container stack, so deep nesting
|
|
-- is a LIMIT error, never a stack overflow), strict grammar (no comments, trailing commas, leading zeros, NaN, single
|
|
-- quotes or raw control characters), \u escapes with surrogate pairs (lone surrogates become WTF-8 bytes). Also the
|
|
-- pull tokenizer behind Json.events. Never throws: failures return nil, message, offset (0-based byte offset).
|
|
local Types = require("@formats/Types")
|
|
|
|
local Decode = {}
|
|
|
|
local byte, sub, find, match, char = string.byte, string.sub, string.find, string.match, string.char
|
|
local tonumber = tonumber
|
|
local concat = table.concat
|
|
|
|
export type Options = {
|
|
ordered: boolean?, -- every object is a Types.object with its key order (default false: only empty objects are)
|
|
bigInts: ("number" | "string")?, -- integers beyond +-2^53: "string" keeps their exact text (default "number")
|
|
null: any?, -- the value for null (default Types.null)
|
|
maxDepth: number?, -- nesting limit (default 1000)
|
|
}
|
|
|
|
local ESC: { [number]: string } = {
|
|
[34] = '"',
|
|
[92] = "\\",
|
|
[47] = "/",
|
|
[98] = "\b",
|
|
[102] = "\f",
|
|
[110] = "\n",
|
|
[114] = "\r",
|
|
[116] = "\t",
|
|
}
|
|
|
|
-- utf8 bytes of a code point (surrogates too: WTF-8)
|
|
local function utf8char(c: number): string
|
|
if c < 0x80 then
|
|
return char(c)
|
|
elseif c < 0x800 then
|
|
return char(0xC0 + c // 64, 0x80 + c % 64)
|
|
elseif c < 0x10000 then
|
|
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
|
end
|
|
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
|
end
|
|
Decode.utf8char = utf8char
|
|
|
|
local function hex4(s: string, i: number): number?
|
|
local h = match(s, "^%x%x%x%x", i)
|
|
if not h then
|
|
return nil
|
|
end
|
|
return tonumber(h, 16)
|
|
end
|
|
|
|
-- string(s, i) -> value, next | nil, message, offset. i is at the opening quote.
|
|
local function str(s: string, i: number): (string?, any, number?)
|
|
local j = find(s, '[%z\1-\31"\\]', i + 1)
|
|
if not j then
|
|
return nil, "unterminated string", #s
|
|
end
|
|
local c = byte(s, j)
|
|
if c == 34 then
|
|
return sub(s, i + 1, j - 1), j + 1
|
|
end
|
|
local parts = {}
|
|
local np = 0
|
|
local start = i + 1
|
|
while true do
|
|
if c == 34 then
|
|
np += 1
|
|
parts[np] = sub(s, start, j - 1)
|
|
return concat(parts), j + 1
|
|
elseif c ~= 92 then
|
|
return nil, "control character in string", j - 1
|
|
end
|
|
np += 1
|
|
parts[np] = sub(s, start, j - 1)
|
|
local e = byte(s, j + 1)
|
|
if e == nil then
|
|
return nil, "unterminated string", #s
|
|
end
|
|
local simple = ESC[e]
|
|
if simple then
|
|
np += 1
|
|
parts[np] = simple
|
|
start = j + 2
|
|
elseif e == 117 then -- \u
|
|
local cp = hex4(s, j + 2)
|
|
if not cp then
|
|
return nil, "bad \\u escape", j - 1
|
|
end
|
|
start = j + 6
|
|
if cp >= 0xD800 and cp <= 0xDBFF and byte(s, start) == 92 and byte(s, start + 1) == 117 then
|
|
local lo = hex4(s, start + 2)
|
|
if lo and lo >= 0xDC00 and lo <= 0xDFFF then
|
|
cp = 0x10000 + (cp - 0xD800) * 1024 + (lo - 0xDC00)
|
|
start += 6
|
|
end
|
|
end
|
|
np += 1
|
|
parts[np] = utf8char(cp)
|
|
else
|
|
return nil, "bad escape", j - 1
|
|
end
|
|
j = find(s, '[%z\1-\31"\\]', start)
|
|
if not j then
|
|
return nil, "unterminated string", #s
|
|
end
|
|
c = byte(s, j)
|
|
end
|
|
end
|
|
Decode.string = str
|
|
|
|
-- number(s, i) -> value, next | nil, message, offset
|
|
local function num(s: string, i: number, bigStrings: boolean): (any, any, number?)
|
|
local p = i
|
|
if byte(s, p) == 45 then
|
|
p += 1
|
|
end
|
|
local c = byte(s, p)
|
|
if c == 48 then
|
|
p += 1
|
|
elseif c and c >= 49 and c <= 57 then
|
|
p = match(s, "^%d*()", p + 1) :: any
|
|
else
|
|
return nil, if c == nil then "truncated: expected a value" elseif p == i then "unexpected character" else "bad number", p - 1
|
|
end
|
|
local int = true
|
|
if byte(s, p) == 46 then
|
|
local q = match(s, "^%d+()", p + 1)
|
|
if not q then
|
|
return nil, "bad fraction", p
|
|
end
|
|
p = q :: any
|
|
int = false
|
|
end
|
|
c = byte(s, p)
|
|
if c == 101 or c == 69 then
|
|
p += 1
|
|
c = byte(s, p)
|
|
if c == 43 or c == 45 then
|
|
p += 1
|
|
end
|
|
local q = match(s, "^%d+()", p)
|
|
if not q then
|
|
return nil, "bad exponent", p - 1
|
|
end
|
|
p = q :: any
|
|
int = false
|
|
end
|
|
local text = sub(s, i, p - 1)
|
|
local n = tonumber(text) :: number
|
|
if n == nil then -- LuaJIT: absurd exponents (1e0066999...) -> nil; they overflow / underflow
|
|
local m, es = match(text, "^([^eE]*)[eE]([%+%-]?)")
|
|
local mv = tonumber(m) or 0
|
|
n = if mv == 0 then mv elseif es == "-" then mv * 0 else mv * math.huge
|
|
end
|
|
if int and bigStrings and p - i > 15 and (n >= 9007199254740992 or n <= -9007199254740992) then
|
|
return text, p
|
|
end
|
|
return n, p
|
|
end
|
|
Decode.number = num
|
|
|
|
-- decode(s, i, opts) -> value, next | nil, message, offset. Parses one value starting at i (leading whitespace
|
|
-- skipped); next is the position after the value (trailing whitespace not skipped).
|
|
function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
|
local o: Options = opts or {}
|
|
local ordered = o.ordered == true
|
|
local bigStrings = o.bigInts == "string"
|
|
local nullv = if o.null ~= nil then o.null else Types.null
|
|
local maxDepth = o.maxDepth or 1000
|
|
local object = Types.object
|
|
|
|
local stackT: { any } = {} -- containers
|
|
local stackK: { any } = {} -- object: the pending key; array: the count
|
|
local stackO: { any } = {} -- object: its key order (or true); array: false
|
|
local depth = 0
|
|
local n = #s
|
|
local value: any
|
|
|
|
while true do
|
|
i = match(s, "^[ \t\n\r]*()", i) :: any
|
|
local c = byte(s, i)
|
|
local haveValue = true
|
|
if c == 123 then -- {
|
|
depth += 1
|
|
if depth > maxDepth then
|
|
return nil, "nested deeper than " .. maxDepth, i - 1
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
|
local t = {}
|
|
c = byte(s, i)
|
|
if c == 125 then
|
|
i += 1
|
|
depth -= 1
|
|
value = object(t, {})
|
|
elseif c == 34 then
|
|
local k, ni, at = str(s, i)
|
|
if k == nil then
|
|
return nil, ni, at
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
|
if byte(s, i) ~= 58 then
|
|
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
|
end
|
|
i += 1
|
|
stackT[depth], stackK[depth] = t, k
|
|
stackO[depth] = if ordered then {} else true
|
|
haveValue = false
|
|
else
|
|
return nil, if c == nil then "truncated object" else "expected a key", i - 1
|
|
end
|
|
elseif c == 91 then -- [
|
|
depth += 1
|
|
if depth > maxDepth then
|
|
return nil, "nested deeper than " .. maxDepth, i - 1
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
|
if byte(s, i) == 93 then
|
|
i += 1
|
|
depth -= 1
|
|
value = {}
|
|
else
|
|
stackT[depth], stackK[depth], stackO[depth] = {}, 0, false
|
|
haveValue = false
|
|
end
|
|
elseif c == 34 then
|
|
local v, ni, at = str(s, i)
|
|
if v == nil then
|
|
return nil, ni, at
|
|
end
|
|
value, i = v, ni
|
|
elseif c == 116 then
|
|
if sub(s, i, i + 3) ~= "true" then
|
|
return nil, "bad literal", i - 1
|
|
end
|
|
value, i = true, i + 4
|
|
elseif c == 102 then
|
|
if sub(s, i, i + 4) ~= "false" then
|
|
return nil, "bad literal", i - 1
|
|
end
|
|
value, i = false, i + 5
|
|
elseif c == 110 then
|
|
if sub(s, i, i + 3) ~= "null" then
|
|
return nil, "bad literal", i - 1
|
|
end
|
|
value, i = nullv, i + 4
|
|
elseif c == nil then
|
|
return nil, "truncated: expected a value", n
|
|
else
|
|
local v, ni, at = num(s, i, bigStrings)
|
|
if v == nil then
|
|
return nil, ni, at
|
|
end
|
|
value, i = v, ni
|
|
end
|
|
-- attach the value to the open containers, closing them as their ends come
|
|
while haveValue do
|
|
if depth == 0 then
|
|
return value, i
|
|
end
|
|
local t = stackT[depth]
|
|
local ord = stackO[depth]
|
|
if ord then
|
|
local k = stackK[depth]
|
|
if ord ~= true and t[k] == nil then
|
|
table.insert(ord, k)
|
|
end
|
|
t[k] = value
|
|
else
|
|
local cnt = stackK[depth] + 1
|
|
t[cnt] = value
|
|
stackK[depth] = cnt
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", i) :: any
|
|
c = byte(s, i)
|
|
if c == 44 then -- ,
|
|
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
|
if ord then
|
|
if byte(s, i) ~= 34 then
|
|
return nil, if i > n then "truncated object" else "expected a key", i - 1
|
|
end
|
|
local k, ni, at = str(s, i)
|
|
if k == nil then
|
|
return nil, ni, at
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
|
if byte(s, i) ~= 58 then
|
|
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
|
end
|
|
i += 1
|
|
stackK[depth] = k
|
|
end
|
|
haveValue = false
|
|
elseif ord and c == 125 then
|
|
i += 1
|
|
value = if ord ~= true then object(t, ord) elseif next(t) == nil then object(t, {}) else t
|
|
stackT[depth] = nil
|
|
depth -= 1
|
|
elseif not ord and c == 93 then
|
|
i += 1
|
|
value = t
|
|
stackT[depth] = nil
|
|
depth -= 1
|
|
elseif c == nil then
|
|
return nil, if ord then "truncated object" else "truncated array", n
|
|
else
|
|
return nil, if ord then "expected ',' or '}'" else "expected ',' or ']'", i - 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- events: a pull tokenizer. next() -> event, value | nil at the end | "error", message, offset. Events: "object",
|
|
-- "endObject", "array", "endArray", "key" (string), "value" (scalar). The grammar is checked as it goes.
|
|
function Decode.events(s: string, i: number, opts: Options?): () -> (string?, any, number?)
|
|
local o: Options = opts or {}
|
|
local bigStrings = o.bigInts == "string"
|
|
local nullv = if o.null ~= nil then o.null else Types.null
|
|
local maxDepth = o.maxDepth or 1000
|
|
local stack: { boolean } = {} -- true = object
|
|
local depth = 0
|
|
-- state: "value" (a value expected), "first" (after { or [: value / key or close), "after" (after a value),
|
|
-- "key" (a key expected), "done", "dead"
|
|
local state = "value"
|
|
local n = #s
|
|
local function fail(msg: string, at: number): (string, any, number)
|
|
state = "dead"
|
|
return "error", msg, at
|
|
end
|
|
return function(): (string?, any, number?)
|
|
if state == "dead" then
|
|
return nil
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", i) :: any
|
|
local c = byte(s, i)
|
|
if state == "done" then
|
|
state = "dead"
|
|
if c ~= nil then
|
|
return "error", "trailing data", i - 1
|
|
end
|
|
return nil
|
|
end
|
|
if state == "after" then
|
|
local obj = stack[depth]
|
|
if c == 44 then
|
|
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
|
c = byte(s, i)
|
|
state = if obj then "key" else "value"
|
|
elseif c == (if obj then 125 else 93) then
|
|
i += 1
|
|
stack[depth] = nil
|
|
depth -= 1
|
|
state = if depth == 0 then "done" else "after"
|
|
return if obj then "endObject" else "endArray", nil
|
|
elseif c == nil then
|
|
return fail("truncated", n)
|
|
else
|
|
return fail("expected ',' or a close", i - 1)
|
|
end
|
|
end
|
|
if state == "first" then
|
|
local obj = stack[depth]
|
|
if c == (if obj then 125 else 93) then
|
|
i += 1
|
|
stack[depth] = nil
|
|
depth -= 1
|
|
state = if depth == 0 then "done" else "after"
|
|
return if obj then "endObject" else "endArray", nil
|
|
end
|
|
state = if obj then "key" else "value"
|
|
end
|
|
if state == "key" then
|
|
if c ~= 34 then
|
|
return fail(if c == nil then "truncated" else "expected a key", i - 1)
|
|
end
|
|
local k, ni, at = str(s, i)
|
|
if k == nil then
|
|
return fail(ni, at :: number)
|
|
end
|
|
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
|
if byte(s, i) ~= 58 then
|
|
return fail("expected ':'", i - 1)
|
|
end
|
|
i += 1
|
|
state = "value"
|
|
return "key", k
|
|
end
|
|
-- a value
|
|
local after = if depth == 0 then "done" else "after"
|
|
if c == 123 or c == 91 then
|
|
depth += 1
|
|
if depth > maxDepth then
|
|
return fail("nested deeper than " .. maxDepth, i - 1)
|
|
end
|
|
stack[depth] = c == 123
|
|
i += 1
|
|
state = "first"
|
|
return if c == 123 then "object" else "array", nil
|
|
elseif c == 34 then
|
|
local v, ni, at = str(s, i)
|
|
if v == nil then
|
|
return fail(ni, at :: number)
|
|
end
|
|
i = ni
|
|
state = after
|
|
return "value", v
|
|
elseif c == 116 and sub(s, i, i + 3) == "true" then
|
|
i += 4
|
|
state = after
|
|
return "value", true
|
|
elseif c == 102 and sub(s, i, i + 4) == "false" then
|
|
i += 5
|
|
state = after
|
|
return "value", false
|
|
elseif c == 110 and sub(s, i, i + 3) == "null" then
|
|
i += 4
|
|
state = after
|
|
return "value", nullv
|
|
elseif c == nil then
|
|
return fail("truncated: expected a value", n)
|
|
end
|
|
local v, ni, at = num(s, i, bigStrings)
|
|
if v == nil then
|
|
return fail(ni, at :: number)
|
|
end
|
|
i = ni
|
|
state = after
|
|
return "value", v
|
|
end
|
|
end
|
|
|
|
return Decode
|