PGN Parser¶
High Level Functions¶
- pgnParse.read_game(f: ~typing.TextIO, Visitor: ~typing.Type[~chess.pgn.BaseVisitor[~chess.pgn.ResultT]] = <class 'chess.pgn.GameBuilder'>) ResultT | None [source]¶
Reads a game from an open handle PGN-file in text mode.
By using text mode, the parser does not need to handle encodings. The file must be opened using the correct encoding, usually
‘utf-8-sig’: UTF-8 with optional BOM,
‘ascii’: 7-bit ASCII,
‘iso-8859-1’: ISO 8859/1 (Latin 1), rarely used but suggested by PGN standard.
Please note: read_game uses a private buffer.
As an extension of the PGN Standard, read_game accepts
null move extensions of the Standard Algebraic Notation (SAN) (0000|@@@@|Z0)
NAG symbols for annotations (?|??|?!|!|!!|!?)
NAG symbols for positions (+=|=+|+/-|+-|+--|-/+|-+|–+|=|~’)
NAG symbols for novelty (N) and display(D)
Improper end of a game (see below)
The parser is configured by using the Visitor object leading to different ResultT nodes
chess.pgn.GameBuilder delivers a chess.pgn.Game object
chess.pgn.HeadersBuilder delivers a chess.pgn.Headers object
chess.pgn.BoardBuilder delivers a chess.Board object
chess.pgn.SkipVisitor delivers a boolean indicating whether a game could be successfully skipped
The end of a game is determined by either
the game termination marker (1-0|0-1|1/2-1/2|*),
the end of file (EOF) occured during parsing,
or the start of a new header.
The last 2 alternatives are reported as an error, but parsing succeeds.
- Parameters:
f – file handle opened in text mode
Visitor – Visitor object, i.e. chess.pgn.BaseVisitor and one of the derived classes
- Returns:
the expected ResultT object or
None
if parsing failed.
- pgnParse.read_headers(handle: TextIO) Headers | None [source]¶
Convenience function representing read_game(handle, Visitor = chess.pgn.HeadersBuilder)
- Parameters:
f – file handle opened in text mode
- Returns:
a chess.pgn.Headers object or
None
if parsing failed.
Lexer¶
- class pgnParse.PGNLexer(bufsize: int = 2147483647, debug: bool = False, **kwargs)[source]¶
A Lexer for Portable Game Notation (PGN) files (see ply) tracking line number, gameID, position (in game)
- Parameters:
bufsize – size of the token buffer (min: 4096). The buffer is refilled at bufsize/4
debug – run the lexer in debug mode
kwargs – other keyword arguments of ply.lex.lex
- newGame(f: TextIO) LexToken [source]¶
Prepared the lexer to deliver tokens for a game
- Parameters:
f – file handle opened in text mode
- Returns:
the first token of the game
- endOfGamePattern() Pattern [source]¶
Returns the compiled t_ENDOFGAME pattern
- Returns:
compiled t_ENDOFGAME pattern
- t_SANPLY(t)[source]¶
(([NBKRQ]?[a-h]?[1-8]?)?[:x]?[a-h][1-8](=[QRBN])?[+#]?|O-O(-O)?|0-0(-0)?|0{4}|@{4}|Z0|–(?![+-]))