|
Boost Users : |
Subject: [Boost-users] help with boost::spirit:parsing out a list of menuitems
From: Littlefield, Tyler (tyler_at_[hidden])
Date: 2011-10-12 13:13:55
Hello:
I have a client I'm writing, where the server sends data to the client
to display as a menu in one instance. I wanted to use boost::spirit to
parse this out. So, here's the code.
I have three questions:
1) What can I do better/differently?
2) When I parse out my menuitem, if there's no flag, can it be set to 0
by default somehow?
3) Where is menuitem returned? Is there a way I can push it to a queue
since I'll have more? My actual parser will look something like:
pstart >> item >> *(delem >> item) >> pend;
Here is the code I have currently:
struct menuitem
{
std::string name;
int flag;
int choice;
};
BOOST_FUSION_ADAPT_STRUCT(
menuitem,
(std::string, name)
(int, flag)
(int, choice))
namespace ascii = boost::spirit::ascii;
namespace qi = boost::spirit::qi;
//I'd like to just take a char* here, since that's my payload. copying
the char* to a std::string will take extra time, is there a way to do that?
BOOL Menu::Parse(const std::string &data, int length)
{
std::string::const_iterator it, itEnd;
it = data.begin();
itEnd = data.end();
//we construct a few parsers to make things easier.
qi::rule<std::string::const_iterator, char, ascii::space_type>
pstart = qi::char_('[');
qi::rule<std::string::const_iterator, char, ascii::space_type> pend
= qi::char_(']');
qi::rule<std::string::const_iterator, char, ascii::space_type>delem
= qi::char_('|');
qi::rule<std::string::const_iterator, menuitem(),
ascii::space_type>item = qi::char_("a-zA-Z0-9 -.?!$@") >> qi::char_('(')
>> -qi::int_ >> qi::char_(':') >> qi::int_ >> qi::char_(')');
if (!qi::phrase_parse(it, itEnd, pstart >> item >> pend, ascii::space))
{
return false;
}
return true;
}
-- Take care, Ty Web: http://tds-solutions.net The Aspen project: a light-weight barebones mud engine http://code.google.com/p/aspenmud Sent from my toaster.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net