py-tree-sitter exposes Tree-sitter’s incremental parser to Python. It ships pre-built wheels without extra native dependencies.
Overview
This package wraps the Tree-sitter parsing library so Python code can load language grammars, parse source text, and inspect syntax trees. Language implementations are installed separately as their own wheels and then loaded as Language objects.
You create a Parser, assign a language, and parse bytes or supply a read callback for non-contiguous buffers using UTF-8 or UTF-16 encodings. Results are Tree objects whose nodes can be walked manually or traversed efficiently with a TreeCursor.
After edits, trees can be updated incrementally and reparsed against an old tree for faster updates than full reparses. changed_ranges highlights syntactic regions affected by edits. Query and QueryCursor support pattern matching over syntax trees with captures and grouped matches.
Documentation describes ABI version constants, parser and node types, points and ranges in multi-line documents, and error types for failed query compilation.
Key features
- Parser and Language loading
- Incremental parse and tree edits
- TreeCursor efficient walking
- Query pattern matching
- Pre-compiled wheels per platform
- UTF-8 and UTF-16 source readers
- changed_ranges after edits
Use cases
- Syntax-aware editor tooling
- Incremental reparsing in IDEs
- Structural code search
- Language tooling prototypes
- Analyzing parse trees in Python