yo35 / kokopu

A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
https://www.npmjs.com/package/kokopu
GNU Lesser General Public License v3.0
27 stars 5 forks source link

Inconsistent handling of PGN with no metadata #46

Closed JeffML closed 6 months ago

JeffML commented 6 months ago

version: kokopu 4.10.0

If given a PGN with no metadata (moves only), kokopu will parse it correctly but returns inconsistent games attributes. For instance, given the input:

   1. e4 Nf6 2. e5 Nd5 3. d4 d6 4. Nf3 Bg4 5. Be2 e6 6. O-O Be7 7. h3 Bh5 8. c4 Nb6

kokopu.pgnRead() will create a db object with a gameCount=1, but the iterator from db.games() sees no games; e.g.,

for (let game of db.games()){...} 

will not enter the loop body. The debug screenshot will hopefully make that clear.

image

yo35 commented 6 months ago

The PGN is ill-formed here: the termination marker (1-0 / 1/2-1/2 / 0-1 or *) is missing.

As mentioned in the documentation (https://kokopu.yo35.org/docs/current/classes/Database.html#games), .games() iterates over the valid games within the database. For this reason, the number of iterations may be smaller than db.gameCount(). If you need to control more finely the behavior of the PGN parser in presence of ill-formed games, you should use the Database.game(..) accessor.

By the way, this has nothing to do with the fact that this game has no header.

yo35 commented 6 months ago

In this particular case though, i.e. when the termination marker is missing at the very at the very end of the PGN text, it should be possible for the parser to recover the game anyway. That's an evolution.

Still, the termination markers will remain mandatory in-between games in a PGN text containing 2 or more games. And it will still be possible to observe a number of iterations with .games() smaller than what is returned by .gameCount().

yo35 commented 6 months ago

The tolerant PGN parsing in case of missing termination marker at the end of the PGN string is available as of version 4.11.0 (just released).

JeffML commented 6 months ago

Thanks for the information. I wasn't aware that PGN requires a terminator (though I always wondered what the * signified). I will pull the newest version and test again. Much appreciated.