1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Per-language metric and AST modules deliberately consume the macro-
// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
// inside match expressions — explicit imports would list dozens of
// variants per arm and obscure the per-language token sets that are the
// point of these files. Allowed at the module level rather than per
// function so the per-language impl blocks stay readable.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;
use crate::abc::Abc;
use crate::checker::Checker;
use crate::cognitive::Cognitive;
use crate::cyclomatic::Cyclomatic;
use crate::halstead::Halstead;
use crate::loc::Loc;
use crate::mi::Mi;
use crate::nargs::NArgs;
use crate::nexits::Exit;
use crate::nom::Nom;
use crate::npa::Npa;
use crate::npm::Npm;
use crate::tokens::Tokens;
use crate::wmc::Wmc;
use crate::alterator::Alterator;
use crate::getter::Getter;
use crate::c_macro;
use crate::langs::*;
use crate::node::{Node, Tree};
use crate::preproc::{PreprocResults, get_macros};
use crate::traits::*;
/// Parsed source plus the tree-sitter `Tree` for a given language `T`.
///
/// Construct with [`Parser::new`] and feed the result into the metric,
/// alterator, or AST-dump entry points. The type parameter `T` is one
/// of the language code tags (`RustCode`, `PythonCode`, etc.) declared
/// by the internal `mk_code!` macro.
#[derive(Debug)]
pub(crate) struct Parser<
T: LanguageInfo
+ Alterator
+ Checker
+ Getter
+ Abc
+ Cognitive
+ Cyclomatic
+ Exit
+ Halstead
+ Loc
+ Mi
+ NArgs
+ Nom
+ Npa
+ Npm
+ Tokens
+ Wmc,
> {
code: Vec<u8>,
tree: Tree,
phantom: PhantomData<T>,
}
type FilterFn = dyn Fn(&Node) -> bool;
/// Collection of node-matching predicates used by the AST-walking
/// metric and dump routines to decide whether to visit a node.
pub(crate) struct Filter {
filters: Vec<Box<FilterFn>>,
}
impl Filter {
/// Returns `true` if *any* of the configured predicates matches `node`.
#[must_use]
pub fn any(&self, node: &Node) -> bool {
for f in &self.filters {
if f(node) {
return true;
}
}
false
}
}
#[inline]
fn get_fake_code<T: LanguageInfo>(
code: &[u8],
path: &Path,
pr: Option<Arc<PreprocResults>>,
) -> Option<Vec<u8>> {
if let Some(pr) = pr {
match T::lang() {
// The C-family languages share the preprocessor
// macro-replacement pass: `C` (#721), upstream `Cpp`, and the
// `Mozcpp` Gecko dialect (#720) all need `#define` expansion.
LANG::C | LANG::Cpp | LANG::Mozcpp => {
let macros = get_macros(path, &pr.files);
c_macro::replace(code, ¯os)
}
_ => None,
}
} else {
None
}
}
impl<
T: 'static
+ LanguageInfo
+ Alterator
+ Checker
+ Getter
+ Abc
+ Cognitive
+ Cyclomatic
+ Exit
+ Halstead
+ Loc
+ Mi
+ NArgs
+ Nom
+ Npa
+ Npm
+ Tokens
+ Wmc,
> ParserTrait for Parser<T>
{
type Checker = T;
type Getter = T;
type Cognitive = T;
type Cyclomatic = T;
type Halstead = T;
type Loc = T;
type Nom = T;
type Mi = T;
type NArgs = T;
type Exit = T;
type Wmc = T;
type Abc = T;
type Npm = T;
type Npa = T;
type Tokens = T;
fn new(code: Vec<u8>, path: &Path, pr: Option<Arc<PreprocResults>>) -> Self {
let fake_code = get_fake_code::<T>(&code, path, pr);
let code = if let Some(fake) = fake_code {
fake
} else {
code
};
let tree = Tree::new::<T>(&code);
Self {
code,
tree,
phantom: PhantomData,
}
}
#[inline]
fn root(&self) -> Node<'_> {
self.tree.get_root()
}
#[inline]
fn code(&self) -> &[u8] {
&self.code
}
fn filters(&self, requested: &[String]) -> Filter {
let mut res: Vec<Box<FilterFn>> = Vec::new();
for f in requested {
let f = f.as_str();
match f {
"all" => res.push(Box::new(|_: &Node| -> bool { true })),
"call" => res.push(Box::new(T::is_call)),
"comment" => res.push(Box::new(T::is_comment)),
"error" => res.push(Box::new(T::is_error)),
"string" => res.push(Box::new(T::is_string)),
"function" => res.push(Box::new(T::is_func)),
_ => {
if let Ok(n) = f.parse::<u16>() {
// A numeric `-t`/`--type` value matches by raw
// tree-sitter `kind_id` rather than node-type name.
// This is an escape hatch for grammar inspection
// (matching a kind that has no stable name, or one
// the string path mis-resolves), but `kind_id` is an
// index into the grammar's symbol table and is *not*
// stable across grammar versions — the same number
// names different nodes after a bump, and `-t 0` is
// the end/ERROR sentinel. Documented as unstable in
// big-code-analysis-book/src/commands/nodes.md; the
// string (`kind()`) path below is the supported one.
res.push(Box::new(move |node: &Node| -> bool { node.kind_id() == n }));
} else {
// Exact match on `node.kind()` — the CLI documents
// `find <NODE>` / `count <NODE_TYPE>` as searching
// for a specific node type, not a substring (see
// big-code-analysis-book/src/commands/nodes.md and
// issue #293).
let f = f.to_owned();
res.push(Box::new(move |node: &Node| -> bool { node.kind() == f }));
}
}
}
}
if res.is_empty() {
res.push(Box::new(|_: &Node| -> bool { true }));
}
Filter { filters: res }
}
}
impl<
T: 'static
+ LanguageInfo
+ Alterator
+ Checker
+ Getter
+ Abc
+ Cognitive
+ Cyclomatic
+ Exit
+ Halstead
+ Loc
+ Mi
+ NArgs
+ Nom
+ Npa
+ Npm
+ Tokens
+ Wmc,
> Parser<T>
{
/// Builds a [`Parser`] from a pre-parsed [`tree_sitter::Tree`]
/// and the matching source bytes.
///
/// Use this when the caller already drives `tree-sitter` for
/// other purposes (e.g. an editor doing incremental reparsing)
/// and wants the metric walker to reuse the parse instead of
/// running its own. The standard byte-based entry point
/// remains [`ParserTrait::new`].
///
/// The supplied `tree` must have been produced from `code` with
/// the tree-sitter language matching `T` — typically obtained
/// via [`crate::LANG::tree_sitter_language`]. A mismatch is
/// not `unsafe`, but metric values will be nonsensical because
/// the tree's `kind_id` values will not correspond to the per-
/// language enum the metric `compute` functions match on.
#[must_use]
pub fn from_tree(tree: tree_sitter::Tree, code: Vec<u8>) -> Self {
Self {
code,
tree: Tree::from_ts_tree(tree),
phantom: PhantomData,
}
}
/// Borrow the underlying [`tree_sitter::Tree`] for callers that
/// want to drive their own traversal alongside the metric walker.
///
/// Doc-hidden because `Parser` itself is hidden from the rendered
/// surface; the stable spelling of this accessor is
/// [`crate::Ast::as_tree_sitter`].
#[must_use]
pub fn ts_tree(&self) -> &tree_sitter::Tree {
self.tree.as_ts_tree()
}
}
#[cfg(test)]
mod tests {
use crate::count::count;
use crate::langs::PythonParser;
use crate::traits::ParserTrait;
use std::path::PathBuf;
fn parse_python(source: &str) -> PythonParser {
PythonParser::new(source.as_bytes().to_vec(), &PathBuf::from("t.py"), None)
}
fn count_kind(source: &str, filter: &str) -> usize {
count(&parse_python(source), &[filter.to_string()]).0
}
// Regression for #293: a named filter that is not a hardcoded
// keyword (`all`/`call`/`comment`/`error`/`string`/`function`) and
// not a numeric `kind_id` must match `node.kind()` exactly, not via
// substring containment.
#[test]
fn get_filters_exact_match_hits_named_kind() {
// Python's `if`/`elif`/`else` clauses each appear as their own
// `if_statement` / `elif_clause` / `else_clause` nodes. Filter
// `if_statement` should match exactly one node here.
let src = "if x:\n pass\nelif y:\n pass\nelse:\n pass\n";
assert_eq!(count_kind(src, "if_statement"), 1);
}
#[test]
fn get_filters_no_substring_match() {
// Filter `expression` must not match `expression_statement`,
// `binary_expression`, etc. Under the old substring behaviour
// every expression-bearing node would count; under exact-match
// there is no node literally named `expression` in this source.
let src = "x = 1 + 2\ny = foo(3)\n";
assert_eq!(
count_kind(src, "expression"),
0,
"exact match must not collapse all *_expression kinds"
);
// Sanity: `assignment` is a real node kind in tree-sitter-python
// and matches exactly twice (one per statement).
assert_eq!(count_kind(src, "assignment"), 2);
}
#[test]
fn get_filters_unknown_kind_returns_empty() {
// A filter that names no real node kind matches nothing — the
// previous substring behaviour would still return 0 here, but
// pinning it guards against a future regression that re-enables
// fuzzy matching.
let src = "x = 1\n";
assert_eq!(count_kind(src, "definitely_not_a_python_kind"), 0);
}
}