srcmetrics 0.2.0

Language-independent source code metrics (size, complexity, nesting, Halstead, duplication, dependencies, documentation) for C, C++, Go, Java, JavaScript, Python, Rust and TypeScript
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Generic tree-sitter -> Common IR converter driven by per-language `Mapping` tables (ADR-0004).

use super::LanguageAdapter;
use crate::error::AnalysisError;
use crate::ir::{
    File, Function, FunctionId, Node, NodeId, NodeKind, Parameter, Position, SourceRange, Token,
    TokenKind,
};
use std::collections::HashMap;
use tree_sitter::{Parser, Point};

/// Declarative description of one language. Everything language-specific lives here.
pub struct Mapping {
    pub language: &'static str,
    pub extensions: &'static [&'static str],
    pub grammar: fn() -> tree_sitter::Language,
    /// Grammar crate name and version, reported as the parser version (design principle P10).
    /// Checked against Cargo.lock by tests.
    pub grammar_crate: (&'static str, &'static str),
    /// tree-sitter node type -> IR node kind. Unlisted named nodes become `Other`.
    pub kinds: &'static [(&'static str, NodeKind)],
    /// Fields of a call node holding the callee, tried in order (ADR-0012).
    pub callee_fields: &'static [&'static str],
    /// Operators that turn a `Binary` node into a `Logical` (short-circuit) node.
    pub logical_operators: &'static [&'static str],
    /// Keyword that marks a `Case` node as the default label (not a decision point).
    pub default_case_keyword: Option<&'static str>,
    /// For grammars without an else-clause node (Go, Java): the field of a `Branch` node holding
    /// its else part. A child in this field that is not itself a `Branch` becomes an `Else`.
    pub else_field: Option<&'static str>,
    /// Node types that become a single comment token.
    pub comments: &'static [&'static str],
    /// Node types that become literal tokens; their subtree is not walked, except for interpolations.
    pub literals: &'static [&'static str],
    /// Node types of code embedded in a literal (e.g. `${...}`, f-string `{...}`), walked as code.
    pub interpolations: &'static [&'static str],
    /// Leaf node types that become identifier tokens.
    pub identifiers: &'static [&'static str],
    /// Field names followed, in order, to find the identifier naming a function or parameter.
    pub name_fields: &'static [&'static str],
    /// Node types written between a function's documentation and the function itself
    /// (Python decorators, Rust attributes); skipped when looking for documentation (ADR-0013).
    pub decorators: &'static [&'static str],
    /// Literal node type that documents a function when it is the first statement of its body
    /// (Python docstrings). Takes precedence over a preceding comment (ADR-0013).
    pub docstring: Option<&'static str>,
    /// Fields holding a function's parameters, in order (e.g. Go: `receiver`, `parameters`).
    /// Looked up on the function node and along its name-field chain.
    pub parameter_fields: &'static [&'static str],
    /// Parameter node texts that are not parameters (e.g. C `f(void)`).
    pub ignored_parameters: &'static [&'static str],
}

/// tree-sitter runtime version. Checked against Cargo.lock by tests.
pub const TREE_SITTER_VERSION: &str = "0.27.0";

pub struct TreeSitterAdapter {
    mapping: &'static Mapping,
}

impl TreeSitterAdapter {
    pub const fn new(mapping: &'static Mapping) -> Self {
        TreeSitterAdapter { mapping }
    }
}

