How to provide custom error instead of "missing delimiter" #43
-
So, instead of saying "missing delimiter" for everything that uses // Look at struct c_style
struct comment : lexy::token_production
{
struct c_style : lexy::transparent_production
{
struct unterminated_error
{
static constexpr auto name = "unterminated comment";
};
static constexpr auto rule = []
{
auto delim = dsl::delimited(LEXY_LIT("/*"), LEXY_LIT("*/"));
return dsl::must(delim(dsl::code_point)).error<unterminated_error>; // compiles, but doesn't help, the error in the output is still "missing delimiter"
//return delim(dsl::code_point) | dsl::error<unterminated_error>; // doesn't compile
}();
};
struct cpp_style : lexy::transparent_production
{
static constexpr auto rule = []
{
// A comment begins with a // and ends at newline or EOF.
auto comment = dsl::delimited(LEXY_LIT("//"), dsl::eol);
// We can use a backslash to escape the next character (or EOF).
auto escape = dsl::backslash_escape.rule(dsl::eof / dsl::code_point);
// A comment consists of code points.
return comment(dsl::code_point, escape);
}();
};
static constexpr auto rule = dsl::p<c_style> | dsl::p<cpp_style>;
static constexpr auto value = lexy::as_string<std::string>;
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This works almost ideally: static constexpr auto rule = []
{
auto delim = dsl::delimited(LEXY_LIT("/*"), LEXY_LIT("*/"));
return dsl::try_(delim(dsl::code_point), dsl::error<unterminated_error>);
}(); Except that "missing delimiter" error is still there. |
Beta Was this translation helpful? Give feedback.
-
There is currently no way to override the error. I will add one in the next couple of days. |
Beta Was this translation helpful? Give feedback.
dsl::must
doesn't work, as it triggers the error when you don't have an initial/*
(it only checks that the branch condition is there).There is currently no way to override the error. I will add one in the next couple of days.