Tree-sitter grammar for Smart Game Format (SGF).
Project description
tree-sitter-sgf
This grammar implements the Smart Game Format, SGF. It targets FF[4] standard, and has limited support for the older versions. It supports Go and other games.
Development
Install dependencies, generate the parser, and run the public corpus tests with:
npm install
npm run generate
npm run check
Installation
Install from npm:
npm install tree-sitter-sgf
const Parser = require("tree-sitter");
const SGF = require("tree-sitter-sgf");
const parser = new Parser();
parser.setLanguage(SGF);
const tree = parser.parse("(;GM[1]FF[4]SZ[19];B[pd];W[dd])");
Install from crates.io:
cargo add tree-sitter-sgf
let mut parser = tree_sitter::Parser::new();
let language = tree_sitter_sgf::LANGUAGE;
parser.set_language(&language.into())?;
let tree = parser.parse("(;GM[1]FF[4])", None).unwrap();
Install from PyPI:
pip install tree-sitter-sgf
import tree_sitter_sgf
from tree_sitter import Language, Parser
language = Language(tree_sitter_sgf.language())
parser = Parser(language)
tree = parser.parse(b"(;GM[1]FF[4])")
The grammar follows the SGF FF[4] syntax definition: a source file is a collection of one or more game trees; game trees contain sequences, nodes, properties, and bracketed property values. It intentionally does not validate SGF semantics such as property placement, point coordinates, move legality, duplicate properties, or game-specific value formats.
Helix
For local Helix testing, point Helix at this repository in
~/.config/helix/languages.toml:
[[language]]
name = "sgf"
scope = "source.sgf"
auto-format = false
injection-regex = "sgf"
file-types = ["sgf"]
[[grammar]]
name = "sgf"
source = { path = "/absolute/path/to/tree-sitter-sgf" }
Then link the Helix query set into Helix's user runtime:
REPO="$(pwd)"
mkdir -p ~/.config/helix/runtime/queries
ln -sfn "$REPO/queries/helix" ~/.config/helix/runtime/queries/sgf
hx -g build
hx --health sgf
queries/helix/highlights.scm intentionally duplicates the shared highlight
query instead of using ; inherits: sgf. When the Helix runtime path is a
symlink named sgf, inheriting from sgf would inherit from itself.
Neovim
Neovim uses queries/sgf/highlights.scm. The Helix indent and textobject
queries are kept under queries/helix/ and are not required for Neovim.
Build a shared parser for local testing:
npm run generate
npm run build:parser
Register the local parser, filetype, and runtime path with the example config
in editors/nvim/init.lua. When copying that file outside this repository, set
TREE_SITTER_SGF_REPO to the repository path or replace the sgf_repo value.
Smoke-test the local Neovim setup with:
npm run smoke:nvim
If you have a local untracked SGF corpus under corpus/, parse it with:
npm run parse:corpus
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_sitter_sgf-0.1.0.tar.gz.
File metadata
- Download URL: tree_sitter_sgf-0.1.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9c0f82343323de2cd105c42234ab7f908f80d4088bd1379113eb52bcf1cf6e1
|
|
| MD5 |
4b1ca02746436411b544cfb73eaf83b3
|
|
| BLAKE2b-256 |
b1b1fc2c26b3f9d1335dbeddbd8caedd6ab2459855b446630e8b1771071b998c
|
File details
Details for the file tree_sitter_sgf-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: tree_sitter_sgf-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.6 kB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8dea68b38501f2ebd2c1090cf4abb1a57bde3e82d2a3abcb2773d9de6afe39bb
|
|
| MD5 |
d91993fa39ddba1a78044c931590325d
|
|
| BLAKE2b-256 |
b44b640e34532a467080df21aa201a4c5487e40a11606be7d549bc4ab78758fa
|