impl LanguageAdapter for TreeSitterAdapter {
    fn language(&self) -> &'static str {
        self.mapping.language
    }

    fn extensions(&self) -> &'static [&'static str] {
        self.mapping.extensions
    }

    fn parser_version(&self) -> String {
        let (name, version) = self.mapping.grammar_crate;
        format!("tree-sitter {TREE_SITTER_VERSION} / {name} {version}")
    }

    fn to_ir(&self, path: &str, source: &str) -> Result<File, AnalysisError> {
        let mut parser = Parser::new();
        parser
            .set_language(&(self.mapping.grammar)())
            .map_err(|e| AnalysisError::IrConversion {
                path: path.into(),
                message: format!("incompatible tree-sitter grammar: {e}"),
            })?;
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| AnalysisError::IrConversion {
                path: path.into(),
                message: "tree-sitter returned no tree".into(),
            })?;
        let root = tree.root_node();
        if root.has_error() {
            return Err(syntax_error(path, root));
        }
        Converter {
            mapping: self.mapping,
            source,
            nodes: vec![],
            tokens: vec![],
            ts_to_ir: HashMap::new(),
        }
        .convert(path, root)
    }
}

/// Finds the first ERROR or MISSING node and reports its position.
fn syntax_error(path: &str, root: tree_sitter::Node) -> AnalysisError {
    let mut cursor = root.walk();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        if node.is_error() || node.is_missing() {
            let p = node.start_position();
            let message = if node.is_missing() {
                format!("missing `{}`", node.kind())
            } else {
                "unexpected syntax".to_string()
            };
            return AnalysisError::Parse {
                path: path.into(),
                line: p.row + 1,
                column: p.column,
                message,
            };
        }
        let children: Vec<_> = node
            .children(&mut cursor)
            .filter(|c| c.has_error() || c.is_missing())
            .collect();
        stack.extend(children.into_iter().rev());
    }
    unreachable!("syntax_error is only called when the tree has an error")
}

struct Converter<'a> {
    mapping: &'static Mapping,
    source: &'a str,
    nodes: Vec<Node>,
    tokens: Vec<Token>,
    /// tree-sitter node id -> IR node id, for resolving function bodies.
    ts_to_ir: HashMap<usize, NodeId>,
}

impl<'a> Converter<'a> {
    /// Walks the concrete syntax tree once, iteratively (deep expressions must not overflow the stack).
    /// Named nodes become IR nodes; leaves and comments become tokens. A literal becomes literal
    /// tokens for its text, and only its interpolated code (e.g. `${...}`) is walked further.
    fn convert(mut self, path: &str, root: tree_sitter::Node<'a>) -> Result<File, AnalysisError> {
        let mut function_nodes = vec![];
        let mut stack: Vec<(tree_sitter::Node, Option<NodeId>, Option<&str>)> =
            vec![(root, None, None)];
        while let Some((ts, parent, field)) = stack.pop() {
            let kind = ts.kind();
            if self.mapping.comments.contains(&kind) {
                self.push_token(TokenKind::Comment, ts.byte_range(), range(ts));
                continue;
            }
            let literal = self.mapping.literals.contains(&kind);
            let children = if literal {
                self.push_literal(ts)
            } else {
                self.push_leaf(ts)
            };
            let ir_parent = if ts.is_named() {
                let id = self.push_node(ts, parent, field);
                if self.nodes[id.0].kind == NodeKind::Function {
                    function_nodes.push((ts, id));
                }
                Some(id)
            } else {
                parent
            };
            stack.extend(children.into_iter().rev().map(|(c, f)| (c, ir_parent, f)));
        }
        // Interpolated code is tokenized after its enclosing literal's text; restore source order.
        self.tokens.sort_by_key(|t| t.range.start.offset);
        let functions = function_nodes
            .into_iter()
            .enumerate()
            .map(|(i, (ts, id))| self.function(FunctionId(i), ts, id))
            .collect();
        Ok(File {
            path: path.into(),
            language: self.mapping.language.into(),
            source: self.source.into(),
            nodes: self.nodes,
            root: NodeId(0),
            tokens: self.tokens,
            functions,
        })
    }

