Search code examples
c++parsingboostboost-spirit-qi

Boost.Spirit - How to use repeat to parse into a struct?


I'm trying to make a little program to parse the cpu usage info from /proc/stat using Boost.Spirit. It is mostly working, but I can't get my grammar to compile when using repeat. What am I missing?

The whole code:

#include <vector>

#include "boost/fusion/include/adapt_struct.hpp"
#define BOOST_SPIRIT_DEBUG
#include "boost/spirit/include/qi.hpp"
#include "boost/iostreams/device/mapped_file.hpp"

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct Cpu
{
   unsigned int user;
   unsigned int nice;
   unsigned int system;
   unsigned int idle;
   unsigned int iowait;
   unsigned int irq;
   unsigned int softirq;
   unsigned int steal;
   unsigned int guest;
   unsigned int guest_nice;
};

BOOST_FUSION_ADAPT_STRUCT(
   Cpu,
   (unsigned int, user)
   (unsigned int, nice)
   (unsigned int, system)
   (unsigned int, idle)
   (unsigned int, iowait)
   (unsigned int, irq)
   (unsigned int, softirq)
   (unsigned int, steal)
   (unsigned int, guest)
   (unsigned int, guest_nice)
)

template< typename Iter, typename Skip = ascii::blank_type >
struct Cpu_parser : qi::grammar< Iter, Cpu(), Skip >
{
   qi::rule< Iter, Cpu(), Skip > start;

   Cpu_parser() : Cpu_parser::base_type(start)
   {
      using namespace qi;

      start = lexeme[lit("cpu") >> omit[-uint_]] >> repeat(10)[uint_];
      //start = lexeme[lit("cpu") >> omit[-uint_]] >> uint_ >> uint_ >> uint_ >> uint_ >> uint_ >> uint_ >> uint_ >> uint_ >> uint_ >> uint_;

      BOOST_SPIRIT_DEBUG_NODE(start);
   }
};

int main(int argc, char** argv)
{
   std::vector< Cpu > cpus;

   {
      std::ifstream ifs("/proc/stat");
      ifs >> std::noskipws;

      Cpu_parser< boost::spirit::istream_iterator > cpu_parser;

      std::cout << phrase_parse(
         boost::spirit::istream_iterator(ifs),
         boost::spirit::istream_iterator(),
         cpu_parser % qi::eol,
         ascii::blank,
         cpus) << std::endl;
   }

   return 0;
}

The commented out line with all of the individual uint_s works fine, but I'd like to know what I'm doing wrong with repeat.

I can also get repeat to work if I replace the Cpu struct with a vector of unsigned ints.


Solution

  • You can't. Easily. That's because repeat()[] synthesizes a container attribute. Your struct is a fusion sequence, not a container.

    You /can/ fake it by

    1. not using FUSION_ADAPT_STRUCT
    2. defining the customization points (e.g. is_container<Cpu>. There's a sample that describes how to do things in the docs here: http://www.boost.org/doc/libs/1_60_0/libs/spirit/doc/html/spirit/advanced/customize/iterate/container_iterator.html#spirit.advanced.customize.iterate.container_iterator.example. There's also an answer that describes how to make it work with a std::array)

    However, there's good news:


    “GOOD NEWS”

    For fusion sequences, the qi::auto_ parser knows what to do!

    This removes 80% of the fat:

    Live On Coliru

    #include "boost/fusion/include/adapt_struct.hpp"
    #include "boost/spirit/include/qi.hpp"
    #include <fstream>
    
    struct Cpu {
       unsigned user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(Cpu, user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice)
    
    int main() {
    
        std::vector<Cpu> cpus;
        bool ok = [&cpus] {
            using It = boost::spirit::istream_iterator;
            using namespace boost::spirit::qi;
    
            std::ifstream ifs("/proc/stat");
    
            return parse(
                    It(ifs >> std::noskipws), {},
                    ("cpu" >> -omit[uint_] >> skip(blank)[auto_]) % eol,
                    cpus);
        }();
    
        std::cout << std::boolalpha << ok << std::endl;
    }
    

    NOTE

    • The output is true. Just not on Coliru (/proc/cpu is not accessible there).
    • The lambda trick is there to make the using namespace scoped, while being able to return the value for ok.