Boost logo

Proto :

Subject: [proto] using BOOST_PROTO_EXTENDS_FUNCTION()
From: Christophe Henry (christophe.j.henry_at_[hidden])
Date: 2010-07-27 08:48:34


Hi,

I have a small issue which puzzles me a little.
I want to provide my DSEL a syntax very much like the map_list_of example:
func( some_grammar_expr )..; //extra parens coming later => func(
some_grammar_expr )( some_other_grammar_expr )
which I would like to generate me a functor object. I have a wrapper
and a domain:

template<typename Expr>
struct my_expr;

struct my_dom
  : proto::domain<proto::pod_generator<my_expr>, my_creation_grammar >
{};

template<typename Expr>
struct my_expr
{
    BOOST_PROTO_BASIC_EXTENDS(Expr, my_expr<Expr>, my_dom)
    BOOST_PROTO_EXTENDS_FUNCTION()
    /* here comes the fun part */

};

I now define a terminal:
my_expr<proto::terminal<my_tag>::type> const func = {{{}}};

All I need for the first usage (just one pair of parens) is a simple
grammar (in the real example, it is of course defined first):

struct my_creation_grammar
    : proto::or_<
        proto::when<
            proto::function< proto::terminal<my_tag> , some_grammar>,
            /* build a functor */
>
>
{};

Now, what I marked as the fun part. How to generate something callable
like say, a simple functor taking a single argument?
map_list_of defined there a simple conversion operator actually doing
the work of returning something useful. But a conversion to any
functor? Hmmm, ok then I define my_expr to be a functor itself:
template <typename A0>
...
operator()(A0 & a0) const
{
/* evaluate my_creation_grammar, forward the call to the functor
returned by evaluating the grammar */
}

Interestingly, this sometimes works, but in some cases, the compiler
complains that I now have a conflict with the operator() provided by
BOOST_PROTO_EXTENDS_FUNCTION().

I didn't find a perfect solution for this. The best I came up with was
to replace:
- BOOST_PROTO_EXTENDS_FUNCTION() by BOOST_PROTO_EXTENDS_SUBSCRIPT()
- proto::function by proto::subscript
- func( some_grammar_expr ) by func [ some_grammar_expr ]

This seems to work but I'm wondering if I could get back my parens.

Any idea?

Thanks,

Christophe


Proto list run by eric at boostpro.com