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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.1.3-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.3-cp38-abi3-musllinux_1_2_aarch64.whl (87.7 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.1.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (145.1 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.1.3-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.3-cp38-abi3-macosx_11_0_arm64.whl (77.7 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.1.3-cp38-abi3-macosx_10_9_x86_64.whl (75.0 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.1.3.tar.gz
  • Upload date:
  • Size: 60.8 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.3.tar.gz
Algorithm Hash digest
SHA256 4fbbfb0caa104ba53af2aff4eb198e88a73b7ad1c459e17624f92dc4c3b217c0
MD5 1a3a19f6679172e4e617e8f1e4700ec7
BLAKE2b-256 932d0a35e1a86f3e1de676de37f94454c1e748cf68ec711ecf64053c17561177

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0b1f123f48e95a840e7026b5929e55a37287f57cdc2a5c1cca933b200a164ead
MD5 a469855e5566918c72cb4052122e3e4a
BLAKE2b-256 ceea5e0ce46d7d968f2dfb71b47e1d2694eb8109011c8266c2e69a1e7da2609f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4f96b4c397d02095c8c9a3df72b74cef8391f1afb20addd75fd79bd9e4a7f1ae
MD5 7799ed910450319414f204ee67751f5d
BLAKE2b-256 2ab2a9b618d5ba72a7acaedbdcc40814906fe831bfe4c6f937cf2da2d8c87a1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d9ce3fe9f6c80fc442ae7036c6a4e4a951935838b48da33fa1bd1e9ccc26b385
MD5 d19529008d24e730adb828c0352d95ba
BLAKE2b-256 d397ab9df2610693da1e59664d3277e6b7d712d92a832a534a55a2efdc6a1db2

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.1.3-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.3-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ed1331daf0cd6ebd177856526705433a4f27c2209c6749cf777584ecc710ae0c
MD5 46cbafed2b1e1ed5eb2f641a58e75e72
BLAKE2b-256 f5d8c64deafca5b96ac49430aae28ebd11b7980c0c29e5f1148a81ad232067a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b31892a1c98e771b86a6c83f5c876271e8eb4ee983690d9707ccb1bfee5f7a04
MD5 410fe746b01b57b775fba62cb61875cd
BLAKE2b-256 ff4f8df300cf58f1528ee0d9fa6491e42962ef97559ccdd1a6c79f2b6f8c5e9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eb1115da7b91c077ca19ffb62d8225c52f787dede8b9996a532054dcdf6ad2c0
MD5 13ec538dd45e512622581e7cdb8eb69e
BLAKE2b-256 777fd32eb4bab7ed1182769d68471935f1665af635e845d68451575132572012

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