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