Is there a way to pass parameters to custom types?

0 votes
asked Apr 24, 2023 by lambdaknight (160 points)
I would like to make a custom type that represents a fixed-point number with a variable binary scaling. Ideally, I'd like to make a custom type that I can input a parameter in to for that scaling factor. Is such a thing possible?

1 Answer

0 votes
answered Apr 24, 2023 by andreas (3,460 points)
So you want to provide for each usage in the Grammar a parameter to the script of the custom type?
And the parameter doesn't depend on any other element parsed previously?
commented Apr 27, 2023 by lambdaknight (160 points)
That's correct! The specific use-case I have is that a protocol stores several values as fixed-point numbers with various lengths of the fractional part. The code to parse them all is identical except for scaling the value by a variable part. I would like to avoid copy and pasting the same code a ridiculous number of times while changing 1 value. My current hacky solution is to parse the field as a raw number and then follow it with a Script element that just takes the raw value from the previous result and does the parsing and replaces that result.
commented Jul 9 by martin-winter (180 points)
I have a similarly hacky solution in my grammar: I use the description field to specify which structure a script element should map. I wish there were an official solution.