Die Dokumentation für dieses Modul kann unter Modul:Benutzer:Vollbracht/SimpleStruct/Doku erstellt werden

--[=[ SimpleStruct 2021-12-21
Module for generating tables and strukts from simple strings
Author: Vollbracht
* SimpleStruct.parse(string)
]=]

-- Module globals
local SimpleStruct = {}

local ElmList
local currentIndex
local StopIndex

-- Iterator, omitting all ipairs
local namePairs = function(namedTable)
  local nmKeys = {}
  for elm in pairs(namedTable) do
    if type(elm) == 'string' then table.insert(nmKeys, elm) end
  end
  local i = 0
  local namePairs_iterator = function()
    i = i + 1
    return nmKeys[i], namedTable[nmKeys[i]]
  end
  return namePairs_iterator
end

local function generateStruct()
  local result = {}
  while currentIndex <= StopIndex do
    if type(ElmList[currentIndex]) == 'string' then
      table.insert(result, ElmList[currentIndex])
      currentIndex = currentIndex + 1
    elseif ElmList[currentIndex] == -1 then
      currentIndex = currentIndex + 1
      return result
    else
      -- assert no changes to ElmList structure: only string, table, or -1
      assert(type(ElmList[currentIndex]) == 'table')
      local name = ElmList[currentIndex][1]
      currentIndex = currentIndex + 1
      local childValue = generateStruct()
      if name == '' then
        -- containing unnamed structure
        -- copy name-value pairs from child
        for pname, value in namePairs(childValue) do result[pname] = value end
        -- copy unnamed values from child
        for _, value in ipairs(childValue) do table.insert(result, value) end
      else
        -- assert store
        if result[name] == nil then result[name] = {} end
        -- copy name-value pairs from child
        for pname, value in namePairs(childValue) do
          if type(value) == 'table' and #value == 1 and type(value[1]) == 'string'
            then result[name][pname] = value[1]
            else result[name][pname] = value
          end
        end
        -- copy unnamed values from child
        for i, value in ipairs(childValue) do
          table.insert(result[name], value)
        end
      end
    end
  end
  return result
end

SimpleStruct.parse = function (StringPar)
  if StringPar == '' then
    ElmList = {}
    currentIndex = 0
    StopIndex = 0
    return
  end
  local tabR = mw.text.split(StringPar, '}')
  -- trim: prevent tailing empty portion from being processed
  if string.gsub(tabR[#tabR], '^%s*(.-)%s*$', '%1') == '' then tabR[#tabR] = nil end
  -- process all portions
  ElmList = {}
  for i, elmR in ipairs(tabR) do
    -- trim:
    elmR = string.gsub(elmR, '^%s*(.-)%s*$', '%1')
    if elmR == '' then -- nothing but closing bracket
      table.insert(ElmList, -1)
    else
      local tabL = mw.text.split(elmR, '{')
      if #tabL == 1 then -- plain value before closing bracket
        -- process plain value
        table.insert(ElmList, elmR)
        -- process bracket
        table.insert(ElmList, -1)
      else
        local j = 1
        while j < #tabL do
          -- process opening brackets
          table.insert(ElmList, {string.gsub(tabL[j], '^%s*(.-)%s*$', '%1')})
          j = j + 1
        end
        -- process plain value
        local v = string.gsub(tabL[j], '^%s*(.-)%s*$', '%1')
        table.insert(ElmList, v)
        -- process bracket
        table.insert(ElmList, -1)
      end
    end
  end
  currentIndex = 1
  StopIndex = #ElmList
  local result = generateStruct()
  return result
end

return SimpleStruct