Skip to main content

Module tree_sitter

Module tree_sitter 

Source
Expand description

§Tree-sitter Integration and AST Backend

Core integration layer between thread-ast-engine and the tree-sitter parsing library. Provides the foundational types and functionality for parsing source code into ASTs, editing trees incrementally, and bridging tree-sitter concepts with thread-ast-engine APIs.

§Key Components

  • StrDoc - Document type that combines source code with parsed tree-sitter trees
  • LanguageExt - Extension trait for languages that work with tree-sitter
  • TSParseError - Error types for tree-sitter parsing failures
  • Tree editing and incremental parsing support
  • Language injection support for multi-language documents

§Core Concepts

§Documents and Parsing

StrDoc represents a parsed document containing both source code and its tree-sitter AST. It handles incremental parsing when the source is modified, automatically updating the tree structure while preserving unchanged portions for performance.

§Language Extensions

LanguageExt extends the base Language trait with tree-sitter specific functionality:

  • Getting tree-sitter language objects for parsing
  • Creating AST-grep instances
  • Handling language injections (like JavaScript in HTML)

§Tree Editing

Supports incremental tree editing through tree-sitter’s edit API, allowing efficient updates when source code changes without full re-parsing.

§Example Usage


// Create a document from source code
let doc = StrDoc::new("let x = 42;", Tsx);

// Access the parsed tree
let root = doc.root_node();
println!("Root kind: {}", root.kind());

// Create an AST-grep instance for pattern matching
let ast_grep = Tsx.ast_grep("function foo() { return 42; }");
let root_node = ast_grep.root();

Re-exports§

pub use traversal::TsPre;
pub use traversal::Visitor;

Modules§

traversal
AST Tree Traversal Algorithms

Structs§

DisplayContext
StrDoc
Document type that combines source code with its parsed tree-sitter AST.
TSLanguage
An opaque object that defines how to parse a particular language. The code for each Language is generated by the Tree-sitter CLI.
TSPoint
A position in a multi-line text document, in terms of rows and columns.
TSRange
A range of positions in a multi-line text document, both in terms of bytes and of rows and columns.

Enums§

TSParseError
Errors that can occur during tree-sitter parsing operations.

Traits§

ContentExt
LanguageExt
Extension trait for languages that integrate with tree-sitter parsing.

Functions§

perform_edit