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
import tree_sitter_pgn as ts_pgn

PGN_LANGUAGE = Language(ts_pgn.language())
parser = Parser(PGN_LANGUAGE)

query = PGN_LANGUAGE.query(
    '''
    (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())

matches = query.captures(tree.root_node)

merged_nodes = [
    *matches.get('game', []),
    *matches.get('san_move', []),
    *matches.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.0.tar.gz (61.8 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.0-cp38-abi3-musllinux_1_2_x86_64.whl (92.0 kB view details)

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

tree_sitter_pgn-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl (88.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (89.4 kB view details)

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

tree_sitter_pgn-1.2.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (92.8 kB view details)

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

tree_sitter_pgn-1.2.0-cp38-abi3-macosx_11_0_arm64.whl (78.6 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tree_sitter_pgn-1.2.0-cp38-abi3-macosx_10_9_x86_64.whl (76.1 kB view details)

Uploaded CPython 3.8+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.2.0.tar.gz
  • Upload date:
  • Size: 61.8 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.0.tar.gz
Algorithm Hash digest
SHA256 ca8f6a551582a47ad17ce157337b67f5abb6c917a2b3a4e7d935bede325a4f25
MD5 55868d6dcdaa19cdad7580738b64118b
BLAKE2b-256 ce8f04900cb9e3f145932fbd06e3179dad9879031b825fd3eac1a9164b3fc2d9

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 550e5d24ca4cb0b21c1b28255441facb0b8bbf519110d00f05b4c0c0f429e206
MD5 25e3edbe1e7ec9a138ce5f3bd5c191af
BLAKE2b-256 a447d37b046de067652c8581a29b52eac4d111e3b52e42243ee62277a8f6023e

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d069fa97e9ed0ff0114ecf8687660447471c755a57383a33e09a2b340a3d242d
MD5 f1c58b2cc02f81c6faccfc7f258ff6f9
BLAKE2b-256 14d09a57c95fc8c3805476d4ae74de4be3d2cd727c170d371ba305b765dd01d5

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f9acc6b6bf4b5997d9b8544bf70b6aa6839a5debdfb87aa7f208a4ebf958dc17
MD5 41a5c75f626dd8537e1b09fdfa4ca493
BLAKE2b-256 1e9ef4814ea42c3b95d870c3c0151411f5dff09449af1575b6fdb29923543481

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-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.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 6ac229ca5cdde8accea47c78d26aba01ab91b6e8b450a8dd4f76081a895aafe7
MD5 7fab05ed82f9bca3022b667d248f45bf
BLAKE2b-256 8f456e63a6b1e97bbf2d4c203e301f05db138bc7c5b6a95da08c7e13abc78e0b

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da53b497fd32c7843b77fbd155a978ff0d831741fd4a82902cd1b9d037cfc51f
MD5 504da83874e43cfb19985f9e9e388b20
BLAKE2b-256 4e0d3b62b20b15684cdf7c10c40ad6e05a30bec2e83bf995af4721baccb09301

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.0-cp38-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.0-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3719f91db1b10a1a072213646fc89b559612b789ced87b687ed71cf963681134
MD5 d2a1a7a6de8b4f43e10da8a3c161db7a
BLAKE2b-256 cf8f72b0503c36e8904ce6446a3e09be61ebda174ddc015cc5b6cbb484c082cc

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