|
YATL
|
YATL is a format-preserving TOML parser and writer for C. Unlike traditional parsers that build an abstract syntax tree or convert to native data structures, YATL provides a lightweight toolset to navigate a TOML document with a DOM-like view allowing for: in-line editing, round-trip serialization and deserialization without loss of formatting. For read operations, it aims to be zero-allocation. Stack memory is primarily used for write operations and temporary structures.
Internally YATL represents documents as a doubly-linked list of lines. Each line contains a pointer to the next and previous lines, along with the raw text from the source file. The complexity of this is abstracted away from the user via simple function calls.
This design enables:
A Span is a view into the document, internally defined by a start cursor and end cursor. Spans are lightweight and do not copy data. They can represent:
Spans have a type (YATL_SpanType_t) indicating what they represent.
A Cursor points to a specific position within the document (line + offset). Cursors are used for iteration and to define span boundaries.
Functions YATL_doc_create, YATL_cursor_create, YATL_span_create initialize structures to known values but do not allocate memory. They are always safe to use when declaring a cursor, span, or document, they are required to call on any object that will be passed as an input parameter to the public API.
Output parameters are initialized by the API. If you do not initialize, for example, an input cursor, the behavior is defined. The API will fail with an error code.
tl;dr - YATL_Cursor_t cursor = YATL_cursor_create() = good.
Table headers with dotted keys like [server.http] are stored with the literal name "server.http". The library does not create an implicit nested structure for these.
To find such a table:
This preserves the original TOML syntax and avoids ambiguity with inline tables.
For table arrays, for example:
One would find the table array with:
When using YATL_span_keyval_slice() to get a string value span, the span text returned by YATL_span_text() contains the decoded string content without the surrounding quote delimiters.
When spans are unlinked (for editing operations), the original lines are moved to a "boneyard" - they remain allocated but are removed from the document's line list. This allows for:
As an example, for the following TOML document:
The code below:
Would provide a span covering the [database] section:
Would result in a span covering:
Would yield localhost and len = 9.
Inline tables can be navigated the same way as regular tables:
Functions prefixed with _YATL_ or _ are internal and subject to change. Test code may include private headers to access these for testing purposes.
Dylan Taft