summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/lxml-tab.lua
blob: 8f6379f2f8bd97cbc314111978862d5568a29921 (plain)
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
if not modules then modules = { } end modules ['lxml-tab'] = {
    version   = 1.001,
    comment   = "this module is the basis for the lxml-* ones",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- this module needs a cleanup: check latest lpeg, passing args, (sub)grammar, etc etc
-- stripping spaces from e.g. cont-en.xml saves .2 sec runtime so it's not worth the
-- trouble

-- todo: when serializing optionally remap named entities to hex (if known in char-ent.lua)
-- maybe when letter -> utf, else name .. then we need an option to the serializer .. a bit
-- of work so we delay this till we cleanup

local trace_entities = false  trackers.register("xml.entities", function(v) trace_entities = v end)

local report_xml = logs and logs.reporter("xml","core") or function(...) print(string.format(...)) end

--[[ldx--
<p>The parser used here is inspired by the variant discussed in the lua book, but
handles comment and processing instructions, has a different structure, provides
parent access; a first version used different trickery but was less optimized to we
went this route. First we had a find based parser, now we have an <l n='lpeg'/> based one.
The find based parser can be found in l-xml-edu.lua along with other older code.</p>
--ldx]]--

if lpeg.setmaxstack then lpeg.setmaxstack(1000) end -- deeply nested xml files

xml = xml or { }
local xml = xml

--~ local xml = xml

local concat, remove, insert = table.concat, table.remove, table.insert
local type, next, setmetatable, getmetatable, tonumber, rawset, select = type, next, setmetatable, getmetatable, tonumber, rawset, select
local lower, find, match, gsub = string.lower, string.find, string.match, string.gsub
local sort = table.sort
local utfchar = utf.char
local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
local P, S, R, C, V, C, Cs = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.V, lpeg.C, lpeg.Cs
local formatters = string.formatters

--[[ldx--
<p>First a hack to enable namespace resolving. A namespace is characterized by
a <l n='url'/>. The following function associates a namespace prefix with a
pattern. We use <l n='lpeg'/>, which in this case is more than twice as fast as a
find based solution where we loop over an array of patterns. Less code and
much cleaner.</p>
--ldx]]--

do -- begin of namespace closure (we ran out of locals)

xml.xmlns = xml.xmlns or { }

--[[ldx--
<p>The next function associates a namespace prefix with an <l n='url'/>. This
normally happens independent of parsing.</p>

<typing>
xml.registerns("mml","mathml")
</typing>
--ldx]]--

local check = P(false)
local parse = check

function xml.registerns(namespace, pattern) -- pattern can be an lpeg
    check = check + C(P(lower(pattern))) / namespace
    parse = P { P(check) + 1 * V(1) }
end

--[[ldx--
<p>The next function also registers a namespace, but this time we map a
given namespace prefix onto a registered one, using the given
<l n='url'/>. This used for attributes like <t>xmlns:m</t>.</p>

<typing>
xml.checkns("m","http://www.w3.org/mathml")
</typing>
--ldx]]--

function xml.checkns(namespace,url)
    local ns = lpegmatch(parse,lower(url))
    if ns and namespace ~= ns then
        xml.xmlns[namespace] = ns
    end
end

--[[ldx--
<p>Next we provide a way to turn an <l n='url'/> into a registered
namespace. This used for the <t>xmlns</t> attribute.</p>

<typing>
resolvedns = xml.resolvens("http://www.w3.org/mathml")
</typing>

This returns <t>mml</t>.
--ldx]]--

function xml.resolvens(url)
     return lpegmatch(parse,lower(url)) or ""
end

--[[ldx--
<p>A namespace in an element can be remapped onto the registered
one efficiently by using the <t>xml.xmlns</t> table.</p>
--ldx]]--

end -- end of namespace closure

--[[ldx--
<p>This version uses <l n='lpeg'/>. We follow the same approach as before, stack and top and
such. This version is about twice as fast which is mostly due to the fact that
we don't have to prepare the stream for cdata, doctype etc etc. This variant is
is dedicated to Luigi Scarso, who challenged me with 40 megabyte <l n='xml'/> files that
took 12.5 seconds to load (1.5 for file io and the rest for tree building). With
the <l n='lpeg'/> implementation we got that down to less 7.3 seconds. Loading the 14
<l n='context'/> interface definition files (2.6 meg) went down from 1.05 seconds to 0.55.</p>

<p>Next comes the parser. The rather messy doctype definition comes in many
disguises so it is no surprice that later on have to dedicate quite some
<l n='lpeg'/> code to it.</p>

<typing>
<!DOCTYPE Something PUBLIC "... ..." "..." [ ... ] >
<!DOCTYPE Something PUBLIC "... ..." "..." >
<!DOCTYPE Something SYSTEM "... ..." [ ... ] >
<!DOCTYPE Something SYSTEM "... ..." >
<!DOCTYPE Something [ ... ] >
<!DOCTYPE Something >
</typing>

<p>The code may look a bit complex but this is mostly due to the fact that we
resolve namespaces and attach metatables. There is only one public function:</p>

<typing>
local x = xml.convert(somestring)
</typing>

<p>An optional second boolean argument tells this function not to create a root
element.</p>

<p>Valid entities are:</p>

<typing>
<!ENTITY xxxx SYSTEM "yyyy" NDATA zzzz>
<!ENTITY xxxx PUBLIC "yyyy" >
<!ENTITY xxxx "yyyy" >
</typing>
--ldx]]--

-- not just one big nested table capture (lpeg overflow)

local nsremap, resolvens = xml.xmlns, xml.resolvens

local stack, level, top, at, xmlnms, errorstr
local entities, parameters
local strip, utfize, resolve, cleanup, resolve_predefined, unify_predefined
local dcache, hcache, acache
local mt, dt, nt
local currentfilename, currentline, linenumbers

local grammar_parsed_text_one
local grammar_parsed_text_two
local grammar_unparsed_text

local handle_hex_entity
local handle_dec_entity
local handle_any_entity_dtd
local handle_any_entity_text

local function preparexmlstate(settings)
    if settings then
        linenumbers        = settings.linenumbers
        stack              = { }
        level              = 0
        top                = { }
        at                 = { }
        mt                 = { }
        dt                 = { }
        nt                 = 0   -- some 5% faster than #dt on cont-en.xml
        xmlns              = { }
        errorstr           = nil
        strip              = settings.strip_cm_and_dt
        utfize             = settings.utfize_entities
        resolve            = settings.resolve_entities            -- enable this in order to apply the dtd
        resolve_predefined = settings.resolve_predefined_entities -- in case we have escaped entities
        unify_predefined   = settings.unify_predefined_entities   -- &#038; -> &amp;
        cleanup            = settings.text_cleanup
        entities           = settings.entities or { }
        currentfilename    = settings.currentresource
        currentline        = 1
        parameters         = { }
        reported_at_errors = { }
        dcache             = { }
        hcache             = { }
        acache             = { }
        if utfize == nil then
            settings.utfize_entities = true
            utfize = true
        end
        if resolve_predefined == nil then
            settings.resolve_predefined_entities = true
            resolve_predefined = true
        end
    else
        linenumbers        = false
        stack              = nil
        level              = nil
        top                = nil
        at                 = nil
        mt                 = nil
        dt                 = nil
        nt                 = nil
        xmlns              = nil
        errorstr           = nil
        strip              = nil
        utfize             = nil
        resolve            = nil
        resolve_predefined = nil
        unify_predefined   = nil
        cleanup            = nil
        entities           = nil
        parameters         = nil
        reported_at_errors = nil
        dcache             = nil
        hcache             = nil
        acache             = nil
        currentfilename    = nil
        currentline        = 1
    end
end

local function initialize_mt(root)
    mt = { __index = root } -- will be redefined later
end

function xml.setproperty(root,k,v)
    getmetatable(root).__index[k] = v
end

function xml.checkerror(top,toclose)
    return "" -- can be set
end

local checkns = xml.checkns

local function add_attribute(namespace,tag,value)
    if cleanup and value ~= "" then
        value = cleanup(value) -- new
    end
    if tag == "xmlns" then
        xmlns[#xmlns+1] = resolvens(value)
        at[tag] = value
    elseif namespace == "" then
        at[tag] = value
    elseif namespace == "xmlns" then
        checkns(tag,value)
        at["xmlns:" .. tag] = value
    else
        -- for the moment this way:
        at[namespace .. ":" .. tag] = value
    end
end

local function add_empty(spacing, namespace, tag)
    if spacing ~= "" then
        nt = nt + 1
        dt[nt] = spacing
    end
    local resolved = namespace == "" and xmlns[#xmlns] or nsremap[namespace] or namespace
    top = stack[level]
    dt = top.dt
    nt = #dt + 1
    local t = linenumbers and {
        ns = namespace or "",
        rn = resolved,
        tg = tag,
        at = at,
        dt = { },
        ni = nt, -- set slot, needed for css filtering
        cf = currentfilename,
        cl = currentline,
        __p__ = top,
    } or {
        ns = namespace or "",
        rn = resolved,
        tg = tag,
        at = at,
        dt = { },
        ni = nt, -- set slot, needed for css filtering
        __p__ = top,
    }
    dt[nt] = t
    setmetatable(t, mt)
    if at.xmlns then
        remove(xmlns)
    end
    at = { }
end

local function add_begin(spacing, namespace, tag)
    if spacing ~= "" then
        nt = nt + 1
        dt[nt] = spacing
    end
    local resolved = namespace == "" and xmlns[#xmlns] or nsremap[namespace] or namespace
    dt = { }
    top = linenumbers and {
        ns = namespace or "",
        rn = resolved,
        tg = tag,
        at = at,
        dt = dt,
        ni = nil, -- preset slot, needed for css filtering
        cf = currentfilename,
        cl = currentline,
        __p__ = stack[level],
    } or {
        ns = namespace or "",
        rn = resolved,
        tg = tag,
        at = at,
        dt = dt,
        ni = nil, -- preset slot, needed for css filtering
        __p__ = stack[level],
    }
    setmetatable(top, mt)
    nt = 0
    level = level + 1
    stack[level] = top
    at = { }
end

local function add_end(spacing, namespace, tag)
    if spacing ~= "" then
        nt = nt + 1
        dt[nt] = spacing
    end
    local toclose = stack[level]
    level = level - 1
    top = stack[level]
    if level < 1 then
        errorstr = formatters["unable to close %s %s"](tag,xml.checkerror(top,toclose) or "")
        report_xml(errorstr)
    elseif toclose.tg ~= tag then -- no namespace check
        errorstr = formatters["unable to close %s with %s %s"](toclose.tg,tag,xml.checkerror(top,toclose) or "")
        report_xml(errorstr)
    end
    dt = top.dt
    nt = #dt + 1
    dt[nt] = toclose
    toclose.ni = nt -- update slot, needed for css filtering
    if toclose.at.xmlns then
        remove(xmlns)
    end
end

-- local spaceonly = lpegpatterns.whitespace^0 * P(-1)
--
-- will be an option: dataonly
--
-- if #text == 0 or     lpegmatch(spaceonly,text) then
--     return
-- end

local function add_text(text)
    if text == "" then
        return
    end
    if cleanup then
        if nt > 0 then
            local s = dt[nt]
            if type(s) == "string" then
                dt[nt] = s .. cleanup(text)
            else
                nt = nt + 1
                dt[nt] = cleanup(text)
            end
        else
            nt = 1
            dt[1] = cleanup(text)
        end
    else
        if nt > 0 then
            local s = dt[nt]
            if type(s) == "string" then
                dt[nt] = s .. text
            else
                nt = nt + 1
                dt[nt] = text
            end
        else
            nt = 1
            dt[1] = text
        end
    end
end

local function add_special(what, spacing, text)
    if spacing ~= "" then
        nt = nt + 1
        dt[nt] = spacing
    end
    if strip and (what == "@cm@" or what == "@dt@") then
        -- forget it
    else
        nt = nt + 1
        dt[nt] = linenumbers and {
            special = true,
            ns      = "",
            tg      = what,
            ni      = nil, -- preset slot
            dt      = { text },
            cf      = currentfilename,
            cl      = currentline,
        } or {
            special = true,
            ns      = "",
            tg      = what,
            ni      = nil, -- preset slot
            dt      = { text },
        }
    end
end

local function set_message(txt)
    errorstr = "garbage at the end of the file: " .. gsub(txt,"([ \n\r\t]*)","")
end

local function attribute_value_error(str)
    if not reported_at_errors[str] then
        report_xml("invalid attribute value %a",str)
        reported_at_errors[str] = true
        at._error_ = str
    end
    return str
end

local function attribute_specification_error(str)
    if not reported_at_errors[str] then
        report_xml("invalid attribute specification %a",str)
        reported_at_errors[str] = true
        at._error_ = str
    end
    return str
end

-- I'm sure that this lpeg can be simplified (less captures) but it evolved ...
-- so i'm not going to change it now.

do

    -- In order to overcome lua limitations we wrap entity stuff in a closure.

    local badentity = "&" -- was "&error;"

    xml.placeholders = {
        unknown_dec_entity = function(str) return str == "" and badentity or formatters["&%s;"](str) end,
        unknown_hex_entity = function(str) return formatters["&#x%s;"](str) end,
        unknown_any_entity = function(str) return formatters["&#x%s;"](str) end,
    }

    local function fromhex(s)
        local n = tonumber(s,16)
        if n then
            return utfchar(n)
        else
            return formatters["h:%s"](s), true
        end
    end

    local function fromdec(s)
        local n = tonumber(s)
        if n then
            return utfchar(n)
        else
            return formatters["d:%s"](s), true
        end
    end

    local p_rest = (1-P(";"))^0
    local p_many = P(1)^0

    local parsedentity =
        P("&#") * (P("x")*(p_rest/fromhex) + (p_rest/fromdec)) * P(";") * P(-1) +
        P ("#") * (P("x")*(p_many/fromhex) + (p_many/fromdec))

    xml.parsedentitylpeg = parsedentity

    -- parsing in the xml file

    local predefined_unified = {
        [38] = "&amp;",
        [42] = "&quot;",
        [47] = "&apos;",
        [74] = "&lt;",
        [76] = "&gt;",
    }

    local predefined_simplified = {
        [38] = "&", amp  = "&",
        [42] = '"', quot = '"',
        [47] = "'", apos = "'",
        [74] = "<", lt   = "<",
        [76] = ">", gt   = ">",
    }

    local nofprivates = 0xF0000 -- shared but seldom used

    local privates_u = { -- unescaped
        [ [[&]] ] = "&amp;",
        [ [["]] ] = "&quot;",
        [ [[']] ] = "&apos;",
        [ [[<]] ] = "&lt;",
        [ [[>]] ] = "&gt;",
    }

    local privates_p = { -- needed for roundtrip as well as serialize to tex
    }

    local privates_s = { -- for tex
        [ [["]] ] = "&U+22;",
        [ [[#]] ] = "&U+23;",
        [ [[$]] ] = "&U+24;",
        [ [[%]] ] = "&U+25;",
        [ [[&]] ] = "&U+26;",
        [ [[']] ] = "&U+27;",
        [ [[<]] ] = "&U+3C;",
        [ [[>]] ] = "&U+3E;",
        [ [[\]] ] = "&U+5C;",
        [ [[{]] ] = "&U+7B;",
        [ [[|]] ] = "&U+7C;",
        [ [[}]] ] = "&U+7D;",
        [ [[~]] ] = "&U+7E;",
    }

    local privates_x = { -- for xml
        [ [["]] ] = "&U+22;",
        [ [[#]] ] = "&U+23;",
        [ [[$]] ] = "&U+24;",
        [ [[%]] ] = "&U+25;",
        [ [[']] ] = "&U+27;",
        [ [[\]] ] = "&U+5C;",
        [ [[{]] ] = "&U+7B;",
        [ [[|]] ] = "&U+7C;",
        [ [[}]] ] = "&U+7D;",
        [ [[~]] ] = "&U+7E;",
    }

    local privates_n = { -- keeps track of defined ones
    }

    local escaped       = utf.remapper(privates_u,"dynamic")
    local unprivatized  = utf.remapper(privates_p,"dynamic")
    local unspecialized = utf.remapper(privates_s,"dynamic")
    local despecialized = utf.remapper(privates_x,"dynamic")

    xml.unprivatized  = unprivatized
    xml.unspecialized = unspecialized
    xml.despecialized = despecialized
    xml.escaped       = escaped

    local function unescaped(s)
        local p = privates_n[s]
        if not p then
            nofprivates = nofprivates + 1
            p = utfchar(nofprivates)
            privates_n[s] = p
            s = "&" .. s .. ";" -- todo: use char-ent to map to hex
            privates_u[p] = s
            privates_p[p] = s
            privates_s[p] = s
        end
        return p
    end

    xml.privatetoken = unescaped
    xml.privatecodes = privates_n
    xml.specialcodes = privates_s

    function xml.addspecialcode(key,value)
        privates_s[key] = value or "&" .. s .. ";"
    end

    handle_hex_entity = function(str)
        local h = hcache[str]
        if not h then
            local n = tonumber(str,16)
            h = unify_predefined and predefined_unified[n]
            if h then
                if trace_entities then
                    report_xml("utfize, converting hex entity &#x%s; into %a",str,h)
                end
            elseif utfize then
                h = (n and utfchar(n)) or xml.unknown_hex_entity(str) or ""
                if not n then
                    report_xml("utfize, ignoring hex entity &#x%s;",str)
                elseif trace_entities then
                    report_xml("utfize, converting hex entity &#x%s; into %a",str,h)
                end
            else
                if trace_entities then
                    report_xml("found entity &#x%s;",str)
                end
                h = "&#x" .. str .. ";"
            end
            hcache[str] = h
        end
        return h
    end

    handle_dec_entity = function(str)
        local d = dcache[str]
        if not d then
            local n = tonumber(str)
            d = unify_predefined and predefined_unified[n]
            if d then
                if trace_entities then
                    report_xml("utfize, converting dec entity &#%s; into %a",str,d)
                end
            elseif utfize then
                d = (n and utfchar(n)) or placeholders.unknown_dec_entity(str) or ""
                if not n then
                    report_xml("utfize, ignoring dec entity &#%s;",str)
                elseif trace_entities then
                    report_xml("utfize, converting dec entity &#%s; into %a",str,d)
                end
            else
                if trace_entities then
                    report_xml("found entity &#%s;",str)
                end
                d = "&#" .. str .. ";"
            end
            dcache[str] = d
        end
        return d
    end

    handle_any_entity_dtd = function(str)
        if resolve then
            local a = resolve_predefined and predefined_simplified[str] -- true by default
            if a then
                if trace_entities then
                    report_xml("resolving entity &%s; to predefined %a",str,a)
                end
            else
                if type(resolve) == "function" then
                    a = resolve(str,entities) or entities[str]
                else
                    a = entities[str]
                end
                if a then
                    if type(a) == "function" then
                        if trace_entities then
                            report_xml("expanding entity &%s; to function call",str)
                        end
                        a = a(str) or ""
                    end
                    a = lpegmatch(parsedentity,a) or a -- for nested
                    if trace_entities then
                        report_xml("resolving entity &%s; to internal %a",str,a)
                    end
                else
                    local unknown_any_entity = placeholders.unknown_any_entity
                    if unknown_any_entity then
                        a = unknown_any_entity(str) or ""
                    end
                    if a then
                        if trace_entities then
                            report_xml("resolving entity &%s; to external %s",str,a)
                        end
                    else
                        if trace_entities then
                            report_xml("keeping entity &%s;",str)
                        end
                        if str == "" then
                            a = badentity
                        else
                            a = "&" .. str .. ";"
                        end
                    end
                end
            end
            return a
        else
            local a = acache[str]
            if not a then
                a = resolve_predefined and predefined_simplified[str]
                if a then
                    -- one of the predefined
                    acache[str] = a
                    if trace_entities then
                        report_xml("entity &%s; becomes %a",str,a)
                    end
                elseif str == "" then
                    if trace_entities then
                        report_xml("invalid entity &%s;",str)
                    end
                    a = badentity
                    acache[str] = a
                else
                    if trace_entities then
                        report_xml("entity &%s; is made private",str)
                    end
                 -- a = "&" .. str .. ";"
                    a = unescaped(str)
                    acache[str] = a
                end
            end
            return a
        end
    end

    handle_any_entity_text = function(str)
        if resolve then
            local a = resolve_predefined and predefined_simplified[str]
            if a then
                if trace_entities then
                    report_xml("resolving entity &%s; to predefined %a",str,a)
                end
            else
                if type(resolve) == "function" then
                    a = resolve(str,entities) or entities[str]
                else
                    a = entities[str]
                end
                if a then
                    if type(a) == "function" then
                        if trace_entities then
                            report_xml("expanding entity &%s; to function call",str)
                        end
                        a = a(str) or ""
                    end
                    a = lpegmatch(grammar_parsed_text_two,a) or a
                    if type(a) == "number" then
                        return ""
                    else
                        a = lpegmatch(parsedentity,a) or a -- for nested
                        if trace_entities then
                            report_xml("resolving entity &%s; to internal %a",str,a)
                        end
                    end
                    if trace_entities then
                        report_xml("resolving entity &%s; to internal %a",str,a)
                    end
                else
                    local unknown_any_entity = placeholders.unknown_any_entity
                    if unknown_any_entity then
                        a = unknown_any_entity(str) or ""
                    end
                    if a then
                        if trace_entities then
                            report_xml("resolving entity &%s; to external %s",str,a)
                        end
                    else
                        if trace_entities then
                            report_xml("keeping entity &%s;",str)
                        end
                        if str == "" then
                            a = badentity
                        else
                            a = "&" .. str .. ";"
                        end
                    end
                end
            end
            return a
        else
            local a = acache[str]
            if not a then
                a = resolve_predefined and predefined_simplified[str]
                if a then
                    -- one of the predefined
                    acache[str] = a
                    if trace_entities then
                        report_xml("entity &%s; becomes %a",str,a)
                    end
                elseif str == "" then
                    if trace_entities then
                        report_xml("invalid entity &%s;",str)
                    end
                    a = badentity
                    acache[str] = a
                else
                    if trace_entities then
                        report_xml("entity &%s; is made private",str)
                    end
                 -- a = "&" .. str .. ";"
                    a = unescaped(str)
                    acache[str] = a
                end
            end
            return a
        end
    end

    -- for tex

    local p_rest = (1-P(";"))^1

    local spec = {
        [0x23] = "\\Ux{23}", -- #
        [0x24] = "\\Ux{24}", -- $
        [0x25] = "\\Ux{25}", -- %
        [0x5C] = "\\Ux{5C}", -- \
        [0x7B] = "\\Ux{7B}", -- {
        [0x7C] = "\\Ux{7C}", -- |
        [0x7D] = "\\Ux{7D}", -- }
        [0x7E] = "\\Ux{7E}", -- ~
    }

    local hash = table.setmetatableindex(spec,function(t,k)
        local v = utfchar(k)
        t[k] = v
        return v
    end)

    local function fromuni(s)
        local n = tonumber(s,16)
        if n then
            return hash[n]
        else
            return formatters["u:%s"](s), true
        end
    end

    local function fromhex(s)
        local n = tonumber(s,16)
        if n then
            return hash[n]
        else
            return formatters["h:%s"](s), true
        end
    end

    local function fromdec(s)
        local n = tonumber(s)
        if n then
            return hash[n]
        else
            return formatters["d:%s"](s), true
        end
    end

    local reparsedentity =
        P("U+") * (p_rest/fromuni)
      + P("#")  * (
            P("x") * (p_rest/fromhex)
          + p_rest/fromdec
        )

    local hash = table.setmetatableindex(function(t,k)
        local v = utfchar(k)
        t[k] = v
        return v
    end)

    local function fromuni(s)
        local n = tonumber(s,16)
        if n then
            return hash[n]
        else
            return formatters["u:%s"](s), true
        end
    end

    local function fromhex(s)
        local n = tonumber(s,16)
        if n then
            return hash[n]
        else
            return formatters["h:%s"](s), true
        end
    end

    local function fromdec(s)
        local n = tonumber(s)
        if n then
            return hash[n]
        else
            return formatters["d:%s"](s), true
        end
    end

    local unescapedentity =
        P("U+") * (p_rest/fromuni)
      + P("#")  * (
            P("x") * (p_rest/fromhex)
          + p_rest/fromdec
        )

    xml.reparsedentitylpeg  = reparsedentity   -- with \Ux{...} for special tex entities
    xml.unescapedentitylpeg = unescapedentity  -- normal characters

end

-- we use these later on

local escaped      = xml.escaped
local unescaped    = xml.unescaped
local placeholders = xml.placeholders

--

local function handle_end_entity(str)
    report_xml("error in entity, %a found without ending %a",str,";")
    return str
end

local function handle_crap_error(chr)
    report_xml("error in parsing, unexpected %a found ",chr)
    add_text(chr)
    return chr
end

local function handlenewline()
    currentline = currentline + 1
end

-- first = ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#x00F8-#x02FF] |
--         [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
--         [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
--         [#x10000-#xEFFFF]
-- rest  = "-" | "." | [0-9] | #xB7 | [#x300-#x36F] | [#x203F-#x2040]
-- name  = first + (first + rest)^1
--
-- We assume utf and do no real checking!

local spacetab         = S(' \t')
local space            = S(' \r\n\t')
local newline          = lpegpatterns.newline / handlenewline
local anything         = P(1)
local open             = P('<')
local close            = P('>')
local squote           = S("'")
local dquote           = S('"')
local equal            = P('=')
local slash            = P('/')
local colon            = P(':')
local semicolon        = P(';')
local ampersand        = P('&')
----- valid_0          = lpegpatterns.utf8two + lpegpatterns.utf8three + lpegpatterns.utf8four
local valid_0          = R("\128\255") -- basically any encoding without checking (fast)
local valid_1          = R('az', 'AZ') + S('_') + valid_0
local valid_2          = valid_1 + R('09') + S('-.')
local valid            = valid_1 * valid_2^0
local name_yes         = C(valid^1) * colon * C(valid^1)
local name_nop         = C(P(true)) * C(valid^1)
local name             = name_yes + name_nop
local utfbom           = lpegpatterns.utfbom -- no capture
local spacing          = C(space^0)

local space_nl         = spacetab + newline
local spacing_nl       = Cs((space_nl)^0)
local anything_nl      = newline + P(1)

local function weirdentity(k,v)
    if trace_entities then
        report_xml("registering %s entity %a as %a","weird",k,v)
    end
    parameters[k] = v
end
local function normalentity(k,v)
    if trace_entities then
        report_xml("registering %s entity %a as %a","normal",k,v)
    end
    entities[k] = v
end
local function systementity(k,v,n)
    if trace_entities then
        report_xml("registering %s entity %a as %a","system",k,v)
    end
    entities[k] = v
end
local function publicentity(k,v,n)
    if trace_entities then
        report_xml("registering %s entity %a as %a","public",k,v)
    end
    entities[k] = v
end
local function entityfile(pattern,k,v,n)
    if n then
        local okay, data
        local loadbinfile = resolvers and resolvers.loadbinfile
        if loadbinfile then
            okay, data = loadbinfile(n)
        else
            data = io.loaddata(n)
            okay = data and data ~= ""
        end
        if okay then
            if trace_entities then
                report_xml("loading public entities %a as %a from %a",k,v,n)
            end
            lpegmatch(pattern,data)
            return
        end
    end
    report_xml("ignoring public entities %a as %a from %a",k,v,n)
end

local function install(spacenewline,spacing,anything)

    local anyentitycontent = (1-open-semicolon-space-close-ampersand)^0
    local hexentitycontent = R("AF","af","09")^1
    local decentitycontent = R("09")^1
    local parsedentity     = P("#")/"" * (
                                    P("x")/"" * (hexentitycontent/handle_hex_entity) +
                                                (decentitycontent/handle_dec_entity)
                                ) +             (anyentitycontent/handle_any_entity_dtd) -- can be Cc(true)
    local parsedentity_text= P("#")/"" * (
                                    P("x")/"" * (hexentitycontent/handle_hex_entity) +
                                                (decentitycontent/handle_dec_entity)
                                ) +             (anyentitycontent/handle_any_entity_text) -- can be Cc(false)
    local entity           = (ampersand/"") * parsedentity   * (semicolon/"")
                           + ampersand * (anyentitycontent / handle_end_entity)
    local entity_text      = (ampersand/"") * parsedentity_text * (semicolon/"")
                           + ampersand * (anyentitycontent / handle_end_entity)

    local text_unparsed    = Cs((anything-open)^1)
    local text_parsed      = (Cs((anything-open-ampersand)^1)/add_text + Cs(entity_text)/add_text)^1

    local somespace        = (spacenewline)^1
    local optionalspace    = (spacenewline)^0

    local value            = (squote * Cs((entity + (anything - squote))^0) * squote) + (dquote * Cs((entity + (anything - dquote))^0) * dquote) -- ampersand and < also invalid in value

    local endofattributes  = slash * close + close -- recovery of flacky html
    local whatever         = space * name * optionalspace * equal
    local wrongvalue       = Cs(P(entity + (1-space-endofattributes))^1) / attribute_value_error

    local attributevalue   = value + wrongvalue

    local attribute        = (somespace * name * optionalspace * equal * optionalspace * attributevalue) / add_attribute

 -- local attributes       = (attribute + somespace^-1 * (((1-endofattributes)^1)/attribute_specification_error))^0
    local attributes       = (attribute + somespace^-1 * (((anything-endofattributes)^1)/attribute_specification_error))^0

    local parsedtext       = text_parsed   -- / add_text
    local unparsedtext     = text_unparsed / add_text
    local balanced         = P { "[" * ((anything - S"[]") + V(1))^0 * "]" } -- taken from lpeg manual, () example

    local emptyelement     = (spacing * open         * name * attributes * optionalspace * slash * close) / add_empty
    local beginelement     = (spacing * open         * name * attributes * optionalspace         * close) / add_begin
    local endelement       = (spacing * open * slash * name              * optionalspace         * close) / add_end

    -- todo: combine the opens in:

    local begincomment     = open * P("!--")
    local endcomment       = P("--") * close
    local begininstruction = open * P("?")
    local endinstruction   = P("?") * close
    local begincdata       = open * P("![CDATA[")
    local endcdata         = P("]]") * close

    local someinstruction  = C((anything - endinstruction)^0)
    local somecomment      = C((anything - endcomment    )^0)
    local somecdata        = C((anything - endcdata      )^0)

    -- todo: separate dtd parser

    local begindoctype     = open * P("!DOCTYPE")
    local enddoctype       = close
    local beginset         = P("[")
    local endset           = P("]")
    local wrdtypename      = C((anything-somespace-P(";"))^1)
    local doctypename      = C((anything-somespace-close)^0)
    local elementdoctype   = optionalspace * P("<!ELEMENT") * (anything-close)^0 * close

    local basiccomment     = begincomment * ((anything - endcomment)^0) * endcomment

    local weirdentitytype  = P("%") * (somespace * doctypename * somespace * value) / weirdentity
    local normalentitytype = (doctypename * somespace * value) / normalentity
    local publicentitytype = (doctypename * somespace * P("PUBLIC") * somespace * value) / publicentity

    local systementitytype = (doctypename * somespace * P("SYSTEM") * somespace * value * somespace * P("NDATA") * somespace * doctypename)/systementity
    local entitydoctype    = optionalspace * P("<!ENTITY") * somespace * (systementitytype + publicentitytype + normalentitytype + weirdentitytype) * optionalspace * close

    local publicentityfile = (doctypename * somespace * P("PUBLIC") * somespace * value * (somespace * value)^0) / function(...)
        entityfile(entitydoctype,...)
    end

    local function weirdresolve(s)
        lpegmatch(entitydoctype,parameters[s])
    end

    local function normalresolve(s)
        lpegmatch(entitydoctype,entities[s])
    end

    local entityresolve    = P("%") * (wrdtypename/weirdresolve ) * P(";")
                           + P("&") * (wrdtypename/normalresolve) * P(";")

    entitydoctype          = entitydoctype + entityresolve

    -- we accept comments in doctypes

    local doctypeset       = beginset * optionalspace * P(elementdoctype + entitydoctype + entityresolve + basiccomment + space)^0 * optionalspace * endset
    local definitiondoctype= doctypename * somespace * doctypeset
    local publicdoctype    = doctypename * somespace * P("PUBLIC") * somespace * value * somespace * value * somespace * doctypeset
    local systemdoctype    = doctypename * somespace * P("SYSTEM") * somespace * value * somespace * doctypeset
    local simpledoctype    = (anything-close)^1 -- * balanced^0
    local somedoctype      = C((somespace * (

publicentityfile +

    publicdoctype + systemdoctype + definitiondoctype + simpledoctype) * optionalspace)^0)

    local instruction      = (spacing * begininstruction * someinstruction * endinstruction) / function(...) add_special("@pi@",...) end
    local comment          = (spacing * begincomment     * somecomment     * endcomment    ) / function(...) add_special("@cm@",...) end
    local cdata            = (spacing * begincdata       * somecdata       * endcdata      ) / function(...) add_special("@cd@",...) end
    local doctype          = (spacing * begindoctype     * somedoctype     * enddoctype    ) / function(...) add_special("@dt@",...) end

    local crap_parsed     = anything - beginelement - endelement - emptyelement - begininstruction - begincomment - begincdata - ampersand
    local crap_unparsed   = anything - beginelement - endelement - emptyelement - begininstruction - begincomment - begincdata

    local parsedcrap      = Cs((crap_parsed^1 + entity_text)^1) / handle_crap_error
    local parsedcrap      = Cs((crap_parsed^1 + entity_text)^1) / handle_crap_error
    local unparsedcrap    = Cs((crap_unparsed              )^1) / handle_crap_error

    --  nicer but slower:
    --
    --  local instruction = (Cc("@pi@") * spacing * begininstruction * someinstruction * endinstruction) / add_special
    --  local comment     = (Cc("@cm@") * spacing * begincomment     * somecomment     * endcomment    ) / add_special
    --  local cdata       = (Cc("@cd@") * spacing * begincdata       * somecdata       * endcdata      ) / add_special
    --  local doctype     = (Cc("@dt@") * spacing * begindoctype     * somedoctype     * enddoctype    ) / add_special

    local trailer = space^0 * (text_unparsed/set_message)^0

    --  comment + emptyelement + text + cdata + instruction + V("parent"), -- 6.5 seconds on 40 MB database file
    --  text + comment + emptyelement + cdata + instruction + V("parent"), -- 5.8
    --  text + V("parent") + emptyelement + comment + cdata + instruction, -- 5.5

    -- local grammar_parsed_text = P { "preamble",
    --     preamble = utfbom^0 * instruction^0 * (doctype + comment + instruction)^0 * V("parent") * trailer,
    --     parent   = beginelement * V("children")^0 * endelement,
    --     children = parsedtext + V("parent") + emptyelement + comment + cdata + instruction + parsedcrap,
    -- }

    local grammar_parsed_text_one = P { "preamble",
        preamble = utfbom^0 * instruction^0 * (doctype + comment + instruction)^0,
    }

    local grammar_parsed_text_two = P { "followup",
        followup = V("parent") * trailer,
        parent   = beginelement * V("children")^0 * endelement,
        children = parsedtext + V("parent") + emptyelement + comment + cdata + instruction + parsedcrap,
    }

    local grammar_unparsed_text = P { "preamble",
        preamble = utfbom^0 * instruction^0 * (doctype + comment + instruction)^0 * V("parent") * trailer,
        parent   = beginelement * V("children")^0 * endelement,
        children = unparsedtext + V("parent") + emptyelement + comment + cdata + instruction + unparsedcrap,
    }

    return grammar_parsed_text_one, grammar_parsed_text_two, grammar_unparsed_text

end

local
    grammar_parsed_text_one_nop ,
    grammar_parsed_text_two_nop ,
    grammar_unparsed_text_nop   = install(space, spacing, anything)

local
    grammar_parsed_text_one_yes ,
    grammar_parsed_text_two_yes ,
    grammar_unparsed_text_yes   = install(space_nl, spacing_nl, anything_nl)

-- maybe we will add settings to result as well

local function _xmlconvert_(data,settings,detail)
    settings = settings or { } -- no_root strip_cm_and_dt given_entities parent_root error_handler
    preparexmlstate(settings)
    if settings.linenumbers then
        grammar_parsed_text_one = grammar_parsed_text_one_yes
        grammar_parsed_text_two = grammar_parsed_text_two_yes
        grammar_unparsed_text   = grammar_unparsed_text_yes
    else
        grammar_parsed_text_one = grammar_parsed_text_one_nop
        grammar_parsed_text_two = grammar_parsed_text_two_nop
        grammar_unparsed_text   = grammar_unparsed_text_nop
    end
    local preprocessor = settings.preprocessor
    if data and data ~= "" and type(preprocessor) == "function" then
        data = preprocessor(data,settings) or data -- settings.currentresource
    end
    if settings.parent_root then
        mt = getmetatable(settings.parent_root)
    else
        initialize_mt(top)
    end
    level = level + 1
    stack[level] = top
    top.dt = { }
    dt = top.dt
    nt = 0
    if not data or data == "" then
        errorstr = "empty xml file"
    elseif data == true then
        errorstr = detail or "problematic xml file"
    elseif utfize or resolve then
        local m = lpegmatch(grammar_parsed_text_one,data)
        if m then
            m = lpegmatch(grammar_parsed_text_two,data,m)
        end
     -- local m = lpegmatch(grammar_parsed_text,data)
        if m then
         -- errorstr = "" can be set!
        else
            errorstr = "invalid xml file - parsed text"
        end
    elseif type(data) == "string" then
        if lpegmatch(grammar_unparsed_text,data) then
            errorstr = ""
        else
            errorstr = "invalid xml file - unparsed text"
        end
    else
        errorstr = "invalid xml file - no text at all"
    end
    local result
    if errorstr and errorstr ~= "" then
        result = { dt = { { ns = "", tg = "error", dt = { errorstr }, at = { }, er = true } } }
        setmetatable(result, mt)
        setmetatable(result.dt[1], mt)
        setmetatable(stack, mt)
        local errorhandler = settings.error_handler
        if errorhandler == false then
            -- no error message
        else
            errorhandler = errorhandler or xml.errorhandler
            if errorhandler then
                local currentresource = settings.currentresource
                if currentresource and currentresource ~= "" then
                    xml.errorhandler(formatters["load error in [%s]: %s"](currentresource,errorstr),currentresource)
                else
                    xml.errorhandler(formatters["load error: %s"](errorstr))
                end
            end
        end
    else
        result = stack[1]
    end
    if not settings.no_root then
        result = { special = true, ns = "", tg = '@rt@', dt = result.dt, at={ }, entities = entities, settings = settings }
        setmetatable(result, mt)
        local rdt = result.dt
        for k=1,#rdt do
            local v = rdt[k]
            if type(v) == "table" and not v.special then -- always table -)
                result.ri = k -- rootindex
                v.__p__ = result  -- new, experiment, else we cannot go back to settings, we need to test this !
                break
            end
        end
    end
    if errorstr and errorstr ~= "" then
        result.error = true
    else
        errorstr = nil
    end
    result.statistics = {
        errormessage = errorstr,
        entities = {
            decimals      = dcache,
            hexadecimals  = hcache,
            names         = acache,
            intermediates = parameters,
        }
    }
    preparexmlstate() -- resets
    return result
end

-- Because we can have a crash (stack issues) with faulty xml, we wrap this one
-- in a protector:

local function xmlconvert(data,settings)
    local ok, result = pcall(function() return _xmlconvert_(data,settings) end)
    if ok then
        return result
    elseif type(result) == "string" then
        return _xmlconvert_(true,settings,result)
    else
        return _xmlconvert_(true,settings)
    end
end

xml.convert = xmlconvert

function xml.inheritedconvert(data,xmldata) -- xmldata is parent
    local settings = xmldata.settings
    if settings then
        settings.parent_root = xmldata -- to be tested
    end
 -- settings.no_root = true
    local xc = xmlconvert(data,settings) -- hm, we might need to locate settings
 -- xc.settings = nil
 -- xc.entities = nil
 -- xc.special = nil
 -- xc.ri = nil
 -- print(xc.tg)
    return xc
end

--[[ldx--
<p>Packaging data in an xml like table is done with the following
function. Maybe it will go away (when not used).</p>
--ldx]]--

function xml.is_valid(root)
    return root and root.dt and root.dt[1] and type(root.dt[1]) == "table" and not root.dt[1].er
end

function xml.package(tag,attributes,data)
    local ns, tg = match(tag,"^(.-):?([^:]+)$")
    local t = { ns = ns, tg = tg, dt = data or "", at = attributes or {} }
    setmetatable(t, mt)
    return t
end

function xml.is_valid(root)
    return root and not root.error
end

xml.errorhandler = report_xml

--[[ldx--
<p>We cannot load an <l n='lpeg'/> from a filehandle so we need to load
the whole file first. The function accepts a string representing
a filename or a file handle.</p>
--ldx]]--

function xml.load(filename,settings)
    local data = ""
    if type(filename) == "string" then
     -- local data = io.loaddata(filename) -- todo: check type in io.loaddata
        local f = io.open(filename,'r') -- why not 'rb'
        if f then
            data = f:read("*all") -- io.readall(f) ... only makes sense for large files
            f:close()
        end
    elseif filename then -- filehandle
        data = filename:read("*all") -- io.readall(f) ... only makes sense for large files
    end
    if settings then
        settings.currentresource = filename
        local result = xmlconvert(data,settings)
        settings.currentresource = nil
        return result
    else
        return xmlconvert(data,{ currentresource = filename })
    end
end

--[[ldx--
<p>When we inject new elements, we need to convert strings to
valid trees, which is what the next function does.</p>
--ldx]]--

local no_root = { no_root = true }

function xml.toxml(data)
    if type(data) == "string" then
        local root = { xmlconvert(data,no_root) }
        return (#root > 1 and root) or root[1]
    else
        return data
    end
end

--[[ldx--
<p>For copying a tree we use a dedicated function instead of the
generic table copier. Since we know what we're dealing with we
can speed up things a bit. The second argument is not to be used!</p>
--ldx]]--

-- local function copy(old)
--     if old then
--         local new = { }
--         for k,v in next, old do
--             if type(v) == "table" then
--                 new[k] = table.copy(v)
--             else
--                 new[k] = v
--             end
--         end
--         local mt = getmetatable(old)
--         if mt then
--             setmetatable(new,mt)
--         end
--         return new
--     else
--         return { }
--     end
-- end
--
-- We need to prevent __p__ recursio, so:

local function copy(old,p)
    if old then
        local new = { }
        for k, v in next, old do
            local t = type(v) == "table"
            if k == "at" then
                local t = { }
                for k, v in next, v do
                    t[k] = v
                end
                new[k] = t
            elseif k == "dt" then
                v.__p__ = nil
                v = copy(v,new)
                new[k] = v
                v.__p__ = p
            else
                new[k] = v -- so we also share entities, etc in root
            end
        end
        local mt = getmetatable(old)
        if mt then
            setmetatable(new,mt)
        end
        return new
    else
        return { }
    end
end

xml.copy = copy

--[[ldx--
<p>In <l n='context'/> serializing the tree or parts of the tree is a major
actitivity which is why the following function is pretty optimized resulting
in a few more lines of code than needed. The variant that uses the formatting
function for all components is about 15% slower than the concatinating
alternative.</p>
--ldx]]--

-- todo: add <?xml version='1.0' standalone='yes'?> when not present

function xml.checkbom(root) -- can be made faster
    if root.ri then
        local dt = root.dt
        for k=1,#dt do
            local v = dt[k]
            if type(v) == "table" and v.special and v.tg == "@pi@" and find(v.dt[1],"xml.*version=") then
                return
            end
        end
        insert(dt, 1, { special = true, ns = "", tg = "@pi@", dt = { "xml version='1.0' standalone='yes'" } } )
        insert(dt, 2, "\n" )
    end
end

--[[ldx--
<p>At the cost of some 25% runtime overhead you can first convert the tree to a string
and then handle the lot.</p>
--ldx]]--

-- new experimental reorganized serialize

local f_attribute = formatters['%s=%q']

-- we could reuse ats .. for high performance we could also
-- have a multiple handle calls instead of multiple arguments
-- but it's not that critical

local function verbose_element(e,handlers,escape) -- options
    local handle = handlers.handle
    local serialize = handlers.serialize
    local ens, etg, eat, edt, ern = e.ns, e.tg, e.at, e.dt, e.rn
    local ats = eat and next(eat) and { }
    if ats then
        -- we now sort attributes
        local n = 0
        for k in next, eat do
            n = n + 1
            ats[n] = k
        end
        if n == 1 then
            local k = ats[1]
            ats = f_attribute(k,escaped(eat[k]))
        else
            sort(ats)
            for i=1,n do
                local k = ats[i]
                ats[i] = f_attribute(k,escaped(eat[k]))
            end
            ats = concat(ats," ")
        end
    end
    if ern and trace_entities and ern ~= ens then
        ens = ern
    end
    local n = edt and #edt
    if ens ~= "" then
        if n and n > 0 then
            if ats then
                handle("<",ens,":",etg," ",ats,">")
            else
                handle("<",ens,":",etg,">")
            end
            for i=1,n do
                local e = edt[i]
                if type(e) == "string" then
                    handle(escaped(e))
                else
                    serialize(e,handlers)
                end
            end
            handle("</",ens,":",etg,">")
        else
            if ats then
                handle("<",ens,":",etg," ",ats,"/>")
            else
                handle("<",ens,":",etg,"/>")
            end
        end
    else
        if n and n > 0 then
            if ats then
                handle("<",etg," ",ats,">")
            else
                handle("<",etg,">")
            end
            for i=1,n do
                local e = edt[i]
                if type(e) == "string" then
                    handle(escaped(e)) -- option: hexify escaped entities
                else
                    serialize(e,handlers)
                end
            end
            handle("</",etg,">")
        else
            if ats then
                handle("<",etg," ",ats,"/>")
            else
                handle("<",etg,"/>")
            end
        end
    end
end

local function verbose_pi(e,handlers)
    handlers.handle("<?",e.dt[1],"?>")
end

local function verbose_comment(e,handlers)
    handlers.handle("<!--",e.dt[1],"-->")
end

local function verbose_cdata(e,handlers)
    handlers.handle("<![CDATA[", e.dt[1],"]]>")
end

local function verbose_doctype(e,handlers)
    handlers.handle("<!DOCTYPE",e.dt[1],">") -- has space at end of string
end

local function verbose_root(e,handlers)
    handlers.serialize(e.dt,handlers)
end

local function verbose_text(e,handlers)
    handlers.handle(escaped(e))
end

local function verbose_document(e,handlers)
    local serialize = handlers.serialize
    local functions = handlers.functions
    for i=1,#e do
        local ei = e[i]
        if type(ei) == "string" then
            functions["@tx@"](ei,handlers)
        else
            serialize(ei,handlers)
        end
    end
end

local function serialize(e,handlers,...)
    if e then
        local initialize = handlers.initialize
        local finalize   = handlers.finalize
        local functions  = handlers.functions
        if initialize then
            local state = initialize(...)
            if not state == true then
                return state
            end
        end
        local etg = e.tg
        if etg then
            (functions[etg] or functions["@el@"])(e,handlers)
     -- elseif type(e) == "string" then
     --     functions["@tx@"](e,handlers)
        else
            functions["@dc@"](e,handlers) -- dc ?
        end
        if finalize then
            return finalize()
        end
    end
end

local function xserialize(e,handlers)
    if e then
        local functions = handlers.functions
        local etg = e.tg
        if etg then
            (functions[etg] or functions["@el@"])(e,handlers)
     -- elseif type(e) == "string" then
     --     functions["@tx@"](e,handlers)
        else
            functions["@dc@"](e,handlers)
        end
    end
end

local handlers = { }

local function newhandlers(settings)
    local t = table.copy(handlers[settings and settings.parent or "verbose"] or { }) -- merge
    if settings then
        for k,v in next, settings do
            if type(v) == "table" then
                local tk = t[k] if not tk then tk = { } t[k] = tk end
                for kk, vv in next, v do
                    tk[kk] = vv
                end
            else
                t[k] = v
            end
        end
        if settings.name then
            handlers[settings.name] = t
        end
    end
    utilities.storage.mark(t)
    return t
end

local nofunction = function() end

function xml.sethandlersfunction(handler,name,fnc)
    handler.functions[name] = fnc or nofunction
end

function xml.gethandlersfunction(handler,name)
    return handler.functions[name]
end

function xml.gethandlers(name)
    return handlers[name]
end

newhandlers {
    name       = "verbose",
    initialize = false, -- faster than nil and mt lookup
    finalize   = false, -- faster than nil and mt lookup
    serialize  = xserialize,
    handle     = print,
    functions  = {
        ["@dc@"]   = verbose_document,
        ["@dt@"]   = verbose_doctype,
        ["@rt@"]   = verbose_root,
        ["@el@"]   = verbose_element,
        ["@pi@"]   = verbose_pi,
        ["@cm@"]   = verbose_comment,
        ["@cd@"]   = verbose_cdata,
        ["@tx@"]   = verbose_text,
    }
}

--[[ldx--
<p>How you deal with saving data depends on your preferences. For a 40 MB database
file the timing on a 2.3 Core Duo are as follows (time in seconds):</p>

<lines>
1.3 : load data from file to string
6.1 : convert string into tree
5.3 : saving in file using xmlsave
6.8 : converting to string using xml.tostring
3.6 : saving converted string in file
</lines>

<p>Beware, these were timing with the old routine but measurements will not be that
much different I guess.</p>
--ldx]]--

-- maybe this will move to lxml-xml

local result

local xmlfilehandler = newhandlers {
    name       = "file",
    initialize = function(name)
        result = io.open(name,"wb")
        return result
    end,
    finalize   = function()
        result:close()
        return true
    end,
    handle     = function(...)
        result:write(...)
    end,
}

-- no checking on writeability here but not faster either
--
-- local xmlfilehandler = newhandlers {
--     initialize = function(name)
--         io.output(name,"wb")
--         return true
--     end,
--     finalize   = function()
--         io.close()
--         return true
--     end,
--     handle     = io.write,
-- }

function xml.save(root,name)
    serialize(root,xmlfilehandler,name)
end

-- local result
--
-- local xmlstringhandler = newhandlers {
--     name       = "string",
--     initialize = function()
--         result = { }
--         return result
--     end,
--     finalize   = function()
--         return concat(result)
--     end,
--     handle     = function(...)
--         result[#result+1] = concat { ... }
--     end,
-- }

local result, r, threshold = { }, 0, 512

local xmlstringhandler = newhandlers {
    name       = "string",
    initialize = function()
        r = 0
        return result
    end,
    finalize   = function()
        local done = concat(result,"",1,r)
        r = 0
        if r > threshold then
            result = { }
        end
        return done
    end,
    handle     = function(...)
        for i=1,select("#",...) do
            r = r + 1
            result[r] = select(i,...)
        end
    end,
}

local function xmltostring(root) -- 25% overhead due to collecting
    if not root then
        return ""
    elseif type(root) == "string" then
        return root
    else -- if next(root) then -- next is faster than type (and >0 test)
        return serialize(root,xmlstringhandler) or ""
    end
end

local function __tostring(root) -- inline
    return (root and xmltostring(root)) or ""
end

initialize_mt = function(root) -- redefinition
    mt = { __tostring = __tostring, __index = root }
end

xml.defaulthandlers = handlers
xml.newhandlers     = newhandlers
xml.serialize       = serialize
xml.tostring        = xmltostring

--[[ldx--
<p>The next function operated on the content only and needs a handle function
that accepts a string.</p>
--ldx]]--

local function xmlstring(e,handle)
    if not handle or (e.special and e.tg ~= "@rt@") then
        -- nothing
    elseif e.tg then
        local edt = e.dt
        if edt then
            for i=1,#edt do
                xmlstring(edt[i],handle)
            end
        end
    else
        handle(e)
    end
end

xml.string = xmlstring

--[[ldx--
<p>A few helpers:</p>
--ldx]]--

--~ xmlsetproperty(root,"settings",settings)

function xml.settings(e)
    while e do
        local s = e.settings
        if s then
            return s
        else
            e = e.__p__
        end
    end
    return nil
end

function xml.root(e)
    local r = e
    while e do
        e = e.__p__
        if e then
            r = e
        end
    end
    return r
end

function xml.parent(root)
    return root.__p__
end

function xml.body(root)
    return root.ri and root.dt[root.ri] or root -- not ok yet
end

function xml.name(root)
    if not root then
        return ""
    end
    local ns = root.ns
    local tg = root.tg
    if ns == "" then
        return tg
    else
        return ns .. ":" .. tg
    end
end

--[[ldx--
<p>The next helper erases an element but keeps the table as it is,
and since empty strings are not serialized (effectively) it does
not harm. Copying the table would take more time. Usage:</p>
--ldx]]--

function xml.erase(dt,k)
    if dt then
        if k then
            dt[k] = ""
        else for k=1,#dt do
            dt[1] = { "" }
        end end
    end
end

--[[ldx--
<p>The next helper assigns a tree (or string). Usage:</p>

<typing>
dt[k] = xml.assign(root) or xml.assign(dt,k,root)
</typing>
--ldx]]--

function xml.assign(dt,k,root)
    if dt and k then
        dt[k] = type(root) == "table" and xml.body(root) or root
        return dt[k]
    else
        return xml.body(root)
    end
end

-- the following helpers may move

--[[ldx--
<p>The next helper assigns a tree (or string). Usage:</p>
<typing>
xml.tocdata(e)
xml.tocdata(e,"error")
</typing>
--ldx]]--

function xml.tocdata(e,wrapper) -- a few more in the aux module
    local whatever = type(e) == "table" and xmltostring(e.dt) or e or ""
    if wrapper then
        whatever = formatters["<%s>%s</%s>"](wrapper,whatever,wrapper)
    end
    local t = { special = true, ns = "", tg = "@cd@", at = { }, rn = "", dt = { whatever }, __p__ = e }
    setmetatable(t,getmetatable(e))
    e.dt = { t }
end

function xml.makestandalone(root)
    if root.ri then
        local dt = root.dt
        for k=1,#dt do
            local v = dt[k]
            if type(v) == "table" and v.special and v.tg == "@pi@" then
                local txt = v.dt[1]
                if find(txt,"xml.*version=") then
                    v.dt[1] = txt .. " standalone='yes'"
                    break
                end
            end
        end
    end
    return root
end

function xml.kind(e)
    local dt = e and e.dt
    if dt then
        local n = #dt
        if n == 1 then
            local d = dt[1]
            if d.special then
                local tg = d.tg
                if tg == "@cd@" then
                    return "cdata"
                elseif tg == "@cm" then
                    return "comment"
                elseif tg == "@pi@" then
                    return "instruction"
                elseif tg == "@dt@" then
                    return "declaration"
                end
            elseif type(d) == "string" then
                return "text"
            end
            return "element"
        elseif n > 0 then
            return "mixed"
        end
    end
    return "empty"
end