    fn push_node(
        &mut self,
        ts: tree_sitter::Node,
        parent: Option<NodeId>,
        field: Option<&str>,
    ) -> NodeId {
        let id = NodeId(self.nodes.len());
        let mut kind = self.node_kind(ts);
        let parent_is_branch = parent.is_some_and(|p| self.nodes[p.0].kind == NodeKind::Branch);
        if parent_is_branch
            && kind != NodeKind::Branch
            && field.is_some()
            && field == self.mapping.else_field
        {
            kind = NodeKind::Else;
        }
        let label = match kind {
            NodeKind::Call => self.callee_name(ts),
            NodeKind::Logical => ts
                .child_by_field_name("operator")
                .map(|op| self.text(op).to_string()),
            _ => None,
        };
        self.nodes.push(Node {
            id,
            kind,
            parent,
            children: vec![],
            range: range(ts),
            label,
        });
        if let Some(p) = parent {
            self.nodes[p.0].children.push(id);
        }
        self.ts_to_ir.insert(ts.id(), id);
        id
    }

    fn node_kind(&self, ts: tree_sitter::Node) -> NodeKind {
        let kind = self
            .mapping
            .kinds
            .iter()
            .find(|(name, _)| *name == ts.kind())
            .map(|(_, k)| *k)
            .unwrap_or(NodeKind::Other);
        match kind {
            NodeKind::Binary if self.is_logical(ts) => NodeKind::Logical,
            NodeKind::Case if self.is_default_case(ts) => NodeKind::Other,
            k => k,
        }
    }

    /// The last identifier leaf of the callee (`f(x)` -> f, `a.b.c()` -> c, `std::sort()` -> sort).
    fn callee_name(&self, call: tree_sitter::Node) -> Option<String> {
        let callee = self
            .mapping
            .callee_fields
            .iter()
            .find_map(|f| call.child_by_field_name(f))?;
        let mut cursor = callee.walk();
        let mut last = None;
        let mut stack = vec![callee];
        while let Some(n) = stack.pop() {
            if n.child_count() == 0 && self.is_identifier(&n) {
                last = Some(n);
            }
            let children: Vec<_> = n.children(&mut cursor).collect();
            stack.extend(children.into_iter().rev());
        }
        last.map(|n| self.text(n).to_string())
    }

    fn is_logical(&self, ts: tree_sitter::Node) -> bool {
        ts.child_by_field_name("operator")
            .is_some_and(|op| self.mapping.logical_operators.contains(&self.text(op)))
    }

    /// A case whose first token is the default keyword (C `default:`, Java `default ->`).
    fn is_default_case(&self, ts: tree_sitter::Node) -> bool {
        let first_leaf = std::iter::successors(Some(ts), |n| n.child(0)).last();
        self.mapping
            .default_case_keyword
            .is_some_and(|kw| first_leaf.is_some_and(|leaf| leaf.kind() == kw))
    }

    fn push_token(&mut self, kind: TokenKind, bytes: std::ops::Range<usize>, range: SourceRange) {
        self.tokens.push(Token {
            kind,
            text: self.source[bytes].into(),
            range,
        });
    }

