m4b / scroll

Scroll - making scrolling through buffers fun since 2016
MIT License
151 stars 36 forks source link

Is it possible to do stateful parsing? #72

Closed thirtythreeforty closed 2 years ago

thirtythreeforty commented 4 years ago

I'm working on parsing media streams for thirtythreeforty/neolink. Currently I'm using Nom for parsing but I'm shopping around because Nom parsers are difficult to read and write in my opinion.

The main kicker about the protocol I'm parsing is that basically every layer is stateful. This sucks for a variety of reasons but it typically means that I have to pass around a mutable context object as the struct is parsed.

Can scroll support this use case? The current Context object feels more like a "configuration" object, in that it's passed by value, and doesn't really seem designed to be modified during parsing. In fact, now that I think about it, it wouldn't necessarily be possible to read different parts in parallel, again because of the need for a single mutable state.

m4b commented 4 years ago

It should be possible to parse really anything, stateful or not.

I’m not sure of your usecase or what you require to be stateful but this should be relatively straightforward. Eg if you need some state that gets updated you could create a struct that contains the state, and has a method something like:

fn parse(&mut self, bytes: &[u8]) -> Something {
  let something = bytes.gread(&mut self.offset).unwrap();
  // update any internal state based on what we just parsed, perhaps using information from the parsed thing itself
  something
}

So you update your state really whenever you want, using whatever information you need. Again not sure of your particular usecase. Perhaps you could show the kind of state you need to update (and based on what)?

For more complicated examples I’d recommend reading the documentation on custom TryFromCtx implementations or take a look at goblin PE parser which has various stateful aspects, along with custom (dynamically constructed) contexts changing the parsing behavior.

You should be able to do whatever you want :) but id say scroll is useful for quick and easy parsing of structs or simple pods out of byte sequences. Of course you can do whatever with it, and I’m sure people have !

In my experience most complicated things can be solved by deriving Pread on a pod that describes the (packed) data structure in the byte sequence, or if not whenever it doesn’t, custom TryFromCtx will solve the rest. :)

m4b commented 2 years ago

closing; please reopen if you have other questions