Skip to main content

oak_c/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2/// C language abstract syntax.
3mod declaration_nodes;
4mod expression_nodes;
5mod statement_nodes;
6
7pub use declaration_nodes::*;
8pub use expression_nodes::*;
9pub use statement_nodes::*;
10
11/// C language AST root node.
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13#[derive(Debug, Clone, PartialEq)]
14pub struct CRoot {
15    /// The translation unit containing the source code structure.
16    pub translation_unit: TranslationUnit,
17    /// The source span of the root node.
18    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
19    pub span: core::range::Range<usize>,
20}
21
22/// Translation unit (the top-level structure of a C program).
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24#[derive(Debug, Clone, PartialEq)]
25pub struct TranslationUnit {
26    /// List of external declarations.
27    pub external_declarations: Vec<ExternalDeclaration>,
28    /// The source span of the translation unit.
29    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
30    pub span: core::range::Range<usize>,
31}
32
33impl CRoot {
34    /// Creates a new AST
35    pub fn new(translation_unit: TranslationUnit, span: core::range::Range<usize>) -> Self {
36        Self { translation_unit, span }
37    }
38}
39
40impl TranslationUnit {
41    /// Creates a new translation unit
42    pub fn new(external_declarations: Vec<ExternalDeclaration>, span: core::range::Range<usize>) -> Self {
43        Self { external_declarations, span }
44    }
45}