Skip to content

Allow iterators for PGN parsing #139

@vladkvit

Description

@vladkvit

I'm using this library as a helper for feeding data to a machine learning model. To that end, I need to be able to iterate over games in a PGN (as opposed to parsing in bulk).

In my code, I do something like this:

class StepParser : pgn::StreamParser<> {
public:
    explicit StepParser(std::istream& stream) : StreamParser{ stream } {}

    bool initRead(pgn::Visitor& vis) {
        visitor = &vis;

        if (!stream_buffer.fill()) {
            return false;
        }
        return true;
    }

    void finishRead() {
        if (!pgn_end) {
            onEnd();
        }
    }

    bool readNextGame() {
        while (auto c = stream_buffer.some()) {
            if (in_header) {
                visitor->skipPgn(false);

                if (*c == '[') {
                    visitor->startPgn();
                    pgn_end = false;

                    processHeader();
                }

            }
            else if (in_body) {
                processBody();
            }

            if (!dont_advance_after_body) {
                stream_buffer.advance();
            }
            dont_advance_after_body = false;

            if (pgn_end) {
                pgn_end = false;
                return true;
            }
        }
        return false;
    }
};

On the library side, I had to switch several members of StreamParser from private to protected.

This is all quite a bit of a hack, so I'm wondering if there is potential for official support to be able to iterate over games.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions