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)
    ''')

with open('input_file.pgn', 'rb') as file:
    content = b''.join(file.readlines())
    tree = parser.parse(content)

matches = query.captures(tree.root_node)

merged_nodes = [
    *matches.get('game', []),
    *matches.get('san_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'):
    san = []
    for node in game:
        if node.type == 'san_move' and node.text is not None:
            san.append(node.text.decode().strip())
            continue
    print(' '.join(san))

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.0.13.tar.gz (44.6 kB view hashes)

Uploaded Source

Built Distributions

tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_x86_64.whl (61.4 kB view hashes)

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

tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_aarch64.whl (59.6 kB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (96.9 kB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.8 kB view hashes)

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

tree_sitter_pgn-1.0.13-cp38-abi3-macosx_11_0_arm64.whl (51.5 kB view hashes)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.0.13-cp38-abi3-macosx_10_9_x86_64.whl (49.8 kB view hashes)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

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