Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Larry Evans <cppljevans <at> suddenlink.net>
Subject: another case for tagged variants (was Re: Look mom! No semantic actions.
Newsgroups: gmane.comp.parsers.spirit.general
Date: Tuesday 27th October 2009 02:17:10 UTC (over 8 years ago)
On 10/26/09 19:54, Michael Caisse wrote:
> Larry Evans wrote:
>> On 10/26/09 15:10, OvermindDL1 wrote:
>> [snip]
>>   
>>> On Mon, Oct 26, 2009 at 1:47 PM, Michael Caisse
>>>     
>> [snip]
>>   
>>>> path_result_type could simply be a std::string. Or....
>>>> maybe I could do something like:
>>>>
>>>>
>>>> struct path_result_type
>>>> {
>>>>      std::string      product;
>>>>      bool             is_regex;
>>>> };
>>>>
>>>> BOOST_FUSION_ADAPT_STRUCT(
>>>>   path_result_type,
>>>>   (std::string, product)
>>>> )
>>>>
>>>>
>>>> and then do some semantic action magic with the is_regex member. But
I'm
>>>> not sure how to access it from _val (not yet at least).
>>>>
>>>> Though that seems to still be old school. So what I was thinking was
something
>>>> more like:
>>>>
>>>> struct path_result_type
>>>> {
>>>>      tuple      product;
>>>> };
>>>>
>>>> BOOST_FUSION_ADAPT_STRUCT(
>>>>   path_result_type,
>>>>   (tuple, product)
>>>> )
>>>>
>>>> and changing the path rule to be
>>>>
>>>>    path %= ( (regex_search > ????)
>>>>              |
>>>>              (product_name > ???? ) );
>>>>
>>>> where ???? is some eps like parser that consumes nothing but
>>>> produces a boolean attribute.
>>>>
>>>> So what should I be doing?
>>>>       
>>> If you have a bool which is "is_regex", then the ???? after
>>> regex_search should be:  attr(true)
>>> The ???? after product_name then would be:  attr(false)
>>>
>>>     
>> What does attr(true) do?  I'm guessing it's like eps except it
>> sets an element in the "corresponding" attribute of of the
>> lhs.  In this case, the path_result_type::is_regex.  Is that
>> right.  (I did scan the docs at:
>>
>>   http://svn.boost.org/svn/boost/trunk/libs/spirit/doc/html/index.html
>>
>> but found no mention of "attr").
>>
>> TIA.
>>
>> -Larry
>>
>>
>>   
> 
> Larry -
> 
> attr does exactly what I needed. Consumes nothing yet exposes an
> attribute. The docs are here:
> 
> http://svn.boost.org/svn/boost/trunk/libs/spirit/doc/html/spirit/qi/reference/auxiliary/attr.html
> 
> My final solution for the simple case actually looks like:
> 
> 
> typedef fusion::tuple product_name;
> 
>   - and -
> 
> path %=	    ( 
> 	       (regex_search > attr(true))
> 		|
> 	       (product_name > attr(false))
> 	    ) ;
> 
> 
> HTH
> michael
> 
> 

Thanks Michael. I just didn't know where to look.

However, now I'm wondering about generalizing this use-case.
For example, suppose that instead of having just a string,
you also had some other type, e.g. int.  IOW, instead of the
above path, you had

   path %= ( regex_search
           | product_name
           | int_
           )

Now, you can't use tuple because it doesn't
contain int; however, neither can you use variant
because variant can't take duplicate types.

However, instead of the current variant, why not use a truly
"tagged" variant which can handle duplicate types by using
the tag to indicate which type?  Then there would be no
need for the trailing:

   > attr(bool)

Thoughts?

-regards,
Larry



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
 
CD: 2ms