Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 11:26:34 -06:00
commit ce41693307
1625 files changed
+43618

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
#! @file xml.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains XML's errors and options, and where in the text something is.
#!
#! XML is read strictly, as XML 1.0 (Fifth Edition) writes it, with its names in
#! namespaces, as Namespaces in XML 1.0 writes them. Text must be UTF-8, and is
#! checked for what XML allows: well-formed markup, matching tags, attributes
#! given once, and references to what is declared. The document type's internal
#! subset is read, and its entities, its attributes' defaults, and how they're
#! normalized are used; nothing outside the text is read, so a reference to an
#! entity declared only there is an error. `xml::parse` reads text into an
#! `xml::document`, which holds every element, attribute, and text of it, and
#! `xml::reader` gives one token at a time.
module std::xml;
#! How deep elements, and entities within entities, may nest before reading
#! fails with `too_deep`, unless the options given say otherwise.
pub const default_depth: u32 = 512;
#! How many bytes of their texts the entities a document refers to may stand
#! for in all before reading fails with `too_much_expansion`, unless the options
#! given say otherwise: 16 MiB.
pub const default_expansion: u64 = 0x1000000;
#! How a document is read, and how far reading it may go, against text written
#! to exhaust a reader.
pub struct options {
pub val depth: u32 = default_depth; #! How deep elements, and entities within entities, may nest.
pub val expansion: u64 = default_expansion; #! How many bytes entities' texts may stand for, in all.
pub val namespaces: bool = true; #! Whether names are read into namespaces, as Namespaces in XML says.
}
#! Why XML could not be read.
pub enum error_kind: u8 {
unexpected_end, #! The text ends where something more must follow, as within an open element.
unexpected_byte, #! A byte stands where XML allows none of its kind.
invalid_utf8, #! The text is not UTF-8.
invalid_character, #! The text holds, or a reference names, a character XML does not allow.
unsupported_encoding, #! The text is in an encoding other than UTF-8, by its byte order mark or its declaration.
invalid_declaration, #! The XML declaration is written wrongly, or a processing instruction named `xml` stands elsewhere.
invalid_reference, #! An `&` begins no reference XML writes, as `&name;`, `
`, or `
` do.
undeclared_entity, #! A reference names an entity nothing in the text declares.
external_entity, #! A reference names an entity whose text is outside the document, which is not read.
recursive_entity, #! An entity refers to itself, or to one that refers to it.
unbalanced_entity, #! An entity's text ends within an element it began, or ends one it did not.
too_much_expansion, #! The entities referred to stand for more text than the limit.
mismatched_tag, #! An end tag names another element than the one open.
repeated_attribute, #! An element gives an attribute twice, or two whose names are the same in namespaces.
text_outside_root, #! Text stands before or after the root element, where only whitespace may.
no_root, #! The text holds no element.
second_root, #! An element follows the root element, where only one may stand.
invalid_name, #! A name holds a colon where namespaces allow none, as an entity's, or more than one.
unbound_prefix, #! A name's prefix is bound to no namespace.
invalid_namespace, #! A namespace is bound against Namespaces in XML, as `xmlns` is, or a prefix to no name.
too_deep, #! Elements, or entities within entities, nest deeper than the limit.
too_long, #! The text is longer than the 4 GiB a document holds.
}
#! Why XML could not be read, and where in the text.
#!
#! An error is plain data, so `else (e)` names it. One within an entity's text
#! is where the document refers to the entity.
pub struct error {
pub var kind: error_kind; #! What went wrong.
pub var offset: u64; #! The byte of the text the error is at.
pub var line: u32; #! Counted from 1.
pub var column: u32; #! The byte within the line, counted from 1.
}
#! Where in the text something is.
pub struct place {
pub var offset: u64; #! The byte of the text.
pub var line: u32; #! Counted from 1.
pub var column: u32; #! The byte within the line, counted from 1.
}
#! Says what the error is, in a sentence, after where it is.
#! @param this The error to describe.
#! @returns The description, as `3:7: An end tag names another element than the
#! one open.`
pub def error::describe(&this): string {
if this.line == 0 {
return string::from(describe(this.kind), nothing {});
}
return $"{this.line}:{this.column}: {describe(this.kind)}";
}
#! Says what a kind of error is, in a sentence.
#! @param kind The kind to describe.
#! @returns The sentence.
pub def describe(kind: error_kind): strview {
return match kind {
unexpected_end => "The text ends where something more must follow.",
unexpected_byte => "A byte stands where XML allows none of its kind.",
invalid_utf8 => "The text is not UTF-8.",
invalid_character => "The text holds, or a reference names, a character XML does not allow.",
unsupported_encoding => "The text is in an encoding other than UTF-8.",
invalid_declaration => "The XML declaration is written wrongly, or a processing instruction named `xml` stands elsewhere.",
invalid_reference => "An `&` begins no reference XML writes.",
undeclared_entity => "A reference names an entity nothing in the text declares.",
external_entity => "A reference names an entity whose text is outside the document, which is not read.",
recursive_entity => "An entity refers to itself, or to one that refers to it.",
unbalanced_entity => "An entity's text ends within an element it began, or ends one it did not.",
too_much_expansion => "The entities referred to stand for more text than the limit the text is read with.",
mismatched_tag => "An end tag names another element than the one open.",
repeated_attribute => "An element gives an attribute twice.",
text_outside_root => "Text stands outside the root element, where only whitespace may.",
no_root => "The text holds no element.",
second_root => "An element follows the root element, where only one may stand.",
invalid_name => "A name holds a colon where namespaces allow none.",
unbound_prefix => "A name's prefix is bound to no namespace.",
invalid_namespace => "A namespace is bound as Namespaces in XML does not allow.",
too_deep => "Elements or entities nest deeper than the limit the text is read with.",
too_long => "The text is longer than the 4 GiB a document holds.",
};
}
#! Where a byte of text is, by its line and its column. A line ends at `\n`, at
#! `\r\n`, or at a `\r` alone.
#! @param text The text.
#! @param offset The byte, which may be the text's length, for its end.
#! @returns Where it is.
pub def place_of(text: strview, offset: u64): place {
var lines := line_counter { _text = text };
return lines.place(offset);
}
# Counts lines up to places further and further on, so finding each costs only
# the text since the last.
struct line_counter {
var _text: strview;
var _at: u64;
var _line: u32 = 1;
var _begun: u64; #! Where the current line begins.
}
# Where a byte at or after the last asked for is.
def line_counter::place(&mut this, offset: u64): place {
val end := offset < this._text.length ? offset : this._text.length;
while this._at < end {
val byte := this._text[this._at];
if byte == '\n' || (byte == '\r' && (this._at + 1 >= this._text.length || this._text[this._at + 1] != '\n')) {
this._line++;
this._begun = this._at + 1;
}
this._at++;
}
return place { offset = offset, line = this._line, column = (offset - this._begun + 1) as u32 };
}
# An error of a kind at a byte of the text.
def error_at(kind: error_kind, text: strview, offset: u64): error {
val at := place_of(text, offset);
return error { kind = kind, offset = offset, line = at.line, column = at.column };
}