sealmove / binarylang

Binary parser/encoder DSL
MIT License
59 stars 0 forks source link

Support automatic length inference #8

Closed ajusa closed 3 years ago

ajusa commented 3 years ago

From talking with sealmove on Discord. It would be nice to have a way to automatically bind/set an integer field based on the length of other fields. Many protocols send length and data together, so that the reader knows how many bytes to read before stopping/moving to the next field. For example with the PascalString example, I should only need to pass in the actual string itself, and have some way in the DSL to show that I want len to be the same length as the str I passed in.

Ideally this would also be able to work with more complex examples.

sealmove commented 3 years ago

This could be implemented as a property which adds the passed node to the setter (_ refers to the element being assigned to the field).

createParser(pascalString):
  u8: len
  s {@hook: len = _.len}: str(len)
sealmove commented 3 years ago

Implemented with v0.2.0.