    /// Tokenizes a leaf (zero-width leaves such as an empty file's root carry no text and are
    /// skipped) and returns the children to walk.
    fn push_leaf(&mut self, ts: tree_sitter::Node<'a>) -> Vec<Child<'a>> {
        if ts.child_count() == 0 && ts.start_byte() < ts.end_byte() {
            self.push_token(self.classify_leaf(ts), ts.byte_range(), range(ts));
        }
        children_with_fields(ts)
    }

    /// Emits one literal token per stretch of text between interpolations, and returns the
    /// interpolation children, which are walked as ordinary code.
    fn push_literal(&mut self, ts: tree_sitter::Node<'a>) -> Vec<Child<'a>> {
        let mut cursor = ts.walk();
        let interpolations: Vec<_> = ts
            .named_children(&mut cursor)
            .filter(|c| self.mapping.interpolations.contains(&c.kind()))
            .collect();
        let start = |n: &tree_sitter::Node| position(n.start_position(), n.start_byte());
        let end = |n: &tree_sitter::Node| position(n.end_position(), n.end_byte());
        let mut text_from = start(&ts);
        let mut text = vec![];
        for c in &interpolations {
            text.push(SourceRange {
                start: text_from,
                end: start(c),
            });
            text_from = end(c);
        }
        text.push(SourceRange {
            start: text_from,
            end: end(&ts),
        });
        for r in text.into_iter().filter(|r| r.start.offset < r.end.offset) {
            self.push_token(TokenKind::Literal, r.start.offset..r.end.offset, r);
        }
        interpolations.into_iter().map(|c| (c, None)).collect()
    }

    /// Generic token classification rule (ADR-0004).
    fn classify_leaf(&self, ts: tree_sitter::Node) -> TokenKind {
        let text = self.text(ts);
        if self.mapping.identifiers.contains(&ts.kind()) {
            TokenKind::Identifier
        } else if text.chars().any(|c| c.is_alphabetic() || c == '_') {
            TokenKind::Keyword
        } else if matches!(text, "," | ";" | "(" | ")" | "[" | "]" | "{" | "}") {
            TokenKind::Punctuation
        } else {
            TokenKind::Operator
        }
    }

    fn function(&self, id: FunctionId, ts: tree_sitter::Node, node: NodeId) -> Function {
        Function {
            id,
            name: self.name_of(ts),
            parameters: self.parameters(ts),
            node,
            body: ts
                .child_by_field_name("body")
                .map(|b| self.ts_to_ir[&b.id()]),
            range: range(ts),
            doc: self.docstring(ts).or_else(|| self.preceding_comments(ts)),
        }
    }

    /// A docstring: the body's first statement consisting of a single docstring literal.
    fn docstring(&self, ts: tree_sitter::Node) -> Option<SourceRange> {
        let kind = self.mapping.docstring?;
        let first = ts.child_by_field_name("body")?.named_child(0)?;
        let is_statement = self
            .mapping
            .kinds
            .contains(&(first.kind(), NodeKind::Statement));
        let literal = first
            .named_child(0)
            .filter(|l| first.named_child_count() == 1 && l.kind() == kind)?;
        is_statement.then(|| range(literal))
    }

    /// The block of comment tokens directly above the function (or above its decorators): no blank
    /// line in between, and not a trailing comment of a preceding line of code.
    fn preceding_comments(&self, ts: tree_sitter::Node) -> Option<SourceRange> {
        let first = std::iter::successors(Some(ts), |n| {
            n.prev_named_sibling()
                .filter(|p| self.mapping.decorators.contains(&p.kind()))
        })
        .last()?;
        let end = self
            .tokens
            .partition_point(|t| t.range.start.offset < first.start_byte());
        let mut start = end;
        let mut next_line = first.start_position().row + 1;
        while start > 0 {
            let comment = &self.tokens[start - 1];
            let adjacent = comment.range.last_line() + 1 >= next_line;
            let trailing = start >= 2 && {
                let before = &self.tokens[start - 2];
                before.kind != TokenKind::Comment
                    && before.range.last_line() == comment.range.first_line()
            };
            if comment.kind != TokenKind::Comment || !adjacent || trailing {
                break;
            }
            next_line = comment.range.first_line();
            start -= 1;
        }
        (start < end).then(|| SourceRange {
            start: self.tokens[start].range.start,
            end: self.tokens[end - 1].range.end,
        })
    }

    /// Parameters from each of the Mapping's parameter fields, in order. A field holding an
    /// identifier is one parameter (JS `x => x`, Java `x -> ...`); any other node is a list whose
    /// named children are the parameters.
    fn parameters(&self, ts: tree_sitter::Node) -> Vec<Parameter> {
        let mut nodes = vec![];
        for field in self.mapping.parameter_fields {
            match self.find_field(ts, field) {
                Some(single) if self.is_identifier(&single) => nodes.push(single),
                Some(list) => {
                    let mut cursor = list.walk();
                    nodes.extend(
                        list.named_children(&mut cursor)
                            .filter(|p| !self.mapping.comments.contains(&p.kind())),
                    );
                }
                None => {}
            }
        }
        nodes
            .into_iter()
            .filter(|p| !self.mapping.ignored_parameters.contains(&self.text(*p)))
            .flat_map(|p| self.declared_parameters(p))
            .collect()
    }

    /// One parameter per name when a declaration names several (Go `a, b int`), else one.
    fn declared_parameters(&self, p: tree_sitter::Node) -> Vec<Parameter> {
        let mut cursor = p.walk();
        let names: Vec<_> = p.children_by_field_name("name", &mut cursor).collect();
        if names.len() > 1 {
            names
                .into_iter()
                .map(|n| Parameter {
                    name: Some(self.text(n).to_string()),
                    range: range(n),
                })
                .collect()
        } else {
            vec![Parameter {
                name: self.parameter_name(p),
                range: range(p),
            }]
        }
    }

    /// Looks up `field` on `ts`, then along its name-field chain (e.g. C's declarator nesting).
    fn find_field<'t>(
        &self,
        ts: tree_sitter::Node<'t>,
        field: &str,
    ) -> Option<tree_sitter::Node<'t>> {
        std::iter::successors(Some(ts), |n| self.next_name_node(*n))
            .find_map(|n| n.child_by_field_name(field))
    }

