google / emboss

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

Introduce keyword like $previous to clearly indicate overlapping fields #138

Open studgeek opened 5 months ago

studgeek commented 5 months ago

It could be helpful to introduce a keyword like $previous to indicate the offset of the previous field.

Currently in .emb if we want to have multiple overlapping fields the .emb has to use variations like the following to indicate that a field is offset at the same place as the previous field:

  let hdr_size = EventHeader.$size_in_bytes
  0       [+hdr_size]  EventHeader  header
  $next   [+1]         UInt         num_hci_command_packets
  $next   [+2]         OpCode       command_opcode_enum
  $next-2 [+2]         OpCodeBits   command_opcode_bits
  0          [+hdr_size]  EventHeader  header
  hdr_size   [+1]         UInt         num_hci_command_packets
  hdr_size+1 [+2]         OpCode       command_opcode_enum
  hdr_size+1 [+2]         OpCodeBits   command_opcode_bits

Introducing a $previous keyword would make these overlaps more obvious and reduce risk of a math error by the developer:

  let hdr_size = EventHeader.$size_in_bytes
  0       [+hdr_size]  EventHeader  header
  $next   [+1]         UInt         num_hci_command_packets
  $next   [+2]         OpCode       command_opcode_enum
  $previous [+2]         OpCodeBits   command_opcode_bits

Note that #99 would provide an alternative like the following also, but it feels like $previous would be easier to implement and clearer in these overlapping case.

  let hdr_size = EventHeader.$size_in_bytes
  0       [+hdr_size]  EventHeader  header
  $next   [+1]         UInt         num_hci_command_packets
  $next   [+2]         OpCode       command_opcode_enum
  $offset_of(command_opcode_enum) [+2]         OpCodeBits   command_opcode_bits
studgeek commented 5 months ago

As an added tweak, could use $prev to keep same column width as $next.

studgeek commented 5 months ago

Implementation would be similar to #36, perhaps with some refactoring to handle commonalities between $next and $previous.

jasongraffius commented 2 months ago

For the specific use-case of a bits view of an existing value, would something like #78 work, if that were to be resolved?

jasongraffius commented 2 months ago

As an added tweak, could use $prev to keep same column width as $next.

FWIW, if we do implement something like this then the formatter should handle this just fine, inserting spaces after $next to match columns with $previous