Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::optional to bool, inside boost::spirit::qi grammar

In my boost::spirit grammar I have the following snippet;

implicit_method_declaration = (-(qi::token(ABSTRACT)) >> ...)

The type of -(qi::token(ABSTRACT) is boost::optional<boost::iterator_range<std::string::iterator>> however I'm only using this construct to check if the abstract keyword, is actually present, that is, I'd rather have -(qi::token(ABSTRACT) have the type bool with the value boost::optional<...> operator bool() const.

How would I go about achieving this?

like image 711
Skeen Avatar asked Sep 09 '13 19:09

Skeen


1 Answers

I think you're looking for qi::matches[]:

implicit_method_declaration = 
     qi::matches[qi::token(ABSTRACT)] >> ...;

An alternative would be to use qi::attr() with alternatives:

implicit_method_declaration = 
       (
           qi::token(ABSTRACT) >> qi::attr(true) 
         | qi::attr(false)
       ) >> ...;

A quick demo again: http://coliru.stacked-crooked.com/a/ed8bbad53e8c1943

#include <boost/spirit/include/qi.hpp>

namespace qi    = boost::spirit::qi;

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, bool(), Skipper>
{
    parser() : parser::base_type(implicit_method_declaration)
    {
        using namespace qi;

        implicit_method_declaration = matches["abstract"];

        BOOST_SPIRIT_DEBUG_NODES((implicit_method_declaration));
    }

  private:
    qi::rule<It, bool(), Skipper> implicit_method_declaration;
};

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;
    bool 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("abstract");
    doParse("static final");
}

Output

parse success
data: 1
parse success
data: 0
trailing unparsed: 'static final' 
like image 127
sehe Avatar answered Sep 23 '22 09:09

sehe