Skip to main content

Module session

Module session 

Source
Expand description

Serialization sessions for FLM: writing parsed fragments and trees in techy’s serialized form (the wire form) and reading them back into in-memory objects.

The wire form is techy’s lossless serialization of a parse, which crate::lang::serialize implements for FLM. Unlike the one-way projection, it reads back into in-memory objects with their sharing and identity intact. The flm dump --view wire command prints it, the library’s round-trip tests use it, and a program can use it to store parses (for example in a cache) and read them back.

A techy serialization session holds one table per kind of object (sources, parsing states, trees, diagnostics, and so on) and writes each object once. An FLM session holds techy’s standard tables plus one table of this library’s own, flm.fragments (FragmentSerdeDriver), whose entry for a fragment ties together its source, its tree, its diagnostics and its region layout (FragmentLayout).

§Creating a session

Every session is created with the recursion guard of the language (BuiltLang::guard), so that writing or reading a deeply nested tree is refused instead of overflowing the stack. Like every guarded run, writing and reading must therefore execute on a worker thread, a thread that declared its stack size; the functions of this module do not create one.

Once a session is created, write_fragment and write_tree write objects into a writing session, and read_fragment and read_tree read them from a reading session. json::segment_line writes the segment a session emits as one line of JSON.

A techy profile is a string that a writing session records in its stream and that a reading session compares with its own: a stream with a different profile is refused. No session of this module sets one, because only the program can declare which configurations are compatible: a program that caches serialized data calls techy’s set_profile on both its writer and its reader with the same string, such as its registry’s digest, while a debugging dump sets none, so that any stream can be inspected.

§What a round trip preserves

Writing a fragment and reading it back preserves: the tree’s structure; every span, into the same sources, shared; each node’s parsing state, shared as before and rebuilt once per state; the specifications by identity (the instances the language’s packages hold); the elements by identity, through those specifications, otherwise through the element resolver; the structural elements (the reading language’s own); the diagnostics’ identifiers, data and spans; the diagnostics collection’s retention limit and its counts (the number of suppressed diagnostics and the number of errors, so that a fragment reports the same thing about whether it holds errors on both sides); and the fragment’s region layout. The collection is rebuilt from those parts, and counts that contradict each other are refused (DeserializeError::InconsistentDiagnosticCounts) rather than corrected.

A round trip does not preserve node ids and tree tags, which are new on reading, nor the Rust types of diagnostic conditions, which come back as techy’s deserialized condition carrying the same identifier and data.

§Identity and positions

A fragment is an object of its table: writing the same Arc<Fragment> twice yields one entry, because a fragment’s identity is that Arc. Its tree is written by its own Arc as well, so no tree is copied on writing.

Every position these functions return or take (FragmentIndex, techy’s TreeIndex) belongs to the session that assigned it and must never be passed to another session. A position is carried between sessions as its table’s name together with its numeric index, and the receiving session rebuilds it against its own table.

Re-exports§

pub use crate::lang::serialize::AmbiguousElement;
pub use crate::lang::serialize::ElementResolver;
pub use crate::lang::serialize::FALLBACK_GATE_IDENTIFIER;
pub use crate::lang::serialize::PRESENTATION_IDENTIFIER;
pub use crate::lang::serialize::PayloadReader;
pub use crate::lang::serialize::PayloadReaders;
pub use crate::lang::serialize::ReadEnvironment;
pub use crate::lang::serialize::SYNTHESIZED_IDENTIFIER;
pub use crate::lang::serialize::WriteEnvironment;
pub use crate::lang::serialize::register;

Structs§

ElementTable
An ElementResolver that looks element values up by name in a table the caller fills: what a test, or a program without a registry, passes to reader.
FragmentIndex
A position in the fragments table, the serialized reference to a Fragment (the Index type of FragmentSerdeDriver). It belongs to the session that assigned it; see the module documentation.
FragmentSerdeDriver
Writes and reads the entries of the fragments table (FRAGMENTS_TABLE), which holds one kind of object, fragments (FRAGMENT_IDENTIFIER).

Constants§

FRAGMENTS_TABLE
The name of the fragments table that every FLM session holds beside techy’s standard tables (FragmentSerdeDriver).
FRAGMENT_IDENTIFIER
The identifier of a fragment’s entry in the fragments table.

Functions§

fragments_table
Returns the handle of the fragments table of session, or None when the session has none (a session not created by writer or reader).
known_providers
Returns the specification providers (techy’s packages, and FLM’s fallback gate) against which a reading session resolves the serialized references to specifications: every provider of the language’s initial parsing state (techy’s built-in _builtin package included) and every provider the language built from the features’ declarations (BuiltLang::providers).
read_fragment
Reads the fragment at position of the fragments table of session, and returns the Arc the session holds: the same one for every call with that position.
read_tree
Reads the tree at position of the trees table of session, and returns the shared Arc the session holds.
reader
Creates a reading session over the built language lang, in which elements resolves every element name other than the structural elements.
reader_with
Creates a reading session over the built language lang, with payload readers.
write_fragment
Writes fragment into the fragments table of session and returns its position, which belongs to session. Writing the same Arc again returns the existing position.
write_tree
Writes tree into the trees table of session and returns its position, which belongs to session.
writer
Creates a writing session over the built language lang.