I'm parsing some input that is vaguely structured like C-ish code. Like this:
Name0
{
Name1
{
//A COMMENT!!
Param0 *= 2
Param2 = "lol"
}
}
Part of that is comments, which I want to totally ignore (and it's not working). I consider two things to be a node
, the named scopes (category
rule) like Name0 {}
and the values (param
rule) like Param0 *= 2
... then there is comment
. I've tried setting things up like this:
typedef boost::variant<boost::recursive_wrapper<Category>, Param> Node;
qi::rule<Iterator, Node(), ascii::space_type> node;
So the node
rule puts either a Category
or a Param
in a variant
. Here are the other rules (I've omitted some rules that don't really matter for this):
qi::rule<Iterator> comment; //comment has no return type
qi::rule<Iterator, Category(), ascii::space_type> category;
qi::rule<Iterator, Param(), ascii::space_type> param;
And their actual code:
comment = "//" >> *(char_ - eol);
param %=
tagstring
>> operators
>> value;
category %=
tagstring
>> '{'
>> *node
> '}';
node %= comment | category | param;
comment
is setup to use =
instead of %=
, and it has no return type. However, comments end up creating null Category
s in my output Node
s wherever they show up. I've tried moving comment
out of the node
rule and into category
like this:
category %=
tagstring
>> '{'
>> *(comment | node)
> '}';
And various other things, but those null entries keep popping up. I had to make comment
output a string and put std::string
in my Node
variant
just to sorta catch them, but that messes up my ability to stick in commenting in other parts of my rules (unless I actually grab the string in every location).
How can I completely ignore the comment
and have it not show up in any output in any way?
edit: You'd think omit
would do it, but didn't seem to change anything...
edit 2: Referencing this SO answer, I have a shaky solution in this:
node %= category | param;
category %=
tagstring
>> '{'
>> *comment >> *(node >> *comment)
> '}';
However, I want to try to stick comments into all sorts of places (between tagstring
and {
, in my unshown root
rule between root category
s, etc). Is there a simpler method than this? I was hoping it could be done with a simple >> commentwrapper
plugged into wherever I wanted...