fallbacks/lxp.lua
author Kim Alvefur <zash@zash.se>
Mon, 12 Dec 2022 07:03:31 +0100
branch0.11
changeset 12802 c4b1b5cbc20b
parent 5776 bd0ff8ae98a8
permissions -rw-r--r--
Tag 0.11.14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     2
local coroutine = coroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     3
local tonumber = tonumber;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     4
local string = string;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     5
local setmetatable, getmetatable = setmetatable, getmetatable;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
local pairs = pairs;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     8
local deadroutine = coroutine.create(function() end);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
coroutine.resume(deadroutine);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    11
module("lxp")
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
local entity_map = setmetatable({
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
	["amp"] = "&";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
	["gt"] = ">";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    16
	["lt"] = "<";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
	["apos"] = "'";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
	["quot"] = "\"";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
}, {__index = function(_, s)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
		if s:sub(1,1) == "#" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
			if s:sub(2,2) == "x" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
				return string.char(tonumber(s:sub(3), 16));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
			else
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    24
				return string.char(tonumber(s:sub(2)));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    25
			end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    28
});
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
local function xml_unescape(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    30
	return (str:gsub("&(.-);", entity_map));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    31
end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    32
local function parse_tag(s)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    33
	local name,sattr=(s):gmatch("([^%s]+)(.*)")();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
	local attr = {};
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    35
	for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    36
	return name, attr;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    37
end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    38
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    39
local function parser(data, handlers, ns_separator)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
	local function read_until(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
		local pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    42
		while not pos do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    43
			data = data..coroutine.yield();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    44
			pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
		local r = data:sub(1, pos);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    47
		data = data:sub(pos+1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    48
		return r;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    49
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    50
	local function read_before(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    51
		local pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    52
		while not pos do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
			data = data..coroutine.yield();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
			pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
		local r = data:sub(1, pos-1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
		data = data:sub(pos);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    58
		return r;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    59
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    60
	local function peek()
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    61
		while #data == 0 do data = coroutine.yield(); end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    62
		return data:sub(1,1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    63
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 3899
diff changeset
    64
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    65
	local ns = { xml = "http://www.w3.org/XML/1998/namespace" };
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    66
	ns.__index = ns;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    67
	local function apply_ns(name, dodefault)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    68
		local prefix,n = name:match("^([^:]*):(.*)$");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    69
		if prefix and ns[prefix] then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    70
			return ns[prefix]..ns_separator..n;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    71
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    72
		if dodefault and ns[""] then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    73
			return ns[""]..ns_separator..name;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    74
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    75
		return name;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    76
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    77
	local function push(tag, attr)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    78
		ns = setmetatable({}, ns);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    79
		for k,v in pairs(attr) do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    80
			local xmlns = k == "xmlns" and "" or k:match("^xmlns:(.*)$");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    81
			if xmlns then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    82
				ns[xmlns] = v;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    83
				attr[k] = nil;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    84
			end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    85
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    86
		local newattr, n = {}, 0;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    87
		for k,v in pairs(attr) do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    88
			n = n+1;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    89
			k = apply_ns(k);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    90
			newattr[n] = k;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    91
			newattr[k] = v;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    92
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    93
		tag = apply_ns(tag, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    94
		ns[0] = tag;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    95
		ns.__index = ns;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    96
		return tag, newattr;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
	local function pop()
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
		local tag = ns[0];
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
		ns = getmetatable(ns);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   101
		return tag;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 3899
diff changeset
   103
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   104
	while true do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   105
		if peek() == "<" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   106
			local elem = read_until(">"):sub(2,-2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   107
			if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
			elseif elem:sub(1,1) == "/" then -- end tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
				elem = elem:sub(2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
				local name = pop();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   111
				handlers:EndElement(name); -- TODO check for start-end tag name match
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   112
			elseif elem:sub(-1,-1) == "/" then -- empty tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   113
				elem = elem:sub(1,-2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
				local name,attr = parse_tag(elem);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   115
				name,attr = push(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   116
				handlers:StartElement(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   117
				name = pop();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   118
				handlers:EndElement(name);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   119
			else -- start tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   120
				local name,attr = parse_tag(elem);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   121
				name,attr = push(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   122
				handlers:StartElement(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   123
			end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   124
		else
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   125
			local text = read_before("<");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   126
			handlers:CharacterData(xml_unescape(text));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   127
		end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   128
	end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   129
end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   130
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   131
function new(handlers, ns_separator)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   132
	local co = coroutine.create(parser);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   133
	return {
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   134
		parse = function(self, data)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   135
			if not data then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   136
				co = deadroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   137
				return true; -- eof
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   138
			end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   139
			local success, result = coroutine.resume(co, data, handlers, ns_separator);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   140
			if result then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   141
				co = deadroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   142
				return nil, result; -- error
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   143
			end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   144
			return true; -- success
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   145
		end;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   146
	};
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   147
end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   148
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   149
return _M;