google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
68 stars 21 forks source link

Using overlapping fields in a `struct` to achieve C `union`-like functionality is not documented well enough #101

Closed fsareshwala closed 6 months ago

fsareshwala commented 6 months ago

I'm attempting to define a simple union. At the moment, there aren't any examples of how to define a union in the Emboss repository. I've come up with my best guess as something like:

union Foo:
  0 [+1] UInt:8 a
  0 [+2] UInt:16 b

In any case, attempting to run this through embossc yields the following results:

17:27:54 INF hci_vendor.emb:260:1: error: Syntax error
17:27:54 INF hci_vendor.emb:260:1: note: Found 'union' (SnakeWord), expected "external", "enum", $, "bits", "struct".

It would appear that defining a union in Emboss is currently not possible. We should probably add the ability to do so.

reventlov commented 6 months ago

Use:

struct Foo:
  0 [+1] UInt:8 a
  0 [+2] UInt:16 b

There is no union keyword because you can already specify overlapping fields in any struct (or bits).

This feature is mentioned in the User Guide (though only briefly), but not, as far as I can see, in the Language Reference. It would also probably be worth having a FAQ page or a "Recipe Book", and answering this question there.

fsareshwala commented 6 months ago

That makes sense. I thought about this approach after making this pull request but I wasn't sure if this is the way it's supposed to be done. Good to know that it is.

I put in a pull request (https://github.com/google/emboss/pull/102) to address the documentation issue for now.