X3 attribute collapsing for alternative

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

X3 attribute collapsing for alternative

Kent Hauser
Hi,

I’m porting a project from qi to x3 & ran into a problem with attribute collapsing for an alternate rule. Here is a sample test grammar. It does not compile w/o errors if the alternate is enabled.  I am using the develop branch on github & g++ 4.9.2

Thanks for insights.

#include <boost/spirit/home/x3.hpp>
#include <boost/variant.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;
using E = boost::variant<int, float>;

namespace parser {
    x3::rule<class expr, E> expr = "expr";
    x3::rule<class term, E> term = "term";

    auto const term_def = x3::int_ | x3::float_;
    auto const expr_def = term | ('(' > term > ')');

    BOOST_SPIRIT_DEFINE(expr, term)
}

int main () {
    std::string input = "(1)";

    E value;
    auto r = x3::phrase_parse(input.begin(), input.end(),  parser::expr, x3::space, value);
    std::cout << "result = " << value << std::endl;
}



Sent from Mailbox

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3 attribute collapsing for alternative

Mario Lang
"Kent Hauser" <[hidden email]> writes:

> I’m porting a project from qi to x3 & ran into a problem with attribute
> collapsing for an alternate rule. Here is a sample test grammar. It
> does not compile w/o errors if the alternate is enabled.  I am using
> the develop branch on github & g++ 4.9.2
>
> Thanks for insights.

There is no overload for

  bool parse_sequence(..., traits::variant_attribute);

which is called inside of x3::sequence<...>::parse() here:

  auto const expr_def = term | ('(' > term > ')');
                                    ^

Duplicating detail::parse_sequence(..., traits::plain_attribute) to do
the same thing for traits::variant_attribute makes the compile error go
away, and seems to return the correct result for the test case.

However, I am not sure if this is the correct solution.

> #include <boost/spirit/home/x3.hpp>
> #include <boost/variant.hpp>
> #include <iostream>
>
> namespace x3 = boost::spirit::x3;
> using E = boost::variant<int, float>;
>
> namespace parser {
>     x3::rule<class expr, E> expr = "expr";
>     x3::rule<class term, E> term = "term";
>
>     auto const term_def = x3::int_ | x3::float_;
>     auto const expr_def = term | ('(' > term > ')');
>
>     BOOST_SPIRIT_DEFINE(expr, term)
> }
>
> int main () {
>     std::string input = "(1)";
>
>     E value;
>     auto r = x3::phrase_parse(input.begin(), input.end(),  parser::expr, x3::space, value);
>     std::cout << "result = " << value << std::endl;
> }

