Modul:Benutzer:Vollbracht/SimpleStruct

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.