Skip to main content

PGN grammar for tree-sitter

Project description

tree-sitter-pgn

Overview

Chess Portable Game Notation (PGN) grammar for tree-sitter.

The tree-sitter-pgn grammar is extremely lenient, deviating from the PGN specification, where possible, to support common alternate forms, such as

  • promotions without the = symbol
  • castling written with zeros
  • null moves
  • nonstandard annotation glyphs

and so on.

Another important deviation from the specification is that input is expected as UTF-8 rather than ISO 8859/1 (Latin1) encoded text.

"Command" sequences such as [%clk 1:55:21] embedded into comments are also recognized per https://www.enpassant.dk/chess/palview/enhancedpgn.htm .

Moves written in Long Algebraic Notation are supported, on a best-effort basis. Where a move may be either LAN or SAN it is denoted as a san_move in the parse tree.

The parser does not know the rules of chess, only the notation. Therefore it is possible to represent illegal moves and missing moves.

Much more information about support for various corner cases can be found in the commentary in grammar.js.

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.4.3.tar.gz (67.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.4.3-cp39-abi3-musllinux_1_2_x86_64.whl (99.8 kB view details)

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

tree_sitter_pgn-1.4.3-cp39-abi3-musllinux_1_2_aarch64.whl (96.1 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.4.3-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (97.0 kB view details)

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

tree_sitter_pgn-1.4.3-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (100.4 kB view details)

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

tree_sitter_pgn-1.4.3-cp39-abi3-macosx_11_0_arm64.whl (85.1 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_pgn-1.4.3.tar.gz
  • Upload date:
  • Size: 67.4 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.4.3.tar.gz
Algorithm Hash digest
SHA256 9eaae52d16090add01ff5a6512bfc7c3d993d38725c7a4bd3bdba491e1131116
MD5 b14f3e3f41d380e9c32cecf5735abe32
BLAKE2b-256 2c84733350dd2454d545d8dc2c4ff19128999d70baaef98e9a50d87a73b42e2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.3-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 318de390b1b43d263b176ac0d8d0fb187aabe9a90db05adf80bb53e1e6be6797
MD5 21242a86ea4eae58a7bd979d47d4aa70
BLAKE2b-256 728d22d6fb71bc446ddc7ab16271a77578a432e698bb6866f0a504d313b7a1d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.3-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 81657932b2464f193ba57b513af8e5e4ec2013c04a47fac69a6e7d2e5828648c
MD5 4f8a0eee89a234d655d19d7a46f398e2
BLAKE2b-256 eb935c4f264e54584c319b7a55cc54586a94d6fc139979dc66af6c80a4e8538f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.3-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f0911eb5e334f7394906fa67c377c2a6d6970cf30be884875df268e7832242db
MD5 00368f837ba1426db5d1a2f2692621d5
BLAKE2b-256 52205927ead1f9bffb1521ac15cb3b8b464c41fce68e9b062f8e8f0b12b64256

See more details on using hashes here.

File details

Details for the file tree_sitter_pgn-1.4.3-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.4.3-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 f4316375388b3ae614767a3be9dc425133f8f1b716b435ff1d49411250ad4218
MD5 38d81f2280b3ab4089b69aad56d9886e
BLAKE2b-256 e1e2f4c8ef19435a7004f45a49f7240d761ffbf78f02eb176e5d8327f8ebd4c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_pgn-1.4.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d471fa844850c8af1fb840fabd873e220ac73e6fb078c877d6ed6e459204fbd0
MD5 1f5ebddea6f97d98e1c32d51997ec0f1
BLAKE2b-256 d31479b166162b04159ca26c4bb635f0222603393f1a2c284eda10a24f160572

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