Skip to main content

oak_ada/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2/// Ada 语法树的根节点。
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct AdaRoot {
5    /// 此 Ada 文件中的项。
6    pub items: Vec<AdaItem>,
7}
8
9impl AdaRoot {
10    /// 创建新的 Ada 根节点。
11    pub fn new(items: Vec<AdaItem>) -> Self {
12        Self { items }
13    }
14
15    /// 获取此 Ada 文件中的所有顶级项。
16    pub fn items(&self) -> &[AdaItem] {
17        &self.items
18    }
19}
20
21/// Ada 文件中的项(包、过程、函数等)。
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum AdaItem {
24    /// 包声明。
25    Package(AdaPackage),
26    /// 过程声明。
27    Procedure(AdaProcedure),
28    /// 函数声明。
29    Function(AdaFunction),
30    /// 类型声明。
31    Type(AdaTypeDeclaration),
32    /// 变量声明。
33    Variable(AdaVariableDeclaration),
34}
35
36/// Ada 包声明。
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct AdaPackage {
39    /// 包名。
40    pub name: String,
41}
42
43impl AdaPackage {
44    /// 创建新的包声明。
45    pub fn new(name: String) -> Self {
46        Self { name }
47    }
48}
49
50/// Ada 过程声明。
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct AdaProcedure {
53    /// 过程名。
54    pub name: String,
55    /// 参数列表。
56    pub parameters: Vec<AdaParameter>,
57}
58
59impl AdaProcedure {
60    /// 创建新的过程声明。
61    pub fn new(name: String, parameters: Vec<AdaParameter>) -> Self {
62        Self { name, parameters }
63    }
64}
65
66/// Ada 函数声明。
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct AdaFunction {
69    /// 函数名。
70    pub name: String,
71    /// 参数列表。
72    pub parameters: Vec<AdaParameter>,
73    /// 返回类型。
74    pub return_type: String,
75}
76
77impl AdaFunction {
78    /// 创建新的函数声明。
79    pub fn new(name: String, parameters: Vec<AdaParameter>, return_type: String) -> Self {
80        Self { name, parameters, return_type }
81    }
82}
83
84/// Ada 参数。
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct AdaParameter {
87    /// 参数名。
88    pub name: String,
89    /// 参数类型。
90    pub param_type: String,
91}
92
93impl AdaParameter {
94    /// 创建新的参数。
95    pub fn new(name: String, param_type: String) -> Self {
96        Self { name, param_type }
97    }
98}
99
100/// Ada 类型声明。
101#[derive(Debug, Clone, PartialEq, Eq)]
102pub struct AdaTypeDeclaration {
103    /// 类型名。
104    pub name: String,
105    /// 类型定义。
106    pub type_def: String,
107}
108
109impl AdaTypeDeclaration {
110    /// 创建新的类型声明。
111    pub fn new(name: String, type_def: String) -> Self {
112        Self { name, type_def }
113    }
114}
115
116/// Ada 变量声明。
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct AdaVariableDeclaration {
119    /// 变量名。
120    pub name: String,
121    /// 变量类型。
122    pub var_type: String,
123}
124
125impl AdaVariableDeclaration {
126    /// 创建新的变量声明。
127    pub fn new(name: String, var_type: String) -> Self {
128        Self { name, var_type }
129    }
130}
131
132impl Default for AdaRoot {
133    fn default() -> Self {
134        Self::new(Vec::new())
135    }
136}