Skip to main content

oak_fortran/ast/
root_nodes.rs

1use core::range::Range;
2use std::{string::String, vec::Vec};
3
4use super::{ExecutableStmt, SpecificationStmt, TypeSpec};
5
6/// A placeholder statement node in the Fortran AST.
7#[derive(Debug, PartialEq, Clone)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Statement {}
10
11/// The root node of the Fortran AST.
12#[derive(Debug, PartialEq, Clone)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct FortranRoot {
15    /// The optional name of the program.
16    pub name: Option<String>,
17    /// The list of program units contained in this root.
18    pub units: Vec<ProgramUnitKind>,
19    /// The byte range of this node in the source text.
20    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
21    pub span: Range<usize>,
22}
23
24/// Fortran program unit kinds.
25#[derive(Debug, Clone, PartialEq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub enum ProgramUnitKind {
28    /// Main program.
29    MainProgram(MainProgramNode),
30    /// Subroutine.
31    Subroutine(SubroutineNode),
32    /// Function.
33    Function(FunctionNode),
34    /// Module.
35    Module(ModuleNode),
36    /// Submodule.
37    Submodule(SubmoduleNode),
38    /// Block data.
39    BlockData(BlockDataNode),
40}
41
42/// Main program node.
43#[derive(Debug, Clone, PartialEq)]
44#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45pub struct MainProgramNode {
46    /// The optional name of the main program.
47    pub name: Option<String>,
48    /// The specification statements in the main program.
49    pub specification_part: Vec<SpecificationStmt>,
50    /// The executable statements in the main program.
51    pub execution_part: Vec<ExecutableStmt>,
52    /// Internal subprograms defined within the main program.
53    pub internal_subprograms: Vec<ProgramUnitKind>,
54    /// The byte range of this node in the source text.
55    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
56    pub span: Range<usize>,
57}
58
59/// Subroutine node.
60#[derive(Debug, Clone, PartialEq)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
62pub struct SubroutineNode {
63    /// The name of the subroutine.
64    pub name: String,
65    /// The list of parameter names.
66    pub parameters: Vec<String>,
67    /// The specification statements in the subroutine.
68    pub specification_part: Vec<SpecificationStmt>,
69    /// The executable statements in the subroutine.
70    pub execution_part: Vec<ExecutableStmt>,
71    /// Internal subprograms defined within the subroutine.
72    pub internal_subprograms: Vec<ProgramUnitKind>,
73    /// The byte range of this node in the source text.
74    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
75    pub span: Range<usize>,
76}
77
78/// Function node.
79#[derive(Debug, Clone, PartialEq)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct FunctionNode {
82    /// The name of the function.
83    pub name: String,
84    /// The list of parameter names.
85    pub parameters: Vec<String>,
86    /// The optional name of the result variable.
87    pub result_name: Option<String>,
88    /// The optional return type specification.
89    pub return_type: Option<TypeSpec>,
90    /// The specification statements in the function.
91    pub specification_part: Vec<SpecificationStmt>,
92    /// The executable statements in the function.
93    pub execution_part: Vec<ExecutableStmt>,
94    /// Internal subprograms defined within the function.
95    pub internal_subprograms: Vec<ProgramUnitKind>,
96    /// The byte range of this node in the source text.
97    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
98    pub span: Range<usize>,
99}
100
101/// Module node.
102#[derive(Debug, Clone, PartialEq)]
103#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
104pub struct ModuleNode {
105    /// The name of the module.
106    pub name: String,
107    /// The specification statements in the module.
108    pub specification_part: Vec<SpecificationStmt>,
109    /// Subprograms defined within the module.
110    pub module_subprograms: Vec<ProgramUnitKind>,
111    /// The byte range of this node in the source text.
112    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
113    pub span: Range<usize>,
114}
115
116/// Submodule node.
117#[derive(Debug, Clone, PartialEq)]
118#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
119pub struct SubmoduleNode {
120    /// The name of the parent module.
121    pub parent_name: String,
122    /// The name of the submodule.
123    pub name: String,
124    /// The specification statements in the submodule.
125    pub specification_part: Vec<SpecificationStmt>,
126    /// Subprograms defined within the submodule.
127    pub module_subprograms: Vec<ProgramUnitKind>,
128    /// The byte range of this node in the source text.
129    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
130    pub span: Range<usize>,
131}
132
133/// Block data node.
134#[derive(Debug, Clone, PartialEq)]
135#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
136pub struct BlockDataNode {
137    /// The optional name of the block data.
138    pub name: Option<String>,
139    /// The specification statements in the block data.
140    pub specification_part: Vec<SpecificationStmt>,
141    /// The byte range of this node in the source text.
142    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
143    pub span: Range<usize>,
144}