|
Proto : |
Subject: Re: [proto] grammars, domains and subdomains
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2010-12-08 05:30:38
Eric Niebler wrote:
> On 12/7/2010 2:37 PM, Thomas Heller wrote:
>> Hi,
>>
>> I have been trying to extend a domain by "subdomaining" it. The sole
>> purpose of this subdomain was to allow another type of terminal
>> expression.
>>
>> Please see the attached code, which is a very simplified version of what
>> I was trying to do.
> <snip>
>
>> So, How to handle that correctly?
>
> Yup, that's a problem. I don't have an answer for you at the moment,
> sorry.
I think i solved the problem. The testcase for this solution is attached.
Let me restate what I wanted to accomplish:
Given a domain, which serves as a common superdomain for certain different
subdomains.
The grammar associated with that domain will serve as the base grammar for
that EDSL.
There might be usecases, where certain proto expressions want to be allowed
in a sub domain, and additionally allowing that expression to be mixed with
the already defined grammar rules in the super domain.
So, in order to achieve that, the grammar of our common super domain needs
to be parametrized on a Grammar type. This will allow to reuse that
grammar, and extend it with other rules.
The implementation looks like this:
struct super_grammar
: proto::switch_<super_grammar>
{
template <typename Tag, typename Grammar = super_grammar>
struct case_;
};
With the different case_ specializations on Tag we can define what valid
expressions are, with respect to the Grammar type. This defaults to
super_grammar.
To extend that grammar in terms of allowing additional expressions, but do
not change what expressions super_grammar matches we can do the following:
struct sub_grammar
: proto::switch_<sub_grammar>
{
template <typename Tag, typename Grammar = sub_grammar>
struct case_ : super_grammar::case_<Tag, sub_grammar>
{};
};
So far so good. With this technique, every expression which was valid in
super_grammar are now valid in sub_grammar with the addition of the
extensions.
This might be what people refer to as type-2 grammars.
Now, super_grammar belongs to super_domain and sub_grammar to sub_domain,
which is a sub domain of super_domain.
At the end of the day, I want to mix expressions from super_domain with
expressions from sub_domain.
The default operator overloads are not suitable for this, cause the deduced
domain of super_domain and sub_domain is super_domain.
This makes expressions of the form t1 OP t2 invalid (where t1 is from
super_domain and t2 from sub_domain) because t1 OP t2 is not a valid
expression in super_domain. However it is in sub_domain.
In this case we do not want the deduced_domain, but the strongest domain,
which is basically the most specialised domain, or strongest domain as I
tagged it.
I hope that makes sense.
Regards,
Thomas
Proto list run by eric at boostpro.com