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

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_pgn-1.2.8-cp38-abi3-musllinux_1_2_x86_64.whl (92.0 kB view details)

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

tree_sitter_pgn-1.2.8-cp38-abi3-musllinux_1_2_aarch64.whl (88.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.2.8-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (89.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_pgn-1.2.8-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (92.8 kB view details)

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

tree_sitter_pgn-1.2.8-cp38-abi3-macosx_11_0_arm64.whl (78.6 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tree_sitter_pgn-1.2.8-cp38-abi3-macosx_10_9_x86_64.whl (76.1 kB view details)

Uploaded CPython 3.8+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.2.8.tar.gz
  • Upload date:
  • Size: 61.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tree_sitter_pgn-1.2.8.tar.gz
Algorithm Hash digest
SHA256 b02ed028b331c032f18496392fefcdcf086a4f4c59064af50c6101b10849e948
MD5 f124270d46ccf89e2ec44081c268fa0f
BLAKE2b-256 fce58f52017164b3b64b768b3fee715359200d0939af5491ff2c37e24310dd7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3d3ad80eaf0610e3b8b5cde08954269acc6177642886da7f7c66c994114eaf12
MD5 b12c07760554cc1a985301a337adc4fe
BLAKE2b-256 f006961583c0f1a02f3a38b21ff65f418e627b40f57904ca12fe73373b4713ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d59bd0d411053eb2e65a12dc16d3d0083c58af57b722630c06fb4ff1dc601da7
MD5 9e94277170a1c99885d6b1f4dcfab5ba
BLAKE2b-256 1084db0fed9a497ddb8e437671627c06c51445fa04a74c838147067870e856ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f52edfb2a97839c28bbcc7bd33d81c63ec8f2421dfaf5306969507e92f031ab3
MD5 ec333678f2611adb2c85585adb40b457
BLAKE2b-256 7a2effdc2b75a4c7f86062c77a869000095bbf5c2f4ccf421c75532f9b634704

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.8-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 846da182d6b16b3576ca8fa34c680d10216a9a2e99616dbc9a757de5299ee5c7
MD5 e583d2984c20bba841d34ef1a45c6a4e
BLAKE2b-256 f061478dc2f729b3c1a13c8bbaec462f28bf184adff959190e226ce6f6de9b43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 595f598a055856ad5cca69ac185154418b0f731d7bc5f7ee4c47fcba73d219f1
MD5 7cd4438bc69d9ee55b64e7e535d9cf8f
BLAKE2b-256 0481855accbe6b71d4c2f3f8c364ebdd579eefc63b4d20a548774b0827b897f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.8-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 03f2c0613efeb0902395dac6a399ed534296772875191be5b8245b53a6b182db
MD5 e5467dc1eb8e8f37dab678f97f20c415
BLAKE2b-256 5d5271f7958ee2542b666e48e4e363fe5cd4ca0dbf0fc7114a75d076ba50a2ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page