PMunch / binaryparse

Binary parser for Nim
MIT License
71 stars 6 forks source link

[suggestion] new "loop" section #2

Closed ghost closed 6 years ago

ghost commented 6 years ago

I have a file which consists of a lot of entries, each of them has 4 int32 fields (so file content is like this: i32 i32 i32 i32 i32 i32 i32 i32 - in this example there's two entries), but there's no thing like "size" in the file.

Of course I can easily read this file with streams module, but maybe it would be good for binaryparse to have that functionality. Maybe it can be possible to implement "loop" section or something like that.

PMunch commented 6 years ago

Something like this should work, reads until the end of file:

createParser(fourInts):
  32: first
  32: second
  32: third
  32: fourth

createParser(entries):
  *fourInts: fields[]

You can also read until a magic sequence.

ghost commented 6 years ago

@PMunch oh, thank you a lot!

ghost commented 6 years ago

@PMunch with the code:

import binaryparse, streams

createParser(point):
  32: ts
  32: x
  32: y
  32: colorId

createParser(entries):
  *point: fields[]

I get:

stack trace: (most recent call last)
/Users/dian/.nimble/pkgs/binaryparse-#head/binaryparse.nim(532) createParser
/Users/dian/Projects/Experiments/dd.nim(9, 13) template/generic instantiation from here
/Users/dian/.nimble/pkgs/binaryparse-#head/binaryparse.nim(532, 19) Error: node lacks field: ident
PMunch commented 6 years ago

Hmm strange, maybe the logic for that isn't implemented after all. I won't be able to look at it before Friday at the earliest, but I'll happily accept a PR if you're able to figure it out.

PMunch commented 6 years ago

This should be fixed in https://github.com/PMunch/binaryparse/commit/b1a2dd628cf45275cef286e7b857c9ad00ccc412, care to give it a try?

ghost commented 6 years ago

Thank you very much, it works!