X3: How to write generic rules

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

X3: How to write generic rules

Mike Gresens
Hi,

lets say you have:

user_rule = ...
user_rule_def = ...

group_rule = ...
group_rule_def = ...

BOOST_SPIRIT_DEFINE(user_rule, group_rule)

Now you want to make a generic list_of_rule:

list_of_rule = ??? MAGIC ???

So you can use it later:

users_rule_def = list_of_rule<user_rule>
groups_rule_def = list_of_rule<groups_rule>

How to do that?

Thanks,
Mike...
Reply | Threaded
Open this post in threaded view
|

Re: X3: How to write generic rules

sehe
On 27-01-16 21:27, Mike Gresens wrote:

> Hi,
>
> lets say you have:
>
> user_rule = ...
> user_rule_def = ...
>
> group_rule = ...
> group_rule_def = ...
>
> BOOST_SPIRIT_DEFINE(user_rule, group_rule)
>
> Now you want to make a generic list_of_rule:
>
> list_of_rule = ??? MAGIC ???
>
> So you can use it later:
>
> users_rule_def = list_of_rule<user_rule>
> groups_rule_def = list_of_rule<groups_rule>
>
> How to do that?
>
How about:

    auto list_of = [](auto p) { return x3::as_parser(p) % ","; };

    auto user_rule   = "user"  >> x3::int_;
    auto group_rule  = "group" >> x3::bool_;

    auto users_rule  = list_of(user_rule);
    auto groups_rule = list_of(group_rule);

I wouldn't overcomplicate things in X3.
You can always look at operator/list.hpp or directive/repeat.hpp to see
how these directives were made.

Here's a simple test: http://melpon.org/wandbox/permlink/m87LpfTAmATGxcyz

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

test.cpp (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: X3: How to write generic rules

Mike Gresens
Beautiful! Thanks a lot!

To be sure when to use BOOST_SPIRIT_DEFINE, rule and rule_def:

I need BOOST_SPIRIT_DEFINE() if i have a x3:rule<ID, DataType> rule and a rule_def.
I need x3:rule<ID, DataType> rule if i want to produce special DataType (like a struct).

For all other cases i can simply use auto xyz_rule = <composition of other rules>, because the produced DataType is implicitly given.

That's right?

Still not sure when to use x3::as_parser() ...
Reply | Threaded
Open this post in threaded view
|

Re: X3: How to write generic rules

Mike Gresens
Oops...

Where to set the rule name if i use:

auto xyz_rule = <composition of other rules>

For meaningful expectation failures...

Thanks,
Mike...