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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_x86_64.whl (77.1 kB view details)

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

tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_aarch64.whl (74.7 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.0.16-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (122.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.0.16-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (83.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.16-cp38-abi3-macosx_11_0_arm64.whl (65.7 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.0.16-cp38-abi3-macosx_10_9_x86_64.whl (63.5 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.0.16.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.16.tar.gz
Algorithm Hash digest
SHA256 20ed33ec753ae06a4ea6b069baa0017835da979d2c43d3ef6e549eae4fdad51b
MD5 f148de0c07d4f37d76f3d3e619b5cab3
BLAKE2b-256 05280fdee8517fedf0e8165ce321d7c27a9f88660f9e88530b3b0c1aa75f3746

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b2bc562760b4d24c846990c3b1bb718f5093ad51355fd9fe33fc7ce233d1d263
MD5 5463b350f3952aa8a496f5f34dfc60fb
BLAKE2b-256 92b06dff28a93ebbae1bb5c0a50fcf6f6fbde45a3db6b3cfce83c1fb9822f437

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 983f3b5341b5cb01bf3db64c1cdea492b0f9af1a45ff04490c8fc87892554dd9
MD5 c23379850f804203c6e39e8b78e283f2
BLAKE2b-256 5f0b263f9047c31474d8b735db91baccefae22c13fd7b445b5bc5ee98ae7397f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eb81218933c06a5796f72b6a8d60848b53e47ebaaaf3402e4aa0e880c8348054
MD5 bf93ffd08c80a8a0fa3bf2e6aed4c3b5
BLAKE2b-256 178a5a51569be8bec6d2be3800f61ded665dae658396d3cea67553e178fb7e0d

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.0.16-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.16-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5c08e31c810f900ffe62e208fcc2d1756057ef06bcb86710be20402d2b8602ec
MD5 0564f6945ae881553bdb0f574e54556c
BLAKE2b-256 75537cea60ba163c146752332b7e843d6428f3b0a1d6c5cbcde1a748d3b28d52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bf182ba220c924a94e9d92889d11d019fc974c12617eca5d3acbbe3d5e5cdb2
MD5 a9b8e438354c3fd83ca28b5e8d9d6785
BLAKE2b-256 5e0bfda41746339742d541735360457f76dec70d738b3f7ee7f7ccd0b7126712

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c0a4385f715e92ec7e78a6560bbdbf69a833c00346cd53605dc40d71ee71c4af
MD5 8bb29ca31a08195d88db7079da6ba2c5
BLAKE2b-256 e81f8392a441f865829dff830689a9966b7a434f03076c4e66e7d9d3c43323f9

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