Skip to content

Commit b9ed669

Browse files
committed
Fix some isues in SNBT parsing
- Accept the full range of unquoted strings - Fix error when failing to parse an unquoted string See #2277. This is not sufficient to close the issue (wow, there's so much more wrong with the code), but at least stops unserialiseJSON crashing.
1 parent d0fbec6 commit b9ed669

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ do
616616
end
617617

618618
local function parse_ident(str, pos)
619-
local _, last, val = find(str, '^([%a][%w_]*)', pos)
619+
local _, last, val = find(str, '^([%w_+.-]+)', pos)
620+
if not last then error_at(pos, "Expected object key") end
620621
return val, last + 1
621622
end
622623

projects/core/src/test/resources/test-rom/spec/apis/textutils_spec.lua

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,47 @@ describe("The textutils library", function()
246246
describe("parses using NBT-style syntax", function()
247247
local function exp(x)
248248
local res, err = textutils.unserializeJSON(x, { nbt_style = true })
249-
if not res then error(err, 2) end
249+
if not res then fail(err) end
250250
return expect(res)
251251
end
252+
253+
local function exp_err(x)
254+
local res, err = textutils.unserializeJSON(x, { nbt_style = true })
255+
if res ~= nil then
256+
fail(("Expected %q not to parse, but returned %s"):format(x, textutils.serialise(res)))
257+
end
258+
return expect(err)
259+
end
260+
252261
it("basic objects", function()
253-
exp([[{ a: 1, b:2 }]]):same { a = 1, b = 2 }
262+
exp("{ a: 1, b:2 }"):same { a = 1, b = 2 }
263+
exp("{0+_-.aA: 1}"):same { ["0+_-.aA"] = 1 }
264+
exp("{}"):same {}
265+
266+
exp_err("{: 123}"):eq("Malformed JSON at position 2: Expected object key")
267+
exp_err("{#: 123}"):eq("Malformed JSON at position 2: Expected object key")
254268
end)
255269

256270
it("suffixed numbers", function()
257271
exp("1b"):eq(1)
258272
exp("1.1d"):eq(1.1)
259273
end)
260274

261-
it("strings", function()
262-
exp("'123'"):eq("123")
263-
exp("\"123\""):eq("123")
275+
describe("strings", function()
276+
it("empty quoted strings", function()
277+
exp("''"):eq("")
278+
exp("\"\""):eq("")
279+
end)
280+
281+
pending("unquoted strings", function()
282+
exp("hello"):eq("hello")
283+
exp("0+_-.aA"):eq("0+_-.aA")
284+
end)
285+
286+
it("quoted strings", function()
287+
exp("'123'"):eq("123")
288+
exp("\"123\""):eq("123")
289+
end)
264290
end)
265291

266292
it("typed arrays", function()

0 commit comments

Comments
 (0)