big_code_analysis_ast/parser.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
9//! [`Parser<T>`](Parser) and the node [`Filter`]s `count` / `find` accept.
10
11use std::marker::PhantomData;
12use std::path::Path;
13use std::sync::Arc;
14
15use crate::checker::Checker;
16use crate::node::Ancestors;
17
18use crate::alterator::Alterator;
19use crate::getter::Getter;
20
21use crate::c_macro;
22use crate::langs::*;
23use crate::node::{Node, Tree};
24use crate::preproc::{PreprocResults, visible_macros};
25use crate::traits::*;
26
27/// Parsed source plus the tree-sitter `Tree` for a given language `T`.
28///
29/// Construct with [`Parser::new`] and feed the result into the metric,
30/// alterator, or AST-dump entry points. The type parameter `T` is one
31/// of the language code tags (`RustCode`, `PythonCode`, etc.) declared
32/// by the internal `mk_code!` macro.
33#[derive(Debug)]
34pub struct Parser<T: LanguageInfo + Alterator + Checker + Getter> {
35 code: Vec<u8>,
36 tree: Tree,
37 phantom: PhantomData<T>,
38}
39
40/// A single node-matching predicate. The `'a` bound lets a predicate
41/// borrow the parser's source buffer, which the `"function"` and
42/// `"string"` filters need to answer for a language whose functions or
43/// string literals are identified by their text rather than their kind
44/// (#1162, #1381).
45///
46/// The `Ancestors` argument is the chain the caller descended through.
47/// Both of those predicates ask about an enclosing construct, and
48/// resolving one from the node alone costs [`Node::parent`]'s
49/// `O(depth)` *per lookup* — which over a whole walk is the quadratic
50/// #1052 and #1122 warn about. The higher-ranked bound ties the chain's
51/// tree lifetime to the node's, so a predicate cannot be handed the
52/// ancestry of a different tree.
53type FilterFn<'a> = dyn for<'t, 'c> Fn(&Node<'t>, Ancestors<'t, 'c>) -> bool + 'a;
54
55/// Collection of node-matching predicates the `find` and `count` walks
56/// apply to each node they visit.
57pub struct Filter<'a> {
58 filters: Vec<Box<FilterFn<'a>>>,
59}
60
61impl Filter<'_> {
62 /// Returns `true` if *any* of the configured predicates matches
63 /// `node`, reached through `ancestors`.
64 ///
65 /// A walker that maintains an ancestor chain should pass it. The
66 /// `"function"` and `"string"` predicates ask about an enclosing
67 /// construct, and off [`Ancestors::unknown`] each such lookup is
68 /// [`Node::parent`]'s `O(depth)` — which made `count --type string`
69 /// quadratic in nesting depth until `find` and `count` threaded a
70 /// real chain (#1381).
71 #[must_use]
72 pub fn any<'t>(&self, node: &Node<'t>, ancestors: Ancestors<'t, '_>) -> bool {
73 for f in &self.filters {
74 if f(node, ancestors) {
75 return true;
76 }
77 }
78 false
79 }
80}
81
82#[inline]
83fn get_fake_code<T: LanguageInfo>(
84 code: &[u8],
85 path: &Path,
86 pr: Option<Arc<PreprocResults>>,
87) -> Option<Vec<u8>> {
88 if let Some(pr) = pr {
89 match T::lang() {
90 // The C-family languages share the preprocessor
91 // macro-replacement pass: `C` (#721), upstream `Cpp`, and the
92 // `Mozcpp` Gecko dialect (#720) all need `#define` expansion.
93 LANG::C | LANG::Cpp | LANG::Mozcpp => {
94 let macros = visible_macros(path, &pr.files);
95 c_macro::replace(code, ¯os)
96 }
97 _ => None,
98 }
99 } else {
100 None
101 }
102}
103
104impl<T: 'static + LanguageInfo + Alterator + Checker + Getter> ParserTrait for Parser<T> {
105 type Checker = T;
106 type Getter = T;
107
108 fn new(code: Vec<u8>, path: &Path, pr: Option<Arc<PreprocResults>>) -> Self {
109 let fake_code = get_fake_code::<T>(&code, path, pr);
110 let code = if let Some(fake) = fake_code {
111 fake
112 } else {
113 code
114 };
115
116 let tree = Tree::new::<T>(&code);
117
118 Self {
119 code,
120 tree,
121 phantom: PhantomData,
122 }
123 }
124
125 #[inline]
126 fn root(&self) -> Node<'_> {
127 self.tree.get_root()
128 }
129
130 #[inline]
131 fn code(&self) -> &[u8] {
132 &self.code
133 }
134
135 fn filters(&self, requested: &[String]) -> Filter<'_> {
136 // Borrowed by the `"function"` and `"string"` arms below, which
137 // is why `Filter` carries a lifetime.
138 let code = self.code();
139 let mut res: Vec<Box<FilterFn<'_>>> = Vec::new();
140 for f in requested {
141 let f = f.as_str();
142 match f {
143 "all" => res.push(Box::new(|_: &Node, _| -> bool { true })),
144 // `is_call` / `is_comment` / `is_error` take `&Node` and
145 // nothing else, so no language *can* make them
146 // text-dependent. The #1162 gap is confined by
147 // construction to the `Checker` predicates that accept
148 // `code`, and `"function"` and `"string"` are the two
149 // filters that reach one.
150 "call" => res.push(Box::new(|node: &Node, _| T::is_call(node))),
151 "comment" => res.push(Box::new(|node: &Node, _| T::is_comment(node))),
152 "error" => res.push(Box::new(|node: &Node, _| T::is_error(node))),
153 // `is_string_with_code`, not `is_string`: a Tcl-family
154 // `braced_word` is a string literal in a value position
155 // and a `proc` / `when` body everywhere else, and only
156 // the bytes of the enclosing command's leading word
157 // separate the two (#1381). This arm is the only caller
158 // of either spelling in the workspace, so the byte-less
159 // one now serves purely as the per-language kind table
160 // that each override narrows.
161 //
162 // This is also the arm that makes the chain load-bearing
163 // rather than merely cheaper. `braced_word` is *every*
164 // Tcl block and list literal, so the candidate set is
165 // dense, and the Tcl override asks about the enclosing
166 // command — roughly ten ancestor lookups per candidate.
167 // Off an unknown chain each is `Node::parent`'s
168 // `O(depth)`, which took `bca find --type string` on 8 KB
169 // of nested braces from 5 ms to 823 ms before the chain
170 // was threaded here.
171 "string" => res.push(Box::new(move |node: &Node, ancestors| {
172 T::is_string_with_code(node, code, ancestors)
173 })),
174 // The JS-family `is_func` and Elixir's `is_func_with_code`
175 // consult the chain to tell a named function from a
176 // closure and a `def` `Call` from any other (#1088,
177 // #1162). Both answer the same off an unknown chain —
178 // only the cost differs, and `find` / `count` now supply
179 // a real one.
180 "function" => res.push(Box::new(move |node: &Node, ancestors| {
181 T::is_func_with_code(node, code, ancestors)
182 })),
183 _ => {
184 if let Ok(n) = f.parse::<u16>() {
185 // A numeric `-t`/`--type` value matches by raw
186 // tree-sitter `kind_id` rather than node-type name.
187 // This is an escape hatch for grammar inspection
188 // (matching a kind that has no stable name, or one
189 // the string path mis-resolves), but `kind_id` is an
190 // index into the grammar's symbol table and is *not*
191 // stable across grammar versions — the same number
192 // names different nodes after a bump, and `-t 0` is
193 // the end/ERROR sentinel. Documented as unstable in
194 // big-code-analysis-book/src/commands/nodes.md; the
195 // string (`kind()`) path below is the supported one.
196 res.push(Box::new(move |node: &Node, _| -> bool {
197 node.kind_id() == n
198 }));
199 } else {
200 // Exact match on `node.kind()` — the CLI documents
201 // `find <NODE>` / `count <NODE_TYPE>` as searching
202 // for a specific node type, not a substring (see
203 // big-code-analysis-book/src/commands/nodes.md and
204 // issue #293).
205 let f = f.to_owned();
206 res.push(Box::new(move |node: &Node, _| -> bool { node.kind() == f }));
207 }
208 }
209 }
210 }
211 if res.is_empty() {
212 res.push(Box::new(|_: &Node, _| -> bool { true }));
213 }
214
215 Filter { filters: res }
216 }
217}
218
219impl<T: 'static + LanguageInfo + Alterator + Checker + Getter> Parser<T> {
220 /// Builds a [`Parser`] from a pre-parsed [`tree_sitter::Tree`]
221 /// and the matching source bytes.
222 ///
223 /// Use this when the caller already drives `tree-sitter` for
224 /// other purposes (e.g. an editor doing incremental reparsing)
225 /// and wants the metric walker to reuse the parse instead of
226 /// running its own. The standard byte-based entry point
227 /// remains [`ParserTrait::new`].
228 ///
229 /// The supplied `tree` must have been produced from `code` with
230 /// the tree-sitter language matching `T` — typically obtained
231 /// via [`crate::LANG::tree_sitter_language`]. A mismatch is
232 /// not `unsafe`, but metric values will be nonsensical because
233 /// the tree's `kind_id` values will not correspond to the per-
234 /// language enum the metric `compute` functions match on.
235 #[must_use]
236 pub fn from_tree(tree: tree_sitter::Tree, code: Vec<u8>) -> Self {
237 Self {
238 code,
239 tree: Tree::from_ts_tree(tree),
240 phantom: PhantomData,
241 }
242 }
243
244 /// Borrow the underlying [`tree_sitter::Tree`] for callers that
245 /// want to drive their own traversal alongside the metric walker.
246 ///
247 /// `Parser` is `pub` in this crate but carries no stability
248 /// promise (see the crate root); the stable spelling of this
249 /// accessor is `big_code_analysis::Ast::as_tree_sitter`.
250 #[must_use]
251 pub fn ts_tree(&self) -> &tree_sitter::Tree {
252 self.tree.as_ts_tree()
253 }
254}
255
256#[cfg(test)]
257mod tests {
258 use crate::count::count;
259 use crate::langs::PythonParser;
260 use crate::traits::ParserTrait;
261 use std::path::PathBuf;
262
263 #[cfg(any(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
264 fn parse_python(source: &str) -> PythonParser {
265 PythonParser::new(source.as_bytes().to_vec(), &PathBuf::from("t.py"), None)
266 }
267
268 #[cfg(any(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
269 fn count_kind(source: &str, filter: &str) -> usize {
270 count(&parse_python(source), &[filter.to_string()]).0
271 }
272
273 // Regression for #293: a named filter that is not a hardcoded
274 // keyword (`all`/`call`/`comment`/`error`/`string`/`function`) and
275 // not a numeric `kind_id` must match `node.kind()` exactly, not via
276 // substring containment.
277
278 #[cfg(all(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
279 #[test]
280 fn get_filters_exact_match_hits_named_kind() {
281 // Python's `if`/`elif`/`else` clauses each appear as their own
282 // `if_statement` / `elif_clause` / `else_clause` nodes. Filter
283 // `if_statement` should match exactly one node here.
284 let src = "if x:\n pass\nelif y:\n pass\nelse:\n pass\n";
285 assert_eq!(count_kind(src, "if_statement"), 1);
286 }
287
288 #[cfg(all(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
289 #[test]
290 fn get_filters_no_substring_match() {
291 // Filter `expression` must not match `expression_statement`,
292 // `binary_expression`, etc. Under the old substring behaviour
293 // every expression-bearing node would count; under exact-match
294 // there is no node literally named `expression` in this source.
295 let src = "x = 1 + 2\ny = foo(3)\n";
296 assert_eq!(
297 count_kind(src, "expression"),
298 0,
299 "exact match must not collapse all *_expression kinds"
300 );
301 // Sanity: `assignment` is a real node kind in tree-sitter-python
302 // and matches exactly twice (one per statement).
303 assert_eq!(count_kind(src, "assignment"), 2);
304 }
305
306 #[cfg(all(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
307 #[test]
308 fn get_filters_unknown_kind_returns_empty() {
309 // A filter that names no real node kind matches nothing — the
310 // previous substring behaviour would still return 0 here, but
311 // pinning it guards against a future regression that re-enables
312 // fuzzy matching.
313 let src = "x = 1\n";
314 assert_eq!(count_kind(src, "definitely_not_a_python_kind"), 0);
315 }
316
317 #[cfg(all(feature = "c", feature = "cpp", feature = "mozcpp", feature = "python"))]
318 #[test]
319 fn get_filters_empty_request_matches_every_node() {
320 // Requesting nothing means "match everything": `filters` falls
321 // back to a match-all predicate when no arm pushed one, because
322 // a `Filter` holding no predicates would make `Filter::any`
323 // return `false` for every node and `bca find` / `bca count`
324 // silently report zero on a bare invocation.
325 //
326 // Asserting against `"all"` rather than a hand-counted total is
327 // what makes this able to fail: the two paths push the identical
328 // closure, so dropping the fallback sends the empty request to 0
329 // while `"all"` stays put. A `> 0` assertion could not tell the
330 // two apart from a source that simply had nodes.
331 let src = "if x:\n pass\nelse:\n y = foo(1 + 2)\n";
332 let parser = parse_python(src);
333 let everything = count(&parser, &["all".to_string()]).0;
334 let unfiltered = count(&parser, &[]).0;
335
336 assert!(
337 everything > 1,
338 "fixture must hold several nodes or this asserts nothing; got {everything}"
339 );
340 assert_eq!(
341 unfiltered, everything,
342 "an empty filter request must match what `all` matches"
343 );
344 }
345}