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.0.tar.gz (67.1 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.0-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.0-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.0-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.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: tree_sitter_pgn-1.4.0.tar.gz
  • Upload date:
  • Size: 67.1 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.0.tar.gz
Algorithm Hash digest
SHA256 2e73e04f0d6aaecb3fb183f865449e3b4cee31224edb149ccf26293510e22aa7
MD5 d65dbaeb9457a395bb06d805805ba48b
BLAKE2b-256 b1d7d12d8d6154bedac4c50cafb6b8cc41d9e8decbdd212f122c6394ee35ef2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 68da56b2908359e60ab25cae8494d2be174b8088d8d0048944d227f66a39327b
MD5 d403d555709ce37f4a4cd35f351cd955
BLAKE2b-256 1ca61328e4951b3f1100bd336d6ea773deb58bbc5c308e3be001130c6c74520f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2e6983ac92c6ba3b3659724121da7768575b7a8eb544268f639ba76955cbc911
MD5 4c96f1187ec6a466ea42d92fb30bc690
BLAKE2b-256 7db7f855a143801a411aac4a5eef589e8f7b7be199d148dbea091a1b8e6f0704

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b9a4cf3ea109f4acdea65ae55760131bb049a5cf91d9edd94298c237ccc3e516
MD5 757c26739994388a9a2cdc460d695303
BLAKE2b-256 032c4e486caac8625a900a13d1162337aba53d80deb76eb6787bca24846d92d7

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.0-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.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 fdd6fce977acb029a65f4726f570ea2f8eb4669f95afeb3ffc5aaae7f40b333b
MD5 877f0fc63bab6be478b5f0202df4ca0a
BLAKE2b-256 f40ea6b8574761c6b51c59b756df9da388def24e25cc664d22044aadcd9c4887

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d8a6f43924b5e65f7bfd3ff3c8e79657f3b7f01f4da83ec9dee075c194b8dbad
MD5 db56221b20895dbf624fbe0ab92c6fba
BLAKE2b-256 7b9d34a77fa37af1093bc80ec21d9f853ea07988821076368d128a888f39123b

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