Header lexy/dsl/context_identifier.hpp
Rule DSL lexy::dsl::context_identifier
lexy/dsl/context_identifier.hppnamespace lexy
{
struct different_identifier {};
}
namespace lexy::dsl
{
struct context_identifier-dsl // note: not a rule itself
{
constexpr rule auto create() const;
constexpr branch-rule auto capture() const;
constexpr see-below rematch() const;
};
template <typename Id>
constexpr context_identifier-dsl context_identifier(identifier-dsl identifier);
}context_identifier is not a rule, but a DSL for specifying rules that manipulate an lexy::dsl::identifier variable of the current context.
struct production
{
static constexpr auto rule = [] {
// Declare an identifier variable - it is not created yet!
auto word = dsl::identifier(dsl::ascii::alpha);
auto word_var = dsl::context_identifier<production>(word);
// Parse a word and capture it in the variable.
auto first_word = word_var.capture();
// Parse another word and compare it agains the variable.
auto second_word = word_var.rematch();
// Create the empty variable, then parse the two words.
return word_var.create() + first_word + dsl::lit_c<','> + second_word;
}();
};Rule .create()
lexy/dsl/context_identifier.hppconstexpr rule auto create() const;.create() returns a rule that creates the identifier variable.
- Parsing
Matches everything, without consuming anything. As a side effect, it creates an identifier with name
Idinside the current context. If a context variable (of any) type with that name already exists, it is shadowed. This identifier is initialized to the empty string; it’s underlying type islexy::lexeme.- Errors
None.
- Values
None.
Rule .capture()
lexy/dsl/context_identifier.hppconstexpr branch-rule auto capture() const;.capture() returns a branch rule that parses an identifier and stores it in the variable.
- Requires
The identifier with the name
Idhas been created in the current context, i.e..create()has been parsed earlier.- (Branch) Parsing
Parses
identifier. As a side effect, it sets the identifier with the nameIdto contain thelexy::lexemealso produced by thelexy::dsl::identifierrule.- Errors
All errors raised by parsing
identifier. The rule then fails ifidentifierhas failed.- Values
The value produced by parsing
identifier, which is the same as the one stored in the variable.
Rule .rematch()
lexy/dsl/context_identifier.hppstruct rematch-dsl // models branch-rule
{
template <typename Tag>
static constexpr rematch-dsl auto error;
};
constexpr rematch-dsl rematch() const;.rematch() returns a branch rule that matches the stored identifier again..
- Requires
The identifier with the name
Idhas been created in the current context, i.e..create()has been parsed earlier.- Parsing
Parses
identifierand checks it against the one stored in the variable.- Branch parsing
Parses
identifierand backtracks when that fails. Also backtracks when the new identifier does not match the old one.- Errors
All errors raised by parsing
identifier. The rule then fails ifidentifierhas failed.A generic error with the specified
Tagorlexy::different_identifierif the new identifier does not match the identifier stored in the variable with nameIdof the current context. Its range covers everything consumed byidentifierand the rule then fails.
- Values
None; discards the value produced by
identifier.