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

import more_itertools
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 = Query(
    PGN_LANGUAGE,
    """
    (series_of_games
      game: (game) @game)

    (movetext
      san_move: (san_move) @san_move)

    (movetext
      lan_move: (lan_move) @lan_move)
    """,
)

with open('input_file.pgn', 'rb') as file:
    tree = parser.parse(file.read())

query_cursor = QueryCursor(query)
captures = query_cursor.captures(tree.root_node)

merged_nodes = [
    *captures.get('game', []),
    *captures.get('san_move', []),
    *captures.get('lan_move', []),
]
merged_nodes = sorted(merged_nodes, key=lambda elt: elt.start_byte)

for game in more_itertools.split_before(merged_nodes, lambda node: node.type == 'game'):
    main_line = []
    for node in game:
        if node.type in ['san_move', 'lan_move'] and node.text is not None:
            main_line.append(node.text.decode().strip())
            continue
    print(' '.join(main_line))

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.2.18.tar.gz (62.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.2.18-cp39-abi3-musllinux_1_2_x86_64.whl (92.9 kB view details)

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

tree_sitter_pgn-1.2.18-cp39-abi3-musllinux_1_2_aarch64.whl (89.6 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.2.18-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (90.4 kB view details)

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

tree_sitter_pgn-1.2.18-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (93.6 kB view details)

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

tree_sitter_pgn-1.2.18-cp39-abi3-macosx_11_0_arm64.whl (79.3 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_pgn-1.2.18.tar.gz
Algorithm Hash digest
SHA256 61e0291c74abaa753b09a130f137d19bb79fb4c1882b080240a37afbbccf5988
MD5 d343b4b3aac3aa3f810eae4ed1af663b
BLAKE2b-256 7c477a66f6a111a320d063ab1d11b8bb23f3839a511d5290ae2bdc399ddc8261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.18-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8b9fb1091530fa627554f071b288a29ba4499fb8054a8f0902b0952cfc0cca3d
MD5 1eccb4b5abfafa75500b58a83bf77a6b
BLAKE2b-256 43cd7b19dc8c061917864f8b5346d4e01e83af793ac060d704dd7ad22abeaab3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.18-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b98a0005569e88907ddc716f51ea9a2d141e6ba3219c27d0b221f89485340d35
MD5 6b809e568e0d979f5bf38254b9a4bb94
BLAKE2b-256 17d79a5391e30e67c6a212ea6a24fb7179b9af991389906e5201efaac2b2832c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.18-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ac9e386cf61a463ee949bf0d60cf6198f27307da3a90916e04cd8de52ee23a21
MD5 1789a436d4b0aff906590c23f6dea56d
BLAKE2b-256 95be1e7de80c216c6a756358de4061c48db1a2cb4a2dd7abe60608c5e275d8e3

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.18-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.2.18-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 90f378417d7373858f957ee6d7acdb66783137246a12ee8b92776aaccbc102d7
MD5 dcd2a49a2493b82f46cc0f11b6af9fec
BLAKE2b-256 30878615734b48d1c044f557d8dfa4c54d43265fb16e57a7f5adce0c008caa00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.18-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 530b230341174bde08228b007a6fc3466a7b5803dc7388ee1ba9add8a7cd7021
MD5 f1a5261be7c0d31384f5c4bf04122d8c
BLAKE2b-256 02c9e93af5fbd07b37e375c66e46c3ad6b688af585dafcaaf582a1d95710731d

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