AST & Node Analysis
Relevant source files
The following files were used as context for generating this wiki page:
AST & Node Analysis is a core component of rust-code-analysis used to represent and inspect the syntactic structure of source code across multiple programming languages. By leveraging the Tree Sitter incremental parsing library, the system generates an Abstract Syntax Tree (AST) where every syntactic construct is represented as a node.
This analysis allows the library to perform tasks such as error detection, node counting, and providing the underlying structural data required to compute complex Supported Metrics. The system supports various languages including C++, Rust, Python, and Java, providing a unified interface for code inspection and maintainability analysis.
Sources: README.md, rust-code-analysis-book/src/commands/README.md, src/lib.rs:1-40
AST Architecture and Logic
The AST serves as the primary data structure for all static analysis within the project. It transforms raw source code into a hierarchical tree of nodes, where each node corresponds to a specific language construct (e.g., statements, expressions, or comments).
Analysis Workflow
The analysis follows a linear progression from raw source to actionable structural data:
The workflow involves parsing the source code into a Tree Sitter AST, which is then traversed by specialized modules to extract metrics or identify specific code patterns.
Sources: README.md, src/lib.rs:42-100
Node Inspection and Management
Nodes are the fundamental units of the AST. The library provides utilities for traversing these nodes and extracting specific metadata such as line spans and kind identifiers.
| Component | Description |
|---|---|
| Node | Represents a syntactic construct in the AST. Contains row/column data and kind info. |
| AST Traversal | Utilities like traverse_children and get_parent to navigate the tree. |
| ParserTrait | A trait implemented by language-specific parsers to standardize AST generation. |
| Dump | A utility to export the AST structure to a readable format (e.g., stdout). |
Sources: src/lib.rs:56-78, rust-code-analysis-book/src/commands/nodes.md
Detailed Functional Modules
1. Error Detection
The system identifies syntactic errors by searching for nodes of a specific "error" type within the AST. This is exposed via the CLI using the -f error flag, which filters the AST for nodes that represent parsing failures.
Sources: rust-code-analysis-book/src/commands/nodes.md, rust-code-analysis-cli/src/main.rs:109-115
2. Metric Extraction from Nodes
Metrics are computed by analyzing node distributions and relationships. For example, the Loc (Lines of Code) metric suite relies on checking the kind_id of nodes to distinguish between code, comments, and blank lines.
The logic for metric computation is language-dependent, as seen in the implementation of the Loc trait for different language parsers.
Sources: src/metrics/loc.rs:260-350, src/metrics/loc.rs:374-420
3. CLI Node Operations
The rust-code-analysis-cli provides direct interaction with the AST through several command-line arguments:
- Counting: Use
--count <NODE_TYPE>to quantify specific constructs. - Dumping: Use
-dor--dumpto print the entire tree structure. - Portion Analysis: Narrow analysis to specific ranges using
--ls(line start) and--le(line end).
Sources: rust-code-analysis-cli/src/main.rs:188-215, rust-code-analysis-book/src/commands/nodes.md
AST Node Data Structure Examples
Internally, the analysis logic matches specific node types to update statistical counters. Below is an example of how Python nodes are categorized for Line of Code (Loc) analysis:
// Simplified logic from src/metrics/loc.rs
match node.kind_id().into() {
Comment => {
add_cloc_lines(stats, start, end);
}
Statement | IfStatement | ForStatement | ReturnStatement => {
stats.lloc.logical_lines += 1;
}
_ => {
stats.ploc.lines.insert(start);
}
}
Sources: src/metrics/loc.rs:265-300
Visualization and Output
The dump_root and dump_space functions in the output module are responsible for rendering the AST and its associated metrics into a visual hierarchy. This is often used by developers to debug how the library perceives a particular file's structure.
Sources: src/output/dump_metrics.rs:40-80
Summary
AST & Node Analysis provides the structural foundation for the rust-code-analysis project. By converting source code into a standardized tree of nodes via Tree Sitter, the library enables cross-language metric computation, syntactic error detection, and deep code inspection. The CLI and Web components act as interfaces to this underlying tree, allowing users to count, dump, and analyze code constructs with high precision.