#! @file document.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the parsed document, which holds every node of XML text as a flat #! array, and `xml::element` and `xml::node`, which read them. module std::xml; #! The kinds of nodes a document holds. pub enum kind: u8 { element, #! An element, which holds attributes, and nodes of its own. text, #! Text, decoded, with what the CDATA sections among it hold. comment, #! A comment. instruction, #! A processing instruction. } # What a record is: the kinds of nodes, then the document itself, whose children # are the root element and the comments and instructions around it. enum record_kind: u8 { element, text, comment, instruction, document, } # Where a string is in the document's `_strings`. struct piece { var start: u32; var length: u32; } # One node of a document, in 52 bytes. # # A node's children follow it, so its `next` is the index just past it and all # it holds, and its next sibling's, so a node is skipped in one step. struct record { var kind: record_kind; var prefix: u32; #! How long the prefix of an element's name is. var name: piece; #! An element's name, or an instruction's target. var value: piece; #! Text, a comment, what follows an instruction's target, or an element's namespace. var parent: u32; var next: u32; var count: u32; #! How many children it holds. var attributes: u32; #! An element's first attribute. var many: u32; #! How many attributes it has. var line: u32; var column: u32; var offset: u32; } # An attribute of an element. struct attribute_record { var name: piece; var prefix: u32; var namespace: piece; var value: piece; var specified: bool; var offset: u32; } #! XML text read whole, every node of it held as a record of a flat array. #! #! A document holds its own copy of every name and text, so the text it was #! parsed from may go once it is. Elements and nodes it gives refer to it, so they #! are good only while it lives and stays where it is. pub struct document : moveable { var _strings: string; var _records: vector; var _attributes: vector; var _root: u32; var _version: piece; var _encoding: piece; var _doctype: piece; var _standalone: bool; } #! An element of a document. pub struct element { var _document: *document; var _at: u32; } #! A node of a document: an element, text, a comment, or a processing #! instruction. pub struct node { var _document: *document; var _at: u32; } #! Reads XML text whole into a document. #! @param text The text, which the document copies what it needs of. #! @param given How the text is read, and how far reading may go. #! @param alloc An optional allocator for the document to take memory from. #! @returns The document, or why the text is not XML, with where it fails. pub def parse(text: strview, given: options = options {}, alloc: maybe<&mut allocator> = nothing {}): result { var made := document { _strings = string::new(alloc), _records = vector::::new(alloc), _attributes = vector::::new(alloc) }; made._records.append(record { kind = record_kind::document }); # The open elements, by their records, within the document's own. var open := vector::::new(alloc); open.append(0); var lines := line_counter { _text = text }; var tokens := reader::new(text, given); var namespace := piece {}; # The last namespace kept, which the next name is often in too. while tokens.next() is some(read) { val at := lines.place(read.offset); val parent: u32 = open.last; val index := made._records.length as u32; var added := record { parent = parent, next = index + 1, line = at.line, column = at.column, offset = read.offset as u32 }; match read.kind { start => { added.kind = record_kind::element; added.name = made.keep(read.name); added.prefix = read.prefix.length as u32; added.value = made.keep_namespace(read.namespace, &mut namespace); added.attributes = made._attributes.length as u32; added.many = tokens.attributes.length as u32; for &held in tokens.attributes { val kept := attribute_record { name = made.keep(held.name), prefix = held.prefix.length as u32, namespace = made.keep_namespace(held.namespace, &mut namespace), value = made.keep(held.value), specified = held.specified, offset = held.offset as u32 }; made._attributes.append(kept); } if made._root == 0 { made._root = index; } open.append(index); } end => { made._records[parent as u64].next = index; open.truncate(); continue; } text => { # Text next to text, as a CDATA section beside the rest, is one node. val last := made._records[index as u64 - 1]; if last.kind == record_kind::text && last.parent == parent && (last.value.start + last.value.length) as u64 == made._strings.length { made._strings.append(read.text); made._records[index as u64 - 1].value.length += read.text.length as u32; continue; } added.kind = record_kind::text; added.value = made.keep(read.text); } comment => { added.kind = record_kind::comment; added.value = made.keep(read.text); } instruction => { added.kind = record_kind::instruction; added.name = made.keep(read.name); added.value = made.keep(read.text); } } made._records[parent as u64].count++; made._records.append(added); } if tokens.error is some(failed) { return failed; } if made._strings.length > 0xFFFFFFFFu { return error { kind = error_kind::too_long }; } made._records[0].next = made._records.length as u32; made._version = made.keep(tokens.version); made._encoding = made.keep(tokens.encoding); made._doctype = made.keep(tokens.doctype); made._standalone = tokens.standalone; return move(made); } #! Creates a document by taking another's records and strings, which leaves the #! other holding nothing. #! @param other The document to move from. #! @returns The document. pub def document::move_of(other: &mut document): document { return document { _strings = move(other._strings), _records = move(other._records), _attributes = move(other._attributes), _root = other._root, _version = other._version, _encoding = other._encoding, _doctype = other._doctype, _standalone = other._standalone }; } #! The root element, which holds the rest. #! @param this The document to read. #! @returns The element. pub val document::root: element { get => element { _document = this as *document, _at = this._root }; } #! Iterates over the nodes outside every other: the root element, and the #! comments and processing instructions before and after it, in order. #! @param this The document to walk. #! @returns An iterator over them. pub def document::children(&this): node_cursor { return node_cursor { _document = this as *document, _at = 1, _left = this._records[0].count }; } #! The version the XML declaration gives, as `1.0`. #! @param this The document to ask. #! @returns The version, or empty where the text has no declaration. pub val document::version: strview { get => this.text_of(this._version); } #! The encoding the XML declaration names, as written. #! @param this The document to ask. #! @returns The encoding, or empty where none is named. pub val document::encoding: strview { get => this.text_of(this._encoding); } #! Whether the XML declaration says the document stands alone. #! @param this The document to ask. #! @returns Whether it says `standalone="yes"`. pub val document::standalone: bool { get => this._standalone; } #! The name the document type gives the root element. #! @param this The document to ask. #! @returns The name, or empty where the text has no document type. pub val document::doctype: strview { get => this.text_of(this._doctype); } #! The element's name as written, as `svg:rect`. #! @param this The element to ask. #! @returns The name. pub val element::name: strview { get => (*this._document).text_of((*this._document).record_at(this._at).name); } #! The prefix of the element's name. #! @param this The element to ask. #! @returns The prefix, or empty where it has none. pub val element::prefix: strview { get => this.name.substr(0, (*this._document).record_at(this._at).prefix as u64); } #! The element's name without its prefix, as `rect` of `svg:rect`. #! @param this The element to ask. #! @returns The local name. pub val element::local: strview { get => local_of(this.name, (*this._document).record_at(this._at).prefix); } #! The namespace the element's name is in. #! @param this The element to ask. #! @returns The namespace's name, or empty where it is in none. pub val element::namespace: strview { get => (*this._document).text_of((*this._document).record_at(this._at).value); } #! Where the element's start tag is in the text it was read from. #! @param this The element to ask. #! @returns Where it is, or where the entity holding it is referred to. pub val element::place: place { get => (*this._document).place_at(this._at); } #! The element holding this one. #! @param this The element to ask. #! @returns The element, or `nothing` for the root element. pub val element::parent: maybe { get { val held := (*this._document).record_at(this._at).parent; if held == 0 { return nothing {}; } return element { _document = this._document, _at = held }; } } #! Finds the value of an attribute by its name as written, as `xlink:href`. #! @param this The element to look in. #! @param name The name. #! @returns The value, or `nothing` where the element has no such attribute. pub def element::attribute(&this, name: strview): maybe { val doc := this._document; val held := (*doc).record_at(this._at); for index in held.attributes..(held.attributes + held.many) { val kept := &(*doc)._attributes[index as u64]; if (*doc).text_of(kept.name) == name { return (*doc).text_of(kept.value); } } return nothing {}; } #! Finds the value of an attribute by its namespace and local name, whatever #! prefix it is written with. #! @param this The element to look in. #! @param namespace The namespace's name, or empty for an attribute in none, as #! one without a prefix is. #! @param local The name without its prefix. #! @returns The value, or `nothing` where the element has no such attribute. pub def element::attribute(&this, namespace: strview, local: strview): maybe { val doc := this._document; val held := (*doc).record_at(this._at); for index in held.attributes..(held.attributes + held.many) { val kept := &(*doc)._attributes[index as u64]; if (*doc).text_of(kept.namespace) == namespace && local_of((*doc).text_of(kept.name), kept.prefix) == local { return (*doc).text_of(kept.value); } } return nothing {}; } #! Iterates over the element's attributes: those its tag gives, in order, then #! the defaults the document type gives for those it does not. #! @param this The element to read. #! @returns An iterator over them. pub def element::attributes(&this): attribute_cursor { val held := (*this._document).record_at(this._at); return attribute_cursor { _document = this._document, _at = held.attributes, _left = held.many }; } #! Iterates over the nodes the element holds, in order. #! @param this The element to walk. #! @returns An iterator over them. pub def element::children(&this): node_cursor { return node_cursor { _document = this._document, _at = this._at + 1, _left = (*this._document).record_at(this._at).count }; } #! Iterates over the elements the element holds, in order, leaving out its text, #! comments, and instructions. #! @param this The element to walk. #! @returns An iterator over them. pub def element::elements(&this): element_cursor { return element_cursor { _document = this._document, _at = this._at + 1, _left = (*this._document).record_at(this._at).count }; } #! Finds the first element the element holds with a name as written. #! @param this The element to look in. #! @param name The name. #! @returns The element, or `nothing` where it holds none of the name. pub def element::child(&this, name: strview): maybe { for each in this.elements() { if each.name == name { return each; } } return nothing {}; } #! The text the element holds, within the elements it holds too, in order, as #! the DOM's `textContent` gives it. #! @param this The element to read. #! @param alloc An optional allocator for the text to take memory from. #! @returns The text. pub def element::text(&this, alloc: maybe<&mut allocator> = nothing {}): string { var made := string::new(alloc); val doc := this._document; val end := (*doc).record_at(this._at).next; for index in (this._at + 1)..end { val held := (*doc).record_at(index); if held.kind == record_kind::text { made.append((*doc).text_of(held.value)); } } return made; } #! The node the element is. #! @param this The element. #! @returns The node. pub def element::as_node(&this): node { return node { _document = this._document, _at = this._at }; } #! What kind of node the node is. #! @param this The node to ask. #! @returns Its kind. pub val node::kind: kind { get => ((*this._document).record_at(this._at).kind as u8) as kind; } #! The element the node is. #! @param this The node to ask. #! @returns The element, or `nothing` where the node is no element. pub def node::as_element(&this): maybe { if (*this._document).record_at(this._at).kind != record_kind::element { return nothing {}; } return element { _document = this._document, _at = this._at }; } #! An element's name as written, or a processing instruction's target. #! @param this The node to ask. #! @returns The name, or empty for text and comments. pub val node::name: strview { get => (*this._document).text_of((*this._document).record_at(this._at).name); } #! Text's text, a comment's, or what follows a processing instruction's target. #! @param this The node to ask. #! @returns The text, or empty for an element, whose text `element::text` gives. pub val node::text: strview { get { val held := (*this._document).record_at(this._at); if held.kind == record_kind::element { return ""; } return (*this._document).text_of(held.value); } } #! Where the node is in the text it was read from. #! @param this The node to ask. #! @returns Where it is, or where the entity holding it is referred to. pub val node::place: place { get => (*this._document).place_at(this._at); } #! An iterator over nodes, in order. pub struct node_cursor : iter { var _document: *document; var _at: u32; var _left: u32; } #! Gives the next node. #! @param this The iterator to advance. #! @returns The next node, or `nothing` once there are no more. pub def node_cursor::next(&mut this): maybe { if this._left == 0 { return nothing {}; } val made := node { _document = this._document, _at = this._at }; this._at = (*this._document).record_at(this._at).next; this._left--; return made; } #! An iterator over elements, in order. pub struct element_cursor : iter { var _document: *document; var _at: u32; var _left: u32; #! How many nodes are left, elements or not. } #! Gives the next element. #! @param this The iterator to advance. #! @returns The next element, or `nothing` once there are no more. pub def element_cursor::next(&mut this): maybe { while this._left > 0 { val at := this._at; val held := (*this._document).record_at(at); this._at = held.next; this._left--; if held.kind == record_kind::element { return element { _document = this._document, _at = at }; } } return nothing {}; } #! An iterator over an element's attributes, in order. pub struct attribute_cursor : iter { var _document: *document; var _at: u32; var _left: u32; } #! Gives the next attribute. #! @param this The iterator to advance. #! @returns The next attribute, or `nothing` once there are no more. pub def attribute_cursor::next(&mut this): maybe { if this._left == 0 { return nothing {}; } val doc := this._document; val kept := &(*doc)._attributes[this._at as u64]; val name := (*doc).text_of(kept.name); this._at++; this._left--; return attribute { name = name, prefix = name.substr(0, kept.prefix as u64), local = local_of(name, kept.prefix), namespace = (*doc).text_of(kept.namespace), value = (*doc).text_of(kept.value), specified = kept.specified, offset = kept.offset as u64 }; } # Keeps a copy of text among the document's strings. def document::keep(&mut this, text: strview): piece { val start := this._strings.length as u32; this._strings.append(text); return piece { start = start, length = text.length as u32 }; } # Keeps a namespace's name, unless it is the last kept, whose copy serves. def document::keep_namespace(&mut this, name: strview, last: &mut piece): piece { if name.length == 0 { return piece {}; } if this.text_of(last) != name { last = this.keep(name); } return last; } def document::text_of(&this, at: piece): strview { return this._strings.substr(at.start as u64, at.length as u64); } def document::record_at(&this, at: u32): &record { return &this._records[at as u64]; } def document::place_at(&this, at: u32): place { val held := this.record_at(at); return place { offset = held.offset as u64, line = held.line, column = held.column }; } # A name without the prefix it has of a length. def local_of(name: strview, prefix: u32): strview { if prefix == 0 { return name; } return name.substr(prefix as u64 + 1, name.length - prefix as u64 - 1); }