From 3fb48d4f29ec54c991a1756c95354f0cbd5371e9 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 27 Jan 2022 08:04:32 +0100 Subject: [PATCH] Improve parser --- src/PGN.cpp | 57 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/PGN.cpp b/src/PGN.cpp index b45632c..3f30c4e 100644 --- a/src/PGN.cpp +++ b/src/PGN.cpp @@ -190,35 +190,46 @@ loctype PGN::ParseHalfMove(loctype loc, HalfMove *hm) { } hm->move = move; - // Check for NAG loc = GotoNextToken(loc); EOF_CHECK(loc); c = pgn_content[loc]; - if (c == '$') { - hm->NAG += c; - loc++; - EOF_CHECK(loc); - c = pgn_content[loc]; - while (IS_DIGIT(c)) { - hm->NAG += c; - loc++; + + while (c == '{' || c == '$' || c == '(' || c == ';' || c == '%') { + if (c == '{') { + // Parse comment + loc = ParseComment(loc, hm); + } else if (c == '$') { + // Check for NAG + loc = GotoNextToken(loc); EOF_CHECK(loc); c = pgn_content[loc]; + if (c == '$') { + hm->NAG += c; + loc++; + EOF_CHECK(loc); + c = pgn_content[loc]; + while (IS_DIGIT(c)) { + hm->NAG += c; + loc++; + EOF_CHECK(loc); + c = pgn_content[loc]; + } + } + } else if (c == '(') { + // Check for variations + loc = GotoNextToken(loc); + while (!IS_EOF && pgn_content[loc] == '(') { + loc++; // Skip '(' + HalfMove *var = new HalfMove; + loc = ParseHalfMove(loc, var); + hm->variations.push_back(var); + loc++; // Skip ')' + // Goto next var + loc = GotoNextToken(loc); + EOF_CHECK(loc); + c = pgn_content[loc]; + } } - } - - // Parse comment - loc = ParseComment(loc, hm); - - // Check for variations - loc = GotoNextToken(loc); - while (!IS_EOF && pgn_content[loc] == '(') { - loc++; // Skip '(' - HalfMove *var = new HalfMove; - loc = ParseHalfMove(loc, var); - hm->variations.push_back(var); - loc++; // Skip ')' - // Goto next var loc = GotoNextToken(loc); EOF_CHECK(loc); c = pgn_content[loc];