--- a/include/boost/spirit/home/x3/operator/detail/sequence.hpp
+++ b/include/boost/spirit/home/x3/operator/detail/sequence.hpp
@@ -340,6 +340,31 @@ namespace boost { namespace spirit { namespace x3 { namespace detail
         return false;
     }
 
+    template <typename Parser, typename Iterator, typename Context
+      , typename RContext, typename Attribute>
+    bool parse_sequence(
+        Parser const& parser , Iterator& first, Iterator const& last
+      , Context const& context, RContext& rcontext, Attribute& attr
+      , traits::variant_attribute)
+    {
+        typedef typename Parser::left_type Left;
+        typedef typename Parser::right_type Right;
+        typedef typename traits::attribute_of<Left, Context>::type l_attr_type;
+        typedef typename traits::attribute_of<Right, Context>::type r_attr_type;
+        typedef traits::make_attribute<l_attr_type, Attribute> l_make_attribute;
+        typedef traits::make_attribute<r_attr_type, Attribute> r_make_attribute;
+
+        typename l_make_attribute::type l_attr = l_make_attribute::call(attr);
+        typename r_make_attribute::type r_attr = r_make_attribute::call(attr);
+
+        Iterator save = first;
+        if (parser.left.parse(first, last, context, rcontext, l_attr)
+            && parser.right.parse(first, last, context, rcontext, r_attr))
+            return true;
+        first = save;
+        return false;
+    }
+
     template <typename Left, typename Right, typename Iterator
       , typename Context, typename RContext, typename Attribute>
     bool parse_sequence(

--
CYa,
  ⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3 attribute collapsing for alternative

Joel de Guzman
On 7/6/15 4:09 PM, Mario Lang wrote:

> "Kent Hauser" <[hidden email]> writes:
>
>> I’m porting a project from qi to x3 & ran into a problem with attribute
>> collapsing for an alternate rule. Here is a sample test grammar. It
>> does not compile w/o errors if the alternate is enabled.  I am using
>> the develop branch on github & g++ 4.9.2
>>
>> Thanks for insights.
>
> There is no overload for
>
>    bool parse_sequence(..., traits::variant_attribute);
>
> which is called inside of x3::sequence<...>::parse() here:
>
>    auto const expr_def = term | ('(' > term > ')');
>                                      ^
>
> Duplicating detail::parse_sequence(..., traits::plain_attribute) to do
> the same thing for traits::variant_attribute makes the compile error go
> away, and seems to return the correct result for the test case.
>
> However, I am not sure if this is the correct solution.
>
Seems like the right solution, except that both overloads can make use of
one templated implementation to avoid duplicate code.

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


------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3 attribute collapsing for alternative

Kent Hauser
In reply to this post by Mario Lang
Mario,

Thanks. This patch seems solves my problem. Do you mind if I leave submitting the patch to you, as I don’t fully understand it :=>



Sent from Mailbox


On Mon, Jul 6, 2015 at 4:10 AM, Mario Lang <[hidden email]> wrote:

"Kent Hauser" <[hidden email]> writes:

> I’m porting a project from qi to x3 & ran into a problem with attribute
> collapsing for an alternate rule. Here is a sample test grammar. It
> does not compile w/o errors if the alternate is enabled.  I am using
> the develop branch on github & g++ 4.9.2
>
> Thanks for insights.

There is no overload for

bool parse_sequence(..., traits::variant_attribute);

which is called inside of x3::sequence<...>::parse() here:

auto const expr_def = term | ('(' > term > ')');
^

Duplicating detail::parse_sequence(..., traits::plain_attribute) to do
the same thing for traits::variant_attribute makes the compile error go
away, and seems to return the correct result for the test case.

However, I am not sure if this is the correct solution.


> #include <boost/spirit/home/x3.hpp>
> #include <boost/variant.hpp>
> #include <iostream>
>
> namespace x3 = boost::spirit::x3;
> using E = boost::variant<int, float>;
>
> namespace parser {
>     x3::rule<class expr, E> expr = "expr";
>     x3::rule<class term, E> term = "term";
>
>     auto const term_def = x3::int_ | x3::float_;
>     auto const expr_def = term | ('(' > term > ')');
>
>     BOOST_SPIRIT_DEFINE(expr, term)
> }
>
> int main () {
>     std::string input = "(1)";
>
>     E value;
>     auto r = x3::phrase_parse(input.begin(), input.end(),  parser::expr, x3::space, value);
>     std::cout << "result = " << value << std::endl;
> }

--- a/include/boost/spirit/home/x3/operator/detail/sequence.hpp
+++ b/include/boost/spirit/home/x3/operator/detail/sequence.hpp
@@ -340,6 +340,31 @@ namespace boost { namespace spirit { namespace x3 { namespace detail
return false;
}

+ template <typename Parser, typename Iterator, typename Context
+ , typename RContext, typename Attribute>
+ bool parse_sequence(
+ Parser const& parser , Iterator& first, Iterator const& last
+ , Context const& context, RContext& rcontext, Attribute& attr
+ , traits::variant_attribute)
+ {
+ typedef typename Parser::left_type Left;
+ typedef typename Parser::right_type Right;
+ typedef typename traits::attribute_of<Left, Context>::type l_attr_type;
+ typedef typename traits::attribute_of<Right, Context>::type r_attr_type;
+ typedef traits::make_attribute<l_attr_type, Attribute> l_make_attribute;
+ typedef traits::make_attribute<r_attr_type, Attribute> r_make_attribute;
+
+ typename l_make_attribute::type l_attr = l_make_attribute::call(attr);
+ typename r_make_attribute::type r_attr = r_make_attribute::call(attr);
+
+ Iterator save = first;
+ if (parser.left.parse(first, last, context, rcontext, l_attr)
+ && parser.right.parse(first, last, context, rcontext, r_attr))
+ return true;
+ first = save;
+ return false;
+ }
+
template <typename Left, typename Right, typename Iterator
, typename Context, typename RContext, typename Attribute>
bool parse_sequence(

--
CYa,
⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general



------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3 attribute collapsing for alternative

Mario Lang
"Kent Hauser" <[hidden email]> writes:

> Mario,
>
> Thanks. This patch seems solves my problem.
> Do you mind if I leave submitting the patch to you, as I don’t fully
> understand it :=>

Done, and merged to develop by Joel.

--
CYa,
  ⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3 attribute collapsing for alternative

Joel de Guzman
On 7/7/15 6:22 AM, Mario Lang wrote:
> "Kent Hauser" <[hidden email]> writes:
>
>> Mario,
>>
>> Thanks. This patch seems solves my problem.
>> Do you mind if I leave submitting the patch to you, as I don’t fully
>> understand it :=>
>
> Done, and merged to develop by Joel.

Yup. Thanks, Kent and Mario!

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


------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general