help with usage about boost::spirit and boost::variant

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

help with usage about boost::spirit and boost::variant

fractal
hi all:
    boost::spirit is a great library, i use it for our company project, but i encountered a compile problem today,  i summary the problem to the
following sample code:

#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>

namespace q = boost::spirit::qi;

typedef boost::variant<std::string, int> variant_t;

struct var
{
variant_t _var;
};

BOOST_FUSION_ADAPT_STRUCT(var, 
 (variant_t, _var));

int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;
rule_t _rule  = q::int_;
rule_t _rule2 = '(' >> _rule; //this compile error.
return 0;
}

    that is, _rule2 will complain that boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node::initialize cannot convert ... 

    any suggestion is appreciated!!





------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

grubino
I think _rule is simply an alias for q::int in this code.  Maybe try changing 

rule_t _rule = q::int_;

to

rule_t _rule = q::int_.alias();

I think I have seen this before, and this was the solution, aside from the obvious:

rule_t _rule2 = '(' >> q::int_;


HTH,
-g

On Fri, Aug 21, 2015 at 3:10 AM, fractal <[hidden email]> wrote:
hi all:
    boost::spirit is a great library, i use it for our company project, but i encountered a compile problem today,  i summary the problem to the
following sample code:

#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>

namespace q = boost::spirit::qi;

typedef boost::variant<std::string, int> variant_t;

struct var
{
variant_t _var;
};

BOOST_FUSION_ADAPT_STRUCT(var, 
 (variant_t, _var));

int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;
rule_t _rule  = q::int_;
rule_t _rule2 = '(' >> _rule; //this compile error.
return 0;
}

    that is, _rule2 will complain that boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node::initialize cannot convert ... 

    any suggestion is appreciated!!





------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general



------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

grubino


On Fri, Aug 21, 2015 at 3:10 AM, fractal <[hidden email]> wrote:
hi all:
    boost::spirit is a great library, i use it for our company project, but i encountered a compile problem today,  i summary the problem to the
following sample code:

#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>

namespace q = boost::spirit::qi;

typedef boost::variant<std::string, int> variant_t;

struct var
{
variant_t _var;
};

BOOST_FUSION_ADAPT_STRUCT(var, 
 (variant_t, _var));

int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;
rule_t _rule  = q::int_;
rule_t _rule2 = '(' >> _rule; //this compile error.
return 0;
}

    that is, _rule2 will complain that boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node::initialize cannot convert ... 

    any suggestion is appreciated!!



 On Fri, Aug 21, 2015 at 8:36 AM, Greg Rubino <[hidden email]> wrote:
I think _rule is simply an alias for q::int in this code.  Maybe try changing 

rule_t _rule = q::int_;

to

rule_t _rule = q::int_.alias();

I think I have seen this before, and this was the solution, aside from the obvious:

rule_t _rule2 = '(' >> q::int_;


HTH,
-g


Sorry for top posting!
-g 
------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general




------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

grubino


On Fri, Aug 21, 2015 at 8:37 AM, Greg Rubino <[hidden email]> wrote:


On Fri, Aug 21, 2015 at 3:10 AM, fractal <[hidden email]> wrote:

int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;

I just noticed that the synthesized attribute is incorrect for this rule.  The synthesized attribute for q::int_ has to be convertible to int, I believe.

However, my comment that _rule is simply an alias of q::int_ still holds.

Also, in order for the synthesized attribute of a qi expression to be variant<a, b, c>, I think you need an expression that reads a_ | b_ | c_.  Here's an article that has more detail:


Good luck!
-g

------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

sehe
 
 
 
 
On Fri, Aug 21, 2015, at 02:45 PM, Greg Rubino wrote:
 
 
On Fri, Aug 21, 2015 at 8:37 AM, Greg Rubino <[hidden email]> wrote:
 
 
 
On Fri, Aug 21, 2015 at 3:10 AM, fractal <[hidden email]> wrote:
 
int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;
 
 
I just noticed that the synthesized attribute is incorrect for this rule.  The synthesized attribute for q::int_ has to be convertible to int, I believe.
 
This is not the case. The variant is perfectly compatible to receive an int
 
 
However, my comment that _rule is simply an alias of q::int_ still holds.
 
Again, nope. `int_` is not even a rule.
 
I looked at this earlier today, and couldn't find a quick workaround. The problem seems to stem from the fact that the struct is a single-element tuple (this is a longstanding edge-case in Spirit V2)
 
 
Also, in order for the synthesized attribute of a qi expression to be variant<a, b, c>, I think you need an expression that reads a_ | b_ | c_.  Here's an article that has more detail:
 
See above
 
 
Seth

------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

Joel de Guzman
On 8/21/15 9:43 PM, Seth wrote:
> Again, nope. `int_` is not even a rule.
> I looked at this earlier today, and couldn't find a quick workaround. The problem seems to
> stem from the fact that the struct is a single-element tuple (this is a longstanding
> edge-case in Spirit V2)

Can anyone test if this is still an issue with X3?

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

fractal
In reply to this post by grubino
    this problem severely limit the usage of variant in spirit: 

    that is, it cannot place any unused attributed rule before/after variant attribute.

    for example, in my project, i have no idea of how to synthesize rule snippet

-('(' >> _var >> ')' ) as _var's attribute is boost::variant. 





在 2015-08-21 20:45:12,"Greg Rubino" <[hidden email]> 写道:


On Fri, Aug 21, 2015 at 8:37 AM, Greg Rubino <[hidden email]> wrote:


On Fri, Aug 21, 2015 at 3:10 AM, fractal <[hidden email]> wrote:

int main(int argc, char* argv[])
{
typedef q::rule<const char*, q::standard::space_type, var()> rule_t;

I just noticed that the synthesized attribute is incorrect for this rule.  The synthesized attribute for q::int_ has to be convertible to int, I believe.

However, my comment that _rule is simply an alias of q::int_ still holds.

Also, in order for the synthesized attribute of a qi expression to be variant<a, b, c>, I think you need an expression that reads a_ | b_ | c_.  Here's an article that has more detail:


Good luck!
-g



------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: help with usage about boost::spirit and boost::variant

Joel de Guzman
On 8/23/15 8:21 AM, fractal wrote:
>      this problem severely limit the usage of variant in spirit:
>
>      that is, it cannot place any unused attributed rule before/after variant attribute.
>
>      for example, in my project, i have no idea of how to synthesize rule snippet
>
> -('(' >> _var >> ')' ) as _var's attribute is boost::variant.

One question that jumps out is: why do you have to wrap the variant in
a fusion struct?

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general