Skip to main content

big_code_analysis/
traits.rs

1// Per-language metric and AST modules deliberately consume the macro-
2// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
3// inside match expressions — explicit imports would list dozens of
4// variants per arm and obscure the per-language token sets that are the
5// point of these files. Allowed at the module level rather than per
6// function so the per-language impl blocks stay readable.
7#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
8
9use std::path::Path;
10use std::sync::Arc;
11
12use crate::abc::Abc;
13use crate::alterator::Alterator;
14use crate::checker::Checker;
15use crate::cognitive::Cognitive;
16use crate::cyclomatic::Cyclomatic;
17use crate::exit::Exit;
18use crate::getter::Getter;
19use crate::halstead::Halstead;
20use crate::langs::*;
21use crate::loc::Loc;
22use crate::mi::Mi;
23use crate::nargs::NArgs;
24use crate::node::Node;
25use crate::nom::Nom;
26use crate::npa::Npa;
27use crate::npm::Npm;
28use crate::parser::Filter;
29use crate::preproc::PreprocResults;
30use crate::tokens::Tokens;
31use crate::wmc::Wmc;
32
33/// A trait for callback functions.
34///
35/// Allows to call a private library function, getting as result
36/// its output value.
37pub trait Callback {
38    /// The output type returned by the callee
39    type Res;
40    /// The input type used by the caller to pass the arguments to the callee
41    type Cfg;
42
43    /// Calls a function inside the library and returns its value
44    fn call<T: ParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res;
45}
46
47/// Static identification of a language code tag.
48///
49/// Implemented by every `XxxCode` type generated by the internal
50/// `mk_code!` macro.
51pub trait LanguageInfo {
52    /// Tree-sitter base language enum carried by this code tag.
53    type BaseLang;
54
55    /// Returns the workspace-level [`LANG`] variant for this code tag.
56    fn get_lang() -> LANG;
57    /// Returns the human-readable language name for this code tag.
58    fn get_lang_name() -> &'static str;
59}
60
61// Internal language-dispatch trait kept `pub` so the macro-generated
62// `Parser<T>` impls in `src/parser.rs` and the `action::<T>` /
63// `Callback` plumbing in `src/macros.rs` and `src/traits.rs` can refer
64// to it, but marked `#[doc(hidden)]` so it does not appear in the
65// curated rustdoc surface (per issue #256). The 15 associated types
66// are not part of any documented extension contract — downstream
67// consumers should drive metric extraction through the non-generic
68// `analyze` / `metrics_from_tree` entry points, not by implementing
69// this trait. See STABILITY.md.
70#[doc(hidden)]
71pub trait ParserTrait {
72    type Checker: Alterator + Checker;
73    type Getter: Getter;
74    type Cognitive: Cognitive;
75    type Cyclomatic: Cyclomatic;
76    type Halstead: Halstead;
77    type Loc: Loc;
78    type Nom: Nom;
79    type Mi: Mi;
80    type NArgs: NArgs;
81    type Exit: Exit;
82    type Wmc: Wmc;
83    type Abc: Abc;
84    type Npm: Npm;
85    type Npa: Npa;
86    type Tokens: Tokens;
87
88    fn new(code: Vec<u8>, path: &Path, pr: Option<Arc<PreprocResults>>) -> Self;
89    fn get_language(&self) -> LANG;
90    fn get_root(&self) -> Node<'_>;
91    fn get_code(&self) -> &[u8];
92    fn get_filters(&self, filters: &[String]) -> Filter;
93}
94
95pub(crate) trait Search<'a> {
96    fn first_occurrence(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
97    fn act_on_node(&self, pred: &mut dyn FnMut(&Node<'a>));
98    fn first_child(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
99    fn act_on_child(&self, action: &mut dyn FnMut(&Node<'a>));
100}