yeslogic / fathom

🚧 (Alpha stage software) A declarative data definition language for formally specifying binary data formats. 🚧

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sugar for guarded fields in record formats

brendanzab opened this issue · comments

A common pattern in the OpenType specification and binary formats in general is to write something like:

let table_directory = fun (file_start : Pos) => {
    sfnt_version <- { version <- u32be | version == 0x00010000 || version == "OTTO" },
    ⋮
};

It would be nice to alter the grammar of record formats to allow us write something like this:

let table_directory = fun (file_start : Pos) => {
    sfnt_version <- u32be when 
        sfnt_version == 0x00010000 || sfnt_version == "OTTO",
    ⋮
};

These guarded fields would be elaborated to conditional formats, so no changes would need to be made in the core language or the binary parser semantics.

The when keyword was chosen rather arbitrarily. if would probably be nicer, but I'm not sure we’d be able to fit it into the grammar that easily. I could see some confusion arising if somebody thought that when meant that the field is optionally present.

As an aside, this would still be a way off Mike’s dream of being able to write something like:

let table_directory = fun (file_start : Pos) => {
    sfnt_version <- u32be == 0x00010000 || "OTTO",
    ⋮
};