Header lexy/dsl/member.hpp

Rule lexy::dsl::member

lexy/dsl/member.hpp
namespace lexy
{
    template <typename Fn>
    struct member {};
}

namespace lexy::dsl
{
    class member-dsl // note: not a rule itself
    {
    public:
        constexpr rule        auto operator=(rule        auto rule);
        constexpr branch-rule auto operator=(branch-rule auto rule);
    };

    template <auto MemberPtr>
    constexpr member-dsl member;
}

#define LEXY_MEM(Name) lexy::dsl::member<...>

member is a rule that can be used to specify a member of an aggregate that will receive the resulting value a rule.

A rule is formed by writing member<MemberPtr> = rule.

Requires
  • MemberPtr is a member point of some aggregate type Aggregate.

  • rule produces exactly one value.

(Branch) Parsing

Parses rule.

Errors

All errors raised by rule. The rule then fails if rule has failed.

Values

Two values. The first is a value of type lexy::member<Fn>{}. Here, Fn is a default constructible function object that takes two arguments: a reference to an Aggregate object and the value of rule. When invoked, it will set the member described MemberPtr of the Aggregate object to the value of rule. The second is the value rule has produced.

The macro version LEXY_MEM(member) = rule behaves identical, except that member is not a member pointer but the name of a member. The resulting Fn can take an arbitrary aggregate type and will assign the member with that name.

It is designed to be used with lexy::as_aggregate  as the callback.

Example 1. Parse a point
struct point
{
    int x, y;
};

struct production
{
    static constexpr auto rule = [] {
        auto value = dsl::integer<int>;

        // Parse an integer into the x/y member of point.
        auto x_coord = (dsl::member<& point::x> = value);
        auto y_coord = (dsl::member<& point::y> = value);

        return x_coord + dsl::comma + y_coord;
    }();

    // `lexy::as_aggregate` accepts the `lexy::member<Fn>` + value pairs.
    static constexpr auto value = lexy::as_aggregate<point>;
};
Tip
The member rule works good in combination with lexy::dsl::combination  where rules are parsed in an arbitrary order.

See also