[spirit2] Containers, Tuples and Sequence attributes

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

[spirit2] Containers, Tuples and Sequence attributes

Joel de Guzman-2
Hi,

I've been thinking about augmenting the rules for attribute
building for sequences.

(I'm using tuple<X,Y,Z> and container<T> to signify fusion
containers and stl containers respectively)

1) If the sequence a >> b has the same type for the attributes of a
    and b, it seems a good idea to allow sequences to take in an stl
    container attribute? For example:

    rule<I, std::string> r;

    r %= char_ >> char_ >> char_; // should be ok?

    In general if we have a tuple attribute tuple<T, T, T> such that
    all the attributes are of the same type, then it should also be
    compatible with container<T>. To make it more interesting, we
    can also allow compatibility of tuple<X, Y, Z> with container<T>
    as long as T is compatible with X, Y and Z.

2) Consider a very common use case:

    r %= int_ >> *(',' >> int_); // comma separated ints

    Shouldn't the attribute also be compatible with container<int> ?
    Currently, the attribute is tuple<int, container<int> >

    So in general: If we have a tuple attribute tuple<T, container<T>, T...>
    The attribute can also be compatible with container<T>.

    (**) Amending this rule should also allow the use-case presented by
    ian eyberg in his post titled "still having trouble w/basic rules"

    This one is a bit tricky to implement but I'd like to hear your
    thoughts first.

Your thoughts?

Regards,
--
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net


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

Re: [spirit2] Containers, Tuples and Sequence attributes

Francois Barel
Joel de Guzman wrote:

> 1) If the sequence a >> b has the same type for the attributes of a
>    and b, it seems a good idea to allow sequences to take in an stl
>    container attribute? For example:
>
>    rule<I, std::string> r;
>
>    r %= char_ >> char_ >> char_; // should be ok?
>
>    In general if we have a tuple attribute tuple<T, T, T> such that
>    all the attributes are of the same type, then it should also be
>    compatible with container<T>. To make it more interesting, we
>    can also allow compatibility of tuple<X, Y, Z> with container<T>
>    as long as T is compatible with X, Y and Z.

When you say "compatible with" you mean this type (container<T>) can
be used e.g. when specified by hand in a rule used with %=, yet the
default attribute type (the auto-synthetized one) will still be
non-flattened version (tuple<T, T, T>), right?

Yeah, that seems like a good idea to me -- gives you more freedom, yet
you don't have to use it (e.g. in cases where you want to stay with
fusion containers for simple compile-time accesses, if the attributes
just so happen to have the same types but actually have distinct
meanings).


> 2) Consider a very common use case:
>
>    r %= int_ >> *(',' >> int_); // comma separated ints
>
>    Shouldn't the attribute also be compatible with container<int> ?
>    Currently, the attribute is tuple<int, container<int> >

Makes sense, especially in that the same attribute type
(container<int>) will now be usable with either
    int_ >> *(',' >> int_)
or
    int_ % ','
which are "supposed" to be equivalent -- it's a good thing if they
become compatible attribute-wise (although, wrt my previous response,
the type of their auto-synthetized attributes would be different --
the first one would still be the non-flattened tuple<int,
container<int> >).


Regards,
Fran├žois

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

Re: [spirit2] Containers, Tuples and Sequence attributes

Joel de Guzman-2
Francois Barel wrote:

> Joel de Guzman wrote:
>> 1) If the sequence a >> b has the same type for the attributes of a
>>    and b, it seems a good idea to allow sequences to take in an stl
>>    container attribute? For example:
>>
>>    rule<I, std::string> r;
>>
>>    r %= char_ >> char_ >> char_; // should be ok?
>>
>>    In general if we have a tuple attribute tuple<T, T, T> such that
>>    all the attributes are of the same type, then it should also be
>>    compatible with container<T>. To make it more interesting, we
>>    can also allow compatibility of tuple<X, Y, Z> with container<T>
>>    as long as T is compatible with X, Y and Z.
>
> When you say "compatible with" you mean this type (container<T>) can
> be used e.g. when specified by hand in a rule used with %=, yet the
> default attribute type (the auto-synthetized one) will still be
> non-flattened version (tuple<T, T, T>), right?

Yes.

> Yeah, that seems like a good idea to me -- gives you more freedom, yet
> you don't have to use it (e.g. in cases where you want to stay with
> fusion containers for simple compile-time accesses, if the attributes
> just so happen to have the same types but actually have distinct
> meanings).
>
>
>> 2) Consider a very common use case:
>>
>>    r %= int_ >> *(',' >> int_); // comma separated ints
>>
>>    Shouldn't the attribute also be compatible with container<int> ?
>>    Currently, the attribute is tuple<int, container<int> >
>
> Makes sense, especially in that the same attribute type
> (container<int>) will now be usable with either
>     int_ >> *(',' >> int_)
> or
>     int_ % ','
> which are "supposed" to be equivalent -- it's a good thing if they
> become compatible attribute-wise (although, wrt my previous response,
> the type of their auto-synthetized attributes would be different --
> the first one would still be the non-flattened tuple<int,
> container<int> >).

Exactly. There will always be one auto-attribute. But the client
will have the added freedom to choose among variants of the
attribute.

There's of course a caveat: it will add into the compile time
wheher you use it or not. Well, I guess I have to test the
impact of that.

Regards,
--
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net


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

Re: [spirit2] Containers, Tuples and Sequence attributes

Joel de Guzman-2
Joel de Guzman wrote:
> Francois Barel wrote:

>> Yeah, that seems like a good idea to me -- gives you more freedom, yet
>> you don't have to use it (e.g. in cases where you want to stay with
>> fusion containers for simple compile-time accesses, if the attributes
>> just so happen to have the same types but actually have distinct
>> meanings).

Ok, this is cool! Now I have code that can do this:

     { // alternative forms of attributes. Allow sequences to take in
       // stl containers.

         std::vector<char> v;
         BOOST_TEST(test_attr("abc", char_ >> char_ >> char_, v));
         BOOST_TEST(v.size() == 3);
         BOOST_TEST(v[0] == 'a');
         BOOST_TEST(v[1] == 'b');
         BOOST_TEST(v[2] == 'c');
     }

 >> Makes sense, especially in that the same attribute type
 >> (container<int>) will now be usable with either
 >>     int_ >> *(',' >> int_)
 >> or
 >>     int_ % ','

And this:

     { // alternative forms of attributes. Allow sequences to take in
       // stl containers.

         std::vector<char> v;
         BOOST_TEST(test_attr("a,b,c", char_ >> *(',' >> char_), v));
         BOOST_TEST(v.size() == 3);
         BOOST_TEST(v[0] == 'a');
         BOOST_TEST(v[1] == 'b');
         BOOST_TEST(v[2] == 'c');

     }

There seems to be no impact on compile time. I'll do a more
comprehensive testing, but I think I am convinced. I'll commit
the code unless there are objections. The feature is available
on sequences (a >> b) and expectations (a > b). Spirit2X.

Regards,
--
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net


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