2

I use vc14 and boost version is 1.60.

#include <boost/config/warning_disable.hpp>
#include <boost\spirit\home\qi.hpp>
#include <boost\variant.hpp>
#include <boost\spirit\include\qi.hpp>
#include <boost\spirit\include\phoenix_core.hpp>
#include <boost\spirit\include\phoenix_operator.hpp>
#include <boost\spirit\include\phoenix_object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost\spirit\include\phoenix_fusion.hpp>
#include <boost\foreach.hpp>
#include <string>
#include <list>
#include <boost\bind.hpp>
#define BOOST_SPIRIT_USE_PHOENIX_V3


namespace testParser {
        namespace qi = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;
        namespace sp = boost::spirit;
        namespace fu = boost::fusion;
        typedef sp::context<
            fu::cons<std::list<std::string>,fu::nil >,
            fu::vector0<> 
        >  context;

        class str_menager
        {
            qi::symbols<char> const& vistrings;

        public:
            typedef void result_type;
                        typedef void type;
            str_menager(qi::symbols<char> const& ss) :vistrings(ss) {  }
            void operator ()(std::string const& s, context& con, bool& m_Flag)
            {
                if (vistrings.find(s) != nullptr)
                {
                    using boost::phoenix::at_c;
                    (fu::at_c<0>(con.attributes)).push_back(s);
                }
                else
                {
                    m_Flag = false;
                }
            }
            void decide(std::string const& s, 
//              boost::spirit::qi::unused_type ,
                context& con,
                bool& m_Flag)

            {
                if (vistrings.find(s) != nullptr)
                {
                    using boost::phoenix::at_c;
                    (fu::at_c<0>(con.attributes)).push_back(s);
                }
                else
                {
                    m_Flag = false;
                }
            }
        };



        typedef std::list<std::string> strings;
        template <typename iterator, typename Skiper = ascii::space_type>
        struct stringParser :qi::grammar <iterator, strings(), Skiper>
        {
            stringParser() : stringParser::base_type(stringslist) {
                using boost::phoenix::at_c;
                using boost::spirit::qi::_val;
                using boost::spirit::qi::int_;



                using boost::spirit::qi::omit;
                using boost::spirit::qi::lexeme;
                using boost::spirit::ascii::alpha;
                using boost::spirit::qi::raw;
                using boost::spirit::qi::fail;
                using boost::spirit::_pass;
                using boost::spirit::false_;
                using boost::spirit::qi::on_error;
                using boost::phoenix::val;
                using boost::phoenix::construct;
                using boost::phoenix::ref;
                using boost::spirit::hold;


                str_menager controler(vistrings);
                name = raw[lexeme[*alpha]];
                stringslist =
                    *(
                        omit[("VIS" > name)[ref(vistrings) += qi::_1]
                        ] |
                        //hold[
                            name
                        //] > vistrings
                       [boost::bind(&str_menager::decide, &controler, _1, _2, _3)]
                        )
                    ;
                name.name("some_name");
                stringslist.name("stringslist");
                on_error<fail>
                    (stringslist,
                        std::cout << val("Error! Expectiong ")
                        << qi::_4
                        << val(" here: \"")
                        << construct<std::string>(qi::_3, qi::_2)
                        << val("\"")
                        << std::endl);
            }
            qi::symbols<char> vistrings;
            qi::rule<iterator, strings(), ascii::space_type> stringslist;
            qi::rule<iterator, std::string(), ascii::space_type> name;



        };
    }

void TestSS()
{
    std::string str = " VIS someString someString otherString";
    typedef std::string::const_iterator iterator_type;
    typedef testParser::stringParser<iterator_type> stringParser;

    stringParser strParser;

    iterator_type end = str.end();
    iterator_type iter = str.begin();

    testParser::strings strings;
    int i = 0;
    boost::spirit::ascii::space_type sp;
    bool r = boost::spirit::qi::phrase_parse(iter, end, strParser, sp, strings);

    BOOST_FOREACH(std::string const& p, strings)
    {
        std::cout << p << "\n";
    }

    std::cout << "\n";

}

Compiler error:

    Error   C2664   'void boost::_mfi::mf3<void,testParser::str_menager,const std::string &,testParser::context &,bool &>::operator ()(T *,A1,A2,A3) const': cannot convert argument 3 from 
'boost::spirit::context<boost::fusion::cons<std::list<std::string,std::allocator<_Ty>> ,boost::fusion::nil_>,boost::fusion::vector0<void>>' to
 'boost::spirit::context<boost::fusion::cons<std::list<std::string,std::allocator<_Ty>>,boost::fusion::nil>,boost::fusion::vector0<void>> ' 

