Spring til indhold

Modul:CoorCheck

Fra Wikipedia, den frie encyklopædi
local p = {}
function p.extract(frame)
	stringy = frame.args[1]
	elem = frame.args[2]
	dosubelem = frame.args[3]
	tab =  p.rec(stringy, { subvalues = {} }, 1)
	if dosubelem then return tab.subvalues[elem] else return tab[elem] end
end

function p.check(frame)
	stringy = frame.args[1]
	return p.innercheck(stringy)
end

function p.innercheck(haystack)
	if not haystack then return "Error" end
	somecolon = string.find(haystack, ":")
	if not somecolon then return "Error" end
	allowed = { globe = 1, region = 1, scale = 1, subvalues = 1}
	allowed["type"] = 1
	tab = p.rec(haystack, { subvalues = {} }, 1)
	for key,value in pairs(tab) do
		found = allowed[key]
		if not found or
		   not value or
		   value == "" or
		   (type(value) == "string" and string.find(value, ":"))
		then return "Error" end
	end
	return
end

function p.rec(haystack, accum, pos)
	poscol = string.find(haystack, ":", pos)
	if not poscol then
		return accum
	end
	key = string.sub(haystack, pos, poscol - 1)
	posunder = string.find(haystack, "_", pos)
	if posunder then
		posbracket = string.find(haystack, "%(", pos)
		if posbracket and posbracket < posunder then
			value = string.sub(haystack, poscol + 1, posbracket - 1)
			subvalue = string.sub(haystack, posbracket + 1, posunder - 2)
			accum[key] = value
			accum.subvalues[key] = subvalue
		else 
			value = string.sub(haystack, poscol + 1, posunder - 1)
			accum[key] = value
		end
		return p.rec(haystack, accum, posunder + 1)
	else
		value = string.sub(haystack, poscol + 1)
		accum[key] = value
		return accum
	end
end

return p