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.

pgnParse.read_board(handle: TextIO) Board | None[source]

Convenience function representing read_game(handle, Visitor = chess.pgn.BoardBuilder)

Parameters:

f – file handle opened in text mode

Returns:

a chess.Board object or None if parsing failed.

pgnParse.skip_game(handle: TextIO) bool[source]

Convenience function representing read_game(handle, Visitor = chess.pgn.SkipVisitor)

Parameters:

f – file handle opened in text mode

Returns:

a bool indicating whether parsing was successful.

Lexer

pgnParse.lexer: PGNLexer = <pgnParse.PGNLexer object>

The global instance of the 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

__init__(bufsize: int = 2147483647, debug: bool = False, **kwargs) None[source]
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

endGame() None[source]

Ends running through a new game

endOfGamePattern() Pattern[source]

Returns the compiled t_ENDOFGAME pattern

Returns:

compiled t_ENDOFGAME pattern

t_NEWLINE(t)[source]

n+

t_tag_NEWLINE(t)[source]

n+

t_tag_TAGVALUE(t)[source]

"(\"|[^”])*"

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|–(?![+-]))

t_LBRACK(t)[source]

[

t_tag_RBRACK(t)[source]

]

t_MOVENUMBER(t)[source]

([1-9][0-9]*.*|.+)(?!-)(?!/)

t_NAG(t)[source]

($[1-9][0-9]{0,2}(?![0-9])|(–+|+–|-/\+|+/-|-\+|+-|-\+|+=|=+)(?![=+-])|(?[?!]|![?!])(?![?!])|[?!=~DN](?![?!=~DN]))

t_comment_COMMENT(t)[source]

[^{}]+

t_LBRACE(t)[source]

{

t_comment_RBRACE(t)[source]

}

t_tag_LINECOMMENT(t)[source]

;[^n]*

t_LINECOMMENT(t)[source]

;[^n]*

t_TAGNAME(t)[source]

[

t_TAGVALUE(t)[source]

]

t_RBRACK(t)[source]

]

t_COMMENT(t)[source]

{

t_RBRACE(t)[source]

}