2011/10/13 Littlefield, Tyler <tyler@tysdomain.com>
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?
See: http://boost-spirit.com/home/articles/doc-addendum/faq/#default
3) Where is menuitem returned?
The synthesized attribute you pass in (if any).
Is there a way I can push it to a queue since I'll have more?
By either auto attribute propagation or semantic action. I suggest the former if possible. My actual parser will look something like:
pstart >> item >> *(delem >> item) >> pend;
FYI, "item >> *(delem >> item)" can be replaced with "item % delem". 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?
const std::string &data -> char const* data std::string::const_**iterator -> char const* data.begin() -> data data.end() -> data + length ...and so on 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; }