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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.1.1-cp38-abi3-musllinux_1_2_x86_64.whl (91.6 kB view details)

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

tree_sitter_pgn-1.1.1-cp38-abi3-musllinux_1_2_aarch64.whl (88.5 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (146.0 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.1.1-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (100.8 kB view details)

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

tree_sitter_pgn-1.1.1-cp38-abi3-macosx_11_0_arm64.whl (78.3 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.1.1-cp38-abi3-macosx_10_9_x86_64.whl (75.9 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.1.1.tar.gz
  • Upload date:
  • Size: 62.1 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.1.tar.gz
Algorithm Hash digest
SHA256 f3215b613e16b7442c54ee4cce1d39e4b27a3c22063867e4ecd46dcd66f30ada
MD5 15d885227e7d61796f1c0f78decd5ea5
BLAKE2b-256 74a6462d939bdf1349c60d4136ea0e0d9ad996dbe2c10db628f348f94db3cf25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 af2db843bf46d75649e3adaa7995ad261d5e6b798800cca5f93e5df00b2fd1d3
MD5 9ce1b73ae8f92323c2855ef83825cf11
BLAKE2b-256 9c60f604b035baf681f22baec9cdfbd96781a0ed0b2cbc1f71b8312ebe889608

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 7bbc604c60d70f9ce4cd0b337829df8a63f131b45fac9f56f027175f7fedff94
MD5 8919d501830b8d7ad47d62d0dcebce06
BLAKE2b-256 de0e7101ec3cee73c14fa6b9e3d327883308a99f4c05604284fb2494944f14c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fbe648476d98b6828b090443d9395646289dcb9db480568fb0c80589a0c05e03
MD5 f7524c99afe6525d39a4a4d9ffa1299f
BLAKE2b-256 0638454971a39325165b436b3d48e4c3617c0bf9a55cd80270dfd95c21772810

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.1.1-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.1-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2afa2dd8a2f8ecc14d51bb41dc29e1c4dcb92d7d9348dc86ffb3f14c0bd83c91
MD5 de13e7a84769182452752491134996dc
BLAKE2b-256 bdd06bec7410128ff27f507cb886973cb55045dee98e820c0032275295a7630b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b75ea557ab59b6021e7a73bdb0126585e5a6344ad55d79506b1eaf70aefbf5e9
MD5 24613ad74bbae235a295a83cbe485840
BLAKE2b-256 0ac5c33cc92f928e75d62f5a8a05daa69cfb124972adb0070660d4d23207af7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.1-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 339df03c3159c0cb5ef96081d37a01ef3f0518456f29c0282dc47f2030823cf1
MD5 add2ba2517c3792863da477f3a85a2c7
BLAKE2b-256 190a7bfcd9fb5e9a571a3adb857d803a30ecf42ed32899a87ab168fbe4d8dca2

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