Headline

An extension of Contribution:c using Language:Lua for parsing and unparsing.

Characteristics

String handling in Language:C is notoriously bonkers: strings are merely null-terminated arrays of char, which means they do not even know their own length and their memory has to be painstakingly managed manually. So doing any kind of parser work directly in C is a lot more work than it is worth.

Instead, Feature:Parsing and Feature:Unparsing in this contribution makes use of embedded programming Language:Lua, as embedding and interacting with it is trivial. The Lua representation can easily be converted to and from other representations like Language:JSON or Language:XML using existing pure-Lua libraries.

Illustration

This contribution uses Language:Lua to interface with plaintext representations of the data model. Embedding it into Language:C is trivial, since it is a library written in standard C89. Lua's table representation can also easily be converted into a different representation, like Language:JSON.

For Feature:Unparsing, the object graph is traversed using visitors and appropriate calls to Lua functions are made to build a serializable table in it. Take for example unparsing a department:

// Fragment from unparsingVisitor in c101_Unparsing.c
    case C101_DEPARTMENT:
        lua_getglobal (lua, "unparseDepartment");
        luaSwap       (lua);
        lua_pushstring(lua, ((struct c101_Department*) unit)->name);
        if (lua_pcall(lua, 2, 2, 0))
            return unparseError(lua);
        break;

Which calls the corresponding function in Lua:

-- Fragment from unparsing.lua
function unparseDepartment(current, n)
    department = {
        name        = n,
        employees   = {},
        departments = {},
    }
    table.insert(current["departments"], department)
    return current, department
end

For Feature:Parsing, the opposite is done: the Lua table is traversed and appropriate C functions are called. Take for example parsing an employee:

-- Fragment from parsing.lua
function parseEmployee(data, employee)
    ensureType(employee,            "table",  "Employee"        )
    ensureType(employee["name"   ], "string", "Employee name"   )
    ensureType(employee["address"], "string", "Employee address")
    ensureType(employee["salary" ], "number", "Employee salary" )
    c101_parseEmployee(data, pop(employee, "name"),
                             pop(employee, "address"),
                             pop(employee, "salary"))
    warnExcessive(employee, "Employee")
end

Which ensures the integrity of the data and calls the appropriate function in C:

// Fragment from c101_Parsing.c
static int
c101_parseEmployee(lua_State* lua)
{
    struct ParseData* data    = lua_touserdata (lua, 1);
    const char      * name    = lua_tostring   (lua, 2);
    const char      * address = lua_tostring   (lua, 3);
    double            salary  = lua_tonumber   (lua, 4);

    c101_addSubunit(c101_back(&data->stack), c101_newEmployee(name, address, salary));

    return 0;
}

Relationships

This contribution is an extension of Contribution:c.

Architecture

Additionally to Contribution:c's architecture, there is a folder with Lua scripting files (.lua). These files are copied into the build folder when running CMake.

Usage

This contribution uses Technology:CMake for handling its build process and its Technology:CTest for testing. Please see the readme.md on GitHub for details.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.