Spirit 2.1: std::pair as attribute

View: New views
3 Messages — Rating Filter:   Alert me  

Spirit 2.1: std::pair as attribute

by nousias :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello

I'm trying to use std::pair with attribute parsing.

I have
top
    %= qi::int_ >> ',' >> qi::int_
    ;

which works with 'top' being an std::vector<int>, but not
std::pair<int,int>. I'm including my little example here.

What should I do for it to accept the std::pair?

thanks,
Ioannis



#include <iostream>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

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


#ifdef USE_PAIR
        typedef std::pair<int,int> Data;
#else
        typedef std::vector<int> Data;
#endif


template <typename Iterator>
struct Grammar
        : qi::grammar<Iterator, Data(), ascii::space_type>
{
        Grammar()
                : Grammar::base_type(top)
        {
                top
                        %= qi::int_ >> ',' >> qi::int_
                        ;
        }
       
        qi::rule<Iterator, Data(), ascii::space_type> top;
};
       
int main()
{
       
        typedef Grammar<std::string::const_iterator> Grammar;
        typedef Data AST;

        Grammar grammar;
        AST ast;

        std::string script = "10,2";
    std::string::const_iterator iter = script.begin();
    std::string::const_iterator end = script.end();
        return phrase_parse(iter, end, grammar, ascii::space, ast);
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Spirit 2.1: std::pair as attribute

by Hartmut Kaiser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I'm trying to use std::pair with attribute parsing.
>
> I have
> top
>     %= qi::int_ >> ',' >> qi::int_
>     ;
>
> which works with 'top' being an std::vector<int>, but not
> std::pair<int,int>. I'm including my little example here.
>
> What should I do for it to accept the std::pair?

Qi sequences are compatible with two types of attributes:

   a) fusion sequences
   b) STL containers

You already saw how it works for std::vector. In order to make it work with
std::pair as well this data type needs to be 'fusionified'. Fortunately this
already is implemented in Boost.Fusion. You just need to include

    #include <boost/fusion/include/std_pair.hpp>

in order to enable the functionality you need.

Regards Hartmut

-------------------
Meet me at BoostCon
http://boostcon.com




------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Spirit 2.1: std::pair as attribute

by nousias :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hartmut Kaiser wrote:

>> I'm trying to use std::pair with attribute parsing.
>>
>> I have
>> top
>>     %= qi::int_ >> ',' >> qi::int_
>>     ;
>>
>> which works with 'top' being an std::vector<int>, but not
>> std::pair<int,int>. I'm including my little example here.
>>
>> What should I do for it to accept the std::pair?
>>    
>
> Qi sequences are compatible with two types of attributes:
>
>    a) fusion sequences
>    b) STL containers
>
> You already saw how it works for std::vector. In order to make it work with
> std::pair as well this data type needs to be 'fusionified'. Fortunately this
> already is implemented in Boost.Fusion. You just need to include
>
>     #include <boost/fusion/include/std_pair.hpp>
>
> in order to enable the functionality you need.
>  

oh, I see. Thanks!


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general