Modul:SimpleStruct

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

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


--[=[ SimpleStruct 2021-12-21
Module for generating tables and strukts from simple strings
Author:		Vollbracht
Version:	2022-07-03	no change to previous; added altParse

* SimpleStruct.parse(string)
* SimpleStruct.altParse(string)	variant interpretation of more limited string
]=]

-- Module globals
local SimpleStruct = {}

--[[
	altParse
	alternative Parser for strictly limited string parameters
	StringPar	string in Format 
					<Label1>{<value1>}
					<Label2>{<value2>}
					<Label3>{<value3>}
	returns table ordered by input positions
		{{<Label1>, <value1>}, {<Label2>, <value2>}, {<Label3>, <value3>}}
	where Parser.parse would return table without order
		{<Label1>{<value1>}, <Label2>{<value2>}, <Label3>{<value3>}}
]]
SimpleStruct.altParse = function(StringPar)
	local result = {}
	local tabR = mw.text.split(StringPar, '}')
	if string.gsub(tabR[#tabR], '^%s*(.-)%s*$', '%1') == '' then
		tabR[#tabR] = nil
	end
	for i, elmR in ipairs(tabR) do
		local tabL = mw.text.split(elmR, '{')
		if #tabL ~= 2 then
			table.insert(result, {"Fehler", "Bitte hier nur Elemente im Format <label>{<Wert>} eintragen! [[Kategorie:Wikipedia:Qualitätssicherung Vorlageneinbindung fehlerhaft]] "})
		else
			table.insert(result,
				{string.gsub(tabL[1], '^%s*(.-)%s*$', '%1'),
				 string.gsub(tabL[2], '^%s*(.-)%s*$', '%1')})
		end
	end
	return result
end

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