1048 lines
38 KiB
Plaintext
1048 lines
38 KiB
Plaintext
#! @file document.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the parsed stream, which holds every document of YAML text as a flat
|
|
#! array of nodes, and `yaml::value`, which reads one of them, following aliases.
|
|
|
|
import std::containers;
|
|
module std::yaml;
|
|
|
|
|
|
#! The kinds of YAML values, in the order of `serial::kind`.
|
|
pub enum kind: u8 {
|
|
empty, #! `null`, `~`, or nothing.
|
|
boolean, #! `true` or `false`, as the core schema reads them.
|
|
number, #! A number, integer or real, as the core schema reads them.
|
|
text, #! Text.
|
|
sequence, #! A list of values, in order.
|
|
mapping, #! Members, each a key and a value.
|
|
}
|
|
|
|
# What a node is: the kinds of values, then an alias of another.
|
|
enum node_kind: u8 {
|
|
empty,
|
|
boolean,
|
|
number,
|
|
text,
|
|
sequence,
|
|
mapping,
|
|
alias,
|
|
}
|
|
|
|
# What a node's form says of it.
|
|
const form_key: u8 = 1; # It is a member's key.
|
|
const form_decoded: u8 = 2; # Its text is in the document's buffer, from `next`.
|
|
const form_truth: u8 = 4; # It is the boolean `true`.
|
|
const form_merged: u8 = 8; # It is the key `<<`, whose mappings are merged in, so its member is not read.
|
|
|
|
# One node of the stream, in 16 bytes.
|
|
#
|
|
# A node's nodes follow it, so a sequence's elements follow it in order, and a
|
|
# mapping's members do, each as its key and then its value. A collection's `length`
|
|
# is how many elements or members it holds, and its `next` is the index just past
|
|
# everything it holds, so a node is skipped in one step. A scalar's text is `length`
|
|
# bytes from `next`, in the text read, or in the buffer where it is decoded. An
|
|
# alias's `length` is the node it names.
|
|
struct node {
|
|
var kind: node_kind;
|
|
var form: u8;
|
|
var tag: u16; #! Where its tag is in the document's tags, counted from 1, or 0 for none.
|
|
var start: u32; #! The byte of the text the node begins at.
|
|
var length: u32;
|
|
var next: u32;
|
|
}
|
|
|
|
#! YAML text read whole: every document of the stream, each value held as a node
|
|
#! of a flat array.
|
|
#!
|
|
#! A document views the text it was parsed from, which must outlive it, and the
|
|
#! values it gives view the document, so they are good only while it lives. A
|
|
#! scalar that needs decoding is decoded once, as the text is parsed, and an
|
|
#! alias is a node naming another, so nothing is copied, and every value it
|
|
#! gives follows aliases.
|
|
pub struct document : serial::decoder, moveable {
|
|
var _text: strview;
|
|
var _nodes: vector<node>;
|
|
var _decoded: string; #! The text of each scalar that needs decoding, decoded.
|
|
var _tags: string; #! The text of each tag, one after another.
|
|
var _tag_at: vector<u64>; #! Where each tag's text begins in `_tags`, and where it ends.
|
|
var _roots: vector<u32>; #! Each document's value.
|
|
var _starts: vector<u64>; #! Where each document begins in the text.
|
|
}
|
|
|
|
#! A value of a document, by its place in it. It is good only while the document
|
|
#! lives and stays where it is.
|
|
#!
|
|
#! A value writes itself through any format, as `serial::encode` writes a typed
|
|
#! value, so a document's values are written again whole, with aliases expanded.
|
|
pub struct value : serial::encodable {
|
|
var _document: *document;
|
|
var _at: u32;
|
|
}
|
|
|
|
#! A member of a mapping, as `value::members` gives each.
|
|
pub struct member {
|
|
pub var name: strview; #! The key's text, where the key is a scalar, and otherwise empty.
|
|
pub var key: value; #! The key, which may be any value.
|
|
pub var value: value; #! Its value.
|
|
}
|
|
|
|
# A collection open as the stream is parsed.
|
|
struct open_collection {
|
|
var at: u32; #! Its node.
|
|
var held: u64; #! How many nodes it holds so far, keys and values counted apart.
|
|
var extra: u64; #! How many more nodes the aliases within it stand for than themselves.
|
|
var anchored: bool; #! Whether an anchor names it, so aliases may stand for it.
|
|
}
|
|
|
|
#! Reads a stream of YAML documents whole.
|
|
#! @param text The text, which must outlive the document and every value of
|
|
#! it.
|
|
#! @param given How deep collections may nest, and how many nodes aliases may
|
|
#! stand for in all.
|
|
#! @param alloc An optional allocator for the document to take memory from.
|
|
#! @returns The documents, or why the text is not YAML, with where it fails.
|
|
pub def parse(text: strview, given: limits = limits {}, alloc: maybe<&mut allocator> = nothing {}): result<document, error> {
|
|
var made := document {
|
|
_text = text,
|
|
_nodes = vector::<node>::new(alloc),
|
|
_decoded = string::new(alloc),
|
|
_tags = string::new(alloc),
|
|
_tag_at = vector::<u64>::new(alloc),
|
|
_roots = vector::<u32>::new(alloc),
|
|
_starts = vector::<u64>::new(alloc)
|
|
};
|
|
|
|
# The first node is the empty value a stream without documents gives as its root.
|
|
made._nodes.append(node { kind = node_kind::empty });
|
|
|
|
var open := vector::<open_collection>::new(alloc);
|
|
var anchors := dict::<strview, u32>::new(alloc);
|
|
var weights := dict::<u32, u64>::new(alloc); # How many nodes each anchored collection stands for.
|
|
var aliased: u64 = 0; # How many nodes every alias stands for, in all.
|
|
var events := reader::new(text, given.depth);
|
|
while events.next() is some(read) {
|
|
val at := made._nodes.length as u32;
|
|
match read.kind {
|
|
begin_document => {
|
|
made._roots.append(at);
|
|
made._starts.append(read.offset);
|
|
}
|
|
end_document => {}
|
|
scalar => {
|
|
made.add_scalar(&read) else (e) { return e; };
|
|
made.count_in(&mut open, at);
|
|
if read.anchor.length > 0 {
|
|
anchors.set(read.anchor, at);
|
|
}
|
|
}
|
|
begin_mapping, begin_sequence => {
|
|
val shape := read.kind == event_kind::begin_mapping ? node_kind::mapping : node_kind::sequence;
|
|
val tag := made.tag_of(read.tag) else { return error_at(error_kind::too_long, text, read.offset); };
|
|
if !collection_tag_fits(shape, read.tag) {
|
|
return error_at(error_kind::mistagged, text, read.offset);
|
|
}
|
|
made._nodes.append(node { kind = shape, tag = tag, start = read.offset as u32 });
|
|
made.count_in(&mut open, at);
|
|
open.append(open_collection { at = at, anchored = read.anchor.length > 0 });
|
|
if read.anchor.length > 0 {
|
|
anchors.set(read.anchor, at);
|
|
}
|
|
}
|
|
end_mapping, end_sequence => {
|
|
val closing: open_collection = open.at(open.length - 1);
|
|
open.remove_at(open.length - 1);
|
|
val mapping := made._nodes.at(closing.at as u64).kind == node_kind::mapping;
|
|
made._nodes.at(closing.at as u64).length = (mapping ? closing.held / 2 : closing.held) as u32;
|
|
made._nodes.at(closing.at as u64).next = made._nodes.length as u32;
|
|
|
|
var extra := closing.extra;
|
|
if mapping {
|
|
val merged := made.merge(closing.at, &weights);
|
|
aliased += merged;
|
|
extra += merged;
|
|
if aliased > given.aliases {
|
|
return error_at(error_kind::too_many_aliases, text, read.offset);
|
|
}
|
|
}
|
|
if closing.anchored {
|
|
weights.set(closing.at, (made._nodes.length as u64 - closing.at as u64) + extra);
|
|
}
|
|
if open.length > 0 {
|
|
open.at(open.length - 1).extra += extra;
|
|
}
|
|
}
|
|
alias => {
|
|
val target := anchors.get(&read.text) else { return error_at(error_kind::unknown_anchor, text, read.offset); };
|
|
val named: u32 = target;
|
|
val aimed := made._nodes.at(named as u64).kind;
|
|
if (aimed == node_kind::mapping || aimed == node_kind::sequence) && made._nodes.at(named as u64).next == 0 {
|
|
return error_at(error_kind::recursive_alias, text, read.offset);
|
|
}
|
|
|
|
var weight: u64 = 1;
|
|
if weights.get(&named) is some(stood) {
|
|
weight = stood;
|
|
}
|
|
aliased += weight;
|
|
if aliased > given.aliases {
|
|
return error_at(error_kind::too_many_aliases, text, read.offset);
|
|
}
|
|
made._nodes.append(node { kind = node_kind::alias, start = read.offset as u32, length = named });
|
|
made.count_in(&mut open, at);
|
|
if open.length > 0 {
|
|
open.at(open.length - 1).extra += weight - 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if events.error is some(failed) {
|
|
return failed;
|
|
}
|
|
return move(made);
|
|
}
|
|
|
|
#! Creates a document by taking another's nodes and decoded text, which leaves
|
|
#! the other holding nothing.
|
|
#! @param other The document to move from.
|
|
#! @returns The document, which views the same text.
|
|
pub def document::move_of(other: &mut document): document {
|
|
return document {
|
|
_text = other._text,
|
|
_nodes = move(other._nodes),
|
|
_decoded = move(other._decoded),
|
|
_tags = move(other._tags),
|
|
_tag_at = move(other._tag_at),
|
|
_roots = move(other._roots),
|
|
_starts = move(other._starts)
|
|
};
|
|
}
|
|
|
|
#! The value of the first document of the stream, which holds the rest of it, or
|
|
#! an empty value where the stream holds no document.
|
|
#! @param this The document to read.
|
|
#! @returns The value.
|
|
pub val document::root: value {
|
|
get {
|
|
val at: u32 = this._roots.length > 0 ? this._roots.at(0) : 0;
|
|
return value { _document = this as *document, _at = at };
|
|
}
|
|
}
|
|
|
|
#! How many documents the stream holds.
|
|
#! @param this The document to ask.
|
|
#! @returns The count.
|
|
pub val document::document_count: u64 {
|
|
get => this._roots.length;
|
|
}
|
|
|
|
#! The text the stream was parsed from.
|
|
#! @param this The document to ask.
|
|
#! @returns The text.
|
|
pub val document::text: strview {
|
|
get => this._text;
|
|
}
|
|
|
|
#! The value of a document of the stream, by its index.
|
|
#! @param this The stream to read.
|
|
#! @param index Which document, counted from 0.
|
|
#! @returns Its value, or `nothing` where the stream holds fewer.
|
|
pub def document::document(&this, index: u64): maybe<value> {
|
|
if index >= this._roots.length {
|
|
return nothing {};
|
|
}
|
|
return value { _document = this as *document, _at = this._roots.at(index) };
|
|
}
|
|
|
|
#! Where a document of the stream begins in the text.
|
|
#! @param this The stream to read.
|
|
#! @param index Which document, which must be fewer than the count.
|
|
#! @returns The byte it begins at.
|
|
pub def document::document_start(&this, index: u64): u64 {
|
|
return this._starts.at(index);
|
|
}
|
|
|
|
#! Iterates over the value of each document of the stream, in order.
|
|
#! @param this The stream to walk.
|
|
#! @returns An iterator over them.
|
|
pub def document::documents(&this): document::iterator {
|
|
return document::iterator { _document = this as *document };
|
|
}
|
|
|
|
#! An iterator over the documents of a stream.
|
|
pub struct document::iterator : iter<value> {
|
|
var _document: *document;
|
|
var _index: u64;
|
|
}
|
|
|
|
#! Gives the root of the next document of the stream.
|
|
#! @param this The iterator to advance.
|
|
#! @returns The next document's root, or `nothing` once there are no more.
|
|
pub def document::iterator::next(&mut this): maybe<value> {
|
|
val made := (*this._document).document(this._index) else { return nothing {}; };
|
|
this._index++;
|
|
return made;
|
|
}
|
|
|
|
#! What kind of value the value is, following an alias.
|
|
#! @param this The value to ask.
|
|
#! @returns Its kind.
|
|
pub val value::kind: kind {
|
|
get => (*this._document).kind_at(this._at);
|
|
}
|
|
|
|
#! Whether the value is empty, as `null`, `~`, or nothing is.
|
|
#! @param this The value to ask.
|
|
#! @returns Whether it is.
|
|
pub val value::is_null: bool {
|
|
get => this.kind == kind::empty;
|
|
}
|
|
|
|
#! How many elements a sequence holds, or members a mapping holds, repeats and
|
|
#! those merged in included.
|
|
#! @param this The value to measure.
|
|
#! @returns The count, which is 0 for any other value.
|
|
pub val value::length: u64 {
|
|
get => (*this._document).count(this._at);
|
|
}
|
|
|
|
#! Where the value begins in the text, by its line and its column.
|
|
#! @param this The value to place.
|
|
#! @returns Where it is.
|
|
pub val value::place: serial::place {
|
|
get => (*this._document).place(this._at);
|
|
}
|
|
|
|
#! The tag the value is written with, resolved, as `tag:yaml.org,2002:str` for
|
|
#! `!!str` or `!circle` for a local tag.
|
|
#! @param this The value to ask.
|
|
#! @returns The tag, or `nothing` where none is written.
|
|
pub val value::tag: maybe<strview> {
|
|
get => (*this._document).tag_at(this._at);
|
|
}
|
|
|
|
#! Finds the first member of a mapping whose key is written as a text.
|
|
#! @param this The mapping to look in, which is searched member by member.
|
|
#! @param key The key's text, as it is written, so `1` finds both `1` and
|
|
#! `"1"`.
|
|
#! @returns The member's value, or `nothing` where the value is not a mapping or
|
|
#! has no member of the key.
|
|
pub def value::get(&this, key: strview): maybe<value> {
|
|
val found := (*this._document).member(this._at, key) else { return nothing {}; };
|
|
return value { _document = this._document, _at = found };
|
|
}
|
|
|
|
#! Iterates over the values of every member of a mapping whose key is written as
|
|
#! a text, in the order they are written.
|
|
#! @param this The mapping to look in.
|
|
#! @param key The key's text.
|
|
#! @returns An iterator over the values, which gives none where the value is not
|
|
#! a mapping.
|
|
pub def value::get_all(&this, key: strview): value::match_cursor {
|
|
return value::match_cursor { _members = this.members(), _key = key };
|
|
}
|
|
|
|
#! Finds an element of a sequence by its index.
|
|
#! @param this The sequence to look in.
|
|
#! @param index The element's index, counted from 0.
|
|
#! @returns The element, or `nothing` where the value is not a sequence or is
|
|
#! shorter.
|
|
pub def value::at(&this, index: u64): maybe<value> {
|
|
if this.kind != kind::sequence || index >= this.length {
|
|
return nothing {};
|
|
}
|
|
return value { _document = this._document, _at = (*this._document).element(this._at, index) };
|
|
}
|
|
|
|
#! Iterates over the elements of a sequence, in order.
|
|
#! @param this The sequence to walk.
|
|
#! @returns An iterator over them, which gives none where the value is not a
|
|
#! sequence.
|
|
pub def value::elements(&this): value::element_cursor {
|
|
val at := (*this._document).resolved(this._at);
|
|
if this.kind != kind::sequence {
|
|
return value::element_cursor { _document = this._document };
|
|
}
|
|
return value::element_cursor { _document = this._document, _at = at + 1, _left = this.length };
|
|
}
|
|
|
|
#! Iterates over the members of a mapping, in the order they are written,
|
|
#! repeats included, then those merged in.
|
|
#! @param this The mapping to walk.
|
|
#! @returns An iterator over them, which gives none where the value is not a
|
|
#! mapping.
|
|
pub def value::members(&this): value::member_cursor {
|
|
val at := (*this._document).resolved(this._at);
|
|
if this.kind != kind::mapping {
|
|
return value::member_cursor { _document = this._document };
|
|
}
|
|
return value::member_cursor { _document = this._document, _at = at + 1, _left = this.length };
|
|
}
|
|
|
|
#! Reads a boolean.
|
|
#! @param this The value to read.
|
|
#! @returns Its truth, or `wrong_kind` where it is not a boolean.
|
|
pub def value::as_bool(&this): result<bool, error> {
|
|
return (*this._document).as_bool(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Reads a number as a signed integer, exactly, in any base it is written in.
|
|
#! @param this The value to read.
|
|
#! @returns The integer, or `wrong_kind` where it is not a whole number, or
|
|
#! `out_of_range` where it does not fit.
|
|
pub def value::as_i64(&this): result<i64, error> {
|
|
return (*this._document).as_i64(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Reads a number as an unsigned integer, exactly, in any base it is written in.
|
|
#! @param this The value to read.
|
|
#! @returns The integer, or `wrong_kind` where it is not a whole number, or
|
|
#! `out_of_range` where it is negative or does not fit.
|
|
pub def value::as_u64(&this): result<u64, error> {
|
|
return (*this._document).as_u64(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Reads a number as the nearest 64-bit float, with `.inf`, `-.inf`, and `.nan`.
|
|
#! @param this The value to read.
|
|
#! @returns The float, or `wrong_kind` where it is not a number, or
|
|
#! `out_of_range` where it is too large for 64 bits.
|
|
pub def value::as_f64(&this): result<f64, error> {
|
|
return (*this._document).as_f64(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Reads text, decoded.
|
|
#! @param this The value to read.
|
|
#! @returns A view of its text, good while the document lives, or `wrong_kind`
|
|
#! where it is not text, as a number or `null` written plain is not.
|
|
pub def value::as_text(&this): result<strview, error> {
|
|
return (*this._document).as_text(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Reads any scalar's text as it is written, decoded where it is quoted, so a
|
|
#! number, a boolean, and text alike read as text, as `2`, `true`, or `0x1F`.
|
|
#! @param this The value to read.
|
|
#! @returns A view of its text, good while the document lives, which is empty
|
|
#! for an empty value, or `wrong_kind` where it is a sequence or a
|
|
#! mapping.
|
|
pub def value::as_scalar(&this): result<strview, error> {
|
|
return (*this._document).as_scalar(this._at) else (e) { return error::from(&e); };
|
|
}
|
|
|
|
#! Writes the value and everything it holds through a format, with aliases
|
|
#! expanded. A number is written as an integer where it is one that fits, and as
|
|
#! the nearest float otherwise. A key that is not a scalar has no name to write,
|
|
#! so its member is left out, and the format keeps `wrong_kind`.
|
|
#! @param this The value to write.
|
|
#! @param out The format to write it through.
|
|
pub def value::encode(&this, out: &mut serial::encoder): void {
|
|
match this.kind {
|
|
empty => out.empty();
|
|
boolean => out.boolean(this.as_bool().value);
|
|
text => out.text(this.as_text().value);
|
|
number => this.encode_number(out);
|
|
sequence => {
|
|
out.begin_array(this.length);
|
|
for item in this.elements() {
|
|
item.encode(out);
|
|
}
|
|
out.end_array();
|
|
}
|
|
mapping => {
|
|
out.begin_object(this.length);
|
|
for each in this.members() {
|
|
val shape := each.key.kind;
|
|
if shape == kind::sequence || shape == kind::mapping {
|
|
out.fail(serial::error_kind::wrong_kind);
|
|
continue;
|
|
}
|
|
out.member(each.name);
|
|
each.value.encode(out);
|
|
}
|
|
out.end_object();
|
|
}
|
|
}
|
|
}
|
|
|
|
# Writes a number as an integer where it is one that fits, and otherwise as the
|
|
# nearest float, or as nothing where it is too large for one, keeping the failure.
|
|
def value::encode_number(&this, out: &mut serial::encoder): void {
|
|
val read := integral_of((*this._document).text_at(this._at));
|
|
val limit: u64 = read.negative ? 0x8000000000000000u : 0x7FFFFFFFFFFFFFFFu;
|
|
if read.whole && read.fits && read.magnitude <= limit {
|
|
out.signed(read.negative ? (0 - read.magnitude) as i64 : read.magnitude as i64);
|
|
return;
|
|
}
|
|
if read.whole && read.fits && !read.negative {
|
|
out.unsigned(read.magnitude);
|
|
return;
|
|
}
|
|
match this.as_f64() {
|
|
value(real) => out.real(real);
|
|
error(_) => {
|
|
out.fail(serial::error_kind::out_of_range);
|
|
out.empty();
|
|
}
|
|
}
|
|
}
|
|
|
|
#! An iterator over a sequence's elements.
|
|
pub struct value::element_cursor : iter<value> {
|
|
var _document: *document;
|
|
var _at: u32;
|
|
var _left: u64;
|
|
}
|
|
|
|
#! Gives the next element of the sequence.
|
|
#! @param this The iterator to advance.
|
|
#! @returns The next element, or `nothing` once there are no more.
|
|
pub def value::element_cursor::next(&mut this): maybe<value> {
|
|
if this._left == 0 {
|
|
return nothing {};
|
|
}
|
|
|
|
val made := value { _document = this._document, _at = this._at };
|
|
this._at = (*this._document).after(this._at);
|
|
this._left--;
|
|
return made;
|
|
}
|
|
|
|
#! An iterator over a mapping's members.
|
|
pub struct value::member_cursor : iter<member> {
|
|
var _document: *document;
|
|
var _at: u32; #! The next member's key.
|
|
var _left: u64;
|
|
}
|
|
|
|
#! Gives the next member of the mapping, in the order the text writes them.
|
|
#! @param this The iterator to advance.
|
|
#! @returns The next member, or `nothing` once there are no more.
|
|
pub def value::member_cursor::next(&mut this): maybe<member> {
|
|
if this._left == 0 {
|
|
return nothing {};
|
|
}
|
|
|
|
val held := &through(this._document);
|
|
val key := held.visible_key(this._at);
|
|
val at := held.after(key);
|
|
this._at = held.after(at);
|
|
this._left--;
|
|
return member {
|
|
name = held.key_text(key),
|
|
key = value { _document = this._document, _at = key },
|
|
value = value { _document = this._document, _at = at }
|
|
};
|
|
}
|
|
|
|
#! An iterator over the values of a mapping's members of one key.
|
|
pub struct value::match_cursor : iter<value> {
|
|
var _members: value::member_cursor;
|
|
var _key: strview;
|
|
}
|
|
|
|
#! Gives the value of the next member of the name.
|
|
#! @param this The iterator to advance.
|
|
#! @returns The next value, or `nothing` once no member left has the name.
|
|
pub def value::match_cursor::next(&mut this): maybe<value> {
|
|
while this._members.next() is some(each) {
|
|
if each.name == this._key {
|
|
return each.value;
|
|
}
|
|
}
|
|
return nothing {};
|
|
}
|
|
|
|
#! What kind of value a node is, following an alias.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns Its kind, as every format names it.
|
|
pub def document::kind_of(&this, at: u32): serial::kind {
|
|
return (this.kind_at(at) as u8) as serial::kind;
|
|
}
|
|
|
|
#! Finds the value of the first member of a mapping whose key is written as a
|
|
#! text.
|
|
#! @param this The document to read.
|
|
#! @param at The mapping's index.
|
|
#! @param name The key's text.
|
|
#! @returns The member's value's index, or `nothing` where there is none of the
|
|
#! key or the value is not a mapping.
|
|
pub def document::member(&this, at: u32, name: strview): maybe<u32> {
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind != node_kind::mapping {
|
|
return nothing {};
|
|
}
|
|
|
|
var key := held + 1;
|
|
for _ in 0..this.node_at(held).length as u64 {
|
|
key = this.visible_key(key);
|
|
val value_at := this.after(key);
|
|
if this.key_text(key) == name {
|
|
return value_at;
|
|
}
|
|
key = this.after(value_at);
|
|
}
|
|
return nothing {};
|
|
}
|
|
|
|
#! How many elements a sequence holds, or members a mapping holds.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns The count, which is 0 for any other value.
|
|
pub def document::count(&this, at: u32): u64 {
|
|
val held := this.node_at(this.resolved(at));
|
|
if held.kind != node_kind::sequence && held.kind != node_kind::mapping {
|
|
return 0;
|
|
}
|
|
return held.length as u64;
|
|
}
|
|
|
|
#! The text of the key of the member of a mapping whose value is given.
|
|
#! @param this The document to read.
|
|
#! @param value The member's value's index.
|
|
#! @returns The key's text, or empty where the key is not a scalar.
|
|
pub def document::name_of(&this, value: u32): strview {
|
|
val key := this.node_at(value - 1);
|
|
if (key.form & form_key) == 0 {
|
|
return strview::blank();
|
|
}
|
|
return this.key_text(value - 1);
|
|
}
|
|
|
|
#! The element of a sequence, or the value of a member of a mapping, after
|
|
#! another.
|
|
#! @param this The document to read.
|
|
#! @param at The sequence's or mapping's index.
|
|
#! @param previous The element or value before, which must not be the last.
|
|
#! @returns The value's index.
|
|
pub def document::next_element(&this, at: u32, previous: u32): u32 {
|
|
val next := this.after(previous);
|
|
if this.node_at(this.resolved(at)).kind != node_kind::mapping {
|
|
return next;
|
|
}
|
|
return this.after(this.visible_key(next));
|
|
}
|
|
|
|
#! An element of a sequence, or the value of a member of a mapping, by its
|
|
#! index.
|
|
#! @param this The document to read.
|
|
#! @param at The sequence's or mapping's index.
|
|
#! @param index Which element or member, which must be less than the count.
|
|
#! @returns The value's index.
|
|
pub def document::element(&this, at: u32, index: u64): u32 {
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind != node_kind::mapping {
|
|
var element := held + 1;
|
|
for _ in 0..index {
|
|
element = this.after(element);
|
|
}
|
|
return element;
|
|
}
|
|
|
|
var key := this.visible_key(held + 1);
|
|
for _ in 0..index {
|
|
key = this.visible_key(this.after(this.after(key)));
|
|
}
|
|
return this.after(key);
|
|
}
|
|
|
|
#! Reads text, decoded.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns A view of the text, or `wrong_kind` where the value is not text.
|
|
pub def document::as_text(&this, at: u32): result<strview, serial::error> {
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind != node_kind::text {
|
|
return this.wrong(at);
|
|
}
|
|
return this.text_at(held);
|
|
}
|
|
|
|
#! Reads any scalar's text as it is written, decoded where it is quoted.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns A view of the text, which is empty for an empty value, or
|
|
#! `wrong_kind` where the value is a sequence or a mapping.
|
|
pub def document::as_scalar(&this, at: u32): result<strview, serial::error> {
|
|
val held := this.resolved(at);
|
|
val shape := this.node_at(held).kind;
|
|
if shape == node_kind::sequence || shape == node_kind::mapping {
|
|
return this.wrong(at);
|
|
}
|
|
return shape == node_kind::empty ? strview::blank() : this.text_at(held);
|
|
}
|
|
|
|
#! Reads a value as a signed integer, as `serial::decode` does.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns The integer, or `wrong_kind` where the value is not a number, or
|
|
#! `out_of_range` where it is not an integer `i64` holds.
|
|
pub def document::as_i64(&this, at: u32): result<i64, serial::error> {
|
|
val read := this.integral_at(at) else (e) { return e; };
|
|
val limit: u64 = read.negative ? 0x8000000000000000u : 0x7FFFFFFFFFFFFFFFu;
|
|
if !read.fits || read.magnitude > limit {
|
|
return serial::error::at(serial::error_kind::out_of_range, this.place(at));
|
|
}
|
|
return read.negative ? (0 - read.magnitude) as i64 : read.magnitude as i64;
|
|
}
|
|
|
|
#! Reads a value as an unsigned integer, as `serial::decode` does.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns The integer, or `wrong_kind` where the value is not a number, or
|
|
#! `out_of_range` where it is not an integer `u64` holds.
|
|
pub def document::as_u64(&this, at: u32): result<u64, serial::error> {
|
|
val read := this.integral_at(at) else (e) { return e; };
|
|
if !read.fits || (read.negative && read.magnitude != 0) {
|
|
return serial::error::at(serial::error_kind::out_of_range, this.place(at));
|
|
}
|
|
return read.magnitude;
|
|
}
|
|
|
|
#! Reads a value as a real number, as `serial::decode` does.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns The number, or `wrong_kind` where the value is not a number, or
|
|
#! `out_of_range` where it is too large for an `f64`.
|
|
pub def document::as_f64(&this, at: u32): result<f64, serial::error> {
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind != node_kind::number {
|
|
return this.wrong(at);
|
|
}
|
|
val read := real_of(this.text_at(held)) else {
|
|
return serial::error::at(serial::error_kind::out_of_range, this.place(at));
|
|
};
|
|
return read;
|
|
}
|
|
|
|
#! Reads a value as `true` or `false`, as `serial::decode` does.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns The value, or `wrong_kind` where it is not a boolean.
|
|
pub def document::as_bool(&this, at: u32): result<bool, serial::error> {
|
|
val held := this.node_at(this.resolved(at));
|
|
if held.kind != node_kind::boolean {
|
|
return this.wrong(at);
|
|
}
|
|
return (held.form & form_truth) != 0;
|
|
}
|
|
|
|
#! Where a value begins in the text, by its line and its column.
|
|
#! @param this The document to read.
|
|
#! @param at The value's index.
|
|
#! @returns Where it is.
|
|
pub def document::place(&this, at: u32): serial::place {
|
|
return serial::place_of(this._text, this.node_at(at).start as u64);
|
|
}
|
|
|
|
# Adds a scalar, resolving what it is: by its tag where it has one, and otherwise as
|
|
# text where it is quoted or a block, and by the core schema where it is plain.
|
|
def document::add_scalar(&mut this, read: &event): result<void, error> {
|
|
var made := node { start = read.offset as u32, length = read.text.length as u32 };
|
|
|
|
# Text the scalar views where it is written is kept by its place, and other text is
|
|
# copied, since the reader uses its memory again.
|
|
val text := read.text;
|
|
val within := text.length == 0
|
|
|| (text.data >= this._text.data && text.data + text.length <= this._text.data + this._text.length);
|
|
if within && text.length > 0 {
|
|
made.next = (text.data - this._text.data) as u32;
|
|
} else if text.length > 0 {
|
|
made.form = form_decoded;
|
|
made.next = this._decoded.length as u32;
|
|
this._decoded.append(text);
|
|
}
|
|
|
|
val tag := read.tag;
|
|
var shape := resolved::text;
|
|
if tag.length == 0 {
|
|
shape = read.style == style::plain ? resolve(text) : resolved::text;
|
|
} else if tag.starts_with("tag:yaml.org,2002:") {
|
|
val name := tag.substr(18, tag.length);
|
|
val written := resolve(text);
|
|
if name == "str" {
|
|
shape = resolved::text;
|
|
} else if name == "null" {
|
|
if written != resolved::empty {
|
|
return error_at(error_kind::mistagged, this._text, read.offset);
|
|
}
|
|
shape = resolved::empty;
|
|
} else if name == "bool" {
|
|
if written != resolved::truth && written != resolved::falsehood {
|
|
return error_at(error_kind::mistagged, this._text, read.offset);
|
|
}
|
|
shape = written;
|
|
} else if name == "int" {
|
|
if written != resolved::integer {
|
|
return error_at(error_kind::mistagged, this._text, read.offset);
|
|
}
|
|
shape = written;
|
|
} else if name == "float" {
|
|
if written != resolved::integer && written != resolved::real {
|
|
return error_at(error_kind::mistagged, this._text, read.offset);
|
|
}
|
|
shape = resolved::real;
|
|
} else if name == "seq" || name == "map" {
|
|
return error_at(error_kind::mistagged, this._text, read.offset);
|
|
}
|
|
}
|
|
|
|
made.kind = match shape {
|
|
empty => node_kind::empty,
|
|
truth, falsehood => node_kind::boolean,
|
|
integer, real => node_kind::number,
|
|
text => node_kind::text,
|
|
};
|
|
if shape == resolved::truth {
|
|
made.form = made.form | form_truth;
|
|
}
|
|
made.tag = this.tag_of(tag) else { return error_at(error_kind::too_long, this._text, read.offset); };
|
|
this._nodes.append(made);
|
|
return;
|
|
}
|
|
|
|
# Whether a collection's tag is one it may have: none, or anything but YAML's tags
|
|
# of the other kinds.
|
|
def collection_tag_fits(shape: node_kind, tag: strview): bool {
|
|
if !tag.starts_with("tag:yaml.org,2002:") {
|
|
return true;
|
|
}
|
|
val name := tag.substr(18, tag.length);
|
|
if name == "str" || name == "null" || name == "bool" || name == "int" || name == "float" {
|
|
return false;
|
|
}
|
|
return !(shape == node_kind::mapping && name == "seq") && !(shape == node_kind::sequence && name == "map");
|
|
}
|
|
|
|
# Counts a node, just added, in the innermost open collection, marking it a key
|
|
# where it is one of a mapping's.
|
|
def document::count_in(&mut this, open: &mut vector<open_collection>, at: u32): void {
|
|
if open.length == 0 {
|
|
return;
|
|
}
|
|
val holder := &mut open.at(open.length - 1);
|
|
if this._nodes.at(holder.at as u64).kind == node_kind::mapping && holder.held % 2 == 0 {
|
|
this._nodes.at(at as u64).form = this._nodes.at(at as u64).form | form_key;
|
|
}
|
|
holder.held++;
|
|
}
|
|
|
|
# The index of a tag in the document's tags, counted from 1, adding it where it is
|
|
# new, or 0 for no tag, or `nothing` where the stream holds too many tags.
|
|
def document::tag_of(&mut this, tag: strview): maybe<u16> {
|
|
if tag.length == 0 {
|
|
return 0 as u16;
|
|
}
|
|
val count := this._tag_at.length / 2;
|
|
for index in 0..count {
|
|
val from := this._tag_at.at(2 * index);
|
|
val to := this._tag_at.at(2 * index + 1);
|
|
if this._tags.substr(from, to - from) == tag {
|
|
return (index + 1) as u16;
|
|
}
|
|
}
|
|
if count >= 0xFFFF {
|
|
return nothing {};
|
|
}
|
|
this._tag_at.append(this._tags.length);
|
|
this._tags.append(tag);
|
|
this._tag_at.append(this._tags.length);
|
|
return (count + 1) as u16;
|
|
}
|
|
|
|
# Merges into a mapping, which has just closed, the members of the mappings its `<<`
|
|
# keys name, that it lacks: an earlier source before a later, and its own members
|
|
# before any. Its `<<` members are no longer read as members. Gives how many more
|
|
# nodes the members merged in stand for than the nodes added, toward the limit.
|
|
def document::merge(&mut this, at: u32, weights: &dict<u32, u64>): u64 {
|
|
val count := this._nodes.at(at as u64).length as u64;
|
|
var extra: u64 = 0;
|
|
var key := at + 1;
|
|
for _ in 0..count {
|
|
val value_at := this.after(key);
|
|
val next_key := this.after(value_at);
|
|
if this.is_merge_key(key) {
|
|
val sources := this.merge_sources(value_at);
|
|
if sources.length > 0 {
|
|
this._nodes.at(key as u64).form = this._nodes.at(key as u64).form | form_merged;
|
|
this._nodes.at(at as u64).length--;
|
|
for source in sources {
|
|
extra += this.merge_from(at, source, weights);
|
|
}
|
|
}
|
|
}
|
|
key = next_key;
|
|
}
|
|
this._nodes.at(at as u64).next = this._nodes.length as u32;
|
|
return extra;
|
|
}
|
|
|
|
# Whether a key is `<<`, written plain without a tag, or tagged `!!merge`.
|
|
def document::is_merge_key(&this, at: u32): bool {
|
|
val held := this.node_at(at);
|
|
if held.kind == node_kind::alias || held.kind == node_kind::sequence || held.kind == node_kind::mapping {
|
|
return false;
|
|
}
|
|
if held.tag != 0 {
|
|
return this.tag_text(held.tag) == "tag:yaml.org,2002:merge";
|
|
}
|
|
return held.kind == node_kind::text && this.text_at(at) == "<<";
|
|
}
|
|
|
|
# The mappings a `<<` key's value names: one mapping, or a sequence of them. Any
|
|
# other value merges nothing, and leaves `<<` an ordinary key.
|
|
def document::merge_sources(&this, at: u32): vector<u32> {
|
|
var made := vector::<u32>::new();
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind == node_kind::mapping {
|
|
made.append(held);
|
|
return move(made);
|
|
}
|
|
if this.node_at(held).kind != node_kind::sequence {
|
|
return move(made);
|
|
}
|
|
|
|
var element := held + 1;
|
|
for _ in 0..this.node_at(held).length as u64 {
|
|
val source := this.resolved(element);
|
|
if this.node_at(source).kind != node_kind::mapping {
|
|
made.clear();
|
|
return move(made);
|
|
}
|
|
made.append(source);
|
|
element = this.after(element);
|
|
}
|
|
return move(made);
|
|
}
|
|
|
|
# Appends to a mapping, which is last, each member of a source it lacks, as a copy of
|
|
# the key and an alias of the value. Gives how many more nodes the aliases stand for
|
|
# than themselves.
|
|
def document::merge_from(&mut this, at: u32, source: u32, weights: &dict<u32, u64>): u64 {
|
|
var extra: u64 = 0;
|
|
var key := source + 1;
|
|
for _ in 0..this.node_at(source).length as u64 {
|
|
key = this.visible_key(key);
|
|
val value_at := this.after(key);
|
|
val next_key := this.after(value_at);
|
|
val named := this.resolved(key);
|
|
val shape := this.node_at(named).kind;
|
|
if shape != node_kind::sequence && shape != node_kind::mapping
|
|
&& this.member(at, this.text_at(named)) is none {
|
|
var copy: node = this.node_at(named);
|
|
copy.form = copy.form | form_key;
|
|
this._nodes.append(copy);
|
|
this._nodes.at(at as u64).length++;
|
|
val aimed := this.resolved(value_at);
|
|
this._nodes.append(node { kind = node_kind::alias, start = this.node_at(value_at).start, length = aimed });
|
|
extra += this.weight(aimed, weights) - 1;
|
|
}
|
|
key = next_key;
|
|
}
|
|
return extra;
|
|
}
|
|
|
|
# How many nodes a node stands for: itself and everything it holds, with aliases
|
|
# within it counted as what they name.
|
|
def document::weight(&this, at: u32, weights: &dict<u32, u64>): u64 {
|
|
if weights.get(&at) is some(known) {
|
|
return known;
|
|
}
|
|
val held := this.node_at(at);
|
|
if held.kind != node_kind::sequence && held.kind != node_kind::mapping {
|
|
return 1;
|
|
}
|
|
var total: u64 = 0;
|
|
for index in at..held.next {
|
|
val inner := this.node_at(index);
|
|
total += inner.kind == node_kind::alias ? this.weight(inner.length, weights) : 1;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
def document::node_at(&this, at: u32): &node {
|
|
return this._nodes.at(at as u64);
|
|
}
|
|
|
|
# The node an alias names, or the node itself.
|
|
def document::resolved(&this, at: u32): u32 {
|
|
val held := this.node_at(at);
|
|
return held.kind == node_kind::alias ? held.length : at;
|
|
}
|
|
|
|
def document::kind_at(&this, at: u32): kind {
|
|
return (this.node_at(this.resolved(at)).kind as u8) as kind;
|
|
}
|
|
|
|
# The index just past a node and everything it holds.
|
|
def document::after(&this, at: u32): u32 {
|
|
val held := this.node_at(at);
|
|
if held.kind == node_kind::sequence || held.kind == node_kind::mapping {
|
|
return held.next;
|
|
}
|
|
return at + 1;
|
|
}
|
|
|
|
# The first member's key from one, past those whose `<<` keys merged other mappings in.
|
|
def document::visible_key(&this, at: u32): u32 {
|
|
var key := at;
|
|
while (this.node_at(key).form & form_merged) != 0 {
|
|
key = this.after(this.after(key));
|
|
}
|
|
return key;
|
|
}
|
|
|
|
# A key's text, following an alias, or empty where it is not a scalar.
|
|
def document::key_text(&this, at: u32): strview {
|
|
val held := this.resolved(at);
|
|
val shape := this.node_at(held).kind;
|
|
if shape == node_kind::sequence || shape == node_kind::mapping {
|
|
return strview::blank();
|
|
}
|
|
return this.text_at(held);
|
|
}
|
|
|
|
# A scalar's text, decoded.
|
|
def document::text_at(&this, at: u32): strview {
|
|
val held := this.node_at(at);
|
|
if (held.form & form_decoded) != 0 {
|
|
return this._decoded.substr(held.next as u64, held.length as u64);
|
|
}
|
|
return this._text.substr(held.next as u64, held.length as u64);
|
|
}
|
|
|
|
# A node's tag, following an alias.
|
|
def document::tag_at(&this, at: u32): maybe<strview> {
|
|
val index := this.node_at(this.resolved(at)).tag;
|
|
if index == 0 {
|
|
return nothing {};
|
|
}
|
|
return this.tag_text(index);
|
|
}
|
|
|
|
def document::tag_text(&this, index: u16): strview {
|
|
val from := this._tag_at.at(2 * (index as u64 - 1));
|
|
val to := this._tag_at.at(2 * (index as u64 - 1) + 1);
|
|
return this._tags.substr(from, to - from);
|
|
}
|
|
|
|
# A number read as an integer exactly, or why it is not a whole one.
|
|
def document::integral_at(&this, at: u32): result<serial::integral, serial::error> {
|
|
val held := this.resolved(at);
|
|
if this.node_at(held).kind != node_kind::number {
|
|
return this.wrong(at);
|
|
}
|
|
|
|
val read := integral_of(this.text_at(held));
|
|
if !read.whole {
|
|
return this.wrong(at);
|
|
}
|
|
return read;
|
|
}
|
|
|
|
def document::wrong(&this, at: u32): serial::error {
|
|
return serial::error::at(serial::error_kind::wrong_kind, this.place(at));
|
|
}
|