Skip to content

Commit

Permalink
Address parts of reviews. Main changes: may_start_line is now a funct…
Browse files Browse the repository at this point in the history
…ion, more documentation
  • Loading branch information
tanjaschindler committed Nov 14, 2024
1 parent f30e600 commit 9ae051f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
23 changes: 10 additions & 13 deletions src/search/utils/input_file_parser.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "input_file_parser.h"

#include "system.h"

#include <cassert>
Expand All @@ -16,10 +17,8 @@ InputFileParser::InputFileParser(istream &stream)
: stream(stream), context(""), only_whitespaces("\\s*") {
}

InputFileParser::~InputFileParser() {
}

string InputFileParser::find_next_line(bool throw_error_on_failure) {
assert(may_start_line); // We probably forgot a confirm_end_of_line.
string next_line;
while (!stream.eof()) {
getline(stream, next_line);
Expand All @@ -35,9 +34,8 @@ string InputFileParser::find_next_line(bool throw_error_on_failure) {
}

void InputFileParser::initialize_tokens() {
assert(may_start_line);
assert(may_start_line());
assert(token_number == 0);
assert(tokens.empty());
assert(line != "");
istringstream stream(line);
string word;
Expand All @@ -48,6 +46,10 @@ void InputFileParser::initialize_tokens() {
assert(tokens.size() > 0);
}

bool InputFileParser::may_start_line() {
return tokens.empty();
}

int InputFileParser::parse_int(const string &str, const string &cause) {
try {
string::size_type parsed_length;
Expand All @@ -65,10 +67,9 @@ void InputFileParser::set_context(const string &context) {
}

string InputFileParser::read(const string &message) {
if (may_start_line) {
if (may_start_line()) {
line = find_next_line(true);
initialize_tokens();
may_start_line = false;
}
if (token_number >= tokens.size()) {
error("Unexpected end of line. Message: " + message);
Expand All @@ -84,7 +85,6 @@ int InputFileParser::read_int(const string &message) {
}

string InputFileParser::read_line(const string &message) {
assert(may_start_line); // We probably forgot a confirm_end_of_line.
line = find_next_line(true);
return line;
}
Expand All @@ -102,11 +102,10 @@ void InputFileParser::read_magic_line(const string &magic) {
}

void InputFileParser::confirm_end_of_line() {
if (may_start_line) {
if (may_start_line()) {
return;
}
if (token_number == tokens.size()) {
may_start_line = true;
token_number = 0;
tokens.clear();
} else {
Expand All @@ -116,7 +115,6 @@ void InputFileParser::confirm_end_of_line() {
}

void InputFileParser::confirm_end_of_file() {
assert(may_start_line);
string next_line = find_next_line(false);
if(next_line != "") {
error("Expected end of file, found non-empty line " + next_line);
Expand All @@ -132,8 +130,7 @@ void InputFileParser::error(const string &message) const {
if (context != "") {
cerr << "Context: " << context << endl;
}
cerr << message << endl
<< "Exiting." << endl;
cerr << message << endl << "Exiting." << endl;
utils::exit_with(ExitCode::SEARCH_INPUT_ERROR);
}
}
38 changes: 21 additions & 17 deletions src/search/utils/input_file_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,48 @@ class InputFileParser {
size_t token_number;
std::string line;
std::vector<std::string> tokens;
bool may_start_line = true;
const std::regex only_whitespaces;
std::string find_next_line(bool throw_error_on_failure=true);
void initialize_tokens();
bool may_start_line();
int parse_int(const std::string &str, const std::string &cause);
public:
explicit InputFileParser(std::istream &stream);
~InputFileParser();

/*
Set context for error reporting.
*/
void set_context(const std::string &context);
/*
Read a single token within a line. Tokens within a line are
separated by arbitrary whitespaces. Set curser to the end of the
read token.
separated by arbitrary whitespaces. Report error if the current
line does not contain a token after the cursor position. Set
cursor to the end of the read token.
*/
std::string read(const std::string &message); // TODO: templates
std::string read(const std::string &message);
/*
Read a single token in a line as integer, analoguously to
read(...). Report an error if the token is not a string of digits
representing an int.
*/
int read_int(const std::string &message); // TODO: templates
int read_int(const std::string &message);
/*
Read a complete line as a single string. Set cursor to the
beginning of the next line.
Read a complete line as a single string token. Report an error if
the cursor is not at the beginning of a line before reading. Set
cursor to the beginning of the next line.
*/
std::string read_line(const std::string &message); // TODO: templates
std::string read_line(const std::string &message);
/*
Read a complete line as a single integer. Report an error if the
line is not a string of digits representing an int.
Read a complete line as a single integer token, analogously to
read_line(...). Report an error if the line is not a string of
digits representing an int.
*/
int read_line_int(const std::string &message); // TODO: templates
int read_line_int(const std::string &message);
/*
Read a complete line and compare it to a *magic* string.
Read a complete line and compare it to a *magic* string. Report an
error if the cursor is not at the beginning of a line before
reading. Report an error if the content of the line is not equal
to the *magic* string.
*/
void read_magic_line(const std::string &magic);
/*
Expand All @@ -63,10 +71,6 @@ class InputFileParser {
void confirm_end_of_file();
// TODO: Should this be public at all? Or should we add a get_line method?
void error(const std::string &message) const;
private:
std::string find_next_line(bool throw_error_on_failure=true);
void initialize_tokens();
int parse_int(const std::string &str, const std::string &cause);
};
}
#endif

0 comments on commit 9ae051f

Please sign in to comment.