#! @file reader.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the pull reader, which reads XML one token at a time, checking it as
#! it goes: its elements and attributes, text, comments, and processing
#! instructions, with the document type's internal subset read on the way, and
#! the entities it declares expanded where they are referred to.
import std::containers;
module std::xml;
#! What a token is.
pub enum token_kind: u8 {
start, #! A start tag, or an empty element's tag, whose attributes `reader::attributes` gives.
end, #! An end tag, or the end of an empty element, which follows its start at once.
text, #! Character data, decoded, as what a CDATA section holds is.
comment, #! A comment, whose text is `text`.
instruction, #! A processing instruction, whose target is `name`, and what follows it `text`.
}
#! A piece of XML a reader gives.
#!
#! What it views is good until the next token: the text read, or the reader's
#! own memory, where the token's text was decoded.
pub struct token {
pub var kind: token_kind; #! What the token is.
pub var empty: bool; #! Whether a start tag is an empty element's, as ` ` is.
pub var cdata: bool; #! Whether text is what a CDATA section holds.
pub var name: strview; #! An element's name as written, as `svg:rect`, or an instruction's target.
pub var prefix: strview; #! The prefix of an element's name, or empty.
pub var local: strview; #! An element's name without its prefix.
pub var namespace: strview; #! The namespace an element's name is in, or empty for none.
pub var text: strview; #! Text, a comment, or what follows an instruction's target, decoded.
pub var offset: u64; #! The byte of the text it begins at, or where the entity holding it is referred to.
}
#! An attribute of an element.
pub struct attribute {
pub var name: strview; #! As written, as `xlink:href`.
pub var prefix: strview; #! The name's prefix, or empty.
pub var local: strview; #! The name without its prefix.
pub var namespace: strview; #! The namespace the name is in, or empty for none, as a name without a prefix is in.
pub var value: strview; #! The value, with its references replaced and its whitespace normalized.
pub var specified: bool; #! Whether the tag gives it, rather than the document type giving its default.
pub var offset: u64; #! The byte of the text its name begins at, or its tag's, for a default.
}
# What the reader looks for next.
enum reading: u8 {
start, #! The XML declaration, which only the very start of the text may hold.
prolog, #! The document type, comments, and instructions, before the root element.
content, #! What an element holds.
epilog, #! Comments and instructions, after the root element.
done, #! Nothing: the text has been read.
failed, #! Nothing: the reader has stopped at an error.
}
# The index of nothing, in the reader's tables.
const no_index: u32 = 0xFFFFFFFF;
# Text the reader went into another's from, as an entity's from where it is
# referred to.
struct input {
var text: strview; #! The text to go back to.
var at: u64; #! Where reading goes on in it.
var reference: u64; #! Where in it the reference is.
}
# An entity whose text is being read, so a reference to it within is recursion.
struct opened {
var name: strview;
var parameter: bool;
}
# An entity the document type declares.
struct entity {
var name: strview;
var text: strview; #! Its replacement text, which the reader holds.
var external: bool; #! Whether its text is outside the document.
var unparsed: bool; #! Whether it is not XML, as `NDATA` says.
}
# An attribute the document type declares for an element.
struct declared {
var name: strview;
var cdata: bool; #! Whether its values are kept as they are, rather than their spaces collapsed.
var value: maybe; #! Its default, normalized, where it has one.
var next: u32; #! The next declared for the same element, or `no_index`.
}
# A namespace bound to a prefix, whose name the reader's `_uris` holds.
struct binding {
var prefix: strview;
var start: u64;
var length: u64;
}
# An element whose end tag has not been read.
struct open_element {
var name: strview;
var prefix: strview;
var local: strview;
var level: u64; #! How many inputs deep its start tag is.
var bindings: u64; #! How many namespaces were bound before it.
var uris: u64; #! How long `_uris` was before it.
var offset: u64;
}
# Where an attribute's value is while its tag is read: in the reader's `_values`
# where it was decoded, and in the text read otherwise.
struct value_span {
var start: u64;
var length: u64;
var decoded: bool;
}
# A reference: to a character, which `code` is, or to an entity by `name`.
struct referral {
var code: u32;
var name: strview;
var end: u64; #! Just past its `;`.
}
#! Reads XML one token at a time, checking it as it goes, and stopping at the
#! first error, which `error` then gives.
#!
#! A reader views the text it reads, which must outlive it. A token's text and an
#! attribute's value view that text where they needed no decoding, and the
#! reader's own memory where they did, which the next token uses again, so what
#! a token gives is good only until the next. The names of elements and
#! attributes, and what the document type declares, view the text, or what the
#! reader holds as long as it lives.
pub struct reader {
var _source: strview; #! The document's text.
var _text: strview; #! The text being read: the document's, or an entity's.
var _at: u64;
var _inputs: vector; #! The texts gone into others from, the document's first.
var _opened: vector;
var _state: reading;
var _given: options;
var _open: vector;
var _attributes: vector; #! The last start tag's.
var _spans: vector;
var _values: string; #! The values of the last start tag's attributes that were decoded.
var _decoded: string; #! The last token's text, where it was decoded.
var _bindings: vector;
var _uris: string; #! The names of the namespaces bound.
var _seen: dict; #! The names of a tag's attributes, where it gives many.
var _keys: string;
var _general: dict;
var _parameters: dict;
var _texts: vector; #! Entities' texts and attributes' defaults, each where it stays.
var _declared: vector;
var _lists: dict; #! The first attribute declared for each element.
var _skipping: bool; #! Whether declarations are left out, after an entity not read.
var _typed: bool; #! Whether the document type has been read.
var _expanded: u64; #! How many bytes of entities' texts have been read.
var _queued: bool; #! Whether an empty element's end is given next.
var _pending: bool; #! Whether the element the last token ended is let go before the next.
var _bad: maybe;
var _version: strview;
var _encoding: strview;
var _standalone: bool;
var _doctype: strview;
var _error: maybe;
}
#! Makes a reader of XML text.
#! @param text The text to read, which must outlive the reader.
#! @param given How the text is read, and how far reading may go.
#! @returns The reader, at the start of the text.
pub def reader::new(text: strview, given: options = options {}): reader {
var made := reader {
_source = text,
_text = text,
_inputs = vector::::new(),
_opened = vector::::new(),
_given = given,
_open = vector::::new(),
_attributes = vector::::new(),
_spans = vector::::new(),
_values = string::new(nothing {}),
_decoded = string::new(nothing {}),
_bindings = vector::::new(),
_uris = string::new(nothing {}),
_seen = dict::::new(),
_keys = string::new(nothing {}),
_general = dict::::new(),
_parameters = dict::::new(),
_texts = vector::::new(),
_declared = vector::::new(),
_lists = dict::::new()
};
if text.length > 0xFFFFFFFFu {
made._error = error { kind = error_kind::too_long };
made._state = reading::failed;
return made;
}
made._bad = first_disallowed(text);
return made;
}
#! Why reading stopped before the end of the text, where it did.
#! @param this The reader to ask.
#! @returns The error, or `nothing` where none has been met.
pub val reader::error: maybe {
get => this._error;
}
#! How many elements are open: those whose start tags have been given, and
#! whose ends have not.
#! @param this The reader to ask.
#! @returns The count.
pub val reader::depth: u64 {
get => this._pending ? this._open.length - 1 : this._open.length;
}
#! The attributes of the element whose start tag was given last, those the tag
#! gives first, in its order, then the document type's defaults for those it
#! does not.
#! @param this The reader to ask.
#! @returns A view of them, good until the next token.
pub val reader::attributes: span {
get => this._attributes.view();
}
#! The version the XML declaration gives, as `1.0`.
#! @param this The reader to ask.
#! @returns The version, or empty where the text has no declaration, or it has
#! not been read yet.
pub val reader::version: strview {
get => this._version;
}
#! The encoding the XML declaration names, as `UTF-8`.
#! @param this The reader to ask.
#! @returns The encoding as written, or empty where none is named.
pub val reader::encoding: strview {
get => this._encoding;
}
#! Whether the XML declaration says the document stands alone, so nothing
#! outside it declares what it refers to.
#! @param this The reader to ask.
#! @returns Whether it says `standalone="yes"`.
pub val reader::standalone: bool {
get => this._standalone;
}
#! The name the document type gives the root element.
#! @param this The reader to ask.
#! @returns The name, or empty where the text has no document type, or it has
#! not been read yet.
pub val reader::doctype: strview {
get => this._doctype;
}
#! Reads the next token.
#! @param this The reader to read with.
#! @returns The token, or `nothing` at the end of the text or at an error, which
#! `error` tells apart.
pub def reader::next(&mut this): maybe {
if this._state == reading::failed || this._state == reading::done {
return nothing {};
}
if this._pending {
this._pending = false;
this.let_go();
}
if this._queued {
this._queued = false;
return this.close_element(this._open.last().offset);
}
while true {
match this._state {
start => {
if !this.read_declaration() {
return nothing {};
}
this._state = reading::prolog;
}
prolog => {
this.skip_space();
if this.at_end() {
return this.fail(error_kind::no_root, this._at);
}
if this.peek() != '<' {
return this.fail(error_kind::text_outside_root, this._at);
}
if this.looking_at("") {
return this.read_instruction();
}
if this.looking_at("