Search code examples
c++boost-spiritboost-spirit-qi

Getting number of time a rule is matched as unsigned int


So I have this, rather ugly rule in my boost::spirit parser;

qi::rule<Iterator, bool()> empty_brackets;
...
empty_brackets = (qi::token(LEFT_BRACKET) >> qi::token(RIGHT_BRACKET))
                     [ qi::_val = true ]
               ;

The rule is used as;

array_typeexp = (primitive_typeexp >> +(empty_brackets))

So what I really want, for the second argument (qi::_2), isn't a std::vector<bool>, but rather an unsigned int, describing the number of times 'empty_brackets' was matched.

How would I go about achieving this?


Solution

  • I'm really unsure what you could need this for, but, hey :/

    I'd suggest

    empty_brackets    = lit('(') >> lit(')');
    n_empty_brackets  = eps [ _val = 0 ] >> +empty_brackets [ ++_val ];
    
    array_typeexp     = primitive_typeexp >> n_empty_brackets;
    

    For simplicity I have made primitive_typeexp a "no-op", and therefore the outermost rules return just the unsigned value in this simple setup:

    qi::rule<It, unsigned(), Skipper> array_typeexp, n_empty_brackets;
    qi::rule<It, Skipper> empty_brackets, primitive_typeexp;
    

    See a complete sample Live on Coliru

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    
    namespace qi    = boost::spirit::qi;
    
    template <typename It, typename Skipper = qi::space_type>
        struct parser : qi::grammar<It, unsigned(), Skipper>
    {
        parser() : parser::base_type(array_typeexp)
        {
            using namespace qi;
    
            empty_brackets    = lit('(') >> lit(')');
            n_empty_brackets  = eps [ _val = 0 ] >> +empty_brackets [ ++_val ];
    
            array_typeexp     = primitive_typeexp >> n_empty_brackets;
    
            // TODO
            primitive_typeexp = eps;
    
            BOOST_SPIRIT_DEBUG_NODES((array_typeexp)(n_empty_brackets)(empty_brackets)(primitive_typeexp));
        }
    
      private:
        qi::rule<It, unsigned(), Skipper> array_typeexp, n_empty_brackets;
        qi::rule<It, Skipper> empty_brackets, primitive_typeexp;
    };
    
    bool doParse(const std::string& input)
    {
        typedef std::string::const_iterator It;
        auto f(begin(input)), l(end(input));
    
        parser<It, qi::space_type> p;
        unsigned data;
    
        try
        {
            bool ok = qi::phrase_parse(f,l,p,qi::space,data);
            if (ok)   
            {
                std::cout << "parse success\n";
                std::cout << "data: " << data << "\n";
            }
            else      std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
    
            if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
            return ok;
        } catch(const qi::expectation_failure<It>& e)
        {
            std::string frag(e.first, e.last);
            std::cerr << e.what() << "'" << frag << "'\n";
        }
    
        return false;
    }
    
    int main()
    {
        doParse("() () (     ) ()");
        doParse("()");
    }