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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_x86_64.whl (90.5 kB view details)

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

tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_aarch64.whl (87.6 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (145.0 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (99.7 kB view details)

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

tree_sitter_pgn-1.1.4-cp38-abi3-macosx_11_0_arm64.whl (77.6 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-macosx_10_9_x86_64.whl (74.9 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_pgn-1.1.4.tar.gz
Algorithm Hash digest
SHA256 913e1a3da536a39144da008b6703bb34dd30904b6929aa38def88c3423e85f2b
MD5 c4b85953bdc73b084fb258675b931eff
BLAKE2b-256 60e70218c37a75ba50895b9e2977a8e6b03c3431a6c5c5012ceb1ae48de3e8d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e9c8cb9f9e0a27004ff32dbcfe23a2d7809dd3dd3e5a115c5a1f424e260012e7
MD5 1abf5c77d2fd7279bbda156d16dcbe6d
BLAKE2b-256 37b4faf33cc9c269ad9722abc1fa2379c690e31333337f833cb5456c27aed248

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c9a4131c325d8a7b36e812b2bf2d7633299a9394e5918a0642eea4100eff5fee
MD5 e8ca5c37ba9de5dc41216e75e8b31f43
BLAKE2b-256 3e1d3555207a26c70005e9cfac5155ee6bc027f166789943a161b5ba3729afde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c78cba994fd18afff8fe9b804a667c2747acbbe4c417537cee72148dd29c678a
MD5 edb954da32b949044586f66bdd3257bb
BLAKE2b-256 25fb9f01e27160d96dc132f7a2e86e3f2a604dea0b6873cc1f9833fc6d98ab28

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.1.4-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.1.4-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a390fd2fa8752af3227273b7e4c8acebdadab3b8565e37b8ad38fd9e7c7eb027
MD5 677169b169506f9eea6bb67b6483bd1f
BLAKE2b-256 318cc2b8041273d6eca639252665f9a4edbdce21300e413f866eb02519426fcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dda6c454a2ffc7c4efebfa8df826b037d42438b96299a947aaaade0917154593
MD5 3e5940832fe67799656b391282f306d8
BLAKE2b-256 5c2f7d9001ed9296024a5cdd0fced96c98c43c85f742eede3f6a94240f55bb91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.4-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1d8bf764ec12d476a01782b29d000d774a451707997c62aa3c4814363d8d28ae
MD5 d331b835c91d2b0af06039a449a1dcf2
BLAKE2b-256 4645f9836487fec85e25adb0f221eac1733012471707398d7a5f04f35480c1df

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