Skip to main content

AnyParser

Enum AnyParser 

Source
pub enum AnyParser {
Show 25 variants Javascript(JavascriptParser), Mozjs(MozjsParser), Java(JavaParser), Go(GoParser), Kotlin(KotlinParser), Lua(LuaParser), Rust(RustParser), Tcl(TclParser), Irules(IrulesParser), C(CParser), Cpp(CppParser), Mozcpp(MozcppParser), Objc(ObjcParser), Csharp(CsharpParser), Elixir(ElixirParser), Python(PythonParser), Tsx(TsxParser), Typescript(TypescriptParser), Bash(BashParser), Ccomment(CcommentParser), Preproc(PreprocParser), Perl(PerlParser), Php(PhpParser), Ruby(RubyParser), Groovy(GroovyParser),
}
Expand description

A parsed tree plus its source bytes for a language chosen at runtime — one variant per LANG, each holding that language’s Parser<XCode>. The public seam is big_code_analysis::Ast; this enum is the language-dispatched carrier it wraps, and the value a caller matches on to reach a concrete parser (with_any_parser!).

Every variant exists regardless of the Cargo feature set: a Parser<XCode> type needs no grammar crate, only Self::parse / Self::from_tree do, and those are the arms that are feature-gated. A disabled language is therefore never constructed — the constructors return Err(LanguageDisabled) for it — but it can always be named, which is what lets with_any_parser! be written once without any cfg of its own (#1376).

The cost is real and was priced: because the match is total over all 25 variants, every generic walk is monomorphised for every language whatever features were selected. Measured on a --no-default-features --features rust build, metrics_inner and ops_inner each get 25 instantiations where the pre-split crate got one.

DECIDED (#1376): it stays. Reversing it means moving the dispatch macro into big-code-analysis, so the cfgs are evaluated in the crate whose features they name — a #[cfg] inside a #[macro_export] macro is evaluated in the invoking crate, which is the whole reason this shape exists — and that buys an eighth site enumerating every language, permanently. A visitor trait does not help: one defined here can only bound its type parameter by traits this crate can name, so it hands back a ParserTrait where the metric walks need the root’s MetricSuite, and Rust cannot let a caller supply that bound. Nothing in this workspace pays the cost — the CLI, the server and the Python bindings all build all-languages — and what it costs is compile time and rlib size, not shipped binary size, since unreferenced instantiations are dropped at link time. Revisit only if a consumer of the narrow feature set actually appears.

Variants§

§

Javascript(JavascriptParser)

The Javascript parser.

§

Mozjs(MozjsParser)

The Mozjs parser.

§

Java(JavaParser)

The Java parser.

§

Go(GoParser)

The Go parser.

§

Kotlin(KotlinParser)

The Kotlin parser.

§

Lua(LuaParser)

The Lua parser.

§

Rust(RustParser)

The Rust parser.

§

Tcl(TclParser)

The Tcl parser.

§

Irules(IrulesParser)

The Irules parser.

§

C(CParser)

The C parser.

§

Cpp(CppParser)

The Cpp parser.

§

Mozcpp(MozcppParser)

The Mozcpp parser.

§

Objc(ObjcParser)

The Objc parser.

§

Csharp(CsharpParser)

The Csharp parser.

§

Elixir(ElixirParser)

The Elixir parser.

§

Python(PythonParser)

The Python parser.

§

Tsx(TsxParser)

The Tsx parser.

§

Typescript(TypescriptParser)

The Typescript parser.

§

Bash(BashParser)

The Bash parser.

§

Ccomment(CcommentParser)

The Ccomment parser.

§

Preproc(PreprocParser)

The Preproc parser.

§

Perl(PerlParser)

The Perl parser.

§

Php(PhpParser)

The Php parser.

§

Ruby(RubyParser)

The Ruby parser.

§

Groovy(GroovyParser)

The Groovy parser.

Implementations§

Source§

impl AnyParser

Source

pub fn parse( lang: LANG, source: Vec<u8>, preproc_path: Option<&Path>, preproc: Option<Arc<PreprocResults>>, ) -> Result<Self, MetricsError>

Parse source as lang.

Parser::new keys the C-family macro-expansion lookup off the caller-supplied path; callers analysing in-memory snippets pass None and get the empty Path (""), which the lookup ignores. That path never leaks into a display name — Ast carries the name separately. source is taken by value so an owned buffer moves straight into the parser instead of being copied.

§Errors

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

Source

pub fn from_tree( lang: LANG, tree: Tree, source: Vec<u8>, ) -> Result<Self, MetricsError>

Adopt a caller-built tree_sitter::Tree produced from source with lang’s grammar.

§Errors

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

Source

pub fn language(&self) -> LANG

The language this parser was built for.

Source

pub fn code(&self) -> &[u8] ⓘ

The bytes the tree was parsed from — after #define expansion for the C family, so not necessarily the bytes handed to Self::parse.

Source

pub fn ts_tree(&self) -> &Tree

Source

pub fn root_node(&self) -> Node<'_>

The root Node of the held tree.

Source

pub fn strip_comments(&self) -> Option<Vec<u8>>

The source with non-doc comments removed, or None when there was nothing to strip.

Source

pub fn dump(&self, cfg: AstCfg) -> AstResponse

The AST dump under cfg.

Source

pub fn count(&self, filters: &[String]) -> (usize, usize)

(matching, total) node counts for filters — the same vocabulary ParserTrait::filters accepts.

Source

pub fn find(&self, filters: &[String]) -> Result<Vec<Node<'_>>, MetricsError>

Every node matching filters, in source order. The nodes borrow the held tree.

§Errors

Currently infallible; the Result is reserved for a future strict-parsing mode.

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.