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

Uploaded Source

Built Distributions

tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_x86_64.whl (90.4 kB view details)

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

tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_aarch64.whl (87.4 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (144.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.1.0-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (99.4 kB view details)

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

tree_sitter_pgn-1.1.0-cp38-abi3-macosx_11_0_arm64.whl (77.4 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.1.0-cp38-abi3-macosx_10_9_x86_64.whl (74.8 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.1.0.tar.gz
  • Upload date:
  • Size: 61.2 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.0.tar.gz
Algorithm Hash digest
SHA256 52fe4b5e63a37a55e0a9940dfcfc4b3a1bbb017985bb58c98c10e4f9af34fb39
MD5 e05b0d38d574d472e31ee07e39586484
BLAKE2b-256 3804088f19861cd9625e6a09788aa0a3c285a6f2feca8cdf5d256cccc8e5b1e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f88197e39c78bc4d7f9fd40fec7eb4b19dde33cf47476e93e9329c5d64db8eb1
MD5 2129f9165763c0cdac5d02602192819f
BLAKE2b-256 73dc1ab93bea6e2541a608d1ef47bd81c3cc53061399f9d56b6c64137a75be47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 889549ecb3cda3c30bc0464dd7dbd54b7660c2285255697f1c08e039f1c45e0f
MD5 d3a7ec973712fdc9d954850b74fae018
BLAKE2b-256 89c6a28f378cca26aebbf26b0c1b713de375bc35668d00323fc1d5922ef7a944

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6014e5b69a08d1071c8ff15df3122bb0de9ac46e72fded5fcf50113d8d37c6fe
MD5 b48515adef23e75ab81bc129586d8d65
BLAKE2b-256 c4440690d4daa6ca18f1e305b1a962b3d03630e9c28258b5cd6f7779ecc25ffc

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.1.0-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.0-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06c0272d3b77ff7784181ac6b34b611fc73abc23d1c342ed17429f67918310bb
MD5 19ba3ae95fcf1f93c90867e2f23df72c
BLAKE2b-256 b804abd2c281bb1ba2138be8023ad48694dbc2bf9440ccffd836488423490dcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9331b0733107a9d29b66ac3d55ba6753c0d2ff4f5fd0329a621f9632d7401f1a
MD5 32ebf5bff134067168ddc2d5bce6f0a6
BLAKE2b-256 6a6248bcb53544a93ca55e178b2cc0121f274b393c9599ade312bab5d308fd37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fcb0acbb56b25c54905ef7018f31020bc1b8a1deeb3fc9015c28978af70561fb
MD5 0607b52e73a558b79e65e2dd750ef92d
BLAKE2b-256 623fb508416634008b95c3d2cbf577f2f47902517e11370fb1237ffb867c5759

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