Registry / serialization / tree-sitter-toml

tree-sitter-toml

JSON →
library0.7.0pypypi✓ verified 23d ago

tree-sitter-toml provides a TOML grammar for the Tree-sitter parsing library, with Python bindings and pre-compiled wheels. It enables efficient, incremental parsing of TOML files, generating concrete syntax trees for various programming tools. The library is currently at version 0.7.0 and is actively maintained within the Tree-sitter grammars ecosystem.

pip install tree-sitter-toml tree-sitter
INSTALL
IMPORT
SIG · TREE-SITTER-TOML
T
tree-sitter-toml
serializationpythonv0.7.0
Install
1.6s avg
Import
47ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.048s · 19.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.046s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

language
import tree_sitter_toml from tree_sitter import Language, Parser TOML_LANGUAGE = Language(tree_sitter_toml.language())
from tree_sitter_toml import Language; TOML_LANGUAGE = Language()
The `language()` function is a module-level function within `tree_sitter_toml` that returns the compiled language object, which must then be wrapped by `tree_sitter.Language`.

This quickstart demonstrates how to initialize the Tree-sitter parser with the `tree-sitter-toml` grammar and parse a simple TOML string. It then walks the resulting syntax tree to find and print the value associated with the 'name' key.

import tree_sitter_toml from tree_sitter import Language, Parser # Load the TOML grammar TOML_LANGUAGE = Language(tree_sitter_toml.language()) # Create a parser instance parser = Parser() parser.set_language(TOML_LANGUAGE) # Sample TOML source code toml_code = b''' [package] name = "my-app" version = "0.1.0" authors = ["John Doe <john@example.com>"] ''' # Parse the code tree = parser.parse(toml_code) # Get the root node and print its type root_node = tree.root_node print(f"Root Node Type: {root_node.type}") # Example: Find a 'name' key-value pair def find_name(node): for child in node.children: if child.type == 'key_value_pair': if child.child_by_field_name('key') and child.child_by_field_name('key').text == b'name': print(f"Found name: {child.child_by_field_name('value').text.decode()}") find_name(child) find_name(root_node)
Debug
Known issues
breakingThe underlying `tree-sitter` core library (which `py-tree-sitter` binds to) may introduce breaking changes, particularly with its Application Binary Interface (ABI). A major ABI version bump in `tree-sitter` can render `tree-sitter-toml` incompatible until it's updated and recompiled for the new ABI, potentially leading to `Language` loading failures.
fix
Ensure `tree-sitter` and `tree-sitter-toml` packages are compatible. If `tree-sitter` is updated to a new major version, check for a corresponding `tree-sitter-toml` update. Reinstalling both might be necessary.
affects: All versions, depends on `tree-sitter` core/bindings version
gotchaChanges to the `tree-sitter-toml` grammar definition, even minor ones, can alter the structure of the generated syntax tree. Existing Tree-sitter queries (used for pattern matching or navigation) written against an older grammar structure might become invalid or return incorrect results.
fix
After updating `tree-sitter-toml`, review and test any custom Tree-sitter queries. Consult the grammar's repository for changes to node types or structure if queries fail unexpectedly.
affects: All versions, especially across `tree-sitter-toml` minor/major updates
gotchaThe `parser.parse()` method of `tree-sitter` strictly requires the input source code to be a `bytes` object, usually UTF-8 encoded. Passing a standard Python string or an incorrectly encoded byte string will lead to a `TypeError` or parsing errors.
fix
Always encode your TOML source code string to bytes, e.g., `toml_code.encode('utf8')`, before passing it to `parser.parse()`.
affects: All versions
Upgrade
Version history
0.7.0latest on PyPI · released Dec 3, 2024
Audit
Dependencies
tree-sitterrequiredProvides the core Tree-sitter parsing engine and Python bindings, essential for loading and using the TOML grammar.
Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
1
Resources
tree-sitter-toml — pip install tree-sitter-toml · libregistry