Expand description
§Rust Abstract Syntax Tree (AST) Module
This module defines the abstract syntax tree structure for the Rust language, used to represent parsed Rust code. AST nodes correspond to various constructs in the Rust language, such as functions, structs, enums, modules, expressions, etc.
§AST Node Types
§Top-level Items
Function: Function definitionStruct: Struct definitionEnum: Enum definitionModule: Module definitionUseItem: use statementTrait: trait definitionImpl: impl blockTypeAlias: Type aliasConst: Constant definitionStatic: Static variable definition
§Type System
Type: Type representation (paths, references, tuples, arrays, slices, function pointers)Identifier: IdentifierParam: Function parameterField: Struct field
§Statements and Expressions
Statement: Statements (let, expression statements, return, break, continue)Expr: Expressions (identifiers, literals, binary operations, function calls, field access, control flow, etc.)Block: Code blockPattern: Pattern matching patterns
§Control Flow
If: if expressionWhile: while loopFor: for loopLoop: loopMatch: match expressionMatchArm: match arm
§Usage Example
ⓘ
use oak_rust::ast::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a simple Rust program AST
let root = RustRoot {
items: vec![
Item::Function(Function {
name: Identifier {
name: "main".to_string(),
span: 0..4,
},
params: vec![],
return_type: None,
body: Block {
statements: vec![],
span: 5..7,
},
span: 0..7,
})
],
};
println!("Created Rust AST with {} items", root.items.len());
Ok(())
}§Design Principles
- Completeness: Supports full Rust syntax structures.
- Extensibility: Easy to add new AST node types.
- Type Safety: Uses Rust’s type system to ensure AST validity.
- Performance: Efficient memory usage and access patterns.
- Position Information: Each node contains source code position information for easy error reporting and tool support.
Structs§
- Block
- Represents a block of statements enclosed in braces.
- Const
- Represents a constant definition in Rust source code.
- Enum
- Represents an enum definition in Rust source code.
- Extern
Block - Represents an extern block in Rust source code.
- Field
- Represents a field in a struct definition.
- Field
Init - Represents a field initialization in a struct expression.
- Field
Pattern - Represents a field pattern in a struct pattern.
- Function
- Represents a function definition in Rust source code.
- Identifier
- Represents an identifier in Rust source code.
- Impl
- Represents an impl block in Rust source code.
- Match
Arm - Represents a match arm in a match expression.
- Module
- Represents a module definition in Rust source code.
- Param
- Represents a function parameter with its type annotation.
- Rust
Root - Strongly-typed AST root node representing the entire Rust source file.
- Static
- Represents a static definition in Rust source code.
- Struct
- Represents a struct definition in Rust source code.
- Trait
- Represents a trait definition in Rust source code.
- Type
Alias - Represents a type alias in Rust source code.
- UseItem
- Represents a use statement in Rust source code.
- Variant
- Represents a variant in an enum definition.
Enums§
- Expr
- Represents different types of expressions in Rust source code.
- Impl
Item - Represents items within an impl block.
- Item
- Top-level items that can appear in a Rust source file.
- Pattern
- Represents a pattern in pattern matching.
- Statement
- Represents different types of statements in Rust source code.
- Trait
Item - Represents items within a trait definition.
- Type
- Represents a type in Rust source code.