pub struct AST { /* private fields */ }Expand description
Represents the Abstract Syntax Tree (AST) for Ada source code.
Manages a collection of NodeData instances in an indextree::Arena, with a root node
anchoring the tree. Provides methods to extract nodes, build the tree, and analyze Ada code
for coding standard enforcement.
§Fields
arena: Theindextreearena holding all nodes.root_id: The ID of the synthetic root node.nodes_data: Temporary storage for extracted nodes before building.
§Examples
use ADA_Standards::{AST, NodeData};
let nodes = vec![NodeData::new("Root".to_string(), "RootNode".to_string(), None, None, false)];
let ast = AST::new(nodes);Implementations§
Source§impl AST
impl AST
pub fn new(nodes_data: Vec<NodeData>) -> Self
pub fn associate_end_lines(&mut self, code_text: &str) -> Result<(), ASTError>
pub fn build(&mut self, code_text: &str) -> Result<(), ASTError>
pub fn print_tree(&self)
pub fn output_tree(&self) -> String
pub fn print_nodes_info(&self) -> Result<(), ASTError>
pub fn get_end_keyword(node_type: &str) -> Option<&'static str>
pub fn extract_end_statements( code_text: &str, ) -> Result<Vec<EndStatement>, ASTError>
pub fn extract_packages(code_text: &str) -> Result<Vec<NodeData>, ASTError>
pub fn extract_procedures_functions( code_text: &str, ) -> Result<Vec<NodeData>, ASTError>
pub fn extract_type_declarations( code_text: &str, ) -> Result<Vec<NodeData>, ASTError>
pub fn extract_declare_blocks( code_text: &str, ) -> Result<Vec<NodeData>, ASTError>
pub fn extract_control_flow_nodes( code_text: &str, nodes: &mut Vec<NodeData>, ) -> Result<Vec<NodeData>, ASTError>
pub fn extract_simple_loops(code_text: &str) -> Result<Vec<NodeData>, ASTError>
Sourcepub fn extract_while_loops(code_text: &str) -> Result<Vec<NodeData>, ASTError>
pub fn extract_while_loops(code_text: &str) -> Result<Vec<NodeData>, ASTError>
Extracts while loops from Ada source code.
Parses the provided source code to identify while loops, capturing their loop condition,
start position, and other metadata. Creates NodeData instances for each while loop,
setting the node_type to “WhileLoop” and populating fields like conditions,
start_line, start_index, and body_start. Fields like end_line and end_index
are set to None, to be resolved later by associate_end_lines. This function is part
of the node extraction phase, enabling analysis of while loops for Ada coding standards,
such as ensuring termination conditions or limiting loop complexity.
§Parameters
code_text: The Ada source code to parse, as a string slice.
§Returns
Ok(Vec<NodeData>): A vector ofNodeDatainstances, each representing awhileloop.Err(ASTError): If the regular expression for parsing fails to compile or another error occurs.
§Errors
ASTError::RegexError: If the regex pattern forwhileloops cannot be compiled.
§Notes
- Captures the condition after
whileup toloop, storing it inconditions.list. - Sets
body_startto the index afterloop, marking the start of the loop body. - Ignores comments and nested constructs during initial extraction.
- Use with
buildandassociate_end_linesto complete node metadata.
pub fn extract_for_loops(code_text: &str) -> Result<Vec<NodeData>, ASTError>
pub fn extract_statement_nodes( code_text: &str, nodes: &mut Vec<NodeData>, ) -> Result<Vec<NodeData>, ASTError>
Sourcepub fn extract_if_statements(code_text: &str) -> Result<Vec<NodeData>, ASTError>
pub fn extract_if_statements(code_text: &str) -> Result<Vec<NodeData>, ASTError>
Extracts if statements from Ada source code.
Parses the provided source code to identify if statements, capturing their conditions,
start position, and other metadata. Creates NodeData instances for each if statement,
setting the node_type to “IfStatement” and populating fields like conditions,
start_line, start_index, and body_start. Fields like end_line and end_index
are set to None, to be resolved later by associate_end_lines. This function is part
of the node extraction phase, enabling analysis of if statements for Ada coding standards,
such as condition complexity or proper nesting.
§Parameters
code_text: The Ada source code to parse, as a string slice.
§Returns
Ok(Vec<NodeData>): A vector ofNodeDatainstances, each representing anifstatement.Err(ASTError): If the regular expression for parsing fails to compile or another error occurs.
§Errors
ASTError::RegexError: If the regex pattern forifstatements cannot be compiled.
§Notes
- Captures the condition after
ifup tothen, storing it inconditions.list. - Sets
body_startto the index afterthen, marking the start of theifbody. - Ignores comments and nested constructs during initial extraction.
- Use with
buildandassociate_end_linesto complete node metadata.
Sourcepub fn extract_case_statements(
code_text: &str,
) -> Result<Vec<NodeData>, ASTError>
pub fn extract_case_statements( code_text: &str, ) -> Result<Vec<NodeData>, ASTError>
Extracts case statements from Ada source code.
Identifies case statements using a regex pattern, capturing their switch expression and
body start position. Creates NodeData instances for each case statement, setting cases
to None for later population by populate_cases.
§Parameters
code_text: The Ada source code to parse.
§Returns
Ok(Vec<NodeData>): A vector ofNodeDatainstances for case statements.Err(ASTError): If the regex pattern fails to compile.
Sourcepub fn populate_cases(&mut self, code_text: &str) -> Result<(), ASTError>
pub fn populate_cases(&mut self, code_text: &str) -> Result<(), ASTError>
Populates the cases field for all CaseStatement nodes in the AST.
This method traverses the AST, identifies nodes with node_type “CaseStatement”, and extracts
the list of case alternatives (e.g., “when X =>”) from their body text, defined by body_start
and end_index. It is called after build to ensure all nodes have valid start and end positions.
The extracted cases are stored in the cases field, enabling analysis like checking for complete
coverage or duplicate cases in Ada code.
§Parameters
code_text: The full Ada source code from which to extract case bodies.
§Returns
Ok(())if cases are populated successfully.Err(ASTError)if a node lacks required positions (body_startorend_index) or if parsing fails.
§Errors
ASTError::NodeNotInArenaif a node ID is invalid.ASTError::InvalidNodeDataifbody_startorend_indexis missing.
§Notes
- Must be called after
buildto ensureend_indexis set. - Handles nested case statements by checking indentation, if implemented.
- Performance depends on the number of case statements and body text size.
Sourcepub fn extract_cases_from_body(body_text: &str) -> Vec<String>
pub fn extract_cases_from_body(body_text: &str) -> Vec<String>
Extracts case alternatives from the body text of a case statement.
Parses the body text to identify “when” clauses (e.g., “when X =>”) and returns their conditions
as a vector of strings. This is a helper function for populate_cases.
§Parameters
body_text: The body text of the case statement (frombody_starttoend_index).
§Returns
A vector of case conditions (e.g., [“1”, “2”, “others”]).