    /// Follows name fields until reaching an identifier (function names, and parameters with name fields).
    fn name_of(&self, ts: tree_sitter::Node) -> Option<String> {
        std::iter::successors(Some(ts), |n| self.next_name_node(*n))
            .find(|n| self.is_identifier(n))
            .map(|n| self.text(n).to_string())
    }

    /// A parameter's name: via name fields, or else the last child that is an identifier or has a
    /// name (Python `x: int` and `*args`, Java `String... xs`, where a type may come first).
    fn parameter_name(&self, p: tree_sitter::Node) -> Option<String> {
        let end = std::iter::successors(Some(p), |n| self.next_name_node(*n)).last()?;
        if self.is_identifier(&end) {
            return Some(self.text(end).to_string());
        }
        let mut cursor = end.walk();
        let named: Vec<_> = end.named_children(&mut cursor).collect();
        named.into_iter().rev().find_map(|c| {
            if self.is_identifier(&c) {
                Some(self.text(c).to_string())
            } else {
                self.name_of(c).filter(|_| self.next_name_node(c).is_some())
            }
        })
    }

    fn is_identifier(&self, n: &tree_sitter::Node) -> bool {
        self.mapping.identifiers.contains(&n.kind())
    }

    fn next_name_node<'t>(&self, ts: tree_sitter::Node<'t>) -> Option<tree_sitter::Node<'t>> {
        self.mapping
            .name_fields
            .iter()
            .find_map(|f| ts.child_by_field_name(f))
    }

    fn text(&self, ts: tree_sitter::Node) -> &'a str {
        &self.source[ts.byte_range()]
    }
}

/// A child node and the field it is in.
type Child<'a> = (tree_sitter::Node<'a>, Option<&'a str>);

fn children_with_fields(ts: tree_sitter::Node<'_>) -> Vec<Child<'_>> {
    let mut cursor = ts.walk();
    let mut children = vec![];
    if cursor.goto_first_child() {
        loop {
            children.push((cursor.node(), cursor.field_name()));
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }
    children
}

fn position(p: Point, offset: usize) -> Position {
    Position {
        line: p.row + 1,
        column: p.column,
        offset,
    }
}

fn range(ts: tree_sitter::Node) -> SourceRange {
    SourceRange {
        start: position(ts.start_position(), ts.start_byte()),
        end: position(ts.end_position(), ts.end_byte()),
    }
}