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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_x86_64.whl (61.4 kB view details)

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

tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_aarch64.whl (59.6 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (96.9 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.8 kB view details)

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

tree_sitter_pgn-1.0.13-cp38-abi3-macosx_11_0_arm64.whl (51.5 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-macosx_10_9_x86_64.whl (49.8 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.0.13.tar.gz
  • Upload date:
  • Size: 44.6 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.13.tar.gz
Algorithm Hash digest
SHA256 c080d1681bee17a278d3ad75524560a116b75fbe98f4397343c93044e148a07a
MD5 f5acbba02b3eea07c5d74470f84637c2
BLAKE2b-256 188f0ea4cb9514bd34da9b989d11527ae3381f56e9b6a3cb7b47c3566b858386

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 908da5669c312782ebda7801c15e75ba338bd05bbf83e53c06467c4d663b7181
MD5 92208031df12b5609a297fe1075918dd
BLAKE2b-256 e190386213a915bceba63207285a3c310545af16d61e44178506c084ae3f6c19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 92216c9ac34fb1a2e1d2567c9ba1ccce9398871ac42c6f6bdc931bc7556466d0
MD5 10bb08385595c5b4f4932131f6fbae23
BLAKE2b-256 9276bd234f4e3d5ed842058d0201e900fff51d84e56ed9fa46385751dcc32cab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fb929e18301b8df8dac58ae4ddf2f241cb3993aba3dfc8b1de5fde7c625f8d9f
MD5 e7d6939c31aac91863e46202956457c4
BLAKE2b-256 d2dde6e1d9fa1b7714b21bc1c1bd5f42eeffbd85c2d2cb0500f1887848c96151

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.0.13-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.13-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68efc88b79157b907dbd28c1105c441e5546d8bbf3398178ea10177591497f07
MD5 cbe92d683fca3585081ca4c03a7bc116
BLAKE2b-256 7e4d05d07a107eedc0e9cc28a5fa48371d01f09d73ec2e77476c8c418ae47865

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 594ea5f9c8b6f68fda664ae303793c88d93cef5aaa2aa06697a4878bf36ca4b3
MD5 e6ca6ca535e4a0451c70b2908898139c
BLAKE2b-256 e46b81625db4ff3c52ac2edfea9a05189b79436044936facc5693d410296d01b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 de6b644be926aba4a87901a2bfa8b692c5891596a59d3d6e0a3a66095ad24d43
MD5 f0c7755081d50472ce9f5f603fa6907e
BLAKE2b-256 3d1332b30523baa42ee4008f4f29fff1450733a9dae63625a1744cd53f165259

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