From 5c189e76e1978b153e969ce2d390c11c033eca11 Mon Sep 17 00:00:00 2001 From: tanjaschindler Date: Wed, 17 Jul 2024 15:54:46 +0200 Subject: [PATCH] [Issue 1146] Error handling only on InputFileParser level; get rid of InputFileLine in root_task --- src/search/tasks/root_task.cc | 6 ++--- src/search/utils/input_file_parser.cc | 32 +++++++++++++-------------- src/search/utils/input_file_parser.h | 13 +++++------ 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/search/tasks/root_task.cc b/src/search/tasks/root_task.cc index 2875fb8720..efbbba7f90 100644 --- a/src/search/tasks/root_task.cc +++ b/src/search/tasks/root_task.cc @@ -190,7 +190,6 @@ ExplicitEffect::ExplicitEffect( void ExplicitOperator::read_pre_post(utils::InputFileParser &in) { utils::InputFileLineParser line = in.parse_line(); vector conditions = read_facts(line); - // TODO: this is also used for axioms, then it should say "affected by axiom" int var = line.read_token().parse_int("variable affected by effect"); int value_pre = line.read_token().parse_int("variable value precondition"); int value_post = line.read_token().parse_int("variable value postcondition"); @@ -253,15 +252,14 @@ static void read_and_verify_version(utils::InputFileParser &in) { static bool read_metric(utils::InputFileParser &in) { in.set_context("metric_section"); in.read_magic_line("begin_metric"); - utils::InputFileLine metric_line = in.read_line(); - string use_metric_string = metric_line.get_line(); + string use_metric_string = in.read_line().get_line(); bool use_metric = false; if (use_metric_string == "1") { use_metric = true; } else if (use_metric_string == "0") { use_metric = false; } else { - metric_line.error("expected boolean"); + in.error("expected boolean"); } in.read_magic_line("end_metric"); return use_metric; diff --git a/src/search/utils/input_file_parser.cc b/src/search/utils/input_file_parser.cc index e0d2ecbb85..32631b9564 100644 --- a/src/search/utils/input_file_parser.cc +++ b/src/search/utils/input_file_parser.cc @@ -62,7 +62,11 @@ void InputFileParser::read_magic_line(const string &magic) { // TODO: name? } void InputFileParser::error(const string &message) const { - cerr << "Error reading input file:" << endl; + cerr << "Error reading input file "; + if (line_number > 0) { + cerr << "line " << line_number; + } + cerr << "." << endl; if (context != "") { cerr << "Context: " << context << endl; } @@ -71,8 +75,8 @@ void InputFileParser::error(const string &message) const { utils::exit_with(ExitCode::SEARCH_INPUT_ERROR); } -InputFileLine::InputFileLine(const shared_ptr input_file, const string &line, int line_number) -: input_file(input_file), line(line) { +InputFileLine::InputFileLine(const shared_ptr file_parser, const string &line, int line_number) +: file_parser(file_parser), line(line) { this->line_number = line_number; } @@ -83,14 +87,8 @@ const string& InputFileLine::get_line() const { return line; } -void InputFileLine::error(const string &cause) const { - ostringstream message; - message << "Line " << line_number << ": " << cause << "."; - input_file->error(message.str()); -} - -InputFileLineParser::InputFileLineParser(const shared_ptrinput_file, const shared_ptr line) -: input_file(input_file), line(line) { +InputFileLineParser::InputFileLineParser(const shared_ptrfile_parser, const shared_ptr line) +: file_parser(file_parser), line(line) { tokens = split_line(line->get_line()); token_number = 0; } @@ -104,20 +102,20 @@ const vector& InputFileLineParser::get_tokens() const { InputFileToken InputFileLineParser::read_token() { if (token_number > tokens.size()) { - line->error("unexpected end of line"); + file_parser->error("unexpected end of line"); } ++token_number; - return InputFileToken(input_file, line, tokens[token_number - 1], token_number); + return InputFileToken(file_parser, line, tokens[token_number - 1], token_number); } void InputFileLineParser::check_last_token() { if (token_number > tokens.size()) { - line->error("expected end of line"); + file_parser->error("expected end of line"); } } -InputFileToken::InputFileToken(const shared_ptr input_file, const shared_ptr line, const string &token, int token_number) - : input_file(input_file), line(line), token(token) { +InputFileToken::InputFileToken(const shared_ptr file_parser, const shared_ptr line, const string &token, int token_number) + : file_parser(file_parser), line(line), token(token) { this->token_number = token_number; } @@ -137,6 +135,6 @@ int InputFileToken::parse_int(const string &cause) const { } } catch (exception &e) { } - line->error("expected number; cause: " + cause); + file_parser->error("expected number; cause: " + cause); } } diff --git a/src/search/utils/input_file_parser.h b/src/search/utils/input_file_parser.h index 3aac14d26d..d27b1511ec 100644 --- a/src/search/utils/input_file_parser.h +++ b/src/search/utils/input_file_parser.h @@ -24,23 +24,22 @@ class InputFileParser { }; class InputFileLine { - const std::shared_ptr input_file; + const std::shared_ptr file_parser; const std::string line; int line_number; public: - InputFileLine(const std::shared_ptr input_file, const std::string &line, int line_number); + InputFileLine(const std::shared_ptr file_parser, const std::string &line, int line_number); ~InputFileLine(); const std::string& get_line() const; - void error(const std::string &message) const; }; class InputFileLineParser { - const std::shared_ptr input_file; + const std::shared_ptr file_parser; const std::shared_ptr line; std::vector tokens; int token_number; public: - InputFileLineParser(const std::shared_ptr input_file, const std::shared_ptr line); + InputFileLineParser(const std::shared_ptr file_parser, const std::shared_ptr line); ~InputFileLineParser(); const std::vector& get_tokens() const; InputFileToken read_token(); @@ -48,12 +47,12 @@ class InputFileLineParser { }; class InputFileToken { - const std::shared_ptr input_file; + const std::shared_ptr file_parser; const std::shared_ptr line; const std::string token; int token_number; public: - InputFileToken(const std::shared_ptr input_file, const std::shared_ptr line, const std::string &token, int token_number); + InputFileToken(const std::shared_ptr file_parser, const std::shared_ptr line, const std::string &token, int token_number); ~InputFileToken(); const std::string& get_token() const; int parse_int(const std::string &cause) const;