Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BOOST_SPIRIT_DEBUG 1
- // ============================================================================
- #include <boost/spirit/include/qi.hpp>
- #include <boost/fusion/include/adapt_struct.hpp>
- // ============================================================================
- namespace qi = boost::spirit::qi;
- // ============================================================================
- struct log_entry_t
- {
- std::string element_0;
- std::string element_1;
- int32_t element_2;
- int32_t element_3;
- int32_t element_4;
- int32_t element_5;
- std::string element_6;
- std::string element_7;
- std::string element_8;
- };
- // ============================================================================
- BOOST_FUSION_ADAPT_STRUCT(
- log_entry_t
- , (std::string, element_0)
- , (std::string, element_1)
- , (int32_t, element_2)
- , (int32_t, element_3)
- , (int32_t, element_4)
- , (int32_t, element_5)
- , (std::string, element_6)
- , (std::string, element_7)
- , (std::string, element_8)
- )
- // ============================================================================
- // Sample log entry:
- // ^+ line-17532.dyn.kponet.fi 2 7 377 1 +1503us[+9103us] +/- 55ms
- //
- template <typename Iterator>
- struct log_line_parser
- : qi::grammar<Iterator, log_entry_t(), boost::spirit::ascii::space_type>
- {
- typedef boost::spirit::ascii::space_type skipper_t;
- log_line_parser()
- : log_line_parser::base_type(log_line)
- {
- element_0 %= qi::string("^+");
- element_1 %= +qi::char_("-.a-zA-Z0-9");
- element_2 %= qi::int_;
- element_3 %= qi::int_;
- element_4 %= qi::int_;
- element_5 %= qi::int_;
- element_6 %= qi::raw[qi::char_('+') >> qi::int_ >> time_unit];
- element_7 %= qi::raw[qi::char_('+') >> qi::int_ >> time_unit];
- element_8 %= qi::raw[qi::int_ >> time_unit];
- time_unit %= -qi::char_("nmu") >> qi::char_('s');
- log_line
- %= element_0
- >> element_1
- >> element_2
- >> element_3
- >> element_4
- >> element_5
- >> element_6
- >> qi::lit('[') >> element_7 >> qi::lit(']')
- >> qi::lit("+/-")
- >> element_8
- ;
- init_debug();
- }
- void init_debug()
- {
- BOOST_SPIRIT_DEBUG_NODE(element_0);
- BOOST_SPIRIT_DEBUG_NODE(element_1);
- BOOST_SPIRIT_DEBUG_NODE(element_2);
- BOOST_SPIRIT_DEBUG_NODE(element_3);
- BOOST_SPIRIT_DEBUG_NODE(element_4);
- BOOST_SPIRIT_DEBUG_NODE(element_5);
- BOOST_SPIRIT_DEBUG_NODE(element_6);
- BOOST_SPIRIT_DEBUG_NODE(element_7);
- BOOST_SPIRIT_DEBUG_NODE(element_8);
- BOOST_SPIRIT_DEBUG_NODE(time_unit);
- BOOST_SPIRIT_DEBUG_NODE(log_line);
- }
- private:
- qi::rule<Iterator, std::string()> element_0;
- qi::rule<Iterator, std::string()> element_1;
- qi::rule<Iterator, int32_t()> element_2;
- qi::rule<Iterator, int32_t()> element_3;
- qi::rule<Iterator, int32_t()> element_4;
- qi::rule<Iterator, int32_t()> element_5;
- qi::rule<Iterator, std::string()> element_6;
- qi::rule<Iterator, std::string()> element_7;
- qi::rule<Iterator, std::string()> element_8;
- qi::rule<Iterator, std::string()> time_unit;
- qi::rule<Iterator, log_entry_t(), skipper_t> log_line;
- };
- // ============================================================================
- int main()
- {
- std::string log("^+ line-17532.dyn.kponet.fi 2 7 377 1 +1503us[+9103us] +/- 55ms");
- std::string::const_iterator iter(log.begin());
- std::string::const_iterator end(log.end());
- log_line_parser<std::string::const_iterator> g;
- log_entry_t entry;
- using boost::spirit::ascii::space;
- bool r(qi::phrase_parse(iter, end, g, space, entry));
- std::cout << "-------------------------\n";
- if (r && (iter == end)) {
- std::cout << "Parsing succeeded\n";
- std::cout << entry.element_0 << "\n"
- << entry.element_1 << "\n"
- << entry.element_2 << "\n"
- << entry.element_3 << "\n"
- << entry.element_4 << "\n"
- << entry.element_5 << "\n"
- << entry.element_6 << "\n"
- << entry.element_7 << "\n"
- << entry.element_8 << "\n";
- return 0;
- } else {
- std::string::const_iterator some = iter + 30;
- std::string context(iter, (some > end) ? end : some);
- std::cout << "Parsing failed\n";
- std::cout << "stopped at: \": " << context << "...\"\n";
- return 1;
- }
- }
- // ============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement