Skip to content

Commit 9f6c203

Browse files
committed
Refactor error handling with new do_return_from_raise method
Extracted complex error handling logic from `return_from_raise` into a separate method: - Created `do_return_from_raise` to centralize error recovery mechanism - Simplified `return_from_raise` by delegating to the new method - Improved code readability and maintainability - Preserved existing error handling behavior
1 parent 98deb02 commit 9f6c203

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

lug/lug.hpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,35 @@ class parser_base
18921892
return true;
18931893
}
18941894

1895+
[[nodiscard]] error_response do_return_from_raise(raise_frame const& frame, syntax const& sx, error_response rec_res)
1896+
{
1897+
error_response err_res{rec_res};
1898+
error_context err{*environment_, sx, frame.label, err_res};
1899+
auto handler_index = static_cast<std::size_t>(frame.eh);
1900+
auto next_frame = stack_frames_.rbegin();
1901+
auto const last_frame = stack_frames_.rend();
1902+
while (handler_index < program_->handlers.size()) {
1903+
err_res = program_->handlers[handler_index](err);
1904+
if (err_res != error_response::rethrow)
1905+
break;
1906+
err_res = error_response::halt;
1907+
handler_index = (std::numeric_limits<std::size_t>::max)();
1908+
for (++next_frame; next_frame != last_frame; ++next_frame) {
1909+
if (auto const* const next_report_frame = std::get_if<report_frame>(&*next_frame); next_report_frame != nullptr) {
1910+
handler_index = static_cast<std::size_t>(next_report_frame->eh);
1911+
break;
1912+
}
1913+
}
1914+
}
1915+
if (err_res >= error_response::backtrack) {
1916+
registers_.sr = frame.sr;
1917+
registers_.rc = frame.rc;
1918+
return err_res;
1919+
}
1920+
registers_.pc = frame.pc;
1921+
return err_res;
1922+
}
1923+
18951924
void do_accept(std::string_view match)
18961925
{
18971926
detail::scope_exit const cleanup{[this, prior_call_depth = environment_->start_accept()]{
@@ -2100,31 +2129,7 @@ class basic_parser : public parser_base
21002129
auto const mat = match();
21012130
auto const sub = subject();
21022131
environment_->set_match_and_subject(mat, sub);
2103-
error_response err_res{rec_res};
2104-
error_context err{*environment_, syntax{((sr0 < sr1) ? mat.substr(sr0, sr1 - sr0) : sub), sr0}, frame.label, err_res};
2105-
auto handler_index = static_cast<std::size_t>(frame.eh);
2106-
auto next_frame = stack_frames_.rbegin();
2107-
auto const last_frame = stack_frames_.rend();
2108-
while (handler_index < program_->handlers.size()) {
2109-
err_res = program_->handlers[handler_index](err);
2110-
if (err_res != error_response::rethrow)
2111-
break;
2112-
err_res = error_response::halt;
2113-
handler_index = (std::numeric_limits<std::size_t>::max)();
2114-
for (++next_frame; next_frame != last_frame; ++next_frame) {
2115-
if (auto const* const next_report_frame = std::get_if<report_frame>(&*next_frame); next_report_frame != nullptr) {
2116-
handler_index = static_cast<std::size_t>(next_report_frame->eh);
2117-
break;
2118-
}
2119-
}
2120-
}
2121-
if (err_res >= error_response::backtrack) {
2122-
registers_.sr = frame.sr;
2123-
registers_.rc = frame.rc;
2124-
return err_res;
2125-
}
2126-
registers_.pc = frame.pc;
2127-
return err_res;
2132+
return do_return_from_raise(frame, syntax{((sr0 < sr1) ? mat.substr(sr0, sr1 - sr0) : sub), sr0}, rec_res);
21282133
}
21292134

21302135
[[nodiscard]] std::pair<error_response, std::ptrdiff_t> return_from_call()
@@ -2264,8 +2269,7 @@ class basic_parser : public parser_base
22642269

22652270
void accept_or_drain_if_deferred()
22662271
{
2267-
if ((registers_.ci & lug::registers::count_mask) == 0)
2268-
{
2272+
if ((registers_.ci & lug::registers::count_mask) == 0) {
22692273
bool const should_cut = (registers_.ci & lug::registers::inhibited_flag) != 0;
22702274
bool const should_accept = (registers_.ci & lug::registers::ignore_errors_flag) != 0;
22712275
if (should_cut || should_accept) {

0 commit comments

Comments
 (0)