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, Query, QueryCursor
import tree_sitter_pgn as ts_pgn

PGN_LANGUAGE = Language(ts_pgn.language())
parser = Parser(PGN_LANGUAGE)

query = Query(
    PGN_LANGUAGE,
    """
    (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())

query_cursor = QueryCursor(query)
captures = query_cursor.captures(tree.root_node)

merged_nodes = [
    *captures.get('game', []),
    *captures.get('san_move', []),
    *captures.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.17.tar.gz (62.3 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.17-cp39-abi3-musllinux_1_2_x86_64.whl (92.9 kB view details)

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

tree_sitter_pgn-1.2.17-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (93.6 kB view details)

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

tree_sitter_pgn-1.2.17-cp39-abi3-macosx_11_0_arm64.whl (79.3 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.2.17.tar.gz
  • Upload date:
  • Size: 62.3 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.17.tar.gz
Algorithm Hash digest
SHA256 b4278279f8e9e2764971ed51de589f38710e05773baf182b82fcf411f9aadbd2
MD5 383289296a7e8125705e0bd9c41a1531
BLAKE2b-256 f956e9b82ef89917d88a853e2d1143c12b57273e125fcb6095d30976239116cd

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.17-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.17-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ddc4d4d6dad5e3a38e3a62d9cf188140de4eb2b1e35eb923620c2c77dbe10446
MD5 06a2d45f3830f41c5855f61b020e397b
BLAKE2b-256 29c725c7ca0d86c5719e05d8cb9ad0344dbe0d4abdbd45a60f8e1b3a63b4b166

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.17-cp39-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.17-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 20fa3f082d65b969c6520d4d30e261dbc0e3b765d0e04926223ffaf7a0a6880c
MD5 587a0823b214bb84df37b5895b1b3460
BLAKE2b-256 a13b216f57899b2c2d1b108de1d92cdb38731047c3ed0ab7c385833bf45e21eb

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.17-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.17-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3987979b823738a9b9a05ffe02e6811021ebb3002d9be3aa377b6fe82e42d90b
MD5 5bf7205e880d49ecb76f9b40e9404150
BLAKE2b-256 674385e11c532de4c0cbf55bb8d48049699235f267998480e2c430333bfdb3f3

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