CodeParser

Struct CodeParser 

Source
pub struct CodeParser { /* private fields */ }
Expand description

A robust code parser system that analyzes source code and produces clean, accurate Abstract Syntax Trees (ASTs) optimized for LLM consumption.

§Key capabilities:

  • Consistent parsing approach using tree-sitter for reliable, accurate parsing
  • Clean, well-documented API for LLM tool use
  • Efficient error recovery for handling malformed code
  • Structured AST output that LLMs can easily interpret
  • Language detection with robust extension mapping
  • Declarative query patterns for extracting meaningful code structures
  • Efficient caching system for parsers and queries

Implementations§

Source§

impl CodeParser

Source

pub fn new() -> Result<Self>

Creates a new CodeParser instance with initialized language support and default configuration.

§Returns
  • Result<Self> - A new CodeParser instance or an error
Source

pub fn with_config( cache_size_limit: Option<usize>, max_file_size: Option<usize>, max_files: Option<usize>, max_depth: Option<usize>, ) -> Result<Self>

Creates a new CodeParser instance with custom configuration.

§Arguments
  • cache_size_limit - Optional cache size limit in bytes (default: 50MB)
  • max_file_size - Optional maximum file size to parse in bytes (default: 1MB)
  • max_files - Optional maximum number of files to parse (default: 25)
  • max_depth - Optional maximum recursion depth (default: 3)
§Returns
  • Result<Self> - A new CodeParser instance or an error
Source

pub fn detect_language(&self, path: &Path) -> Option<String>

Determines the programming language from a file extension

§Arguments
  • path - Path to the file
§Returns
  • Option<String> - Language name if detected
Source

pub fn parse_file(&mut self, path: &Path) -> Result<CodeAST>

Parses a single file using tree-sitter and generates an AST

§Arguments
  • path - Path to the file to parse
§Returns
  • Result<CodeAST> - The abstract syntax tree or an error
Source

pub fn create_simplified_ast( &self, path: &Path, language: &str, source_code: &str, ) -> Result<CodeAST>

Create a simplified AST directly from the source code This is a fallback method when tree-sitter parsing doesn’t work

§Arguments
  • path - Path to the file
  • language - Language of the source code
  • source_code - Source code text
§Returns
  • Result<CodeAST> - Simplified AST or error
Source

pub fn extract_search_terms(&self, query: &str) -> Vec<String>

Extract search terms from a query for grep search

§Arguments
  • query - User query string
§Returns
  • Vec<String> - Extracted search terms
Source

pub fn parse_codebase( &mut self, root_dir: &Path, query: &str, ) -> Result<Vec<CodeAST>>

Parse an entire codebase and generate ASTs for selected files

§Arguments
  • root_dir - Root directory of the codebase
  • query - User query to determine relevant files
§Returns
  • Result<Vec<CodeAST>> - List of ASTs for relevant files
Source

pub fn generate_llm_friendly_ast( &mut self, root_dir: &Path, query: &str, ) -> Result<String>

Generate a structured AST optimized for LLM consumption

§Arguments
  • root_dir - Root directory of the codebase or path to a single file
  • query - User query to determine relevant files
§Returns
  • Result<String> - Structured AST as a string
Source

pub fn determine_relevant_files(&self, query: &str) -> Vec<String>

Determine which files to parse based on user query

§Arguments
  • query - User query string
§Returns
  • Vec<String> - List of glob patterns for relevant files

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T