Skip to content

Commit 6b5bbfb

Browse files
author
Sabine Lim
authored
Refactor XML parser (#82)
* Move XMLParser into its own file * Move xmlValue to xmlParser * Remove getters and setters * Simplify XMLNode data structure * Remove unused functions * Clarify comments * Rename data to node * Rename name to tag * Reorder * Remove all special uses of xmlValue
1 parent d6642ad commit 6b5bbfb

File tree

11 files changed

+328
-660
lines changed

11 files changed

+328
-660
lines changed

Basalt/libraries/utils.lua

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -273,28 +273,6 @@ removeTags = removeTags,
273273

274274
wrapText = wrapText,
275275

276-
xmlValue = function(name, tab)
277-
local var
278-
if(type(tab)~="table")then return end
279-
if(tab[name]~=nil)then
280-
if(type(tab[name])=="table")then
281-
if(tab[name].value~=nil)then
282-
var = tab[name]:value()
283-
end
284-
end
285-
end
286-
if(var==nil)then var = tab["@"..name] end
287-
288-
if(var=="true")then
289-
var = true
290-
elseif(var=="false")then
291-
var = false
292-
elseif(tonumber(var)~=nil)then
293-
var = tonumber(var)
294-
end
295-
return var
296-
end,
297-
298276
convertRichText = convertRichText,
299277

300278
--- Writes text with special color tags

Basalt/libraries/xmlParser.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
local XMLNode = {
2+
new = function(tag)
3+
return {
4+
tag = tag,
5+
value = nil,
6+
attributes = {},
7+
children = {},
8+
9+
addChild = function(self, child)
10+
table.insert(self.children, child)
11+
end,
12+
13+
addAttribute = function(self, tag, value)
14+
self.attributes[tag] = value
15+
end
16+
}
17+
end
18+
}
19+
20+
local parseAttributes = function(node, s)
21+
-- Parse "" style string attributes
22+
local _, _ = string.gsub(s, "(%w+)=([\"'])(.-)%2", function(attribute, _, value)
23+
node:addAttribute(attribute, "\"" .. value .. "\"")
24+
end)
25+
-- Parse {} style computed attributes
26+
local _, _ = string.gsub(s, "(%w+)={(.-)}", function(attribute, expression)
27+
node:addAttribute(attribute, expression)
28+
end)
29+
end
30+
31+
local XMLParser = {
32+
parseText = function(xmlText)
33+
local stack = {}
34+
local top = XMLNode.new()
35+
table.insert(stack, top)
36+
local ni, c, label, xarg, empty
37+
local i, j = 1, 1
38+
while true do
39+
ni, j, c, label, xarg, empty = string.find(xmlText, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
40+
if not ni then break end
41+
local text = string.sub(xmlText, i, ni - 1);
42+
if not string.find(text, "^%s*$") then
43+
local lVal = (top.value or "") .. text
44+
stack[#stack].value = lVal
45+
end
46+
if empty == "/" then -- empty element tag
47+
local lNode = XMLNode.new(label)
48+
parseAttributes(lNode, xarg)
49+
top:addChild(lNode)
50+
elseif c == "" then -- start tag
51+
local lNode = XMLNode.new(label)
52+
parseAttributes(lNode, xarg)
53+
table.insert(stack, lNode)
54+
top = lNode
55+
else -- end tag
56+
local toclose = table.remove(stack) -- remove top
57+
58+
top = stack[#stack]
59+
if #stack < 1 then
60+
error("XMLParser: nothing to close with " .. label)
61+
end
62+
if toclose.tag ~= label then
63+
error("XMLParser: trying to close " .. toclose.tag .. " with " .. label)
64+
end
65+
top:addChild(toclose)
66+
end
67+
i = j + 1
68+
end
69+
local text = string.sub(xmlText, i);
70+
if #stack > 1 then
71+
error("XMLParser: unclosed " .. stack[#stack].tag)
72+
end
73+
return top
74+
end
75+
}
76+
77+
return XMLParser

Basalt/plugins/advancedBackground.lua

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
local utils = require("utils")
2-
local xmlValue = utils.xmlValue
1+
local XMLParser = require("xmlParser")
32

43
return {
54
VisualObject = function(base)
@@ -29,12 +28,6 @@ return {
2928
return bgSymbolColor
3029
end,
3130

32-
setValuesByXMLData = function(self, data, scripts)
33-
base.setValuesByXMLData(self, data, scripts)
34-
if(xmlValue("background-symbol", data)~=nil)then self:setBackgroundSymbol(xmlValue("background-symbol", data), xmlValue("background-symbol-color", data)) end
35-
return self
36-
end,
37-
3831
draw = function(self)
3932
base.draw(self)
4033
self:addDraw("advanced-bg", function()
@@ -45,8 +38,8 @@ return {
4538
self:addForegroundBox(1, 1, w, h, bgSymbolColor)
4639
end
4740
end
48-
end, 2)
49-
end,
41+
end, 2)
42+
end
5043
}
5144

5245
return object

Basalt/plugins/animations.lua

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ local lerp = {
215215
easeInOutBounce=easeInOutBounce,
216216
}
217217

218-
local utils = require("utils")
219-
local xmlValue = utils.xmlValue
218+
local XMLParser = require("xmlParser")
220219

221220
return {
222221
VisualObject = function(base, basalt)
@@ -337,24 +336,7 @@ return {
337336
end
338337
end
339338
end
340-
end,
341-
342-
setValuesByXMLData = function(self, data, scripts)
343-
base.setValuesByXMLData(self, data, scripts)
344-
local animX, animY, animateDuration, animeteTimeOffset, animateMode = xmlValue("animateX", data), xmlValue("animateY", data), xmlValue("animateDuration", data), xmlValue("animateTimeOffset", data), xmlValue("animateMode", data)
345-
local animW, animH, animateDuration, animeteTimeOffset, animateMode = xmlValue("animateW", data), xmlValue("animateH", data), xmlValue("animateDuration", data), xmlValue("animateTimeOffset", data), xmlValue("animateMode", data)
346-
local animXOffset, animYOffset, animateDuration, animeteTimeOffset, animateMode = xmlValue("animateXOffset", data), xmlValue("animateYOffset", data), xmlValue("animateDuration", data), xmlValue("animateTimeOffset", data), xmlValue("animateMode", data)
347-
if(animX~=nil and animY~=nil)then
348-
self:animatePosition(animX, animY, animateDuration, animeteTimeOffset, animateMode)
349-
end
350-
if(animW~=nil and animH~=nil)then
351-
self:animateSize(animW, animH, animateDuration, animeteTimeOffset, animateMode)
352-
end
353-
if(animXOffset~=nil and animYOffset~=nil)then
354-
self:animateOffset(animXOffset, animYOffset, animateDuration, animeteTimeOffset, animateMode)
355-
end
356-
return self
357-
end,
339+
end
358340
}
359341

360342
return object

Basalt/plugins/bigfonts.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ local function makeText(nSize, sString, nFC, nBC, bBlit)
140140
end
141141

142142
-- The following code is related to basalt and has nothing to do with bigfonts, it creates a plugin which will be added to labels:
143-
local utils = require("utils")
144-
local xmlValue = utils.xmlValue
143+
local XMLParser = require("xmlParser")
145144
return {
146145
Label = function(base)
147146
local fontsize = 1
@@ -196,12 +195,6 @@ return {
196195
end
197196
end,
198197

199-
setValuesByXMLData = function(self, data, scripts)
200-
base.setValuesByXMLData(self, data, scripts)
201-
if(xmlValue("fontSize", data)~=nil)then self:setFontSize(xmlValue("fontSize", data)) end
202-
return self
203-
end,
204-
205198
draw = function(self)
206199
base.draw(self)
207200
self:addDraw("bigfonts", function()
@@ -220,7 +213,7 @@ return {
220213
end
221214
end
222215
end)
223-
end,
216+
end
224217
}
225218
return object
226219
end

Basalt/plugins/border.lua

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
local utils = require("utils")
2-
local xmlValue = utils.xmlValue
1+
local XMLParser = require("xmlParser")
32

43
return {
54
VisualObject = function(base)
@@ -80,23 +79,6 @@ return {
8079
end
8180
end
8281
end)
83-
end,
84-
85-
setValuesByXMLData = function(self, data, scripts)
86-
base.setValuesByXMLData(self, data)
87-
local borders = {}
88-
if(xmlValue("border", data)~=nil)then
89-
borders["top"] = colors[xmlValue("border", data)]
90-
borders["bottom"] = colors[xmlValue("border", data)]
91-
borders["left"] = colors[xmlValue("border", data)]
92-
borders["right"] = colors[xmlValue("border", data)]
93-
end
94-
if(xmlValue("borderTop", data)~=nil)then borders["top"] = colors[xmlValue("borderTop", data)] end
95-
if(xmlValue("borderBottom", data)~=nil)then borders["bottom"] = colors[xmlValue("borderBottom", data)] end
96-
if(xmlValue("borderLeft", data)~=nil)then borders["left"] = colors[xmlValue("borderLeft", data)] end
97-
if(xmlValue("borderRight", data)~=nil)then borders["right"] = colors[xmlValue("borderRight", data)] end
98-
self:setBorder(borders["top"], borders["bottom"], borders["left"], borders["right"])
99-
return self
10082
end
10183
}
10284

Basalt/plugins/dynamicValues.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require("utils")
22
local count = utils.tableCount
3-
local xmlValue = utils.xmlValue
43

54
return {
65
VisualObject = function(base, basalt)

0 commit comments

Comments
 (0)