From 0514221a2bc5e80890672f9a866e41c5bf02dc9d Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@lis-lab.fr> Date: Sat, 16 May 2020 21:32:59 +0200 Subject: [PATCH] Added more detailed error messages and fixed invalid read in assert actions --- reading_machine/src/Action.cpp | 28 ++++++++++++++++++++++++---- reading_machine/src/Transition.cpp | 21 +++++++++++++++------ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp index 2908148..7c496ee 100644 --- a/reading_machine/src/Action.cpp +++ b/reading_machine/src/Action.cpp @@ -384,8 +384,18 @@ Action Action::assertIsEmpty(const std::string & colName, Config::Object object, auto appliable = [colName, object, relativeIndex](const Config & config, const Action &) { - auto lineIndex = config.getRelativeWordIndex(object, relativeIndex); - return util::isEmpty(config.getAsFeature(colName, lineIndex)); + try + { + if (!config.hasRelativeWordIndex(object, relativeIndex)) + return false; + auto lineIndex = config.getRelativeWordIndex(object, relativeIndex); + return util::isEmpty(config.getAsFeature(colName, lineIndex)); + } catch (std::exception & e) + { + util::myThrow(fmt::format("colName='{}' object='{}' relativeIndex='{}' {}", colName, object == Config::Object::Stack ? "Stack" : "Buffer", relativeIndex, e.what())); + } + + return false; }; return {Type::Check, apply, undo, appliable}; @@ -403,8 +413,18 @@ Action Action::assertIsNotEmpty(const std::string & colName, Config::Object obje auto appliable = [colName, object, relativeIndex](const Config & config, const Action &) { - auto lineIndex = config.getRelativeWordIndex(object, relativeIndex); - return !util::isEmpty(config.getAsFeature(colName, lineIndex)); + try + { + if (!config.hasRelativeWordIndex(object, relativeIndex)) + return false; + auto lineIndex = config.getRelativeWordIndex(object, relativeIndex); + return !util::isEmpty(config.getAsFeature(colName, lineIndex)); + } catch (std::exception & e) + { + util::myThrow(fmt::format("colName='{}' object='{}' relativeIndex='{}' {}", colName, object == Config::Object::Stack ? "Stack" : "Buffer", relativeIndex, e.what())); + } + + return false; }; return {Type::Check, apply, undo, appliable}; diff --git a/reading_machine/src/Transition.cpp b/reading_machine/src/Transition.cpp index 2e3400c..1a2ad12 100644 --- a/reading_machine/src/Transition.cpp +++ b/reading_machine/src/Transition.cpp @@ -81,19 +81,28 @@ void Transition::apply(Config & config) bool Transition::appliable(const Config & config) const { - if (!state.empty() && state != config.getState()) - return false; - - for (const Action & action : sequence) - if (!action.appliable(config, action)) + try + { + if (!state.empty() && state != config.getState()) return false; + for (const Action & action : sequence) + if (!action.appliable(config, action)) + return false; + } catch (std::exception & e) + { + util::myThrow(fmt::format("transition '{}' {}", name, e.what())); + } + return true; } int Transition::getCost(const Config & config) const { - return cost(config); + try {return cost(config);} + catch (std::exception & e) { util::myThrow(fmt::format("transition '{}' {}", name, e.what()));} + + return 0; } const std::string & Transition::getName() const -- GitLab