Expand description
§Rust 抽象语法树 (AST) 模块
这个模块定义了 Rust 语言的抽象语法树结构,用于表示解析后的 Rust 代码。 AST 节点对应于 Rust 语言中的各种构造,如函数、结构体、枚举、模块、表达式等。
§AST 节点类型
§顶层项目
Function: 函数定义Struct: 结构体定义Enum: 枚举定义Module: 模块定义UseItem: use 语句Trait: trait 定义Impl: impl 块TypeAlias: 类型别名Const: 常量定义Static: 静态变量定义
§类型系统
Type: 类型表示(路径、引用、元组、数组、切片、函数指针)Identifier: 标识符Param: 函数参数Field: 结构体字段
§语句和表达式
Statement: 语句(let、表达式语句、return、break、continue)Expr: 表达式(标识符、字面量、二元运算、函数调用、字段访问、控制流等)Block: 代码块Pattern: 模式匹配模式
§控制流
If: if 表达式While: while 循环For: for 循环Loop: loop 循环Match: match 表达式MatchArm: match 分支
§使用示例
ⓘ
use oak_rust::ast::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建简单的 Rust 程序 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(())
}§设计原则
- 完整性: 支持完整的 Rust 语法结构
- 可扩展性: 易于添加新的 AST 节点类型
- 类型安全: 使用 Rust 的类型系统确保 AST 的有效性
- 性能: 高效的内存使用和访问模式
- 位置信息: 每个节点都包含源代码位置信息,便于错误报告和工具支持
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.