Skip to content

Commit

Permalink
[Issue 1146] Error handling only on InputFileParser level; get rid of…
Browse files Browse the repository at this point in the history
… InputFileLine in root_task
  • Loading branch information
tanjaschindler committed Jul 17, 2024
1 parent 777bef8 commit 5c189e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/search/tasks/root_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ ExplicitEffect::ExplicitEffect(
void ExplicitOperator::read_pre_post(utils::InputFileParser &in) {
utils::InputFileLineParser line = in.parse_line();
vector<FactPair> 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");
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 15 additions & 17 deletions src/search/utils/input_file_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -71,8 +75,8 @@ void InputFileParser::error(const string &message) const {
utils::exit_with(ExitCode::SEARCH_INPUT_ERROR);
}

InputFileLine::InputFileLine(const shared_ptr<InputFileParser> input_file, const string &line, int line_number)
: input_file(input_file), line(line) {
InputFileLine::InputFileLine(const shared_ptr<InputFileParser> file_parser, const string &line, int line_number)
: file_parser(file_parser), line(line) {
this->line_number = line_number;
}

Expand All @@ -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_ptr<InputFileParser>input_file, const shared_ptr<InputFileLine> line)
: input_file(input_file), line(line) {
InputFileLineParser::InputFileLineParser(const shared_ptr<InputFileParser>file_parser, const shared_ptr<InputFileLine> line)
: file_parser(file_parser), line(line) {
tokens = split_line(line->get_line());
token_number = 0;
}
Expand All @@ -104,20 +102,20 @@ const vector<string>& 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<InputFileParser> input_file, const shared_ptr<InputFileLine> line, const string &token, int token_number)
: input_file(input_file), line(line), token(token) {
InputFileToken::InputFileToken(const shared_ptr<InputFileParser> file_parser, const shared_ptr<InputFileLine> line, const string &token, int token_number)
: file_parser(file_parser), line(line), token(token) {
this->token_number = token_number;
}

Expand All @@ -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);
}
}
13 changes: 6 additions & 7 deletions src/search/utils/input_file_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,35 @@ class InputFileParser {
};

class InputFileLine {
const std::shared_ptr<InputFileParser> input_file;
const std::shared_ptr<InputFileParser> file_parser;
const std::string line;
int line_number;
public:
InputFileLine(const std::shared_ptr<InputFileParser> input_file, const std::string &line, int line_number);
InputFileLine(const std::shared_ptr<InputFileParser> 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<InputFileParser> input_file;
const std::shared_ptr<InputFileParser> file_parser;
const std::shared_ptr<InputFileLine> line;
std::vector<std::string> tokens;
int token_number;
public:
InputFileLineParser(const std::shared_ptr<InputFileParser> input_file, const std::shared_ptr<InputFileLine> line);
InputFileLineParser(const std::shared_ptr<InputFileParser> file_parser, const std::shared_ptr<InputFileLine> line);
~InputFileLineParser();
const std::vector<std::string>& get_tokens() const;
InputFileToken read_token();
void check_last_token();
};

class InputFileToken {
const std::shared_ptr<InputFileParser> input_file;
const std::shared_ptr<InputFileParser> file_parser;
const std::shared_ptr<InputFileLine> line;
const std::string token;
int token_number;
public:
InputFileToken(const std::shared_ptr<InputFileParser> input_file, const std::shared_ptr<InputFileLine> line, const std::string &token, int token_number);
InputFileToken(const std::shared_ptr<InputFileParser> file_parser, const std::shared_ptr<InputFileLine> line, const std::string &token, int token_number);
~InputFileToken();
const std::string& get_token() const;
int parse_int(const std::string &cause) const;
Expand Down

0 comments on commit 5c189e7

Please sign in to comment.