ELTN in C (Work in Progress)

Frank Mitchell

Posted: 2023-04-12
Last Modified: 2023-04-13
Word Count: 314
Tags: c-programming lua programming python ruby

Table of Contents

This project will create a C library to parse and emit ELTN text. It will then provide wrappers for various languages including Lua, Python, and Ruby.

Design

Reading

TODO

typedef const unsigned char* (*Eltn_Reader)(void* rdata, size_t *sptr);

TODO

Parsing

The core parser essentially ports ELTNPP to C:

FILE* fp;
Eltn_Parser* epp;
/* try { */
Eltn_Parser_new(&epp, File_Reader, fp);
while (Eltn_Parser_has_next(epp)) {
    Eltn_Parser_next(epp);
    switch (Eltn_Parser_event(epp)) {
        /* etc. ... */
    }
}
/* finally { */
fclose(fp);
Eltn_Parser_del(*epp);
/* } */

Eltn_Parser_event will signal a read error in addition to parse errors, but the API is otherwise remarkably similar.

For those who want something “simpler”, there’s Eltn_read.

Tree

Instead of managing a tree of (reference-counted?) C “objects” directly, Eltn_read returns a tree walker.

void Eltn_read(Eltn_Walker* *tiptr, Eltn_Reader r, void* rdata);

One walks the tree with the following functions:

bool Eltn_Walker_next(Eltn_Walker* ti);
bool Eltn_Walker_prev(Eltn_Walker* ti);
bool Eltn_Walker_down(Eltn_Walker* ti);
bool Eltn_Walker_up(Eltn_Walker* ti);

Each returns whether it could successfully move the cursor. Failure means that there’s no next entry, previous entry, child table, or parent table at the current position.

– [[ TODO: some kind of graphic showing next, prev, down, and up on a tree of nested tables:

To inspect the keys and values under the iterator walker, use:

Eltn_Type    Eltn_Walker_key_type(Eltn_Walker* ti);
Eltn_String* Eltn_Walker_key_string(Eltn_Walker* ti);
Eltn_Number  Eltn_Walker_key_number(Eltn_Walker* ti);
bool         Eltn_Walker_key_boolean(Eltn_Walker* ti);
Eltn_Type    Eltn_Walker_value_type(Eltn_Walker* ti);
Eltn_String* Eltn_Walker_value_string(Eltn_Walker* ti);
Eltn_Number  Eltn_Walker_value_number(Eltn_Walker* ti);
bool         Eltn_Walker_value_boolean(Eltn_Walker* ti);

To inspect a table value, use Eltn_Walker_down.

API

TODO

I/O

TODO

Parser

TODO

Tree

TODO

Wrappers

Wrappers will use native I/O to construct a tree of native associative arrays.

Lua

The parser will create a tree of tables.

TODO

Python

The parser will create a tree of dictionaries.

TODO

Ruby

The parser will create a tree of Hashes.

TODO