parsingdata / metal

A Java library for parsing binary data formats, using declarative descriptions.
Apache License 2.0
18 stars 9 forks source link

Extend Until to exclude terminator from ParseGraph #360

Closed mvanaken closed 1 year ago

mvanaken commented 1 year ago

There are three scenarios where you would like to use Until:

  1. when there is a separate terminator between structure A and B (A|T|B)
  2. when you know how the structure A ends (AT|B)
  3. when you know how the next structure B starts (A|TB)

The current implementation of Until parses two structures: a def and the terminator. The current implementation can handle 1. correctly, but for 2 and 3 requires some hacky solutions with some drawbacks. See examples solutions below.

  1. until("A", T)
  2. until("A", post(EMPTY, endsWith(T)))
  3. until("A", sub(T, CURRENT_OFFSET))

Especially for scenario 3, the "T" part is parsed twice and is also available twice in the parseGraph. This can be resolved by allowing two implementations of Until where you can specify if the terminator should be part of the parseGraph.