Skip to main content

Ast

Struct Ast 

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

Parse-once, compute-many handle.

Owns the parsed tree_sitter::Tree and the source bytes it was parsed from, so callers can run Ast::metrics repeatedly against the same parse — with different MetricsOptions subsets, interleaved with custom tree_sitter traversal via Ast::as_tree_sitter, or cached across configuration changes in an analysis pipeline.

Build one via Ast::parse (mirrors analyze) or Ast::from_tree_sitter (mirrors crate::metrics_from_tree but with an explicit display name instead of a lossy path-to-string conversion).

Ast is a snapshot — it does not pick up changes to the source after construction. Incremental reparse via tree_sitter::InputEdit is out of scope for this seam.

§C++ preprocessor

When Ast::parse is given a Source carrying preprocessor inputs and the language is LANG::Cpp, Ast::source returns the expanded bytes the parser actually saw (the macro pre-pass runs before tree-sitter does). Ast::from_tree_sitter adopts whatever tree the caller supplied; whatever expansion they applied before building it is what Ast::source reflects.

§Examples

Parse once, run two disjoint metric subsets without re-parsing:

use big_code_analysis::{Ast, LANG, Metric, MetricsOptions, Source};

let ast = Ast::parse(
    Source::new(LANG::Rust, b"fn f() { if true { 1 } else { 2 }; }"),
)
.expect("rust feature enabled");

let loc = ast
    .metrics(MetricsOptions::default().with_only(&[Metric::Loc]))
    .expect("walker succeeds");
let cyc = ast
    .metrics(MetricsOptions::default().with_only(&[Metric::Cyclomatic]))
    .expect("walker succeeds");
// Each call's `with_only` filters to its requested family — the other
// metric stays at its `Default` (zero) value, confirming options are
// honored per call rather than carried over.
assert!(loc.metrics.loc.ploc() > 0.0);
assert_eq!(loc.metrics.cyclomatic.cyclomatic_sum(), 0.0);
assert!(cyc.metrics.cyclomatic.cyclomatic_sum() > 0.0);
assert_eq!(cyc.metrics.loc.ploc(), 0.0);

Walk the underlying tree_sitter::Tree and then run metrics on the same parse:

use big_code_analysis::{Ast, LANG, MetricsOptions, Source};

let ast = Ast::parse(Source::new(LANG::Rust, b"fn f() {}"))
    .expect("rust feature enabled");
let root = ast.as_tree_sitter().root_node();
assert_eq!(root.kind(), "source_file");
let _ = ast.metrics(MetricsOptions::default()).expect("walker succeeds");

Implementations§

Source§

impl Ast

Source

pub fn parse(source: Source<'_>) -> Result<Self, MetricsError>

Parse source into a reusable Ast. Equivalent to the parse half of analyze: every Ast::metrics call on the returned handle produces the same FuncSpace as a freshly-issued analyze(source, options) would.

§Errors

Returns MetricsError::LanguageDisabled when the source language’s per-language Cargo feature is not enabled in this build.

Source

pub fn from_tree_sitter( lang: LANG, tree: Tree, code: Vec<u8>, name: Option<String>, ) -> Result<Self, MetricsError>

Adopt a caller-built tree_sitter::Tree. The Source-flavored counterpart of crate::metrics_from_tree: same tree-reuse semantics, but with name: Option<String> carried end-to-end instead of derived from a path via lossy UTF-8 conversion.

The supplied tree must have been produced from code with the tree_sitter::Language returned by LANG::get_tree_sitter_language for lang; a mismatch is not unsafe but yields nonsensical metric values.

§Errors

Returns MetricsError::LanguageDisabled when lang’s per-language Cargo feature is not enabled in this build.

Source

pub fn metrics( &self, options: MetricsOptions, ) -> Result<FuncSpace, MetricsError>

Run the metric walker against the held parse. Safe to call repeatedly — the tree is reused.

Two metrics calls with different MetricsOptions::with_only selections walk the tree twice; the savings versus analyze come from not re-parsing the source.

§Errors

The return type carries MetricsError::EmptyRoot for forward compatibility, but the walker always pushes a synthetic top-level SpaceKind::Unit FuncSpace before walking, so this method does not return Err in practice today.

Source

pub fn language(&self) -> LANG

Source language of the parsed tree.

Source

pub fn source(&self) -> &[u8]

Source bytes the held tree was parsed from. For LANG::Cpp with preprocessor inputs supplied to Ast::parse, these are the expanded bytes (see the type-level “C++ preprocessor” note).

Source

pub fn name(&self) -> Option<&str>

Display name carried through to FuncSpace::name by every Ast::metrics call.

Source

pub fn as_tree_sitter(&self) -> &Tree

Borrow the underlying tree_sitter::Tree for callers that want to drive their own traversal alongside the metric walker.

The returned reference is valid only while self lives; nodes obtained from it must be resolved against Ast::source (the tree_sitter::Tree is lazy and lifetime-bound to that byte buffer).

Trait Implementations§

Source§

impl Debug for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Ast

§

impl RefUnwindSafe for Ast

§

impl Send for Ast

§

impl Sync for Ast

§

impl Unpin for Ast

§

impl UnsafeUnpin for Ast

§

impl UnwindSafe for Ast

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, 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> 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.