Parsing an empty member into a fusion adapted struct
Thomas Ellis <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
I am currently working on developing a parse grammar which would allow me to handle the two input lines identified below with a single parse grammar. The first input parses fine. It is when there is nothing for that "field" where the difficulty lies. Does anyone have any words of wisdom on how I might accomplish this?
//***********************************
#define BOOST_SPIRIT_DEBUG
#define FUSION_MAX_VECTOR_SIZE 20
#define SPIRIT_ARGUMENTS_LIMIT 20
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
struct LineStruct {
std::uint16_t year;
std::uint16_t month;
std::uint16_t day;
std::uint16_t hour;
std::uint16_t minute;
std::uint16_t second;
std::string node;
std::string pcc;
std::uint16_t request;
};
BOOST_FUSION_ADAPT_STRUCT( LineStruct,
(std::uint16_t,year)
(std::uint16_t,month)
(std::uint16_t,day)
(std::uint16_t,hour)
(std::uint16_t,minute)
(std::uint16_t,second)
(std::string,node)
(std::string,pcc)
(std::uint16_t,request)
);
template <typename Iterator, typename Skipper >
struct Parser : boost::spirit::qi::grammar<Iterator, LineStruct(), Skipper >
{
Parser() : Parser::base_type(linestruct)
{
linestruct = boost::spirit::qi::ushort_ >> boost::spirit::qi::lit("-") >> boost::spirit::qi::ushort_ >> boost::spirit::qi::lit("-") >> boost::spirit::qi::ushort_ >>
boost::spirit::qi::ushort_ >> boost::spirit::qi::lit(":") >> boost::spirit::qi::ushort_ >> boost::spirit::qi::lit(":") >> boost::spirit::qi::ushort_ >>
-boost::spirit::qi::lexeme[ +boost::spirit::qi::char_("a-zA-Z0-9") ] >>
-boost::spirit::qi::lexeme[ +boost::spirit::qi::char_("a-zA-Z0-9") ] >>
boost::spirit::qi::ushort_;
BOOST_SPIRIT_DEBUG_NODES( (linestruct) )
}
private:
boost::spirit::qi::rule<Iterator, LineStruct(), Skipper> linestruct;
};
int main()
{
try{
std::string input1("2017-04-18 23:59:55 hpchlp200 6ZLA 9");
std::string input2("2017-04-18 23:59:55 hpchlp200 9");
std::string::const_iterator f(input2.begin()), l(input2.end());
Parser<std::string::const_iterator, boost::spirit::qi::space_type> parser;
LineStruct line;
bool ok = boost::spirit::qi::phrase_parse(f, l, parser, boost::spirit::qi::space, line);
if (ok && f==l ) {
std::cout << "Parse success\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
} catch( const boost::spirit::qi::expectation_failure< std::string::const_iterator >& e ) {
std::cerr << "FIXME: expected " << e.what_ << ", got '" << std::string(e.first, e.last) << "'" << std::endl;
return false;
}
}
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general