Zum Inhalt springen

Modul:SimpleStruct

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 21. Dezember 2021 um 03:39 Uhr durch Vollbracht (Diskussion | Beiträge). Sie kann sich erheblich von der aktuellen Version unterscheiden.
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[=[ SimpleStruct 2021-12-21
Modul zur Generierung von Tabllen und Strukturen aus einfachen Zeichenketten
* SimpleStruct.parse(string)
]=]

-- Module globals
local messagePrefix = "lua-module-simpleStruct-"
local SimpleStruct = {}

local ElmList
local currentIndex
local StopIndex

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 all pairs from child
        for i, value in pairs(childValue) do
          if type(i) == 'number' then table.insert(result[name], value)
          else
			assert(type(i) == 'string')
	        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
        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