pub struct Parser { /* private fields */ }Expand description
Code parser using tree-sitter grammars Extracts functions, methods, classes, and other code elements from source files in supported languages.
§Example
use cqs::Parser;
let parser = Parser::new()?;
let chunks = parser.parse_file(std::path::Path::new("src/main.rs"))?;
for chunk in chunks {
println!("{}: {} ({})", chunk.file.display(), chunk.name, chunk.chunk_type);
}Implementations§
Source§impl Parser
impl Parser
Sourcepub fn extract_calls(
&self,
source: &str,
language: Language,
start_byte: usize,
end_byte: usize,
line_offset: u32,
) -> Vec<CallSite>
pub fn extract_calls( &self, source: &str, language: Language, start_byte: usize, end_byte: usize, line_offset: u32, ) -> Vec<CallSite>
Extract function calls from a chunk’s source code Returns call sites found within the given byte range of the source.
Sourcepub fn extract_calls_from_chunk(&self, chunk: &Chunk) -> Vec<CallSite>
pub fn extract_calls_from_chunk(&self, chunk: &Chunk) -> Vec<CallSite>
Extract function calls from a parsed chunk Convenience method that extracts calls from the chunk’s content.
Sourcepub fn extract_types(
&self,
source: &str,
tree: &Tree,
language: Language,
start_byte: usize,
end_byte: usize,
) -> Vec<TypeRef>
pub fn extract_types( &self, source: &str, tree: &Tree, language: Language, start_byte: usize, end_byte: usize, ) -> Vec<TypeRef>
Extract type references from a chunk’s byte range
Returns classified type references with merge logic: if a type name
was captured by any classified pattern (Param/Return/Field/Impl/Bound/Alias),
the catch-all duplicate is dropped. Types found ONLY by the catch-all
get kind = None.
Sourcepub fn parse_file_calls(
&self,
path: &Path,
) -> Result<Vec<FunctionCalls>, ParserError>
pub fn parse_file_calls( &self, path: &Path, ) -> Result<Vec<FunctionCalls>, ParserError>
Extract all function calls from a file, ignoring size limits
Returns calls for every function in the file, including those >100 lines
that would normally be skipped during chunk extraction.
Thin wrapper around parse_file_relationships().
Sourcepub fn parse_file_relationships(
&self,
path: &Path,
) -> Result<(Vec<FunctionCalls>, Vec<ChunkTypeRefs>), ParserError>
pub fn parse_file_relationships( &self, path: &Path, ) -> Result<(Vec<FunctionCalls>, Vec<ChunkTypeRefs>), ParserError>
Extract all function calls AND type references from a file in a single parse pass
Returns (calls, type_refs) for every chunk in the file. Single file read,
single tree-sitter parse, two query cursors on the same tree.
Coupling note: This function and parse_file() must agree on line numbering
(node.start_position().row as u32 + 1) and chunk identity (same query, same
post-process hooks). If either changes, the other must be updated to keep
chunk names and line_start values consistent across phases.
Source§impl Parser
impl Parser
Sourcepub fn new() -> Result<Self, ParserError>
pub fn new() -> Result<Self, ParserError>
Create a new parser (queries are compiled lazily on first use)
Sourcepub fn parse_file(&self, path: &Path) -> Result<Vec<Chunk>, ParserError>
pub fn parse_file(&self, path: &Path) -> Result<Vec<Chunk>, ParserError>
Parse a source file and extract code chunks Returns an empty Vec for non-UTF8 files (with a warning logged). Returns an error for unsupported file types.
Sourcepub fn parse_source(
&self,
source: &str,
language: Language,
path: &Path,
) -> Result<Vec<Chunk>, ParserError>
pub fn parse_source( &self, source: &str, language: Language, path: &Path, ) -> Result<Vec<Chunk>, ParserError>
Parse in-memory source code and extract code chunks.
Like parse_file, but operates on already-read source content with a
known language. The path is used only for chunk origin metadata
(Chunk.file field), not for filesystem access.
Used by train_data to parse git show output without writing temp files.
Sourcepub fn parse_file_all(&self, path: &Path) -> Result<ParseAllResult, ParserError>
pub fn parse_file_all(&self, path: &Path) -> Result<ParseAllResult, ParserError>
Parse a source file and extract chunks, calls, AND type references in one pass.
Combines parse_file() and parse_file_relationships() to avoid double
file read + double tree-sitter parse. Single file read, single outer parse,
two query cursor passes on the same tree, single injection parse.
Returns (chunks, function_calls, chunk_type_refs).
Used by pipeline::parser_stage() for single-pass indexing and
watch::reindex_files() for incremental updates.
Sourcepub fn supported_extensions(&self) -> Vec<&'static str>
pub fn supported_extensions(&self) -> Vec<&'static str>
Retrieves the list of file extensions supported by the language registry.
§Returns
A vector of supported file extensions as static string slices (e.g., “rs”, “py”, “js”).
Auto Trait Implementations§
impl Freeze for Parser
impl RefUnwindSafe for Parser
impl Send for Parser
impl Sync for Parser
impl Unpin for Parser
impl UnsafeUnpin for Parser
impl UnwindSafe for Parser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more