Skip to main content

oak_go/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4use std::{boxed::Box, string::String, vec::Vec};
5
6/// Strongly typed AST root for the Go language.
7#[derive(Debug, PartialEq, Clone)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct GoRoot {
10    /// The package name declared in the source file.
11    pub package: Option<String>,
12    /// The list of import declarations.
13    pub imports: Vec<Import>,
14    /// The list of top-level declarations.
15    pub declarations: Vec<Declaration>,
16}
17
18/// Represents an import declaration in Go source code.
19#[derive(Debug, PartialEq, Clone)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct Import {
22    /// The import path as a string.
23    pub path: String,
24    /// An optional alias for the import.
25    pub alias: Option<String>,
26    /// The source span of the import declaration.
27    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
28    pub span: Range<usize>,
29}
30
31/// Represents a top-level declaration in Go source code.
32#[derive(Debug, PartialEq, Clone)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34pub enum Declaration {
35    /// A function declaration.
36    Function(Function),
37    /// A variable declaration.
38    Variable(Variable),
39    /// A type declaration.
40    Type(TypeDecl),
41    /// A constant declaration.
42    Const(Const),
43}
44
45/// Represents a function declaration in Go source code.
46#[derive(Debug, PartialEq, Clone)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub struct Function {
49    /// The function name.
50    pub name: String,
51    /// The optional receiver for methods.
52    pub receiver: Option<Parameter>,
53    /// The list of parameters.
54    pub params: Vec<Parameter>,
55    /// The list of return type names.
56    pub return_types: Vec<String>,
57    /// The function body block.
58    pub body: Block,
59    /// The source span of the function declaration.
60    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
61    pub span: Range<usize>,
62}
63
64/// Represents a parameter in a function declaration.
65#[derive(Debug, PartialEq, Clone)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub struct Parameter {
68    /// The parameter name.
69    pub name: String,
70    /// The parameter type name.
71    pub param_type: String,
72    /// The source span of the parameter.
73    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
74    pub span: Range<usize>,
75}
76
77/// Represents a block of statements in Go source code.
78#[derive(Debug, PartialEq, Clone)]
79#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
80pub struct Block {
81    /// The list of statements in the block.
82    pub statements: Vec<Statement>,
83    /// The source span of the block.
84    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
85    pub span: Range<usize>,
86}
87
88/// Represents a statement in Go source code.
89#[derive(Debug, PartialEq, Clone)]
90#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
91pub enum Statement {
92    /// An expression statement.
93    Expression(Expression),
94    /// An assignment statement.
95    Assignment {
96        /// The target variable names.
97        targets: Vec<String>,
98        /// The assigned values.
99        values: Vec<Expression>,
100        /// The source span of the assignment.
101        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
102        span: Range<usize>,
103    },
104    /// A return statement.
105    Return {
106        /// The values being returned.
107        values: Vec<Expression>,
108        /// The source span of the return statement.
109        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
110        span: Range<usize>,
111    },
112    /// An if statement.
113    If {
114        /// The condition expression.
115        condition: Expression,
116        /// The then block.
117        then_block: Block,
118        /// The optional else block.
119        else_block: Option<Block>,
120        /// The source span of the if statement.
121        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
122        span: Range<usize>,
123    },
124    /// A for statement.
125    For {
126        /// The optional initialization statement.
127        init: Option<Box<Statement>>,
128        /// The optional condition expression.
129        condition: Option<Expression>,
130        /// The optional post iteration statement.
131        post: Option<Box<Statement>>,
132        /// The loop body block.
133        body: Block,
134        /// The source span of the for statement.
135        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
136        span: Range<usize>,
137    },
138}
139
140/// Represents an expression in Go source code.
141#[derive(Debug, PartialEq, Clone)]
142#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
143pub enum Expression {
144    /// An identifier expression.
145    Identifier {
146        /// The identifier name.
147        name: String,
148        /// The source span of the identifier.
149        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
150        span: Range<usize>,
151    },
152    /// A literal expression.
153    Literal {
154        /// The literal value as a string.
155        value: String,
156        /// The source span of the literal.
157        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
158        span: Range<usize>,
159    },
160    /// A binary expression.
161    Binary {
162        /// The left operand.
163        left: Box<Expression>,
164        /// The operator.
165        op: String,
166        /// The right operand.
167        right: Box<Expression>,
168        /// The source span of the binary expression.
169        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
170        span: Range<usize>,
171    },
172    /// A function call expression.
173    Call {
174        /// The function being called.
175        func: Box<Expression>,
176        /// The arguments passed to the function.
177        args: Vec<Expression>,
178        /// The source span of the call expression.
179        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
180        span: Range<usize>,
181    },
182}
183
184/// Represents a variable declaration in Go source code.
185#[derive(Debug, PartialEq, Clone)]
186#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
187pub struct Variable {
188    /// The variable name.
189    pub name: String,
190    /// The optional type annotation.
191    pub var_type: Option<String>,
192    /// The optional initial value.
193    pub value: Option<Expression>,
194    /// The source span of the variable declaration.
195    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
196    pub span: Range<usize>,
197}
198
199/// Represents a type declaration in Go source code.
200#[derive(Debug, PartialEq, Clone)]
201#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
202pub struct TypeDecl {
203    /// The type name.
204    pub name: String,
205    /// The type definition as a string.
206    pub definition: String,
207    /// The source span of the type declaration.
208    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
209    pub span: Range<usize>,
210}
211
212/// Represents a constant declaration in Go source code.
213#[derive(Debug, PartialEq, Clone)]
214#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
215pub struct Const {
216    /// The constant name.
217    pub name: String,
218    /// The optional type annotation.
219    pub const_type: Option<String>,
220    /// The constant value.
221    pub value: Expression,
222    /// The source span of the constant declaration.
223    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
224    pub span: Range<usize>,
225}