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)
    ''')

with open('input_file.pgn', 'rb') as file:
    content = b''.join(file.readlines())
    tree = parser.parse(content)

matches = query.captures(tree.root_node)

merged_nodes = [
    *matches.get('game', []),
    *matches.get('san_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'):
    san = []
    for node in game:
        if node.type == 'san_move' and node.text is not None:
            san.append(node.text.decode().strip())
            continue
    print(' '.join(san))

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.0.14.tar.gz (52.3 kB view details)

Uploaded Source

Built Distributions

tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_x86_64.whl (76.1 kB view details)

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

tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_aarch64.whl (73.5 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (121.2 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (82.9 kB view details)

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

tree_sitter_pgn-1.0.14-cp38-abi3-macosx_11_0_arm64.whl (65.4 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.0.14-cp38-abi3-macosx_10_9_x86_64.whl (63.2 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.0.14.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.5

File hashes

Hashes for tree_sitter_pgn-1.0.14.tar.gz
Algorithm Hash digest
SHA256 5bdca86ab45236df8770935a316956e2720140c885fde582788f3bdb3273b3ca
MD5 59a6b659a2d46bfc6d3d1c58b16524b8
BLAKE2b-256 b102114dfea15e5a75cfa663c1582b668151235ecadf22d88c936e68c9285494

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e1c7d5bbd75bf47ea0d1f8ae5356a63cb367d512a3ee0c1afa9290f32096b878
MD5 e12ad18229138beb0d8e2043430732de
BLAKE2b-256 c39fb646b837c248be61ae5f0e5f7620e602a3122bfdd2730698609a9d97688d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 31ee4db4c0032d9711c5d67f7b80822a19cfd127eb72005f95b33d2eda481a15
MD5 7c25e847f3c53406e530c51789fb2535
BLAKE2b-256 c4310e2fa137e2724e2ac0231f126c4a113c4a33891069329772327b1dd58725

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0ec1c899d2a68a021262e75bd3dc29497bcaf3f60201b1b959715a628376f914
MD5 ad9a2581d7e64c38a414faa2b4d95778
BLAKE2b-256 1f150dc07a5f21b80c5bf13f6ebc38734d3deac700884cb417ef59d4e886cd7e

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 32d4b92ea45a72261c19e317ae38c3ff3daba5b90642d26b2db6e3287d84d0cb
MD5 874730c54634c9a3244dac4464b3e0ca
BLAKE2b-256 5078eb9c91c836a71c8de005174394747dc12cae3dfe8b497cf4bfda7154b260

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 20544324adcbf26694f5df3af392ac233e83efd3f5b979c344b581e76fb5a0a0
MD5 55053f69e0447bb89fd3d96e27a48927
BLAKE2b-256 e5c0ae637616d61bb2aecea24ca0e3acde9a275db99ffa516aaf16cbbe50ebd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d4c7d2d4727831d7fcf07a809451fee6ae0886f82b2621288a2d09f850d5eecf
MD5 863bb011ae80683e12087359297604c7
BLAKE2b-256 e9eed257509591aafb775d5a322f3552aad84da771997d749288f81ef5f56360

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page