Header lexy/dsl/context_identifier.hpp
Rule DSL lexy::dsl::context_identifier
lexy/dsl/context_identifier.hpp
namespace 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.hpp
constexpr 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
Id
inside 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.hpp
constexpr 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
Id
has 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 nameId
to contain thelexy::lexeme
also produced by thelexy::dsl::identifier
rule.- Errors
All errors raised by parsing
identifier
. The rule then fails ifidentifier
has 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.hpp
struct 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
Id
has been created in the current context, i.e..create()
has been parsed earlier.- Parsing
Parses
identifier
and checks it against the one stored in the variable.- Branch parsing
Parses
identifier
and 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 ifidentifier
has failed.A generic error with the specified
Tag
orlexy::different_identifier
if the new identifier does not match the identifier stored in the variable with nameId
of the current context. Its range covers everything consumed byidentifier
and the rule then fails.
- Values
None; discards the value produced by
identifier
.