new to boost, I actually need boost spirit to write a simple parser to fill some data structure.
Here are roughly what they look like:
struct Task
{
const string dataname;
const Level level;
const string aggregator;
const set<string> groupby;
void operator();
};
struct Schedule
{
map<Level, ComputeTask> tasks;
// I have left just to make it seems that
// the struct wrapping over the map is not
// useless (this is not the full code)
void operator()(const InstancePtr &node);
};
Regarding Task, I don't know how I could use BOOST_FUSION_ADAPT_STRUCT, as mentioned in the employee example, or a variant, to make it work with enum and STL container fields.
Similar question for Schedule, but this time I am also using a user type (already registered to fusion maybe, is it recursive?).
I am designing the file format, the struct definitions and file formats may change so I prefer using boost instead of hand-crafted but hard to maintain code. I also do this for a learning purpose.
Here what the file could look like:
level: level operation name on(data1, data2, data3)
level: level operation name on()
level: level operation name on(data1, data2)
A line of is an entry of the map in Schedule, preceding the : is the key and then the rest of it defines the Task.
Where level are replaced by some level keywords corresponding to the enum Level, similar case for operation, name is one of the allowed name (in a set of keywords), on() is a keyword and inside the parenthesis are zero or more strings provided by the user that should fill the set<string> groupby field in a Task.
I want it to be readable and I could even add english keywords which does not add anything else than readability, that is another reason to use some parsing library instead of handcrafted code.
Feel free to ask for more details if you think my question is not clear enough..
Thank you.
So, making some assumptions as your examples don't make the meaning very clear. But here goes:
Going with a random enum:
Adapting:
Note that I subtly put the adapted fields in the grammar order. That helps a lot down the road.
The simplest grammar that comes to mind:
I changed the input to have unique keys for
Levelin the schedule, otherwise only one entry would actually result.Prints
And, additonally with
BOOST_SPIRIT_DEBUGdefined:Full Listing
Live On Coliru