-
E.g. std::expected<X, std::string> parseX(
const std::string &str)
{
auto result = lexy::parse<grammar::Table>(lexy::string_input(str),
lexy_ext::report_error);
if (result.is_success()) {
return result.value();
} else {
return std::unexpected( ? ); //<-
}
} Or do I have to implement my own |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You can do lexy/include/lexy_ext/report_error.hpp Lines 318 to 382 in 14467db |
Beta Was this translation helpful? Give feedback.
-
Thank you a lot for the answer! Regarding the color information, it seems to me that the default doesn't use any colors:
So that should be fine. But here is still a small issue with this solution, it prints a |
Beta Was this translation helpful? Give feedback.
-
Currently my best solution is: struct CollectErrors
{
struct _sink
{
using return_type = std::vector<std::string>;
template <typename Input, typename Reader, typename Tag>
void operator()(const lexy::error_context<Input> &context,
const lexy::error<Reader, Tag> &error)
{
std::string out;
lexy_ext::_detail::write_error(std::back_inserter(out),
context,
error,
{},
nullptr);
errors.push_back(out);
}
std::vector<std::string> finish() && { return errors; }
std::vector<std::string> errors;
};
constexpr auto sink() const { return _sink{}; }
}; |
Beta Was this translation helpful? Give feedback.
You can do
lexy_ext::report_error.to(std::back_inserter(error))
, whereerror
is astd::string
. It will then format the diagnostic into thestd::string
. You probably want to override the visualization options to something that doesn't include color information though:lexy/include/lexy_ext/report_error.hpp
Lines 318 to 382 in 14467db