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.2.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.2-cp39-abi3-musllinux_1_2_x86_64.whl (99.9 kB view details)

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

tree_sitter_pgn-1.4.2-cp39-abi3-musllinux_1_2_aarch64.whl (96.2 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.4.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (97.1 kB view details)

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

tree_sitter_pgn-1.4.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (100.5 kB view details)

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

tree_sitter_pgn-1.4.2-cp39-abi3-macosx_11_0_arm64.whl (85.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.4.2.tar.gz
  • Upload date:
  • Size: 67.4 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.2.tar.gz
Algorithm Hash digest
SHA256 398b423c5a3394d61bd09e8d5a8e37424c6406965d806cbe82586926a838df47
MD5 8e5396ae3a2999f052c601a689c6d575
BLAKE2b-256 c40efc2ceb669745a216f516a10ec123cd4d1784dc2635d14f0a64618381a344

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ef164d75cd41c2579922a88fbfbd171413371e5e377b844a9f58ea3b71960bdf
MD5 82c8f9bb45ac99c0e615724be384ac2f
BLAKE2b-256 85b7f2453ccbcdb9acbaff9e32aa6467974db598c67c76df5d166a151e35beec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.2-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f51f4dafd809e71ff5e89dd77c6f7a3647d70389e88f3ecbb610a13a3e294926
MD5 368996c3bfd8d1a19997c1790fd49ab7
BLAKE2b-256 4d26832332abb978375e28de32971f1a42ee6166a0845c8558fb7f648dd725fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5f5822cecbf2005d1bb22fe47e2681ed2908cc8b5d1c523fb2b365d153446d71
MD5 43b746fc7ad12eaf5ef3ea85208fd579
BLAKE2b-256 24da085d5dfd297cd9b2d57a72ad7d0d830ed6584bc1032a8d38cce09a169329

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.2-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.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 289fcfa9dd54c94e574dc41ad9658f4bc5df3d047e50c3ed7e5af615d11accef
MD5 7e16cb31be93a96ea4989f7c87165a03
BLAKE2b-256 cbc6bc5b89ebbc4bbc44d55ad2e1600c76416044b38f4d1426e4749e7d8681af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a9934f2111fc39bd397346e3e51c003876b0d70256905e10a8231f50a59d1305
MD5 8234a5e3e590b7f526def840b9c31a7c
BLAKE2b-256 5ab7be72cac4dc2d9e97d835fb1545f8d04c1ef736836cdab47151b99e1eb0c4

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