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.3.tar.gz
(60.8 kB
view hashes)
Built Distributions
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b1f123f48e95a840e7026b5929e55a37287f57cdc2a5c1cca933b200a164ead |
|
MD5 | a469855e5566918c72cb4052122e3e4a |
|
BLAKE2b-256 | ceea5e0ce46d7d968f2dfb71b47e1d2694eb8109011c8266c2e69a1e7da2609f |
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f96b4c397d02095c8c9a3df72b74cef8391f1afb20addd75fd79bd9e4a7f1ae |
|
MD5 | 7799ed910450319414f204ee67751f5d |
|
BLAKE2b-256 | 2ab2a9b618d5ba72a7acaedbdcc40814906fe831bfe4c6f937cf2da2d8c87a1e |
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9ce3fe9f6c80fc442ae7036c6a4e4a951935838b48da33fa1bd1e9ccc26b385 |
|
MD5 | d19529008d24e730adb828c0352d95ba |
|
BLAKE2b-256 | d397ab9df2610693da1e59664d3277e6b7d712d92a832a534a55a2efdc6a1db2 |
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed1331daf0cd6ebd177856526705433a4f27c2209c6749cf777584ecc710ae0c |
|
MD5 | 46cbafed2b1e1ed5eb2f641a58e75e72 |
|
BLAKE2b-256 | f5d8c64deafca5b96ac49430aae28ebd11b7980c0c29e5f1148a81ad232067a5 |
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b31892a1c98e771b86a6c83f5c876271e8eb4ee983690d9707ccb1bfee5f7a04 |
|
MD5 | 410fe746b01b57b775fba62cb61875cd |
|
BLAKE2b-256 | ff4f8df300cf58f1528ee0d9fa6491e42962ef97559ccdd1a6c79f2b6f8c5e9d |
Close
Hashes for tree_sitter_pgn-1.1.3-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb1115da7b91c077ca19ffb62d8225c52f787dede8b9996a532054dcdf6ad2c0 |
|
MD5 | 13ec538dd45e512622581e7cdb8eb69e |
|
BLAKE2b-256 | 777fd32eb4bab7ed1182769d68471935f1665af635e845d68451575132572012 |