Header lexy/dsl/context_flag.hpp
Rule DSL lexy::dsl::context_flag
lexy/dsl/context_flag.hppnamespace 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.
(a|b|c)* where the first b must occur after the first astruct 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.hpptemplate <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
Idinside the current context. If a context variable (of any) type with that name already exists, it is shadowed. This boolean is initialized toInitialValue.- Errors
None.
- Values
None.
Rules .set(), .reset(), .toggle()
lexy/dsl/context_flag.hppconstexpr 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
Idhas 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
Idof the current context:.set()sets it totrue..reset()sets it tofalse..toggle()changes its value fromtruetofalseor fromfalsetotrue.
- Errors
None.
- Values
None.
Branch rules .is_set(), .is_reset()
lexy/dsl/context_flag.hppconstexpr 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
Idhas 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
Idof the current context istrue(.is_set()) orfalse(.is_reset()). Then matches everything without consuming anything.- Errors
None.
- Values
None.
Rule .value()
lexy/dsl/context_flag.hppconstexpr rule auto value() const;.value() is a rule that returns the value of the boolean variable.
- Requires
A boolean with the name
Idhas 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
Idof the current context.