Refactor XML parser #82

Merged
thesabinelim merged 10 commits from xml-parser-refactor into master 2023-05-16 21:07:42 +08:00
2 changed files with 21 additions and 21 deletions
Showing only changes of commit 3ded892887 - Show all commits

View File

@@ -1,27 +1,27 @@
local XMLNode = { local XMLNode = {
new = function(name) new = function(tag)
return { return {
name = name, tag = tag,
value = nil, value = nil,
children = {}, children = {},
attributes = {}, attributes = {},
addChild = function(self, child) addChild = function(self, child)
if self[child.name] ~= nil then if self[child.tag] ~= nil then
if type(self[child.name].name) == "function" then if type(self[child.tag].tag) == "function" then
local tempTable = {} local tempTable = {}
table.insert(tempTable, self[child.name]) table.insert(tempTable, self[child.tag])
self[child.name] = tempTable self[child.tag] = tempTable
end end
table.insert(self[child.name], child) table.insert(self[child.tag], child)
else else
self[child.name] = child self[child.tag] = child
end end
table.insert(self.children, child) table.insert(self.children, child)
end, end,
addAttribute = function(self, name, value) addAttribute = function(self, tag, value)
self.attributes[name] = value self.attributes[tag] = value
end end
} }
end end
@@ -39,17 +39,17 @@ local parseAttributes = function(node, s)
end end
local XMLParser = { local XMLParser = {
xmlValue = function(name, tab) xmlValue = function(tag, tab)
local var local var
if(type(tab)~="table")then return end if(type(tab)~="table")then return end
if(tab[name]~=nil)then if(tab[tag]~=nil)then
if(type(tab[name])=="table")then if(type(tab[tag])=="table")then
if(tab[name].value~=nil)then if(tab[tag].value~=nil)then
var = tab[name].value var = tab[tag].value
end end
end end
end end
if(var==nil)then var = tab["@"..name] end if(var==nil)then var = tab["@"..tag] end
if(var=="true")then if(var=="true")then
var = true var = true
@@ -91,8 +91,8 @@ local XMLParser = {
if #stack < 1 then if #stack < 1 then
error("XMLParser: nothing to close with " .. label) error("XMLParser: nothing to close with " .. label)
end end
if toclose.name ~= label then if toclose.tag ~= label then
error("XMLParser: trying to close " .. toclose.name .. " with " .. label) error("XMLParser: trying to close " .. toclose.tag .. " with " .. label)
end end
top:addChild(toclose) top:addChild(toclose)
end end
@@ -100,7 +100,7 @@ local XMLParser = {
end end
local text = string.sub(xmlText, i); local text = string.sub(xmlText, i);
if #stack > 1 then if #stack > 1 then
error("XMLParser: unclosed " .. stack[#stack].name) error("XMLParser: unclosed " .. stack[#stack].tag)
end end
return top return top
end end

View File

@@ -4,7 +4,7 @@ local uuid = utils.uuid
local function maybeExecuteScript(nodeTree, renderContext) local function maybeExecuteScript(nodeTree, renderContext)
for _, node in ipairs(nodeTree.children) do for _, node in ipairs(nodeTree.children) do
if (node.name == "script") then if (node.tag == "script") then
return load(node.value, nil, "t", renderContext.env)() return load(node.value, nil, "t", renderContext.env)()
end end
end end
@@ -201,7 +201,7 @@ return {
local _OBJECTS = basalt.getObjects() local _OBJECTS = basalt.getObjects()
for _, childNode in pairs(node.children) do for _, childNode in pairs(node.children) do
local tagName = childNode.name local tagName = childNode.tag
if (tagName ~= "animation") then if (tagName ~= "animation") then
local layout = renderContext.env[tagName] local layout = renderContext.env[tagName]
local objectKey = tagName:gsub("^%l", string.upper) local objectKey = tagName:gsub("^%l", string.upper)