将Lua简单表格存储为字符串反序列化

我正在将一个包含Lua表格文本的字符串从网络应用程序传输到 PICO-8,试图将其反序列化为 PICO-8 中的Lua表格。

该字符串的格式为 '{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'

为了保持简单,我只打算在字符串中使用小写字符,并且只允许嵌套表格中存在字符串。

我感觉我已经理解如何解析这些字符,但我不知道如何跟踪我在重新创建的数据中的位置,包括结构深度和索引。

通常要如何处理这个问题?

问题在于 PICO-8 Lua 不包含 loadloadstring,因此必须手动进行解析。以下代码使用 table.insertstring.sub 代替 PICO-8 的等效函数,因为我在使用 Lua REPL 原型化此代码。

这是我到目前为止的代码,其中包含所需的打印语句。

如果能得到帮助,将不胜感激。

test_obj = {"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}
data_string = '{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'
data = nil
string = ''
level = 0
while #data_string > 0 do
 local d=string.sub(data_string,1,1)
  if stringChar(d) then
    string = string..d
  end
  if comma(d) then
    print(string)
    table.insert(data, string)
    string = ''
  end
  if openBracket(d) then
      if data == nil then
      data = {}
      print('new table')
    else
      print('insert table')
    end
    level = level + 1
    print('on level', level)
  end
  if closeBracket(d) then
    print('end of table')
    level = level - 1
    print('up to level', level)
  end
  data_string=string.sub(data_string,2)
end
点赞
用户6834680
用户6834680

使用 Lua 模式来避免每个字符的解析

local function read_exp_list(s)
   local exps, res = {}, {}
   local function save(v)
      exps[#exps + 1] = v
      return ('\0'):rep(#exps)
   end
   s = s:gsub('%b{}', function(s) return save{read_exp_list(s:sub(2, -2))} end) -- 数组
   s = s:gsub('"(.-)"', save)                                                   -- 字符串
   s = s:gsub('%-?%d+', function(s) return save(tonumber(s)) end)               -- 整数
   for k in s:gmatch'%z+' do
      res[#res + 1] = exps[#k]
   end
   return (table.unpack or unpack)(res)
end

local data_string = '{-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'
local obj = read_exp_list(data_string)
-- obj == {-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}

字符串必须用 " 括起来,不能包含字符 {}\。字符串可以是空的。

数字必须是十进制表示的整数,可选带负号。

数组必须包含只有字符串、数字和子数组。数组可以是空的。


更新:

以下代码仅使用函数 string.subtable.inserttonumbertype

local function is_digit(c)
   return c >= '0' and c <= '9'
end

local function read_from_string(input)
   if type(input) == 'string' then
      local data = input
      local pos = 0
      function input(undo)
         if undo then
            pos = pos - 1
         else
            pos = pos + 1
            return string.sub(data, pos, pos)
         end
      end
   end
   local c
   repeat
      c = input()
   until c ~= ' ' and c ~= ','
   if c == '"' then
      local s = ''
      repeat
         c = input()
         if c == '"' then
            return s
         end
         s = s..c
      until c == ''
   elseif c == '-' or is_digit(c) then
      local s = c
      repeat
         c = input()
         local d = is_digit(c)
         if d then
            s = s..c
         end
      until not d
      input(true)
      return tonumber(s)
   elseif c == '{' then
      local arr = {}
      local elem
      repeat
         elem = read_from_string(input)
         table.insert(arr, elem)
      until not elem
      return arr
   end
end

local data_string = '{-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'
local obj = read_from_string(data_string)
-- obj == {-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}

字符串必须用 " 括起来,不能包含字符 \。字符串可以是空的。

数字必须是十进制表示的整数,可选带负号。

数组必须包含只有字符串、数字和子数组。数组可以是空的。

2018-08-06 11:41:18