-
Notifications
You must be signed in to change notification settings - Fork 4
/
nxml.lua
571 lines (468 loc) · 14.7 KB
/
nxml.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
local ffi = nil
if require then
pcall(function()
ffi = require("ffi")
end)
end
local str_sub
local str_index
local str_normalize
if ffi then
str_normalize = function(str)
return ffi.cast("const char*", str)
end
str_sub = function(ptr, start_idx, len)
return ffi.string(ptr + start_idx, len)
end
str_index = function(ptr, idx)
return ptr[idx]
end
else
str_normalize = function(str) return str end
str_sub = function(str, start_idx, len)
return str:sub(start_idx + 1, start_idx + len)
end
str_index = function(str, idx)
return string.byte(str:sub(idx + 1, idx + 1))
end
end
--[[
* The following is a Lua port of the NXML parser:
* https://github.com/xwitchproject/nxml
*
* The NXML Parser is heavily based on code from poro
* https://github.com/gummikana/poro
*
* The poro project is licensed under the Zlib license:
*
* --------------------------------------------------------------------------
* Copyright (c) 2010-2019 Petri Purho, Dennis Belfrage
* Contributors: Martin Jonasson, Olli Harjola
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* --------------------------------------------------------------------------
]]
local nxml = {}
local TOKENIZER_FUNCS = {}
local TOKENIZER_MT = {
__index = TOKENIZER_FUNCS,
__tostring = function(self) return "natif.nxml.tokenizer" end
}
local function new_tokenizer(cstring, len)
return setmetatable({
data = cstring,
cur_idx = 0,
cur_row = 1,
cur_col = 1,
prev_row = 1,
prev_col = 1,
len = len
}, TOKENIZER_MT)
end
local ws = {
[string.byte(" ")] = true,
[string.byte("\t")] = true,
[string.byte("\n")] = true,
[string.byte("\r")] = true
}
function TOKENIZER_FUNCS:is_whitespace(char)
local n = tonumber(char)
return ws[n] or false
end
local punct = {
[string.byte("<")] = true,
[string.byte(">")] = true,
[string.byte("=")] = true,
[string.byte("/")] = true,
}
function TOKENIZER_FUNCS:is_whitespace_or_punctuation(char)
local n = tonumber(char)
return self:is_whitespace(n) or punct[n] or false
end
function TOKENIZER_FUNCS:move(n)
n = n or 1
local prev_idx = self.cur_idx
self.cur_idx = self.cur_idx + n
if self.cur_idx >= self.len then
self.cur_idx = self.len
return
end
for i = prev_idx, self.cur_idx - 1 do
if str_index(self.data, i) == string.byte("\n") then
self.cur_row = self.cur_row + 1
self.cur_col = 1
else
self.cur_col = self.cur_col + 1
end
end
end
function TOKENIZER_FUNCS:peek(n)
n = n or 1
local idx = self.cur_idx + n
if idx >= self.len then return 0 end
return str_index(self.data, idx)
end
function TOKENIZER_FUNCS:match_string(str)
local len = #str
str = str_normalize(str)
for i = 0, len - 1 do
if self:peek(i) ~= str_index(str, i) then return false end
end
return true
end
function TOKENIZER_FUNCS:eof()
return self.cur_idx >= self.len
end
function TOKENIZER_FUNCS:cur_char()
if self:eof() then return 0 end
return tonumber(str_index(self.data, self.cur_idx))
end
function TOKENIZER_FUNCS:skip_whitespace()
while not self:eof() do
if self:is_whitespace(self:cur_char()) then
self:move()
elseif self:match_string("<!--") then
self:move(4)
while not self:eof() and not self:match_string("-->") do
self:move()
end
if self:match_string("-->") then
self:move(3)
end
elseif self:cur_char() == string.byte("<") and self:peek(1) == string.byte("!") then
self:move(2)
while not self:eof() and self:cur_char() ~= string.byte(">") do
self:move()
end
if self:cur_char() == string.byte(">") then
self:move()
end
elseif self:match_string("<?") then
self:move(2)
while not self:eof() and not self:match_string("?>") do
self:move()
end
if self:match_string("?>") then
self:move(2)
end
else
break
end
end
end
function TOKENIZER_FUNCS:read_quoted_string()
local start_idx = self.cur_idx
local len = 0
while not self:eof() and self:cur_char() ~= string.byte("\"") do
len = len + 1
self:move()
end
self:move() -- skip "
return str_sub(self.data, start_idx, len)
end
function TOKENIZER_FUNCS:read_unquoted_string()
local start_idx = self.cur_idx - 1 -- first char is move()d
local len = 1
while not self:eof() and not self:is_whitespace_or_punctuation(self:cur_char()) do
len = len + 1
self:move()
end
return str_sub(self.data, start_idx, len)
end
local C_NULL = 0
local C_LT = string.byte("<")
local C_GT = string.byte(">")
local C_SLASH = string.byte("/")
local C_EQ = string.byte("=")
local C_QUOTE = string.byte("\"")
function TOKENIZER_FUNCS:next_token()
self:skip_whitespace()
self.prev_row = self.cur_row
self.prev_col = self.cur_col
if self:eof() then return nil end
local c = self:cur_char()
self:move()
if c == C_NULL then return nil
elseif c == C_LT then return { type = "<" }
elseif c == C_GT then return { type = ">" }
elseif c == C_SLASH then return { type = "/" }
elseif c == C_EQ then return { type = "=" }
elseif c == C_QUOTE then return { type = "string", value = self:read_quoted_string() }
else return { type = "string", value = self:read_unquoted_string() }
end
end
local PARSER_FUNCS = {}
local PARSER_MT = {
__index = PARSER_FUNCS,
__tostring = function(self) return "natif.nxml.parser" end
}
local function new_parser(tokenizer, error_reporter)
return setmetatable({
tok = tokenizer,
errors = {},
error_reporter = error_reporter or function(type, msg) print("parser error: [" .. type .. "] " .. msg) end
}, PARSER_MT)
end
local XML_ELEMENT_FUNCS = {}
local XML_ELEMENT_MT = {
__index = XML_ELEMENT_FUNCS,
__tostring = function(self)
return nxml.tostring(self)
end,
}
function PARSER_FUNCS:report_error(type, msg)
self.error_reporter(type, msg)
table.insert(self.errors, { type = type, msg = msg, row = self.tok.prev_row, col = self.tok.prev_col })
end
function PARSER_FUNCS:parse_attr(attr_table, name)
local tok = self.tok:next_token()
if tok.type == "=" then
tok = self.tok:next_token()
if tok.type == "string" then
attr_table[name] = tok.value
else
self:report_error("missing_attribute_value", string.format("parsing attribute '%s' - expected a string after =, but did not find one"), name)
end
else
self:report_error("missing_equals_sign", string.format("parsing attribute '%s' - did not find equals sign after attribute name", name))
end
end
function PARSER_FUNCS:parse_element(skip_opening_tag)
local tok
if not skip_opening_tag then
tok = self.tok:next_token()
if tok.type ~= "<" then
self:report_error("missing_tag_open", "couldn't find a '<' to start parsing with")
end
end
tok = self.tok:next_token()
if tok.type ~= "string" then
self:report_error("missing_element_name", "expected an element name after '<'")
end
local elem_name = tok.value
local elem = nxml.new_element(elem_name)
local content_idx = 0
local self_closing = false
while true do
tok = self.tok:next_token()
if tok == nil then
return elem
elseif tok.type == "/" then
if self.tok:cur_char() == C_GT then
self.tok:move()
self_closing = true
end
break
elseif tok.type == ">" then
break
elseif tok.type == "string" then
self:parse_attr(elem.attr, tok.value)
end
end
if self_closing then return elem end
while true do
tok = self.tok:next_token()
if tok == nil then
return elem
elseif tok.type == "<" then
if self.tok:cur_char() == C_SLASH then
self.tok:move()
local end_name = self.tok:next_token()
if end_name.type == "string" and end_name.value == elem_name then
local close_greater = self.tok:next_token()
if close_greater.type == ">" then
return elem
else
self:report_error("missing_element_close", string.format("no closing '>' found for element '%s'", elem_name))
end
else
self:report_error("mismatched_closing_tag", string.format("closing element is in wrong order - expected '</%s>', but instead got '%s'", elem_name, tostring(end_name.value)))
end
return elem
else
local child = self:parse_element(elem, true)
table.insert(elem.children, child)
end
else
if not elem.content then
elem.content = {}
end
content_idx = content_idx + 1
elem.content[content_idx] = tok.value or tok.type
end
end
end
function PARSER_FUNCS:parse_elements()
local tok = self.tok:next_token()
local elems = {}
local elems_i = 1
while tok and tok.type == "<" do
elems[elems_i] = self:parse_element(true)
elems_i = elems_i + 1
tok = self.tok:next_token()
end
return elems
end
local function is_punctuation(str)
return str == "/" or str == "<" or str == ">" or str == "="
end
function XML_ELEMENT_FUNCS:text()
local content_count = #self.content
if self.content == nil or content_count == 0 then
return ""
end
local text = self.content[1]
for i = 2, content_count do
local elem = self.content[i]
local prev = self.content[i - 1]
if is_punctuation(elem) or is_punctuation(prev) then
text = text .. elem
else
text = text .. " " .. elem
end
end
return text
end
function XML_ELEMENT_FUNCS:add_child(child)
self.children[#self.children + 1] = child
end
function XML_ELEMENT_FUNCS:add_children(children)
local children_i = #self.children + 1
for i = 1, #children do
self.children[children_i] = children[i]
children_i = children_i + 1
end
end
function XML_ELEMENT_FUNCS:remove_child(child)
for i = 1, #self.children do
if self.children[i] == child then
table.remove(self.children, i)
break
end
end
end
function XML_ELEMENT_FUNCS:remove_child_at(index)
table.remove(self.children, index)
end
function XML_ELEMENT_FUNCS:clear_children()
self.children = {}
end
function XML_ELEMENT_FUNCS:clear_attrs()
self.attr = {}
end
function XML_ELEMENT_FUNCS:first_of(element_name)
local i = 0
local n = #self.children
while i < n do
i = i + 1
local c = self.children[i]
if c.name == element_name then return c end
end
return nil
end
function XML_ELEMENT_FUNCS:each_of(element_name)
local i = 1
local n = #self.children
return function()
while i <= n and self.children[i].name ~= element_name do
i = i + 1
end
i = i + 1
return self.children[i - 1]
end
end
function XML_ELEMENT_FUNCS:all_of(element_name)
local table = {}
local i = 1
for elem in self:each_of(element_name) do
table[i] = elem
i = i + 1
end
return table
end
function XML_ELEMENT_FUNCS:each_child()
local i = 0
local n = #self.children
return function()
while i <= n do
i = i + 1
return self.children[i]
end
end
end
function nxml.parse(data)
local data_len = #data
local tok = new_tokenizer(str_normalize(data), data_len)
local parser = new_parser(tok)
local elem = parser:parse_element(false)
if not elem or (elem.errors and #elem.errors > 0) then
error("parser encountered errors")
end
return elem
end
function nxml.parse_many(data)
local data_len = #data
local tok = new_tokenizer(str_normalize(data), data_len)
local parser = new_parser(tok)
local elems = parser:parse_elements(false)
for i = 1, #elems do
local elem = elems[i]
if elem.errors and #elem.errors > 0 then
error("parser encountered errors")
end
end
return elems
end
function nxml.new_element(name, attrs)
return setmetatable({
name = name,
attr = attrs or {},
children = {},
content = nil
}, XML_ELEMENT_MT)
end
local function attr_value_to_str(value)
local t = type(value)
if t == "string" then return value end
if t == "boolean" then return value and "1" or "0" end
return tostring(value)
end
function nxml.tostring(elem, packed, indent_char, cur_indent)
indent_char = indent_char or "\t"
cur_indent = cur_indent or ""
local s = "<" .. elem.name
local self_closing = #elem.children == 0 and (not elem.content or #elem.content == 0)
for k, v in pairs(elem.attr) do
s = s .. " " .. k .. "=\"" .. attr_value_to_str(v) .. "\""
end
if self_closing then
s = s .. " />"
return s
end
s = s .. ">"
local deeper_indent = cur_indent .. indent_char
if elem.content and #elem.content ~= 0 then
if not packed then s = s .. "\n" .. deeper_indent end
s = s .. elem:text()
end
if not packed then s = s .. "\n" end
for i, v in ipairs(elem.children) do
if not packed then s = s .. deeper_indent end
s = s .. nxml.tostring(v, packed, indent_char, deeper_indent)
if not packed then s = s .. "\n" end
end
s = s .. cur_indent .. "</" .. elem.name .. ">"
return s
end
return nxml