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

Metadata

Release files for tree-sitter-pgn 1.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tree-sitter-pgn 1.4.4
File Size Uploaded
tree_sitter_pgn-1.4.4.tar.gz 67.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for tree-sitter-pgn 1.4.4
File
tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details
tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details
tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.9 abi3 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details

Total release size: 545.8 kB

Release files / tree_sitter_pgn-1.4.4.tar.gz

Download URL tree_sitter_pgn-1.4.4.tar.gz
Size 67.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5da67f18ceb3558d097a271f45520db3ef07d0a8234ba749156a7f31fcaadd54
BLAKE2b-256 checksum
How to use checksums
a8e58d729f8f9083ad2f79ea56d7a76bd0085d51a31cb089e68b0e962727ae41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release files / tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_x86_64.whl
Size 99.8 kB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
eed5431e337fdfd99532a752e35fa19a62e6196001f085a169346800291f0f08
BLAKE2b-256 checksum
How to use checksums
3de5be8376dbd36681ccc676dd2ea91f7fe54a55c8d2f92e75d941416dab77f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release files / tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL tree_sitter_pgn-1.4.4-cp39-abi3-musllinux_1_2_aarch64.whl
Size 96.1 kB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
abb9d02e56c8000a02e9af403f195be620f89007bff9b00e7d2223f59963ecd8
BLAKE2b-256 checksum
How to use checksums
c18219513298b0fb313a53b512b874a1528feff637191edc0358cda0f7f6c681
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release files / tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL tree_sitter_pgn-1.4.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 97.0 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
cc16ac9e2274d42edd3b06afd8896547b846a568cf562c139e06605c608142b3
BLAKE2b-256 checksum
How to use checksums
01f6d096aae847ab169d6b8d280b90260f35d6efc2952f1bf39116bfae0d5f96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release files / tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL tree_sitter_pgn-1.4.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 100.4 kB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 abi3
SHA-256 checksum
How to use checksums
aef0640319eeaea008591f79dae44fbdd4844b5c52aea7b1ecd2d77b70dc8742
BLAKE2b-256 checksum
How to use checksums
75e0b627483389c443e67018cd6d8d1322f41fd3e9715e6c30108b13f14ec667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release files / tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl

Download URL tree_sitter_pgn-1.4.4-cp39-abi3-macosx_11_0_arm64.whl
Size 85.1 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7376d568d5eb20a785e07adebdd091dbca0ee2cdd533c2eeafa54e983823059c
BLAKE2b-256 checksum
How to use checksums
aa72e561c29c3c47b5a04cb6bec41b84436bf1092c1d0015141163a391682ae8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.13

Release history Release notifications | RSS feed

This release

1.4.4 This release

6 release files

1.4.3

6 release files

1.4.2

6 release files

1.4.1

6 release files

1.4.0

6 release files

1.3.2

6 release files

1.3.1

6 release files

1.3.0

6 release files

1.2.19

6 release files

1.2.18

6 release files

1.2.17

4 release files

1.2.16

4 release files

1.2.12

7 release files

1.2.11

7 release files

1.2.10

7 release files

1.2.8

7 release files

1.2.0

7 release files

1.1.4

7 release files

1.1.3

7 release files

1.1.1

7 release files

1.1.0

7 release files

1.0.6

7 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page