-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to store the current parse index only? #378
Comments
Generally, what you're asking here is some sort of stateful parsing with backtracking, i.e. when you try one branch of parsing and after that (for example, if it fails), you want another branch, retrying from a previously memorized position. This is generally not what you'd want to write into ksy, i.e. you'd want to write declarative data structures, not imperative parsing algorithm. Current way to do "multiple parsing attempts" in ksy would be isolating piece to be parsed as a substream, and then use several different instances to operate on that substream, i.e., something like that: seq:
- id: config_field
type: dummy
size: 0x100
instances:
config_field_as_type1:
io: config_field._io
pos: 0
type: type1
config_field_as_type2:
io: config_field._io
pos: 0
type: type2
types:
dummy: {} # or even something meaningful here
type1:
# ...
type2:
# ... The devil is, of course, in the details. You need to somehow understand the size of this "isolated portion", or at least make sure that The hack that you're using is fine as a workaround, but it's obviously it's stateful and not very declarative as well. |
That is really not what I need, I only need to change individual bytes in some arbitrary The approach I'm currently taking is trying to avoid serializing parsed data again by simply copying the whole KS knows all that already because if it parses things from beginning to end it simply passes the flags I need and I only need to remember their positions and make them available. The problem is that those positions can not be known beforehand, only KS can know them after parsing. Additionally, everything that KS knows about the data is for benefit for all target languages, so I make my parser pretty verbose anyway. Think of it as a GIT tag compared to branches, really only a name for the current parse position to remember, because that has a special meaning to someone. But this is really special, so might not be worth it to think about it any longer. |
I would like to reopen this issue for (a bit of) further discussion and would start with making clear that things are really easier then I might have described: I only need to store some very special parsing index either before or after some I'm only thinking of something like auto-generating a simple value instance providing the stored position for some
The reason why I though again of this is #546. I'm using a type there to store things so that I can use some parse instance in combination with |
@ams-tschoening Does |
Things read like they are what I need, but don't have the time to dig further into this right now anyway. So feel free to close as duplicate, things can always be reopened otherwise in the future. |
I have some
byte[]
containing various different types and don't necessarily know in which order those types are present in the stream. At least some of those types are encrypted and I need to decrypt them by copying thebyte[]
in whole and decrypt the individual encrypted types in place. If a type is encrypted is indicated using flags and because my overall result is the whole record of individual types unencrypted, I need to switch those flags to show unencrypted state.The problem I have is to know the position of the bytes to change in the copied
byte[]
. In former versions I was able to calculate it depending on the available types in the stream, but that doesn't work anymore, as I get more and more types with different order than before etc.Because I encounter the bytes I need during parsing from the front to the end, I thought of simply storing the current parse index in some instance. Yeah, it's not pretty safe, but the best solution I can think of currently. Is something like that supported? I could only think of the following workaround to implement that manually:
config_field_start_idx
is executed always and only stores the current index, without actually parsing anything, so I don't need to reset things. But is there some easier way to achieve this I'm missing?Thanks!
The text was updated successfully, but these errors were encountered: