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
writercreates a writing session over a built language (BuiltLang, available asFlmRegistry::lang). The language supplies the writing environment (WriteEnvironment::for_lang), which records which specification carries which element.readerandreader_withcreate a reading session over a built language. Reading needs the reading environment: the language’s specification providers (known_providers), its structural elements, anElementResolverfor every other element name found in the data (anElementTablewhen no registry is at hand), and thePayloadReadersthat rebuild the payloads of presentation nodes.
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§
- Element
Table - An
ElementResolverthat looks element values up by name in a table the caller fills: what a test, or a program without a registry, passes toreader. - Fragment
Index - A position in the fragments table, the serialized reference to a
Fragment(theIndextype ofFragmentSerdeDriver). It belongs to the session that assigned it; see the module documentation. - Fragment
Serde Driver - 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, orNonewhen the session has none (a session not created bywriterorreader). - 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
_builtinpackage included) and every provider the language built from the features’ declarations (BuiltLang::providers). - read_
fragment - Reads the fragment at
positionof the fragments table ofsession, and returns theArcthe session holds: the same one for every call with that position. - read_
tree - Reads the tree at
positionof the trees table ofsession, and returns the sharedArcthe session holds. - reader
- Creates a reading session over the built language
lang, in whichelementsresolves 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
fragmentinto the fragments table ofsessionand returns its position, which belongs tosession. Writing the sameArcagain returns the existing position. - write_
tree - Writes
treeinto the trees table ofsessionand returns its position, which belongs tosession. - writer
- Creates a writing session over the built language
lang.