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.14.tar.gz
(52.3 kB
view hashes)
Built Distributions
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1c7d5bbd75bf47ea0d1f8ae5356a63cb367d512a3ee0c1afa9290f32096b878 |
|
MD5 | e12ad18229138beb0d8e2043430732de |
|
BLAKE2b-256 | c39fb646b837c248be61ae5f0e5f7620e602a3122bfdd2730698609a9d97688d |
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31ee4db4c0032d9711c5d67f7b80822a19cfd127eb72005f95b33d2eda481a15 |
|
MD5 | 7c25e847f3c53406e530c51789fb2535 |
|
BLAKE2b-256 | c4310e2fa137e2724e2ac0231f126c4a113c4a33891069329772327b1dd58725 |
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ec1c899d2a68a021262e75bd3dc29497bcaf3f60201b1b959715a628376f914 |
|
MD5 | ad9a2581d7e64c38a414faa2b4d95778 |
|
BLAKE2b-256 | 1f150dc07a5f21b80c5bf13f6ebc38734d3deac700884cb417ef59d4e886cd7e |
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32d4b92ea45a72261c19e317ae38c3ff3daba5b90642d26b2db6e3287d84d0cb |
|
MD5 | 874730c54634c9a3244dac4464b3e0ca |
|
BLAKE2b-256 | 5078eb9c91c836a71c8de005174394747dc12cae3dfe8b497cf4bfda7154b260 |
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20544324adcbf26694f5df3af392ac233e83efd3f5b979c344b581e76fb5a0a0 |
|
MD5 | 55053f69e0447bb89fd3d96e27a48927 |
|
BLAKE2b-256 | e5c0ae637616d61bb2aecea24ca0e3acde9a275db99ffa516aaf16cbbe50ebd7 |
Close
Hashes for tree_sitter_pgn-1.0.14-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4c7d2d4727831d7fcf07a809451fee6ae0886f82b2621288a2d09f850d5eecf |
|
MD5 | 863bb011ae80683e12087359297604c7 |
|
BLAKE2b-256 | e9eed257509591aafb775d5a322f3552aad84da771997d749288f81ef5f56360 |