644 lines
16 KiB
Luau
644 lines
16 KiB
Luau
--!strict
|
|
-- data/toml/Decode: the TOML 1.0.0 parser behind data/toml. Tables (and inline tables) are Types.object values with
|
|
-- their key order; arrays are arrays; datetimes are Types.Date values. Checks the 1.0 validity rules: UTF-8, control
|
|
-- characters, table / key redefinition (explicit, implicit, dotted, inline and array-of-tables), number grammar and
|
|
-- 64-bit range, escapes, datetimes. Never throws: failures return nil, message, offset (0-based).
|
|
local Types = require("@formats/Types")
|
|
|
|
local Decode = {}
|
|
|
|
local byte, sub, find, match, gsub, char = string.byte, string.sub, string.find, string.match, string.gsub, string.char
|
|
|
|
export type Options = {
|
|
bigInts: ("number" | "string")?, -- integers beyond +-2^53: "string" keeps their digits (default "number")
|
|
}
|
|
|
|
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
|
|
|
|
local ESC: { [number]: string } = { [98] = "\b", [116] = "\t", [110] = "\n", [102] = "\f", [114] = "\r", [34] = '"', [92] = "\\" }
|
|
|
|
-- digits with single underscores between them
|
|
local function digitsOk(d: string, class: string): boolean
|
|
return d ~= "" and not find(d, "__", 1, true) and byte(d, 1) ~= 95 and byte(d, #d) ~= 95 and find(d, "^[" .. class .. "_]+$") ~= nil
|
|
end
|
|
|
|
function Decode.decode(s: string, opts: Options?): (any, any, number?)
|
|
local bigStrings = opts ~= nil and opts.bigInts == "string"
|
|
local errMsg: string = ""
|
|
local errAt = 0
|
|
local function fail(msg: string, at: number): nil
|
|
errMsg, errAt = msg, at - 1
|
|
return nil
|
|
end
|
|
|
|
-- input checks: BOM, UTF-8, control characters, bare CR
|
|
if sub(s, 1, 3) == "\239\187\191" then
|
|
s = sub(s, 4)
|
|
end
|
|
do
|
|
local bad = find(s, "[%z\1-\8\11\12\14-\31\127]")
|
|
if bad then
|
|
return nil, "control character", bad - 1
|
|
end
|
|
local cr = find(s, "\r[^\n]") or (if byte(s, #s) == 13 then #s else nil)
|
|
if cr then
|
|
return nil, "carriage return without a line feed", cr - 1
|
|
end
|
|
if find(s, "[\128-\255]") then
|
|
if not utf8.len(s) then
|
|
return nil, "invalid UTF-8", 0
|
|
end
|
|
local sur = find(s, "\237[\160-\191]")
|
|
if sur then
|
|
return nil, "invalid UTF-8 (surrogate)", sur - 1
|
|
end
|
|
end
|
|
if find(s, "\r", 1, true) then
|
|
s = gsub(s, "\r\n", "\n")
|
|
end
|
|
end
|
|
local n = #s
|
|
|
|
-- table bookkeeping: kind = "explicit" | "implicit" | "dotted" | "inline"; section = where dotted tables were made
|
|
local kinds: { [any]: string } = {}
|
|
local sections: { [any]: number } = {}
|
|
local aot: { [any]: boolean } = {} -- arrays of tables ([[x]]); other arrays are static
|
|
local arrays: { [any]: boolean } = {}
|
|
local section = 0
|
|
local building: { any }? = nil -- tables created inside the inline table being parsed (frozen when it closes)
|
|
|
|
local function newTable(kind: string): any
|
|
local t = Types.object({}, {})
|
|
kinds[t] = kind
|
|
if building then
|
|
table.insert(building, t)
|
|
end
|
|
return t
|
|
end
|
|
|
|
local function ws(i: number): number
|
|
return match(s, "^[ \t]*()", i) :: any
|
|
end
|
|
-- whitespace, newlines and comments (inside arrays)
|
|
local function wsnl(i: number): number
|
|
while true do
|
|
i = match(s, "^[ \t\n]*()", i) :: any
|
|
if byte(s, i) == 35 then
|
|
i = find(s, "\n", i, true) or (n + 1)
|
|
else
|
|
return i
|
|
end
|
|
end
|
|
end
|
|
|
|
local function escape(p: number, parts: { string }): number?
|
|
local e = byte(s, p + 1)
|
|
local simple = if e then ESC[e] else nil
|
|
if simple then
|
|
table.insert(parts, simple)
|
|
return p + 2
|
|
elseif e == 117 or e == 85 then
|
|
local len = if e == 117 then 4 else 8
|
|
local h = match(s, "^" .. string.rep("%x", len), p + 2)
|
|
if not h then
|
|
return fail("bad unicode escape", p)
|
|
end
|
|
local cp = tonumber(h, 16) :: number
|
|
if cp > 0x10FFFF or (cp >= 0xD800 and cp <= 0xDFFF) then
|
|
return fail("unicode escape is not a scalar value", p)
|
|
end
|
|
table.insert(parts, utf8char(cp))
|
|
return p + 2 + len
|
|
end
|
|
return fail("bad escape", p)
|
|
end
|
|
|
|
-- strings: value, next | nil
|
|
local function basic(i: number): (string?, number)
|
|
local parts = {}
|
|
local j = i + 1
|
|
while true do
|
|
local p = find(s, '["\\\n]', j)
|
|
if not p or byte(s, p) == 10 then
|
|
return fail("unterminated string", p or n + 1), 0
|
|
end
|
|
table.insert(parts, sub(s, j, p - 1))
|
|
if byte(s, p) == 34 then
|
|
return table.concat(parts), p + 1
|
|
end
|
|
local nj = escape(p, parts)
|
|
if not nj then
|
|
return nil, 0
|
|
end
|
|
j = nj
|
|
end
|
|
end
|
|
local function multiBasic(i: number): (string?, number)
|
|
local j = i + 3
|
|
if byte(s, j) == 10 then
|
|
j += 1
|
|
end
|
|
local parts = {}
|
|
while true do
|
|
local p = find(s, '["\\]', j)
|
|
if not p then
|
|
return fail("unterminated multi-line string", i), 0
|
|
end
|
|
table.insert(parts, sub(s, j, p - 1))
|
|
if byte(s, p) == 34 then
|
|
if sub(s, p, p + 2) == '"""' then
|
|
local q = p + 3
|
|
local extra = 0
|
|
while byte(s, q) == 34 and extra < 2 do
|
|
extra += 1
|
|
q += 1
|
|
end
|
|
if byte(s, q) == 34 then
|
|
return fail("too many quotes", q), 0
|
|
end
|
|
table.insert(parts, string.rep('"', extra))
|
|
return table.concat(parts), q
|
|
end
|
|
table.insert(parts, '"')
|
|
j = p + 1
|
|
else
|
|
local k = match(s, "^\\[ \t]*\n[ \t\n]*()", p)
|
|
if k then
|
|
j = k :: any
|
|
else
|
|
local nj = escape(p, parts)
|
|
if not nj then
|
|
return nil, 0
|
|
end
|
|
j = nj
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local function literal(i: number): (string?, number)
|
|
local e = find(s, "['\n]", i + 1)
|
|
if not e or byte(s, e) == 10 then
|
|
return fail("unterminated literal string", e or n + 1), 0
|
|
end
|
|
return sub(s, i + 1, e - 1), e + 1
|
|
end
|
|
local function multiLiteral(i: number): (string?, number)
|
|
local j = i + 3
|
|
if byte(s, j) == 10 then
|
|
j += 1
|
|
end
|
|
local e = find(s, "'''", j, true)
|
|
if not e then
|
|
return fail("unterminated multi-line literal string", i), 0
|
|
end
|
|
local q = e + 3
|
|
local extra = 0
|
|
while byte(s, q) == 39 and extra < 2 do
|
|
extra += 1
|
|
q += 1
|
|
end
|
|
if byte(s, q) == 39 then
|
|
return fail("too many quotes", q), 0
|
|
end
|
|
return sub(s, j, e - 1) .. string.rep("'", extra), q
|
|
end
|
|
|
|
-- a (dotted) key: { parts }, next | nil
|
|
local function key(i: number): ({ string }?, number)
|
|
local keys = {}
|
|
while true do
|
|
i = ws(i)
|
|
local c = byte(s, i)
|
|
local k: string?
|
|
if c == 34 then
|
|
if sub(s, i, i + 2) == '"""' then
|
|
return fail("multi-line string as a key", i), 0
|
|
end
|
|
k, i = basic(i)
|
|
elseif c == 39 then
|
|
if sub(s, i, i + 2) == "'''" then
|
|
return fail("multi-line string as a key", i), 0
|
|
end
|
|
k, i = literal(i)
|
|
else
|
|
k = match(s, "^[%w_%-]+", i)
|
|
if k then
|
|
i += #k
|
|
else
|
|
return fail("expected a key", i), 0
|
|
end
|
|
end
|
|
if k == nil then
|
|
return nil, 0
|
|
end
|
|
table.insert(keys, k)
|
|
i = ws(i)
|
|
if byte(s, i) == 46 then
|
|
i += 1
|
|
else
|
|
return keys, i
|
|
end
|
|
end
|
|
end
|
|
|
|
-- numbers and datetimes
|
|
local function number(i: number): (any, number)
|
|
local tok = match(s, "^[%+%-]?[%w_%.]+", i)
|
|
if not tok then
|
|
return fail("expected a value", i), 0
|
|
end
|
|
local last = byte(tok, #tok)
|
|
if (last == 101 or last == 69) and not find(tok, "^[%+%-]?0[xob]") then
|
|
local ext = match(s, "^[%+%-][%w_]*", i + #tok)
|
|
if ext then
|
|
tok ..= ext
|
|
end
|
|
end
|
|
local nexti = i + #tok
|
|
local sign, body = match(tok, "^([%+%-]?)(.*)$")
|
|
if body == "inf" or body == "nan" then
|
|
return (if body == "nan" then 0 / 0 elseif sign == "-" then -math.huge else math.huge), nexti
|
|
end
|
|
local prefix = sub(body :: string, 1, 2)
|
|
if prefix == "0x" or prefix == "0o" or prefix == "0b" then
|
|
if sign ~= "" then
|
|
return fail("sign on a non-decimal integer", i), 0
|
|
end
|
|
local d = sub(body :: string, 3)
|
|
local class = if prefix == "0x" then "%x" elseif prefix == "0o" then "0-7" else "01"
|
|
if not digitsOk(d, class) then
|
|
return fail("bad integer", i), 0
|
|
end
|
|
d = gsub(gsub(d, "_", ""), "^0+", "")
|
|
local over = if prefix == "0x" then #d > 16 or (#d == 16 and (tonumber(sub(d, 1, 1), 16) :: number) >= 8)
|
|
elseif prefix == "0o" then #d > 21
|
|
else #d > 63
|
|
if over then
|
|
return fail("integer out of 64-bit range", i), 0
|
|
end
|
|
if d == "" then
|
|
return 0, nexti
|
|
end
|
|
local v = tonumber(d, if prefix == "0x" then 16 elseif prefix == "0o" then 8 else 2) :: number
|
|
if bigStrings and v >= 9007199254740992 then
|
|
return prefix .. d, nexti
|
|
end
|
|
return v, nexti
|
|
end
|
|
local ip, rest = match(body :: string, "^([%d_]+)(.*)$")
|
|
if not ip or not digitsOk(ip, "%d") then
|
|
return fail("bad number", i), 0
|
|
end
|
|
local digits = gsub(ip, "_", "")
|
|
if #digits > 1 and byte(digits, 1) == 48 then
|
|
return fail("leading zero", i), 0
|
|
end
|
|
if rest == "" then
|
|
local limit = if sign == "-" then "9223372036854775808" else "9223372036854775807"
|
|
if #digits > 19 or (#digits == 19 and digits > limit) then
|
|
return fail("integer out of 64-bit range", i), 0
|
|
end
|
|
local v = tonumber(digits) :: number
|
|
if bigStrings and v >= 9007199254740992 then
|
|
return (if sign == "-" then "-" else "") .. digits, nexti
|
|
end
|
|
return if sign == "-" then -v else v, nexti
|
|
end
|
|
local text = sign .. digits
|
|
local frac, r2 = match(rest :: string, "^%.([%d_]*)(.*)$")
|
|
if frac then
|
|
if not digitsOk(frac, "%d") then
|
|
return fail("bad fraction", i), 0
|
|
end
|
|
text ..= "." .. gsub(frac, "_", "")
|
|
rest = r2
|
|
end
|
|
if rest ~= "" then
|
|
local es, ed = match(rest :: string, "^[eE]([%+%-]?)([%d_]*)$")
|
|
if not es or not digitsOk(ed, "%d") then
|
|
return fail("bad float", i), 0
|
|
end
|
|
text ..= "e" .. es .. gsub(ed, "_", "")
|
|
end
|
|
local v = tonumber(text)
|
|
if v == nil then
|
|
return fail("bad float", i), 0
|
|
end
|
|
return v, nexti
|
|
end
|
|
|
|
local function datetime(i: number): (any, number)
|
|
local j = i
|
|
if match(s, "^%d%d%d%d%-%d%d%-%d%d", i) then
|
|
j = i + 10
|
|
local c = byte(s, j)
|
|
if (c == 84 or c == 116 or c == 32) and match(s, "^%d%d:", j + 1) then
|
|
j += 1
|
|
elseif c == 84 or c == 116 then
|
|
return fail("bad datetime", i), 0
|
|
else
|
|
local d = Types.Date.parse(sub(s, i, j - 1))
|
|
if not d then
|
|
return fail("bad date", i), 0
|
|
end
|
|
return d, j
|
|
end
|
|
end
|
|
local t = match(s, "^%d%d:%d%d:%d%d", j)
|
|
if not t then
|
|
return fail("bad time (seconds are required)", j), 0
|
|
end
|
|
j += 8
|
|
local frac = match(s, "^%.%d+", j)
|
|
if frac then
|
|
j += #frac
|
|
elseif byte(s, j) == 46 then
|
|
return fail("bad fraction", j), 0
|
|
end
|
|
if j - i > 19 or match(s, "^%d%d%d%d%-", i) then
|
|
local off = match(s, "^[Zz]", j) or match(s, "^[%+%-]%d%d:%d%d", j)
|
|
if off then
|
|
j += #off
|
|
end
|
|
end
|
|
local d = Types.Date.parse(sub(s, i, j - 1))
|
|
if not d then
|
|
return fail("bad datetime", i), 0
|
|
end
|
|
return d, j
|
|
end
|
|
|
|
local value: (i: number) -> (any, number)
|
|
local assign: (tbl: any, keys: { string }, v: any, sid: number, at: number) -> boolean
|
|
|
|
local function array(i: number): (any, number)
|
|
local arr = {}
|
|
arrays[arr] = true
|
|
i += 1
|
|
while true do
|
|
i = wsnl(i)
|
|
local c = byte(s, i)
|
|
if c == 93 then
|
|
return arr, i + 1
|
|
elseif c == nil then
|
|
return fail("unterminated array", i), 0
|
|
end
|
|
local v, ni = value(i)
|
|
if v == nil then
|
|
return nil, 0
|
|
end
|
|
table.insert(arr, v)
|
|
i = wsnl(ni)
|
|
c = byte(s, i)
|
|
if c == 44 then
|
|
i += 1
|
|
elseif c == 93 then
|
|
return arr, i + 1
|
|
else
|
|
return fail(if c == nil then "unterminated array" else "expected ',' or ']'", i), 0
|
|
end
|
|
end
|
|
end
|
|
|
|
local function inline(i: number): (any, number)
|
|
local outer = building
|
|
local mine = {}
|
|
building = mine
|
|
local t = newTable("inline")
|
|
section += 1
|
|
local sid = section
|
|
i = ws(i + 1)
|
|
if byte(s, i) == 125 then
|
|
building = outer
|
|
return t, i + 1
|
|
end
|
|
while true do
|
|
local keys, ni = key(i)
|
|
if not keys then
|
|
return nil, 0
|
|
end
|
|
i = ws(ni)
|
|
if byte(s, i) ~= 61 then
|
|
return fail("expected '='", i), 0
|
|
end
|
|
local v, vi = value(ws(i + 1))
|
|
if v == nil then
|
|
return nil, 0
|
|
end
|
|
if not assign(t, keys, v, sid, i) then
|
|
return nil, 0
|
|
end
|
|
i = ws(vi)
|
|
local c = byte(s, i)
|
|
if c == 44 then
|
|
i = ws(i + 1)
|
|
if byte(s, i) == 125 then
|
|
return fail("trailing comma in an inline table", i), 0
|
|
end
|
|
elseif c == 125 then
|
|
break
|
|
else
|
|
return fail(if c == 10 then "newline in an inline table" else "expected ',' or '}'", i), 0
|
|
end
|
|
end
|
|
for _, x in mine do
|
|
kinds[x] = "inline"
|
|
end
|
|
building = outer
|
|
if outer then
|
|
for _, x in mine do
|
|
table.insert(outer, x)
|
|
end
|
|
end
|
|
return t, i + 1
|
|
end
|
|
|
|
function value(i: number): (any, number)
|
|
local c = byte(s, i)
|
|
if c == 34 then
|
|
if sub(s, i, i + 2) == '"""' then
|
|
return multiBasic(i)
|
|
end
|
|
return basic(i)
|
|
elseif c == 39 then
|
|
if sub(s, i, i + 2) == "'''" then
|
|
return multiLiteral(i)
|
|
end
|
|
return literal(i)
|
|
elseif c == 116 then
|
|
if match(s, "^true", i) and not match(s, "^[%w_%-]", i + 4) then
|
|
return true, i + 4
|
|
end
|
|
elseif c == 102 then
|
|
if match(s, "^false", i) and not match(s, "^[%w_%-]", i + 5) then
|
|
return false, i + 5
|
|
end
|
|
elseif c == 91 then
|
|
return array(i)
|
|
elseif c == 123 then
|
|
return inline(i)
|
|
elseif c and ((c >= 48 and c <= 57) or c == 43 or c == 45 or c == 105 or c == 110) then
|
|
if match(s, "^%d%d%d%d%-", i) or match(s, "^%d%d:", i) then
|
|
return datetime(i)
|
|
end
|
|
return number(i)
|
|
end
|
|
return fail(if c == nil or c == 10 then "expected a value" else "bad value", i), 0
|
|
end
|
|
|
|
function assign(tbl: any, keys: { string }, v: any, sid: number, at: number): boolean
|
|
local t = tbl
|
|
for j = 1, #keys - 1 do
|
|
local k = keys[j]
|
|
local x = t[k]
|
|
if x == nil then
|
|
local nt = newTable("dotted")
|
|
sections[nt] = sid
|
|
Types.set(t, k, nt)
|
|
t = nt
|
|
elseif kinds[x] == "implicit" or (kinds[x] == "dotted" and sections[x] == sid) then
|
|
t = x
|
|
else
|
|
fail("cannot add to " .. k .. " with a dotted key (already defined)", at)
|
|
return false
|
|
end
|
|
end
|
|
local k = keys[#keys]
|
|
if t[k] ~= nil then
|
|
fail("duplicate key " .. k, at)
|
|
return false
|
|
end
|
|
Types.set(t, k, v)
|
|
return true
|
|
end
|
|
|
|
-- after a key / value or a header: spaces, an optional comment, then a newline or the end
|
|
local function eol(i: number): number?
|
|
i = ws(i)
|
|
local c = byte(s, i)
|
|
if c == 35 then
|
|
return find(s, "\n", i, true) or (n + 1)
|
|
elseif c == 10 or c == nil then
|
|
return i
|
|
end
|
|
return fail("expected the end of the line", i)
|
|
end
|
|
|
|
local root = newTable("explicit")
|
|
local cur = root
|
|
local i = 1
|
|
while true do
|
|
i = ws(i)
|
|
local c = byte(s, i)
|
|
if c == nil then
|
|
break
|
|
elseif c == 10 then
|
|
i += 1
|
|
elseif c == 35 then
|
|
i = find(s, "\n", i, true) or (n + 1)
|
|
elseif c == 91 then
|
|
local at = i
|
|
local isAot = byte(s, i + 1) == 91
|
|
local keys, ni = key(i + (if isAot then 2 else 1))
|
|
if not keys then
|
|
return nil, errMsg, errAt
|
|
end
|
|
ni = ws(ni)
|
|
if isAot then
|
|
if sub(s, ni, ni + 1) ~= "]]" then
|
|
return nil, "expected ']]'", ni - 1
|
|
end
|
|
ni += 2
|
|
else
|
|
if byte(s, ni) ~= 93 then
|
|
return nil, "expected ']'", ni - 1
|
|
end
|
|
ni += 1
|
|
end
|
|
-- walk to the parent
|
|
local t = root
|
|
for j = 1, #keys - 1 do
|
|
local k = keys[j]
|
|
local x = t[k]
|
|
if x == nil then
|
|
local nt = newTable("implicit")
|
|
Types.set(t, k, nt)
|
|
t = nt
|
|
elseif kinds[x] and kinds[x] ~= "inline" then
|
|
t = x
|
|
elseif aot[x] then
|
|
t = x[#x]
|
|
else
|
|
return nil, "cannot define a table under " .. k .. " (already defined)", at - 1
|
|
end
|
|
end
|
|
local k = keys[#keys]
|
|
local x = t[k]
|
|
if isAot then
|
|
local arr = x
|
|
if arr == nil then
|
|
arr = {}
|
|
aot[arr] = true
|
|
Types.set(t, k, arr)
|
|
elseif not aot[arr] then
|
|
return nil, "cannot define an array of tables " .. k .. " (already defined)", at - 1
|
|
end
|
|
local nt = newTable("explicit")
|
|
table.insert(arr, nt)
|
|
cur = nt
|
|
else
|
|
if x == nil then
|
|
local nt = newTable("explicit")
|
|
Types.set(t, k, nt)
|
|
cur = nt
|
|
elseif kinds[x] == "implicit" then
|
|
kinds[x] = "explicit"
|
|
cur = x
|
|
else
|
|
return nil, "table " .. k .. " defined twice", at - 1
|
|
end
|
|
end
|
|
section += 1
|
|
local e = eol(ni)
|
|
if not e then
|
|
return nil, errMsg, errAt
|
|
end
|
|
i = e
|
|
else
|
|
local at = i
|
|
local keys, ni = key(i)
|
|
if not keys then
|
|
return nil, errMsg, errAt
|
|
end
|
|
ni = ws(ni)
|
|
if byte(s, ni) ~= 61 then
|
|
return nil, "expected '='", ni - 1
|
|
end
|
|
local v, vi = value(ws(ni + 1))
|
|
if v == nil then
|
|
return nil, errMsg, errAt
|
|
end
|
|
if not assign(cur, keys, v, section, at) then
|
|
return nil, errMsg, errAt
|
|
end
|
|
local e = eol(vi)
|
|
if not e then
|
|
return nil, errMsg, errAt
|
|
end
|
|
i = e
|
|
end
|
|
end
|
|
return root, nil, nil
|
|
end
|
|
|
|
return Decode
|