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.16.tar.gz (62.2 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.16-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.16-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (93.5 kB view details)

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

tree_sitter_pgn-1.2.16-cp39-abi3-macosx_11_0_arm64.whl (79.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.2.16.tar.gz
  • Upload date:
  • Size: 62.2 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.16.tar.gz
Algorithm Hash digest
SHA256 ce41200a9078835cab4d35109d49e6a8ec6815ec538fa412ddc993e6590eda86
MD5 05fe7f36b1ee56ddd16b92219ecee0f4
BLAKE2b-256 64e63fc7f4c6e48d16f26a30a8b3edf8007843603a5a8b819bfc9bbc453765e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.16-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 51d57f7bd4e6b6bd0bfb48b61988ea369394c02988b2eab02207fe950565fbe3
MD5 678bb03d107b2279329caefcbc94fd4c
BLAKE2b-256 d50d48c13b6fafacb65164c4a9ef34056bcb83f8ce5acb7f07624c2257123c4d

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.16-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.16-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 1fa25cbf4403440843f776d9db51327c0ec9f7d5370ac240c9621eab761475db
MD5 b5b741c2ac23c0aca61fd5d624b91926
BLAKE2b-256 39f1a141717ae7e4059fa17c4b032d8489b55cac48251013ef88753998d5bb2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.16-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 83d6cd502c527515e4572c2a3682c036a564315a9b4bccf49dd46af6db9c977d
MD5 be9af3d018f0bd5c77b1ccf3dbbc0b7d
BLAKE2b-256 27fee1086150b418790863e4fb8fecf422dce6b713cdbe465660aef1ef5f4213

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