Registry / serialization / tree-sitter-cpp

tree-sitter-cpp

JSON →
library0.23.4pypypi✓ verified 28d ago

tree-sitter-cpp provides the C++ grammar for the Tree-sitter parsing library. It enables Python applications to parse C++ code, build abstract syntax trees (ASTs), and perform code analysis tasks like syntax highlighting, linting, and refactoring. The library is actively maintained with frequent minor updates (as seen in its v0.23.x releases), and it requires the core `tree-sitter` Python package to function.

pip install tree-sitter-cpp tree-sitter
INSTALL
IMPORT
SIG · TREE-SITTER-CPP
T
tree-sitter-cpp
serializationpythonv0.23.4
Install
1.7s avg
Import
11ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.23.4 · 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.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 22.7MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.008s · 24MB
21MB installed
● package 21MB
Code
Verified usage

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

Language, Parser
✓ from tree_sitter import Language, Parser
✗ from tree_sitter_cpp import Language, Parser
The `Language` and `Parser` classes are part of the core `tree-sitter` library, not `tree-sitter-cpp` itself. `tree_sitter_cpp` provides the grammar object.
language()
✓ import tree_sitter_cpp as ts_cpp cpp_language = Language(ts_cpp.language())
✗ cpp_language = Language.build_library('/path/to/grammar.so', ['/path/to/tree-sitter-cpp'])
For pre-compiled grammars installed via pip, the `language()` function from the grammar package (e.g., `tree_sitter_cpp.language()`) should be used directly with `tree_sitter.Language`. The `Language.build_library` method was removed from `py-tree-sitter` around version 0.21.x and is no longer the recommended approach for using distributed grammars.

This quickstart demonstrates how to load the `tree-sitter-cpp` grammar, initialize a `tree_sitter` parser with it, and parse a simple C++ code snippet. It then shows how to access the root node of the generated syntax tree and print basic information about it and its children. The code uses `bytes` objects for source code, as required by the `tree-sitter` library.

import tree_sitter_cpp as ts_cpp from tree_sitter import Language, Parser # Load the C++ grammar CPP_LANGUAGE = Language(ts_cpp.language()) # Create a parser and set its language parser = Parser() parser.set_language(CPP_LANGUAGE) # C++ code to parse cpp_code = b"""#include <iostream> int main() { std::cout << "Hello, Tree-sitter!" << std::endl; return 0; } """ # Parse the code tree = parser.parse(cpp_code) # Get the root node of the syntax tree root_node = tree.root_node # Print the root node type and its first child (for demonstration) print(f"Root node type: {root_node.type}") if root_node.children: print(f"First child type: {root_node.children[0].type}") print(f"First child text: {root_node.children[0].text.decode('utf8')}") # Example of traversing the tree (optional, for deeper analysis) def traverse(node, depth=0): print(' ' * depth + f'- {node.type} [ {node.start_point} - {node.end_point} ] {node.text.decode('utf8') if node.text else ''}') for child in node.children: traverse(child, depth + 1) # print("\nFull AST:") # traverse(root_node)
Debug
Known issues
breakingThe `Language.build_library` static method was removed from the `py-tree-sitter` library around version 0.21.x.
fix
Instead of `Language.build_library(...)`, you should `pip install` the specific grammar package (e.g., `tree-sitter-cpp`) and load it using `Language(grammar_package.language())`. For `tree-sitter-cpp`, this means `import tree_sitter_cpp as ts_cpp; cpp_language = Language(ts_cpp.language())`.
affects: tree-sitter>=0.21.x
gotchaInstalling `tree-sitter-cpp` alone is not sufficient to parse C++ code in Python. The core `tree-sitter` Python package, which provides the `Language` and `Parser` classes, must also be installed and imported.
fix
Ensure both packages are installed: `pip install tree-sitter tree-sitter-cpp`. Then, import `Language` and `Parser` from `tree_sitter` and the grammar from `tree_sitter_cpp`.
affects: All
gotchaThe `tree-sitter` parsing functions expect source code as a `bytes` object (UTF-8 or UTF-16 encoded), not a standard Python `str`.
fix
Encode your source strings before passing them to `parser.parse()`: `parser.parse(your_string.encode('utf8'))`.
affects: All
Upgrade
Version history
0.23.4latest on PyPI · released Nov 11, 2024
Audit
Dependencies
tree-sitterrequiredProvides the core Python bindings and parsing engine required to utilize the C++ grammar. tree-sitter-cpp only provides the language definition.
Agent activity
13 hits · last 30 days
node
11
OpenAI (training)
1
Resources
tree-sitter-cpp — pip install tree-sitter-cpp · libregistry