Returning a constant on a match #144
-
I'm trying to make a rule that matches So instead, I thought to make a For that to work, namespace dsl = lexy::dsl;
struct unit {
unit(const std::string& word, bool has_colon = false);
};
struct word {
struct str {
static constexpr
auto rule = identifier(dsl::ascii::word);
static constexpr
auto value = ::lexy::as_string<std::string>;
};
static constexpr
auto rule = dsl::p<str>;
static constexpr
auto value = ::lexy::forward<std::string>;
};
struct unit_rule {
struct has_colon {
static constexpr
auto rule = (identifier(dsl::ascii::space) >> dsl::lit_c<':'>)
| dsl::lit_c<':'>;
static constexpr
auto value = ::lexy::constant(true);
};
static constexpr
auto rule = dsl::p<word> >> opt(dsl::p<has_colon>);
static constexpr
auto value = ::lexy::construct<unit>;
};
void test() {
auto input = lexy::read_file<lexy::utf8_encoding>("");
//error here: In template: static assertion failed due to requirement
//'_detail::error<unit_rule::has_colon, lexy::lexeme<lexy::_br8>>':
//missing value callback overload for production
auto result = lexy::parse<unit_rule>(input.buffer()
, lexy_ext::report_error.path(""));
} The question is how to actually produce a boolean value Currently, the code above has 2 issues:
Even though a Note, that I cannot use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The compilation error here is because The whitespace issue is because after you skip whitespace, you expect a colon, and don't have a way to parse whitespace without a colon. I don't understand why you can't use automatic whitespace skipping. Is whitespace only relevant in some parts of your grammars and not in others? Because otherwise, it's enough to add a |
Beta Was this translation helpful? Give feedback.
The compilation error here is because
opt(dsl::p<has_colon>)
returnstrue
orlexy::nullopt
, but there is no way to constructunit
fromlexy::nullopt
.The whitespace issue is because after you skip whitespace, you expect a colon, and don't have a way to parse whitespace without a colon. I don't understand why you can't use automatic whitespace skipping. Is whitespace only relevant in some parts of your grammars and not in others? Because otherwise, it's enough to add a
whitespace
member to the root production and have it propagate through. That way, you don't have to worry about it at all.