I'm writing a PLY file parser and serialisation library as part of a research project in point clouds compression. The PLY file specifies the data format in a header before a payload section with all the data. The data is described by means of elements containing properties. For example:
element vertex 12
property float x
property float y
property float z
informs that the payload will have a list with 12 elements called "vertex" with three float properties called x, y and z all of which are floating point numbers. For an arbitrary file, I don't know in advance which elements it contains nor the name, type or the amount of properties each one has. I solve this by parsing the header to create a list of elements
data Element = Element Name Qty [Property]
data Property = Property PropType Name
which tells me the overall structure of the payload with some functions to search if a given element is present and locate/parse the data in the payload section.
I have a working version, but I'm thinking about a new possibility (just for fun right now). What if I wanted to build a data structure that represents the data in the type level? Something like
data Vertex = Vertex { elX :: Float
, elY :: Float
, elY :: Float }
but with the detail I can only discover which fields (properties) my datatype (or types in case of more than one kind of element being described in the file) would have in execution time after parsing the file header so I can later build a function parseVertex :: Parser [Vertex] which then takes the payload data and parsers it?
May someone give me some advice on what to start looking for solving such a problem?