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

    (game
      (movetext
        lan_move: (lan_move) @lan_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
        # some move notation is eagerly classified as LAN, an oddity to keep in mind
        if nodes := item[1].get('lan_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.2.19.tar.gz (62.4 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.19-cp39-abi3-musllinux_1_2_x86_64.whl (93.1 kB view details)

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

tree_sitter_pgn-1.2.19-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.2.19-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (90.6 kB view details)

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

tree_sitter_pgn-1.2.19-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (93.7 kB view details)

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

tree_sitter_pgn-1.2.19-cp39-abi3-macosx_11_0_arm64.whl (79.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.2.19.tar.gz
  • Upload date:
  • Size: 62.4 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.19.tar.gz
Algorithm Hash digest
SHA256 7f06bc0c7e6881c7498bb8adff9747f30814623ccfa0856dcf77c89b09286fcf
MD5 2aa69f2b2b11b0d24817dbb4cb2cff5b
BLAKE2b-256 55da22fe576543f742e768a291fb8c0ed490627caaaa955fec527030921b4848

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.19-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6620174a58f055ece8ecff20a80e582da245e555c6eb8dfc2d5f038620be44cc
MD5 144fbbd20ce04a1ee1e8ce00f9636483
BLAKE2b-256 b198378e0be55123c6db291d8db31b4dc9f5f9dbae11ae1d0d0b708a1fb7664f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.19-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bfd840ca3d6bcbaa1b3ee1d43943ac45c6d5c7cf29c340a0e4077c2a9d0f4532
MD5 416f5e5f52e5ee3ad7e6bff011ad30fb
BLAKE2b-256 c347c5a9b7289667dd864f11709a663491f696c0aa939594e7edf3641c6d19e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.19-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 afca0955d051981251ce99386a5dca36e6b56f3028eef0baae688f21896aae54
MD5 7461e37ca2fe8c8dcb46e53c3e6991a8
BLAKE2b-256 e6ab7c873c0638ab041d2ca95fea078ebdc602adef5dc9ff7119ffb099aa3b17

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.2.19-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.19-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0127f02a6ec4aa32b3d8580b09c3911062b889fa34de1cf67037606230c50532
MD5 d8cfa26717b43d4c7551088fceceaca7
BLAKE2b-256 d08a5403e552e85ee7a0d04ad418443c85f8cc9a8bf76ea2d8c7dc3938dfd115

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.2.19-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7dfca6645fe96a4c4acad7ee5755422f6c08b3262417136958b3cc5d17eb6cdf
MD5 e5ceeffc5c9dc8710e99bcaaf44ea9f6
BLAKE2b-256 af3d4e1add010718d731c9912d4e2dbcad2f3b9e4b6d8d5c8100dff9b852ee5b

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