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

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 for game boundaries and main-line moves.
# Ignore all headers, comments, variations, and result codes.
QUERY = Query(
    PGN_LANGUAGE,
    """
    (series_of_games
      game: (game) @game)

    (game
      (movetext
        san_move: (san_move) @san_move))
    """,
)


def emit_output(main_line: list[bytes | None]) -> None:
    if not main_line:
        return
    # eg: d4 d5 c4 c6 Nc3 Nf6 Nf3 e6 e3 Nbd7 Bd3 dc4
    print(' '.join(x.decode().strip() for x in main_line if x is not None))


def main() -> None:
    with open('multi_game.pgn', 'rb') as file:
        tree = PARSER.parse(file.read())

    query_cursor = QueryCursor(QUERY)
    matches = query_cursor.matches(tree.root_node)

    main_line: list[bytes | None] = []
    for item in matches:
        if item[1].get('game'):
            emit_output(main_line)
            main_line = []
            continue
        if nodes := item[1].get('san_move'):
            main_line.append(nodes[0].text)
            continue

    emit_output(main_line)


if __name__ == '__main__':
    main()

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.3.0.tar.gz (62.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.3.0-cp39-abi3-musllinux_1_2_x86_64.whl (93.2 kB view details)

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

tree_sitter_pgn-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl (89.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.3.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (90.7 kB view details)

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

tree_sitter_pgn-1.3.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (93.8 kB view details)

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

tree_sitter_pgn-1.3.0-cp39-abi3-macosx_11_0_arm64.whl (79.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_pgn-1.3.0.tar.gz
Algorithm Hash digest
SHA256 97d288dec3449a83a21e31ac8b5f3523bcb6bf5b9fea134c0374ab8aef5acf40
MD5 de86b0b90321978d97e6a4b68e49a19f
BLAKE2b-256 cd281d18aea98b87e59b384ff1e9f5fcfdfabf25516811e5912bba05d870108a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dbdd8da5cf33a16d8a91aa4a32a34dd5e465fd0fcc43e1b12a108992090248bd
MD5 5a86e7095c7434bd0f5b03d131e59315
BLAKE2b-256 8e672eab13618063ce8425e8537a67072ab800a77f49065fd105fa26d6183dfd

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0c34a418d2a1e005b02f610d943cc1faab3aeaef509ca361a412da944f5962e8
MD5 5dd7af106a3becc9e77d55e452fddc19
BLAKE2b-256 8b49c07f77744f5371453b58e4530dc5d2e2434baf7e9d299a287ef278d26f4f

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.3.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c7c6a731a95272ed0dc5734f001150c3ba5c5b48f8a097d8d1787c49e0888239
MD5 9e4f22deb25552a4cb5fed279bf2b647
BLAKE2b-256 477463180503cccdd6c8873889496884854176c369a28abb22fecc04448912bc

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.3.0-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.3.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 5010be7b8d7428e64ffa49a2b6f6044de7cb4754380ae9220fb70ad7498008c8
MD5 17059cb437fff5042e81428329ce1bd1
BLAKE2b-256 738c571c78e57cc203ec6eb7b6e3e15013e37877993545e721925dceefe6f560

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0869b5510367bdeca9e6a093b8e4438bf334ec9d7d08a922699905eb1414e443
MD5 04f58dc10966b80d67aba884bd693712
BLAKE2b-256 605ef968b3973ff5866730653e77d1045733129f121dc27772bddf208823154d

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