跳转到主要内容

tree-sitter的PGN语法

项目描述

tree-sitter-pgn

概述

Chess Portable Game Notation (PGN) grammar for tree-sitter.

使用于

高亮示例

Python示例

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))

参考文献

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

tree_sitter_pgn-1.1.4.tar.gz (60.6 kB 查看哈希值)

上传时间

构建分布

tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_x86_64.whl (90.5 kB 查看哈希值)

上传于 CPython 3.8+ musllinux: musl 1.2+ x86-64

tree_sitter_pgn-1.1.4-cp38-abi3-musllinux_1_2_aarch64.whl (87.6 kB 查看哈希值)

上传于 CPython 3.8+ musllinux: musl 1.2+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (145.0 kB 查看哈希值)

上传于 CPython 3.8+ manylinux: glibc 2.17+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (99.7 kB 查看哈希值)

上传于 CPython 3.8+ manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

tree_sitter_pgn-1.1.4-cp38-abi3-macosx_11_0_arm64.whl (77.6 kB 查看哈希值)

上传于 CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_pgn-1.1.4-cp38-abi3-macosx_10_9_x86_64.whl (74.9 kB 查看哈希值)

上传于 CPython 3.8+ macOS 10.9+ x86-64

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面