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.2.tar.gz (63.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.3.2-cp39-abi3-musllinux_1_2_x86_64.whl (94.3 kB view details)

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

tree_sitter_pgn-1.3.2-cp39-abi3-musllinux_1_2_aarch64.whl (90.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.3.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (91.6 kB view details)

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

tree_sitter_pgn-1.3.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (94.8 kB view details)

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

tree_sitter_pgn-1.3.2-cp39-abi3-macosx_11_0_arm64.whl (80.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.3.2.tar.gz
  • Upload date:
  • Size: 63.2 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.2.tar.gz
Algorithm Hash digest
SHA256 379a3cc7d866016c55d79502c79148a6ac58643af1f7e017b7abca42ca00d1d3
MD5 afd48d0516b602f6a960ee6acf77789c
BLAKE2b-256 e272e0ecdad70b5c5967fe9b5927b051e88c21b64776fd3754cdc81defb9eaef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f8904db379ec363e4165399c75a62d87a181ca728beb90f38bd3278b98fd01b4
MD5 405ca61d82a826a96b87b65a5f674c0f
BLAKE2b-256 3d9e94d5e41526d8e15fc881230dc9fceb1385a8bef4973b456061346fbc1c17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.2-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2c376cc42d50634fd0389b3187b7041485f6a340ed60a51409b3c123a0bba0b4
MD5 32a8368df75f105dd0bf64b0b50b92bc
BLAKE2b-256 45dd4cd0a0b3b1d3d9bb1e497e92eafdde24e6f26fee44adabdda002258c1484

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9435ed5e9f777998d572016a59282588acda7aa0e851f34d0c578a3e54abe163
MD5 142cb32f4cab49d5f633b70924201fe7
BLAKE2b-256 314bbccaaab0ed40d252c167c011ecfb560f35ec75b3357f50f8b21e340113d0

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.3.2-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.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 abd3aa7f01748732acf31001728ec578ca8fbe7849f145b4dad96c973dd1dc81
MD5 637aae1bab43be6741d22658fcb6b733
BLAKE2b-256 64d464392a063d7a1cd72f92f4c8a5c40ab3ef9debbf8d8fea036798c5c30b60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.3.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c6e8747f01868ec3f881a3f7f6d9747faf18cf2b5784e9331b9b5f8b8abc6c5b
MD5 03cb25bf8dd3f518c4a4cda60549aa96
BLAKE2b-256 e0002fb82a9ee3d75db10650ccde98818ef6e39e365b7bcd665ad3d498a004c5

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