tree-sitter-pgn
Overview
Chess Portable Game Notation (PGN) grammar for tree-sitter.
The tree-sitter-pgn grammar is extremely lenient, deviating from the PGN specification, where possible, to support common alternate forms, such as
- promotions without the
=symbol - castling written with zeros
- null moves
- nonstandard annotation glyphs
and so on.
Another important deviation from the specification is that input is expected as UTF-8 rather than ISO 8859/1 (Latin1) encoded text.
"Command" sequences such as [%clk 1:55:21] embedded into comments are also recognized
per https://www.enpassant.dk/chess/palview/enhancedpgn.htm .
Moves written in Long Algebraic Notation are supported, on a best-effort basis. Where
a move may be either LAN or SAN it is denoted as a san_move in the parse tree.
The parser does not know the rules of chess, only the notation. Therefore it is possible to represent illegal moves and missing moves.
Much more information about support for various corner cases can be found in the commentary in grammar.js.
Used in
Highlighting Example
Python Example
=
=
# Query for game boundaries and main-line moves.
# Ignore all headers, comments, variations, and result codes.
=
return
# eg: d4 d5 c4 c6 Nc3 Nf6 Nf3 e6 e3 Nbd7 Bd3 dc4
=
=
=
: =
=
continue
continue