Skip to main content

PGN grammar for tree-sitter

Project description

tree-sitter-pgn

Overview

Chess Portable Game Notation (PGN) grammar for tree-sitter.

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.3.1.tar.gz (63.3 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.3.1-cp39-abi3-musllinux_1_2_x86_64.whl (94.2 kB view details)

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

tree_sitter_pgn-1.3.1-cp39-abi3-musllinux_1_2_aarch64.whl (90.7 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.3.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (91.6 kB view details)

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

tree_sitter_pgn-1.3.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (94.8 kB view details)

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

tree_sitter_pgn-1.3.1-cp39-abi3-macosx_11_0_arm64.whl (80.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.3.1.tar.gz
  • Upload date:
  • Size: 63.3 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.3.1.tar.gz
Algorithm Hash digest
SHA256 bdfbbfee7dfc8034826db95a99009c45c82257aa668c949783a3c1f23069b475
MD5 27e05ec338580a1b314ed49150158a6b
BLAKE2b-256 663bf95a5bb337ef04be3aab96ff429148e75ab3ad2f06146a4c60d860948156

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d499cc71170aeabfa55ca7210531e52c3d6b2a40a902c8d0c027da01afd17a5d
MD5 c5d3c55c088973953101b17ad2db89a5
BLAKE2b-256 a9f7a47dbd6a7984b5dd9de1033d42f658a8aba2f884d0dbab9fb87c9bcaf834

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d0ccfad696cd309ddf3eeb668ae9ef4eb0d7e1904a619276b0b539301095c50c
MD5 efd82216ceb9aa0be911182fe33029b9
BLAKE2b-256 4856e2f67648f0c66c360ae1bf3508410ed03ad10b657c6ff0353d88edf5cc88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e7b2c23cab46c9bf96a557f9497347858700bcd6ac811d7bf48473ac95f6a32b
MD5 0d42e85d39062709e40c58ffb735279c
BLAKE2b-256 db0bc417d54c2e5d3d8d785fb4ebff9859c203e4836e6508022114c395c94cc9

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.3.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.3.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 c3523e252f54aa35195e97ea4b915be0af67dd5d38e54d4ad050beb7d56ee89c
MD5 270eb44dbb688caaa0ee5a1381ef4467
BLAKE2b-256 4820168049dbca50031bf98c94c7715095ed8911833352c281a561249051b90b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ceb282f973fd1fdbf2e4e0ff86d9ed1075955a4164cf75598ddc1bc29c083ab0
MD5 91a3a011c52d84145c2022749e66b1e0
BLAKE2b-256 9e486203562a5cf53415b74818c6fe251766928ec52edfa0ebc90ce3bc3fd002

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