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)
(movetext
lan_move: (lan_move) @lan_move)
''')
with open('input_file.pgn', 'rb') as file:
tree = parser.parse(file.read())
matches = query.captures(tree.root_node)
merged_nodes = [
*matches.get('game', []),
*matches.get('san_move', []),
*matches.get('lan_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'):
main_line = []
for node in game:
if node.type in ['san_move', 'lan_move'] and node.text is not None:
main_line.append(node.text.decode().strip())
continue
print(' '.join(main_line))
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.1.0.tar.gz
(61.2 kB
view hashes)
Built Distributions
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f88197e39c78bc4d7f9fd40fec7eb4b19dde33cf47476e93e9329c5d64db8eb1 |
|
MD5 | 2129f9165763c0cdac5d02602192819f |
|
BLAKE2b-256 | 73dc1ab93bea6e2541a608d1ef47bd81c3cc53061399f9d56b6c64137a75be47 |
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 889549ecb3cda3c30bc0464dd7dbd54b7660c2285255697f1c08e039f1c45e0f |
|
MD5 | d3a7ec973712fdc9d954850b74fae018 |
|
BLAKE2b-256 | 89c6a28f378cca26aebbf26b0c1b713de375bc35668d00323fc1d5922ef7a944 |
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6014e5b69a08d1071c8ff15df3122bb0de9ac46e72fded5fcf50113d8d37c6fe |
|
MD5 | b48515adef23e75ab81bc129586d8d65 |
|
BLAKE2b-256 | c4440690d4daa6ca18f1e305b1a962b3d03630e9c28258b5cd6f7779ecc25ffc |
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06c0272d3b77ff7784181ac6b34b611fc73abc23d1c342ed17429f67918310bb |
|
MD5 | 19ba3ae95fcf1f93c90867e2f23df72c |
|
BLAKE2b-256 | b804abd2c281bb1ba2138be8023ad48694dbc2bf9440ccffd836488423490dcb |
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9331b0733107a9d29b66ac3d55ba6753c0d2ff4f5fd0329a621f9632d7401f1a |
|
MD5 | 32ebf5bff134067168ddc2d5bce6f0a6 |
|
BLAKE2b-256 | 6a6248bcb53544a93ca55e178b2cc0121f274b393c9599ade312bab5d308fd37 |
Close
Hashes for tree_sitter_pgn-1.1.0-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcb0acbb56b25c54905ef7018f31020bc1b8a1deeb3fc9015c28978af70561fb |
|
MD5 | 0607b52e73a558b79e65e2dd750ef92d |
|
BLAKE2b-256 | 623fb508416634008b95c3d2cbf577f2f47902517e11370fb1237ffb867c5759 |