Skip to content

Commit

Permalink
Improved error reporting from parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 8, 2021
1 parent 3d545d6 commit 7355656
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Shared/Engine/Helpers/FPGN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@

#define REMEMBER unsigned savedCursor = cursor;

#define RETURN_FAILURE(error) { \
#define RETURN_FAILURE_SILENT(error, silent) { \
cursor = savedCursor; \
if (!silent) { \
std::cout << error << " around '" << character(-2) << character(-1) << character() << character(1) << character(2) << "' at " << __FUNCTION__ << ":" << __LINE__ << std::endl; \
} \
return false; }

#define RETURN_FAILURE(error) RETURN_FAILURE_SILENT(error, false)

static bool isFile(char c) {
return c >= 'a' && c <= 'h';
}
Expand Down Expand Up @@ -385,7 +389,7 @@ bool FPGN::parseTerminationMarker() {
game.outcome = ChessGame::Outcome::in_progress;
return true;
} else {
RETURN_FAILURE("Unexpected termination marker")
RETURN_FAILURE_SILENT("Unexpected termination marker", true)
}
}

Expand Down Expand Up @@ -515,7 +519,7 @@ bool FPGN::parseVariation() {
eatWhiteSpaces(); // TODO: should we do this at a high level?

if (character() != '(') {
RETURN_FAILURE("Unexpected start of variation")
RETURN_FAILURE_SILENT("Unexpected start of variation", true)
} else {
cursor++;
}
Expand Down Expand Up @@ -649,7 +653,7 @@ bool FPGN::parseComment() {

return true;
} else {
RETURN_FAILURE("Unexpected comment starting character")
RETURN_FAILURE_SILENT("Unexpected comment starting character", true)
}
}

Expand All @@ -664,7 +668,7 @@ bool FPGN::parseTag(bool lookahead) {

// Expecting a tag opening bracket
if (character() != '[') {
RETURN_FAILURE("Unexpected start TAG character")
RETURN_FAILURE_SILENT("Unexpected start TAG character", true)
}

if (lookahead) {
Expand Down Expand Up @@ -706,6 +710,18 @@ bool FPGN::setGame(std::string pgn, ChessGame &game, unsigned & cursor) {
fpgn.cursor = cursor;
bool parsedMoveText = false;
while (fpgn.hasMoreCharacters()) {
fpgn.eatWhiteSpaces();

if (fpgn.character() == '*') {
// We have reached the end of this game
fpgn.cursor++;
fpgn.eatWhiteSpaces();

game = fpgn.game;
cursor = fpgn.cursor;
return true;
}

// Check if there is tag ahead and if we have already
// parsed at least one moveText section. If that is the case,
// it means we are now parsing the tag section of another game.
Expand All @@ -724,19 +740,7 @@ bool FPGN::setGame(std::string pgn, ChessGame &game, unsigned & cursor) {
} else {
return false;
}

fpgn.eatWhiteSpaces();

if (fpgn.character() == '*') {
// We have reached the end of this game
fpgn.cursor++;
fpgn.eatWhiteSpaces();

game = fpgn.game;
cursor = fpgn.cursor;
return true;
}


fpgn.eatWhiteSpaces();
}

Expand Down

0 comments on commit 7355656

Please sign in to comment.