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-sitterVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
Always encode your TOML source code string to bytes, e.g., `toml_code.encode('utf8')`, before passing it to `parser.parse()`.