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)
''')
with open('input_file.pgn', 'rb') as file:
content = b''.join(file.readlines())
tree = parser.parse(content)
matches = query.captures(tree.root_node)
merged_nodes = [
*matches.get('game', []),
*matches.get('san_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'):
san = []
for node in game:
if node.type == 'san_move' and node.text is not None:
san.append(node.text.decode().strip())
continue
print(' '.join(san))
References
Project details
Release history Release notifications | RSS feed
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.0.13.tar.gz
(44.6 kB
view hashes)
Built Distributions
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 908da5669c312782ebda7801c15e75ba338bd05bbf83e53c06467c4d663b7181 |
|
MD5 | 92208031df12b5609a297fe1075918dd |
|
BLAKE2b-256 | e190386213a915bceba63207285a3c310545af16d61e44178506c084ae3f6c19 |
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92216c9ac34fb1a2e1d2567c9ba1ccce9398871ac42c6f6bdc931bc7556466d0 |
|
MD5 | 10bb08385595c5b4f4932131f6fbae23 |
|
BLAKE2b-256 | 9276bd234f4e3d5ed842058d0201e900fff51d84e56ed9fa46385751dcc32cab |
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb929e18301b8df8dac58ae4ddf2f241cb3993aba3dfc8b1de5fde7c625f8d9f |
|
MD5 | e7d6939c31aac91863e46202956457c4 |
|
BLAKE2b-256 | d2dde6e1d9fa1b7714b21bc1c1bd5f42eeffbd85c2d2cb0500f1887848c96151 |
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68efc88b79157b907dbd28c1105c441e5546d8bbf3398178ea10177591497f07 |
|
MD5 | cbe92d683fca3585081ca4c03a7bc116 |
|
BLAKE2b-256 | 7e4d05d07a107eedc0e9cc28a5fa48371d01f09d73ec2e77476c8c418ae47865 |
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 594ea5f9c8b6f68fda664ae303793c88d93cef5aaa2aa06697a4878bf36ca4b3 |
|
MD5 | e6ca6ca535e4a0451c70b2908898139c |
|
BLAKE2b-256 | e46b81625db4ff3c52ac2edfea9a05189b79436044936facc5693d410296d01b |
Close
Hashes for tree_sitter_pgn-1.0.13-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de6b644be926aba4a87901a2bfa8b692c5891596a59d3d6e0a3a66095ad24d43 |
|
MD5 | f0c7755081d50472ce9f5f603fa6907e |
|
BLAKE2b-256 | 3d1332b30523baa42ee4008f4f29fff1450733a9dae63625a1744cd53f165259 |