1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
-- originally from http://lua-users.org/wiki/LuaXml
-- modified by me a bit
local function parseargs(s)
local arg = {}
string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
local function tag(x)
return setmetatable(x, {__call=function(x, label)
-- search for child with given label
for _,c in ipairs(x) do
if c.label == label then
return c
end
end
return nil
end})
end
local psingle
local function pmulti(s, i, parent)
::again::
local nexti, child = psingle(s, i)
if child.close and child.label == parent.label then
return nexti, parent
else
table.insert(parent, child)
i = nexti
goto again
end
end
psingle = function(s, i)
i = i or 1
local ts,j,c,label,xarg,empty = s:find("<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ts then
local rest = s:sub(i)
if rest:find("<",i) then
error('ill formed (eof?)')
elseif #rest == 0 then
error('empty string')
else
return i+#rest, rest
end
end
local nexti = j+1
local pretext = s:sub(i, ts-1)
if not pretext:find("^%s*$") then -- not entirely whitespace
return ts, pretext
end
if empty == "/" then
return nexti, tag{label=label, xarg=parseargs(xarg), empty=true}
elseif c == "" then -- start tag
return pmulti(s, nexti, tag{label=label, xarg=parseargs(xarg)})
else -- end tag
return nexti, tag{label=label, close=true}
end
end
local wrap
do
local _, cqa = pcall(require, 'cqueues.auxlib')
wrap = cqa and cqa.wrap or coroutine.wrap
end
local function stanzae(getmore)
return wrap(function()
local buf = ''
while true do
local ok, ni, el = pcall(psingle, buf)
if ok then
coroutine.yield(el)
buf = buf:sub(ni)
else
local more = getmore()
buf = buf .. more
end
end
end)
end
return { psingle = psingle, stanzae = stanzae, wrap=wrap }
|