Skip to main content

oak_zig/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3use std::sync::Arc;
4
5/// Zig language abstract syntax tree root.
6#[derive(Debug, Clone, PartialEq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct ZigRoot {
9    /// Items in the source file.
10    pub items: Vec<Item>,
11}
12
13/// A top-level item in a Zig source file.
14#[derive(Debug, Clone, PartialEq)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub enum Item {
17    /// A declaration.
18    Declaration(Arc<Declaration>),
19    /// A container field.
20    ContainerField(Arc<ContainerField>),
21    /// A comptime block at top level.
22    Comptime(Arc<Block>),
23}
24
25/// A Zig declaration.
26#[derive(Debug, Clone, PartialEq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct Declaration {
29    /// Name of the declaration.
30    pub name: String,
31    /// Whether it's a comptime declaration.
32    pub is_comptime: bool,
33    /// Span of the declaration.
34    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
35    pub span: Range<usize>,
36}
37
38/// A Zig container field.
39#[derive(Debug, Clone, PartialEq)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41pub struct ContainerField {
42    /// Name of the field.
43    pub name: String,
44    /// Span of the field.
45    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
46    pub span: Range<usize>,
47}
48
49/// Represents an expression in Zig.
50#[derive(Debug, Clone, PartialEq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub enum Expression {
53    /// A literal value.
54    Literal(String),
55    /// An identifier.
56    Identifier(String),
57    /// A comptime expression.
58    Comptime(Box<Expression>),
59    /// A block expression.
60    Block(Arc<Block>),
61    // Add more variants as needed
62}
63
64/// Represents a block of code in Zig.
65#[derive(Debug, Clone, PartialEq)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub struct Block {
68    /// Statements in the block.
69    pub statements: Vec<Statement>,
70    /// Span of the block.
71    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
72    pub span: Range<usize>,
73}
74
75/// Represents a statement in Zig.
76#[derive(Debug, Clone, PartialEq)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub enum Statement {
79    /// An expression statement.
80    Expression(Expression),
81    /// A comptime statement.
82    Comptime(Arc<Block>),
83    /// A declaration statement.
84    Declaration(Arc<Declaration>),
85    /// A block statement.
86    Block(Arc<Block>),
87}