Skip to main content

oak_dart/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// Identifier in the Dart language
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[derive(Debug, PartialEq, Eq, Clone)]
7pub struct Identifier {
8    /// The identifier name text
9    pub name: String,
10    /// The source span where this identifier appears
11    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12    pub span: Range<usize>,
13}
14
15/// Strongly-typed AST root for Dart language
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[derive(Debug, PartialEq, Clone)]
18pub struct DartRoot {
19    /// Top-level items in the Dart source file
20    pub items: Vec<Item>,
21}
22
23/// Top-level items: classes, functions, variable declarations, etc.
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[derive(Debug, PartialEq, Clone)]
26pub enum Item {
27    /// A class declaration
28    Class(ClassDeclaration),
29    /// A function declaration
30    Function(FunctionDeclaration),
31    /// A variable declaration
32    Variable(VariableDeclaration),
33}
34
35/// Class declaration in Dart
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37#[derive(Debug, PartialEq, Clone)]
38pub struct ClassDeclaration {
39    /// The class name
40    pub name: Identifier,
41    /// The class body containing members
42    pub body: Vec<Item>,
43    /// The source span of the entire class declaration
44    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
45    pub span: Range<usize>,
46}
47
48/// Function declaration in Dart
49#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
50#[derive(Debug, PartialEq, Clone)]
51pub struct FunctionDeclaration {
52    /// The function name
53    pub name: Identifier,
54    /// The function parameters
55    pub parameters: Vec<Parameter>,
56    /// The function body
57    pub body: Vec<Item>,
58    /// The source span of the entire function declaration
59    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
60    pub span: Range<usize>,
61}
62
63/// Variable declaration in Dart.
64#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
65#[derive(Debug, PartialEq, Clone)]
66pub struct VariableDeclaration {
67    /// The variable name.
68    pub name: Identifier,
69    /// Optional type annotation.
70    pub type_annotation: Option<Identifier>,
71    /// Optional initial value.
72    pub value: Option<Expression>,
73    /// The source span of the entire variable declaration.
74    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
75    pub span: Range<usize>,
76}
77
78/// Parameter in a Dart function.
79#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
80#[derive(Debug, PartialEq, Clone)]
81pub struct Parameter {
82    /// The parameter name.
83    pub name: Identifier,
84    /// Optional type annotation.
85    pub type_annotation: Option<Identifier>,
86}
87
88/// Basic expression in Dart
89#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
90#[derive(Debug, PartialEq, Clone)]
91pub enum Expression {
92    /// An identifier expression
93    Identifier(Identifier),
94    /// A literal value (string, number, boolean, etc.)
95    Literal(String),
96}