Skip to main content

oak_valkyrie/ast/
common_nodes.rs

1use super::{Block, Expr, GenericParam, Identifier, Param, Pattern, Span, Type};
2
3/// A field in a class or struct
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct Field {
7    /// The field name.
8    pub name: Identifier,
9    /// The field type.
10    pub ty: Type,
11    /// Optional default value expression.
12    pub default: Option<Expr>,
13    /// Annotations applied to the field.
14    pub annotations: Vec<Attribute>,
15    /// The source code span.
16    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
17    pub span: Span,
18}
19
20/// An attribute
21#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct Attribute {
24    /// The attribute name.
25    pub name: Identifier,
26    /// The attribute arguments.
27    pub args: Vec<Expr>,
28    /// The source code span.
29    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
30    pub span: Span,
31}
32
33/// A string literal node.
34#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36pub struct StringLiteral {
37    /// DSL prefix (e.g., `s`, `f`, `r`, `sql`).
38    pub prefix: Option<Identifier>,
39    /// Number of quotes (1, 2, 3, 4, ...).
40    pub quote_count: u8,
41    /// String segments.
42    pub segments: Vec<StringSegment>,
43    /// The source code span.
44    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
45    pub span: Span,
46}
47
48/// A string segment.
49#[derive(Debug, Clone, PartialEq, Eq, Hash)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51pub enum StringSegment {
52    /// Text content.
53    Text {
54        /// The text content.
55        content: String,
56        /// The source code span.
57        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
58        span: Span,
59    },
60    /// Interpolation expression.
61    Interpolation {
62        /// The interpolation expression.
63        expr: Box<Expr>,
64        /// Whether this is a Fluent variable (with the ߷ marker).
65        is_fluent: bool,
66        /// The source code span.
67        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
68        span: Span,
69    },
70}
71
72/// A function definition
73#[derive(Debug, Clone, PartialEq, Eq, Hash)]
74#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
75pub struct Function {
76    /// The function name.
77    pub name: Identifier,
78    /// Generic parameters for the function.
79    pub generics: Vec<GenericParam>,
80    /// The function parameters.
81    pub params: Vec<Param>,
82    /// Optional return type annotation.
83    pub return_type: Option<Type>,
84    /// The optional function body.
85    pub body: Option<Block>,
86    /// Annotations applied to the function.
87    pub annotations: Vec<Attribute>,
88    /// The source code span.
89    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
90    pub span: Span,
91    /// Whether this function is abstract (has no body implementation).
92    pub is_abstract: bool,
93    /// Whether this function is final (cannot be overridden).
94    pub is_final: bool,
95}
96
97/// An enum variant
98#[derive(Debug, Clone, PartialEq, Eq, Hash)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100pub struct EnumVariant {
101    /// The variant name.
102    pub name: Identifier,
103    /// The variant fields.
104    pub fields: Vec<Field>,
105    /// Annotations applied to the variant.
106    pub annotations: Vec<Attribute>,
107    /// The source code span.
108    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
109    pub span: Span,
110    /// Optional value expression for flags (e.g., `READ = 1` or `ALL = READ | WRITE`).
111    pub value: Option<Expr>,
112}
113
114/// A variant case
115#[derive(Debug, Clone, PartialEq, Eq, Hash)]
116#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
117pub struct VariantCase {
118    /// The pattern for this case.
119    pub pattern: Pattern,
120    /// The body expression for this case.
121    pub body: Expr,
122    /// The source code span.
123    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
124    pub span: Span,
125}