I've found a way to get what I want using 'hold' directive but I don't know way code with boost::bind with context does not compile. I'm open on solution with phoenix usage.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
  • FYI, there is never a reason to use `boost::bind` since C++11 – The Vivandiere Jun 17 '16 at 00:56
  • When i've used std::bind(&str_menager::decide, &controler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) I've got : 'error C2338: tuple index out of bounds' and still does not work – Marcin Domarski Jun 17 '16 at 06:36
  • @typetraitor I was going to protest, but apparently you are correct. There are, however, [some things that `std::bind`](http://melpon.org/wandbox/permlink/hROWxOiDpiLhwVjX) allows that you can't do with a lambda before C++14. – llonesmiz Jun 17 '16 at 08:41

1 Answers1

0

Looking at the body of your decide member function you need four things:

  • a qi::symbols<char> vistring which belongs to str_menager (and thus does not need to be a parameter).
  • a std::string s which is the attribute of name.
  • a std::list<std::string> which is the attribute of stringslist.
  • a bool m_Flag that allows you to signal a failure when parsing.

So ideally your decide member function should be simply:

void define(const std::string& s, std::list<std::string>& list, bool& m_Flag)
{
    if (vistrings.find(s) != nullptr)
    {
        list.push_back(s);
    }
    else
    {
        m_Flag = false;
    }
}

and should be called with:

name[adapted_decide(qi::_1,qi::_val,qi::_pass)]

The problem is that since decide is a member function the Phoenix function adaptation macros don't work directly (and defining your own phoenix::function is a lot of boilerplate).

One workaround could be using:

BOOST_PHOENIX_ADAPT_FUNCTION(void,decide_,boost::mem_fn(&str_menager::decide),4);

Note that you need to pass 4 (not 3) in order to take into account the need to also pass an instance of str_menager (and on this subject you need to make controler a member of your grammar, since in your example its lifetime ends when the constructor completes but you try to use it long after that).

Here is the full example: (Running on Coliru)

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <list>
#include <boost/mem_fn.hpp>


namespace testParser {
        namespace qi = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;
        namespace sp = boost::spirit;
        namespace fu = boost::fusion;
        namespace phx = boost::phoenix;


        class str_menager
        {
            qi::symbols<char> const& vistrings;

        public:
            typedef void result_type;
                        typedef void type;
            str_menager(qi::symbols<char> const& ss) :vistrings(ss) {  }


            void decide(std::string const& s, 
                std::list<std::string>& list,
                bool& m_Flag)

            {
                if (vistrings.find(s) != nullptr)
                {
                    list.push_back(s);
                }
                else
                {
                    m_Flag = false;
                }
            }
        };

        BOOST_PHOENIX_ADAPT_FUNCTION(void,decide_,boost::mem_fn(&str_menager::decide),4);//you need to put here number_of_args+1 to take into account the instance parameter



        typedef std::list<std::string> strings;
        template <typename iterator, typename Skiper = ascii::space_type>
        struct stringParser :qi::grammar <iterator, strings(), Skiper>
        {
            stringParser() : stringParser::base_type(stringslist),vistrings(),controler(vistrings) {
                using boost::spirit::qi::omit;
                using boost::spirit::qi::lexeme;
                using boost::spirit::ascii::alpha;
                using boost::spirit::qi::raw;
                using boost::spirit::qi::fail;
                using boost::spirit::qi::on_error;
                using phx::val;
                using phx::ref;
                using phx::construct;


                name = raw[lexeme[*alpha]];
                stringslist =
                    *(
                        omit[("VIS" > name)[ref(vistrings) += qi::_1]
                        ] |
                            name
                       [decide_(&controler, qi::_1, qi::_val, qi::_pass)]
                        )
                    ;
                name.name("some_name");
                stringslist.name("stringslist");
                on_error<fail>
                    (stringslist,
                        std::cout << val("Error! Expectiong ")
                        << qi::_4
                        << val(" here: \"")
                        << construct<std::string>(qi::_3, qi::_2)
                        << val("\"")
                        << std::endl);
            }
            qi::symbols<char> vistrings;
            str_menager controler;
            qi::rule<iterator, strings(), ascii::space_type> stringslist;
            qi::rule<iterator, std::string(), ascii::space_type> name;



        };
    }

void parse(const std::string& str)
{
    typedef std::string::const_iterator iterator_type;
    typedef testParser::stringParser<iterator_type> stringParser;

    stringParser strParser;

    iterator_type end = str.end();
    iterator_type iter = str.begin();

    testParser::strings strings;
    boost::spirit::ascii::space_type sp;
    bool r = boost::spirit::qi::phrase_parse(iter, end, strParser, sp, strings);
    if(r)
    {
        std::cout << "Success.\n";
        BOOST_FOREACH(std::string const& p, strings)
        {
            std::cout << p << "\n";
        }
    }
    else
    {
        std::cout << "Something failed.\n";
    }
    if(iter!=end)
    {
        std::cout << "Unparsed: [" << std::string(iter,end) << "]";
    }

    std::cout << std::endl;
}

int main()
{
    parse(" VIS someString someString otherString");
    parse("VIS foo VIS bar foo bar baz");
    parse("VIS foo bar foo VIS bar baz");
}

P.S.: Unless this is a simplified example and you plan to do a lot more in your str_menager struct, you could simply define decide as a free function, and pass vistring directly as a parameter, i.e:

void decide(const qi::symbols<char>& vistring, const std::string& s, std::list<std::string>& list, bool& m_Flag)
{
    //same as before
}
BOOST_PHOENIX_ADAPT_FUNCTION(void,decide_,decide,4);
...
name[decide_(phx::ref(vistring),qi::_1,qi::_val,qi::_pass)]
...
Community
  • 1
  • 1
llonesmiz
  • 155
  • 2
  • 11
  • 20