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

How do I assign the result of a function object to a local in a boost::spirit semantic action


Im not really sure why the following code gives me the following error in GCC 4.6.3

no match for ‘operator=’ in ‘boost::spirit::_a = boost::phoenix::function::operator()(const A0&) const [with A0 = boost::phoenix::actor >, F = make_line_impl, typename boost::phoenix::as_composite, F, A0>::type = boost::phoenix::composite, boost::fusion::vector, boost::spirit::argument<0>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> >]((* & boost::spirit::_1))’

Is it even possible to assign the result of a lazy function object to a qi placeholder?

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

using std::string;

using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::qi::space_type;
using boost::spirit::qi::skip_flag;
using boost::spirit::unused_type;

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct make_line_impl
{
  int* _context;
  make_line_impl(int* context)
  {
    _context = context;
  }

  template <typename Sig>
  struct result;

  template <typename This, typename Arg>
  struct result<This(Arg const &)>
  {
    typedef int* type;
  };
  template <typename Arg>
  int* operator()(Arg const & content)
  {
    return new int(5);
  }
};


template<typename Iterator>
struct MyGrammar : grammar<Iterator, unused_type, space_type>
{
  rule<Iterator, unused_type, space_type> start;
  rule<Iterator, int*(), space_type> label;
  rule<Iterator, string*(), qi::locals<int*>, space_type> line;

  MyGrammar() : MyGrammar::base_type(start)
  {
    make_line_impl mlei(new int(5));
    phx::function<make_line_impl> make_line(mlei);

    start = *(line);

    line = label[qi::_a = make_line(qi::_1)];
  }
};


int main(int argc, char **argv) {

  string contents;

  qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip);
    return 0;
}

Solution

  • I have fixed some spots in the code to make it compile:

    • I rewrote the nested ::result<>::type logic according to the documentation of BOOST_RESULT_OF.

      Note if you compile in c++11 mode, you're probably better off defining

       #define BOOST_RESULT_OF_USE_DECLTYPE
      

      in which case you don't have to bother with the nested result type template.

    • The operator(...) method needed to be const

    Resulting code:

    #include <string>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/spirit/include/qi.hpp>
    
    using std::string;
    
    using boost::spirit::qi::grammar;
    using boost::spirit::qi::rule;
    using boost::spirit::qi::space_type;
    using boost::spirit::qi::skip_flag;
    using boost::spirit::unused_type;
    
    namespace qi = boost::spirit::qi;
    namespace phx = boost::phoenix;
    
    struct make_line_impl
    {
      int* _context;
      make_line_impl(int* context)
      {
        _context = context;
      }
    
      template <typename Arg> struct result { typedef int* type; };
    
      template <typename Arg>
      int* operator()(Arg const & content) const
      {
        return new int(5);
      }
    };
    
    
    template<typename Iterator>
    struct MyGrammar : grammar<Iterator, unused_type, space_type>
    {
      rule<Iterator, unused_type, space_type> start;
      rule<Iterator, int*(), space_type> label;
      rule<Iterator, string*(), qi::locals<int*>, space_type> line;
    
      MyGrammar() : MyGrammar::base_type(start)
      {
        make_line_impl mlei(new int(5));
        phx::function<make_line_impl> make_line(mlei);
    
        start = *(line);
    
        line = label[qi::_a = make_line(qi::_1)];
      }
    };
    
    
    int main(int argc, char **argv) {
    
      string contents;
    
      qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip);
        return 0;
    }