Header lexy/dsl/context_flag.hpp

Rule DSL lexy::dsl::context_flag

lexy/dsl/context_flag.hpp
namespace lexy::dsl
{
    struct context_flag-dsl // note: not a rule itself
    {
        template <bool InitialValue = false>
        constexpr rule auto create() const;

        constexpr rule auto set() const;
        constexpr rule auto reset() const;
        constexpr rule auto toggle() const;

        constexpr branch-rule auto is_set() const;
        constexpr branch-rule auto is_reset() const;

        constexpr rule auto value() const;
    };

    template <typename Id>
    constexpr context_flag-dsl context_flag;
}

context_flag is not a rule, but a DSL for specifying rules that manipulate a boolean variable of the current context.

Example 1. Parse (a|b|c)* where the first b must occur after the first a
struct production
{
    struct expected_a_before_b
    {
        static constexpr auto name = "expected a before b";
    };

    static constexpr auto rule = [] {
        // Declare a flag - it is not created yet!
        auto flag = dsl::context_flag<production>;

        // Parsing a sets the flag to true.
        auto a = dsl::lit_c<'a'> >> flag.set();
        // Parsing b requires that the flag has been set already.
        auto b = dsl::lit_c<'b'> >> dsl::must(flag.is_reset()).error<expected_a_before_b>;
        // Parsing c doesn't care about the flag.
        auto c = dsl::lit_c<'c'>;

        // Create the flag initialized to false.
        // Then parse a|b|c in a loop.
        return flag.create() + dsl::loop(a | b | c | dsl::break_);
    }();
};

Rule .create()

lexy/dsl/context_flag.hpp
template <bool InitialValue = false>
constexpr rule auto create() const;

.create() returns a rule that creates the boolean.

Parsing

Matches everything, without consuming anything. As a side effect, it creates a boolean with name Id inside the current context. If a context variable (of any) type with that name already exists, it is shadowed. This boolean is initialized to InitialValue.

Errors

None.

Values

None.

Rules .set(), .reset(), .toggle()

lexy/dsl/context_flag.hpp
constexpr rule auto set() const;
constexpr rule auto reset() const;
constexpr rule auto toggle() const;

.set(), .reset(), .toggle() return rules that change the value of the boolean variable.

Requires

A boolean with the name Id has been created in the current context, i.e. .create() has been parsed earlier.

Parsing

Matches everything, without consuming anything. As a side effect, it modifies the boolean with name Id of the current context:

  • .set() sets it to true.

  • .reset() sets it to false.

  • .toggle() changes its value from true to false or from false to true.

Errors

None.

Values

None.

Branch rules .is_set(), .is_reset()

lexy/dsl/context_flag.hpp
constexpr branch-rule auto is_set() const;
constexpr branch-rule auto is_reset() const;

.is_set() and .is_reset() return branch rules that check the value of the boolean variable.

Requires

A boolean with the name Id has been created in the current context, i.e. .create() has been parsed earlier.

Parsing

Matches everything, without consuming anything.

Branch parsing

Backtracks unless the value of the boolean with name Id of the current context is true (.is_set()) or false (.is_reset()). Then matches everything without consuming anything.

Errors

None.

Values

None.

Rule .value()

lexy/dsl/context_flag.hpp
constexpr rule auto value() const;

.value() is a rule that returns the value of the boolean variable.

Requires

A boolean with the name Id has been created in the current context, i.e. .create() has been parsed earlier.

Parsing

Matches everything, without consuming anything.

Errors

None.

Values

The current value of the boolean with name Id of the current context.

See also