A lossless XML parser for MoonBit with UTF-16 source offsets, compact tree storage, and lazy entity decoding. About 15x faster than Milky2018/xml on the same input
moon add justjavac/xml///|
test "parse and query" {
let doc = @xml.parse(
"<r xmlns:p=\"urn:p\"><p:item id=\"1\">Hi & bye</p:item></r>",
)
let item = doc.find("item").unwrap()
assert_eq(item.namespace_uri(), Some("urn:p"))
assert_eq(item.get_attr("id"), Some("1"))
assert_eq(item.text(), "Hi & bye")
}pub struct Attributes {
// private fields
}test "read attributes through the view" {
let doc = @xml.parse("<r a=\"1\" b=\"x & y\"/>")
let attrs = doc.root().unwrap().attributes()
assert_eq(attrs.length(), 2)
assert_eq(attrs.get("b"), Some("x & y"))
assert_true(attrs.has("a"))
let seen : Array[String] = []
attrs.each(fn(name, value) { seen.push("\{name}=\{value}") })
assert_eq(seen, ["a=1", "b=x & y"])
}impl Eq for Attributesimpl Debug for Attributespub struct CData {
// private fields
}pub struct Comment {
// private fields
}pub struct Cursor {
// private fields
}test "navigate with a cursor" {
let doc = @xml.parse("<a><b id=\"1\"/><c/></a>")
let a = doc.cursor().first_child().unwrap()
assert_eq(a.kind(), @xml.NodeKind::Element)
let b = a.first_child().unwrap()
assert_eq(b.element().unwrap().get_attr("id"), Some("1"))
assert_eq(b.next_sibling().unwrap().element().unwrap().name(), "c")
assert_eq(b.parent().unwrap().kind(), @xml.NodeKind::Element)
}test "collect elements during a walk" {
let doc = @xml.parse("<r><a/><b/></r>")
let names : Array[String] = []
doc.walk(fn(cursor) {
if cursor.element() is Some(element) {
names.push(element.name())
}
})
assert_eq(names, ["r", "a", "b"])
}pub struct DocType {
// private fields
}pub struct Document {
// private fields
}test "a document is the entry point" {
let doc = @xml.parse("<r><a/></r>")
assert_eq(doc.source(), "<r><a/></r>")
assert_eq(doc.stringify(), "<r><a/></r>")
assert_true(doc.is_well_formed())
}test "an error carries its offset" {
let doc = @xml.parse("<a>\n <b>\n</a>")
let at = doc.error().unwrap()
assert_eq(at, 10)
assert_eq(doc.position(at), { line: 3, column: 1, })
}test "find matches the local part" {
let doc = @xml.parse(
"<r xmlns:p=\"urn:p\"><p:item>1</p:item><item>2</item></r>",
)
assert_eq(doc.find("item").unwrap().text(), "1")
assert_eq(doc.find("p:item").unwrap().text(), "1")
assert_eq(doc.all("item").length(), 2)
}test "position maps offsets to lines" {
let doc = @xml.parse("<a>\n<b/>\n</a>")
assert_eq(doc.position(0), { line: 1, column: 1, })
assert_eq(doc.position(4), { line: 2, column: 1, })
assert_eq(doc.position(9), { line: 3, column: 1, })
}test "root skips the prolog" {
let doc = @xml.parse("<?xml version=\"1.0\"?><!-- c --><r/>")
assert_eq(doc.root().unwrap().name(), "r")
assert_eq(doc.root().unwrap().children().length(), 0)
}test "walk counts nodes by kind" {
let doc = @xml.parse("<r>t<!--c--><a/></r>")
let kinds : Array[@xml.NodeKind] = []
doc.walk(fn(cursor) { kinds.push(cursor.kind()) })
assert_eq(kinds.length(), 4)
assert_eq(kinds[0], @xml.NodeKind::Element)
assert_eq(kinds[1], @xml.NodeKind::Text)
assert_eq(kinds[2], @xml.NodeKind::Comment)
assert_eq(kinds[3], @xml.NodeKind::Element)
}pub struct Element {
// private fields
}test "attributes need an explicit prefix" {
let doc = @xml.parse(
"<r xmlns=\"urn:d\" xmlns:q=\"urn:q\" q:id=\"1\" id=\"2\"/>",
)
let r = doc.root().unwrap()
assert_eq(r.attr_namespace_uri("q:id"), Some("urn:q"))
assert_eq(r.attr_namespace_uri("id"), None)
}test "resolve namespaces" {
let doc = @xml.parse("<r xmlns=\"urn:d\" xmlns:p=\"urn:p\"><p:i/><j/></r>")
let p = doc.find("i").unwrap()
assert_eq(p.prefix(), "p")
assert_eq(p.namespace_uri(), Some("urn:p"))
assert_eq(doc.find("j").unwrap().namespace_uri(), Some("urn:d"))
}test "text concatenates across descendants" {
let doc = @xml.parse("<a>x<b>y</b>z<![CDATA[&]]></a>")
assert_eq(doc.root().unwrap().text(), "xyz&")
}pub struct PI {
// private fields
}pub struct Text {
// private fields
}test "text decodes and raw does not" {
let doc = @xml.parse("<a>x & A</a>")
let text = match doc.root().unwrap().children()[0] {
@xml.Node::Text(text) => text
_ => abort("expected a text node")
}
assert_eq(text.value(), "x & A")
assert_eq(text.raw(), "x & A")
}test "parse recovers instead of throwing" {
let doc = @xml.parse("<a><b></a>")
assert_false(doc.is_well_formed())
assert_eq(doc.error(), Some(6))
assert_eq(doc.position(6), { line: 1, column: 7, })
}Install
Download zipA lossless XML parser for MoonBit with UTF-16 source offsets, compact tree storage, and lazy entity decoding. About 15x faster than Milky2018/xml on the same input