mirror of
https://gitlab.com/manzerbredes/pgnp.git
synced 2025-04-06 10:06:25 +02:00
Handle line comments ('%' and ';')
This commit is contained in:
parent
23275701a3
commit
5de6bf399b
2 changed files with 41 additions and 17 deletions
50
src/PGN.cpp
50
src/PGN.cpp
|
@ -10,7 +10,7 @@
|
||||||
#define IS_EOF (pgn_content.IsEOF())
|
#define IS_EOF (pgn_content.IsEOF())
|
||||||
#define EOF_CHECK(loc) \
|
#define EOF_CHECK(loc) \
|
||||||
{ \
|
{ \
|
||||||
if (IS_EOF) \
|
if (IS_EOF) \
|
||||||
throw UnexpectedEOF(); \
|
throw UnexpectedEOF(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ void PGN::ParseNextGame() {
|
||||||
if (IS_EOF) {
|
if (IS_EOF) {
|
||||||
throw NoGameFound();
|
throw NoGameFound();
|
||||||
}
|
}
|
||||||
long loc = NextNonBlank(LastGameEndLoc);
|
long loc = GotoNextToken(LastGameEndLoc);
|
||||||
if (IS_EOF) {
|
if (IS_EOF) {
|
||||||
throw NoGameFound();
|
throw NoGameFound();
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,8 @@ void PGN::ParseNextGame() {
|
||||||
} else if (c == '{') {
|
} else if (c == '{') {
|
||||||
loc = ParseComment(loc, moves);
|
loc = ParseComment(loc, moves);
|
||||||
continue; // No need loc++
|
continue; // No need loc++
|
||||||
|
} else if (c == '%' || c == ';') {
|
||||||
|
loc = GotoEOL(loc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loc++;
|
loc++;
|
||||||
|
@ -103,7 +105,7 @@ bool PGN::HasTag(std::string key) {
|
||||||
|
|
||||||
long PGN::ParseComment(long loc, HalfMove *hm) {
|
long PGN::ParseComment(long loc, HalfMove *hm) {
|
||||||
// Goto next char
|
// Goto next char
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
char c = pgn_content[loc];
|
char c = pgn_content[loc];
|
||||||
|
|
||||||
|
@ -121,8 +123,8 @@ long PGN::ParseComment(long loc, HalfMove *hm) {
|
||||||
loc++; // Skip '}'
|
loc++; // Skip '}'
|
||||||
|
|
||||||
// Goto next non blank to look for another comment token
|
// Goto next non blank to look for another comment token
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
if(!IS_EOF){
|
if (!IS_EOF) {
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,7 +133,7 @@ long PGN::ParseComment(long loc, HalfMove *hm) {
|
||||||
|
|
||||||
long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
// Goto next char
|
// Goto next char
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
char c = pgn_content[loc];
|
char c = pgn_content[loc];
|
||||||
|
|
||||||
|
@ -176,7 +178,7 @@ long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the HalfMove
|
// Parse the HalfMove
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
std::string move;
|
std::string move;
|
||||||
|
@ -189,7 +191,7 @@ long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
hm->move = move;
|
hm->move = move;
|
||||||
|
|
||||||
// Check for NAG
|
// Check for NAG
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
if (c == '$') {
|
if (c == '$') {
|
||||||
|
@ -209,7 +211,7 @@ long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
loc = ParseComment(loc, hm);
|
loc = ParseComment(loc, hm);
|
||||||
|
|
||||||
// Check for variations
|
// Check for variations
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
while (!IS_EOF && pgn_content[loc] == '(') {
|
while (!IS_EOF && pgn_content[loc] == '(') {
|
||||||
loc++; // Skip '('
|
loc++; // Skip '('
|
||||||
HalfMove *var = new HalfMove;
|
HalfMove *var = new HalfMove;
|
||||||
|
@ -217,13 +219,13 @@ long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
hm->variations.push_back(var);
|
hm->variations.push_back(var);
|
||||||
loc++; // Skip ')'
|
loc++; // Skip ')'
|
||||||
// Goto next var
|
// Goto next var
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip end of variation
|
// Skip end of variation
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
EOF_CHECK(loc);
|
EOF_CHECK(loc);
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
if (c == ')') {
|
if (c == ')') {
|
||||||
|
@ -231,7 +233,7 @@ long PGN::ParseHalfMove(long loc, HalfMove *hm) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse next HalfMove
|
// Parse next HalfMove
|
||||||
loc = NextNonBlank(loc);
|
loc = GotoNextToken(loc);
|
||||||
if (!IS_EOF) {
|
if (!IS_EOF) {
|
||||||
HalfMove *next_hm = new HalfMove;
|
HalfMove *next_hm = new HalfMove;
|
||||||
next_hm->count = hm->count;
|
next_hm->count = hm->count;
|
||||||
|
@ -262,7 +264,7 @@ long PGN::ParseNextTag(long start_loc) {
|
||||||
|
|
||||||
// Parse value
|
// Parse value
|
||||||
std::string value;
|
std::string value;
|
||||||
long valueloc = NextNonBlank(keyloc) + 1;
|
long valueloc = GotoNextToken(keyloc) + 1;
|
||||||
EOF_CHECK(keyloc);
|
EOF_CHECK(keyloc);
|
||||||
c = pgn_content[valueloc];
|
c = pgn_content[valueloc];
|
||||||
while (c != '"' or IS_EOF) {
|
while (c != '"' or IS_EOF) {
|
||||||
|
@ -310,7 +312,7 @@ std::string PGN::Dump() {
|
||||||
return (ss.str());
|
return (ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
long PGN::NextNonBlank(long loc) {
|
long PGN::GotoNextToken(long loc) {
|
||||||
char c = pgn_content[loc];
|
char c = pgn_content[loc];
|
||||||
while (IS_BLANK(c)) {
|
while (IS_BLANK(c)) {
|
||||||
loc++;
|
loc++;
|
||||||
|
@ -318,9 +320,29 @@ long PGN::NextNonBlank(long loc) {
|
||||||
return (loc);
|
return (loc);
|
||||||
}
|
}
|
||||||
c = pgn_content[loc];
|
c = pgn_content[loc];
|
||||||
|
if (c == '%' || c == ';') {
|
||||||
|
loc = GotoEOL(loc);
|
||||||
|
if (IS_EOF) {
|
||||||
|
return (loc);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (loc);
|
return (loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long PGN::GotoEOL(long loc) {
|
||||||
|
char c = pgn_content[loc];
|
||||||
|
while (true) {
|
||||||
|
loc++;
|
||||||
|
if (IS_EOF) {
|
||||||
|
return (loc);
|
||||||
|
}
|
||||||
|
c = pgn_content[loc];
|
||||||
|
if (c == '\n') {
|
||||||
|
return (loc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pgnp
|
} // namespace pgnp
|
|
@ -53,13 +53,15 @@ private:
|
||||||
/// @brief Populate @a tags with by parsing the one starting at location in
|
/// @brief Populate @a tags with by parsing the one starting at location in
|
||||||
/// argument
|
/// argument
|
||||||
long ParseNextTag(long);
|
long ParseNextTag(long);
|
||||||
/// @brief Get the next non-blank char location starting from location in
|
|
||||||
/// argument
|
|
||||||
long NextNonBlank(long);
|
|
||||||
/// @brief Parse a HalfMove at a specific location into @a pgn_content
|
/// @brief Parse a HalfMove at a specific location into @a pgn_content
|
||||||
long ParseHalfMove(long, HalfMove *);
|
long ParseHalfMove(long, HalfMove *);
|
||||||
/// @brief Parse a consecutive sequence of comment
|
/// @brief Parse a consecutive sequence of comment
|
||||||
long ParseComment(long, HalfMove *);
|
long ParseComment(long, HalfMove *);
|
||||||
|
/// @brief Get the next non-blank char location ignoring line comments ('%'
|
||||||
|
/// and ';')
|
||||||
|
long GotoNextToken(long);
|
||||||
|
/// @brief Goto the end of the current line
|
||||||
|
long GotoEOL(long);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UnexpectedEOF : public std::exception {
|
struct UnexpectedEOF : public std::exception {
|
||||||
|
|
Loading…
Add table
Reference in a new issue