Skip to main content

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

from tree_sitter import Language, Parser, Query, QueryCursor
import tree_sitter_pgn as ts_pgn

PGN_LANGUAGE = Language(ts_pgn.language())

PARSER = Parser(PGN_LANGUAGE)

# Query for game boundaries and main-line moves.
# Ignore all headers, comments, variations, and result codes.
QUERY = Query(
    PGN_LANGUAGE,
    """
    (series_of_games
      game: (game) @game)

    (game
      (movetext
        san_move: (san_move) @san_move))
    """,
)


def emit_output(main_line: list[bytes | None]) -> None:
    if not main_line:
        return
    # eg: d4 d5 c4 c6 Nc3 Nf6 Nf3 e6 e3 Nbd7 Bd3 dc4
    print(' '.join(x.decode().strip() for x in main_line if x is not None))


def main() -> None:
    with open('multi_game.pgn', 'rb') as file:
        tree = PARSER.parse(file.read())

    query_cursor = QueryCursor(QUERY)
    matches = query_cursor.matches(tree.root_node)

    main_line: list[bytes | None] = []
    for item in matches:
        if item[1].get('game'):
            emit_output(main_line)
            main_line = []
            continue
        if nodes := item[1].get('san_move'):
            main_line.append(nodes[0].text)
            continue

    emit_output(main_line)


if __name__ == '__main__':
    main()

References

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_pgn-1.4.4.tar.gz (67.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl (99.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl (96.1 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (97.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (100.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl (85.1 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file tree_sitter_pgn-1.4.4.tar.gz.

File metadata

  • Download URL: tree_sitter_pgn-1.4.4.tar.gz
  • Upload date:
  • Size: 67.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for tree_sitter_pgn-1.4.4.tar.gz
Algorithm Hash digest
SHA256 5da67f18ceb3558d097a271f45520db3ef07d0a8234ba749156a7f31fcaadd54
MD5 a72312c8380c15f2c7c758f960e0c46c
BLAKE2b-256 a8e58d729f8f9083ad2f79ea56d7a76bd0085d51a31cb089e68b0e962727ae41

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eed5431e337fdfd99532a752e35fa19a62e6196001f085a169346800291f0f08
MD5 814d86af00ad31de76b2290fa2ff970c
BLAKE2b-256 3de5be8376dbd36681ccc676dd2ea91f7fe54a55c8d2f92e75d941416dab77f2

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 abb9d02e56c8000a02e9af403f195be620f89007bff9b00e7d2223f59963ecd8
MD5 608ce38ef55a3d188aacc4ae92c6c038
BLAKE2b-256 c18219513298b0fb313a53b512b874a1528feff637191edc0358cda0f7f6c681

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cc16ac9e2274d42edd3b06afd8896547b846a568cf562c139e06605c608142b3
MD5 716e89a5a15cac42c988b4708366da14
BLAKE2b-256 01f6d096aae847ab169d6b8d280b90260f35d6efc2952f1bf39116bfae0d5f96

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 aef0640319eeaea008591f79dae44fbdd4844b5c52aea7b1ecd2d77b70dc8742
MD5 5d9e1c2bd613a9d0830c1c270994a67f
BLAKE2b-256 75e0b627483389c443e67018cd6d8d1322f41fd3e9715e6c30108b13f14ec667

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7376d568d5eb20a785e07adebdd091dbca0ee2cdd533c2eeafa54e983823059c
MD5 4556a5c89d3e3c922661b4713e70fe1c
BLAKE2b-256 aa72e561c29c3c47b5a04cb6bec41b84436bf1092c1d0015141163a391682ae8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.4.4 This release

6 files

1.4.3

6 files

1.4.2

6 files

1.4.1

6 files

1.4.0

6 files

1.3.2

6 files

1.3.1

6 files

1.3.0

6 files

1.2.19

6 files

1.2.18

6 files

1.2.17

4 files

1.2.16

4 files

1.2.12

7 files

1.2.11

7 files

1.2.10

7 files

1.2.8

7 files

1.2.0

7 files

1.1.4

7 files

1.1.3

7 files

1.1.1

7 files

1.1.0

7 files

1.0.16

7 files

1.0.14

7 files

1.0.13

7 files

1.0.12

7 files

1.0.6

7 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page