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.16.tar.gz
(52.3 kB
view hashes)
Built Distributions
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2bc562760b4d24c846990c3b1bb718f5093ad51355fd9fe33fc7ce233d1d263 |
|
MD5 | 5463b350f3952aa8a496f5f34dfc60fb |
|
BLAKE2b-256 | 92b06dff28a93ebbae1bb5c0a50fcf6f6fbde45a3db6b3cfce83c1fb9822f437 |
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 983f3b5341b5cb01bf3db64c1cdea492b0f9af1a45ff04490c8fc87892554dd9 |
|
MD5 | c23379850f804203c6e39e8b78e283f2 |
|
BLAKE2b-256 | 5f0b263f9047c31474d8b735db91baccefae22c13fd7b445b5bc5ee98ae7397f |
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb81218933c06a5796f72b6a8d60848b53e47ebaaaf3402e4aa0e880c8348054 |
|
MD5 | bf93ffd08c80a8a0fa3bf2e6aed4c3b5 |
|
BLAKE2b-256 | 178a5a51569be8bec6d2be3800f61ded665dae658396d3cea67553e178fb7e0d |
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c08e31c810f900ffe62e208fcc2d1756057ef06bcb86710be20402d2b8602ec |
|
MD5 | 0564f6945ae881553bdb0f574e54556c |
|
BLAKE2b-256 | 75537cea60ba163c146752332b7e843d6428f3b0a1d6c5cbcde1a748d3b28d52 |
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bf182ba220c924a94e9d92889d11d019fc974c12617eca5d3acbbe3d5e5cdb2 |
|
MD5 | a9b8e438354c3fd83ca28b5e8d9d6785 |
|
BLAKE2b-256 | 5e0bfda41746339742d541735360457f76dec70d738b3f7ee7f7ccd0b7126712 |
Close
Hashes for tree_sitter_pgn-1.0.16-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0a4385f715e92ec7e78a6560bbdbf69a833c00346cd53605dc40d71ee71c4af |
|
MD5 | 8bb29ca31a08195d88db7079da6ba2c5 |
|
BLAKE2b-256 | e81f8392a441f865829dff830689a9966b7a434f03076c4e66e7d9d3c43323f9 |