Skip to main content

PGN grammar for tree-sitter

Project description

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

Project details


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.1.tar.gz (67.2 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.1-cp39-abi3-musllinux_1_2_x86_64.whl (99.6 kB view details)

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

tree_sitter_pgn-1.4.1-cp39-abi3-musllinux_1_2_aarch64.whl (95.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (96.7 kB view details)

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

tree_sitter_pgn-1.4.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (100.2 kB view details)

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

tree_sitter_pgn-1.4.1-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.1.tar.gz.

File metadata

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

File hashes

Hashes for tree_sitter_pgn-1.4.1.tar.gz
Algorithm Hash digest
SHA256 342271986cf64b8006be1de18df00003de25135316edca4cb4070e536e1fdb5a
MD5 11dc163aa197b7c8a65bf0f5e77c3b4d
BLAKE2b-256 19f6fe2de49b5346f22020b14c495774caa8c4870df5b0256289b24bb7bba669

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 40cecad7d834403596809d89bbcb923da945d470b8bcd453d91ad5041dde463f
MD5 d1f3e0214a44fc543760585413ab8778
BLAKE2b-256 dc05676420e7dd2e78112ebdf372b3122782a1b66ff7c9f4b9b215679148a291

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 10e3335d43602e77a796d2eab6892b79e2e0b18fb259bac971659b19534b5907
MD5 e128d1649925a51f0e1ac92a800a02cc
BLAKE2b-256 9c3ab07cce8a61d70ba5f696d37481cf209b5a6fcfb2fffc7ec565caeb0b3158

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e63258286c7b76ce35dd75ecb31e9ae68c9653ab214c55a01c859932d3a94a1b
MD5 58a2d97448707ac8632e7be74b4118aa
BLAKE2b-256 48f30e70a72bd363e93991a0e1f01c414ab146ea583b551b986034ab7d5ed006

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.1-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.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e4cd098959fe0b428d5a1282a77154b018916c5de0ff7e999cc5d978ade51a8c
MD5 a0cb5c9938bf2e0e4ae85b68a6828863
BLAKE2b-256 fc41cb5dc45b29de7936d2ff15121a44acb9e0466e567720b201b975f71b2b6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db17773e7915726e73ae06e7e64a5cc58fd22732cac9b02ff2ba42ec2547dbf2
MD5 4ca22329ca7a1033ba48006b924d18bc
BLAKE2b-256 95d662a06d565ff4f781241aa7fbdf3d7cadfe9b720c0035d5fe48d657014bcd

See more details on using hashes here.

Supported by

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