oak_rust/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::lexer::RustTokenType;
3use core::range::Range;
4use serde::{Deserialize, Serialize};
5
6/// Represents an identifier in Rust source code.
7///
8/// Identifiers are names used for variables, functions, types, and other named entities.
9/// Each identifier carries its textual representation and source location information.
10///
11/// # Examples
12///
13/// ```rust,ignore
14/// /// use oak_rust::ast::Identifier;
15/// let ident = Identifier { name: "main".to_string(), span: 0..4 };
16/// assert_eq!(ident.name, "main");
17/// ```
18#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub struct Identifier {
20    /// The textual name of the identifier
21    pub name: String,
22    /// Source code span where this identifier appears
23    #[serde(with = "oak_core::serde_range")]
24    pub span: Range<usize>,
25}
26
27/// Strongly-typed AST root node representing the entire Rust source file.
28///
29/// This is the top-level structure that contains all items (functions, statements, etc.)
30/// parsed from a Rust source file.
31#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
32pub struct RustRoot {
33    /// Collection of top-level items in the Rust file
34    pub items: Vec<Item>,
35}
36
37/// Top-level items that can appear in a Rust source file.
38///
39/// These represent the main constructs that can exist at the module level,
40/// such as function definitions, structs, enums, modules, and other declarations.
41#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub enum Item {
43    /// A function definition
44    Function(Function),
45    /// A struct definition
46    Struct(Struct),
47    /// An enum definition
48    Enum(Enum),
49    /// A module definition
50    Module(Module),
51    /// A use statement
52    Use(UseItem),
53    /// A trait definition
54    Trait(Trait),
55    /// An impl block
56    Impl(Impl),
57    /// A type alias
58    TypeAlias(TypeAlias),
59    /// A constant definition
60    Const(Const),
61    /// A static definition
62    Static(Static),
63    /// An extern block
64    ExternBlock(ExternBlock),
65}
66
67/// Represents a function definition in Rust source code.
68///
69/// Functions are fundamental building blocks in Rust that encapsulate reusable logic.
70/// They can have parameters, return types, and contain executable code blocks.
71#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
72pub struct Function {
73    /// The name identifier of the function
74    pub name: Identifier,
75    /// List of function parameters
76    pub params: Vec<Param>,
77    /// Optional return type
78    pub return_type: Option<Type>,
79    /// The function body containing executable statements
80    pub body: Block,
81    /// Generic parameters
82    pub generics: Vec<String>,
83    /// Whether the function is async
84    pub is_async: bool,
85    /// Whether the function is unsafe
86    pub is_unsafe: bool,
87    /// Whether the function is extern
88    pub is_extern: bool,
89    /// Source code span where this function appears
90    #[serde(with = "oak_core::serde_range")]
91    pub span: Range<usize>,
92}
93
94/// Represents a struct definition in Rust source code.
95#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
96pub struct Struct {
97    /// The name identifier of the struct
98    pub name: Identifier,
99    /// List of struct fields
100    pub fields: Vec<Field>,
101    /// Source code span where this struct appears
102    #[serde(with = "oak_core::serde_range")]
103    pub span: Range<usize>,
104}
105
106/// Represents a field in a struct definition.
107#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
108pub struct Field {
109    /// The field name identifier
110    pub name: Identifier,
111    /// The field type
112    pub ty: Type,
113    /// Source code span where this field appears
114    #[serde(with = "oak_core::serde_range")]
115    pub span: Range<usize>,
116}
117
118/// Represents an enum definition in Rust source code.
119#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
120pub struct Enum {
121    /// The name identifier of the enum
122    pub name: Identifier,
123    /// List of enum variants
124    pub variants: Vec<Variant>,
125    /// Source code span where this enum appears
126    #[serde(with = "oak_core::serde_range")]
127    pub span: Range<usize>,
128}
129
130/// Represents a variant in an enum definition.
131#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
132pub struct Variant {
133    /// The variant name identifier
134    pub name: Identifier,
135    /// Optional fields for tuple or struct variants
136    pub fields: Option<Vec<Field>>,
137    /// Source code span where this variant appears
138    #[serde(with = "oak_core::serde_range")]
139    pub span: Range<usize>,
140}
141
142/// Represents a module definition in Rust source code.
143#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
144pub struct Module {
145    /// The name identifier of the module
146    pub name: Identifier,
147    /// List of items within the module
148    pub items: Vec<Item>,
149    /// Source code span where this module appears
150    #[serde(with = "oak_core::serde_range")]
151    pub span: Range<usize>,
152}
153
154/// Represents a use statement in Rust source code.
155#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
156pub struct UseItem {
157    /// The path being imported
158    pub path: String,
159    /// Source code span where this use statement appears
160    #[serde(with = "oak_core::serde_range")]
161    pub span: Range<usize>,
162}
163
164/// Represents a trait definition in Rust source code.
165#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
166pub struct Trait {
167    /// The name identifier of the trait
168    pub name: Identifier,
169    /// List of trait items (methods, associated types, etc.)
170    pub items: Vec<TraitItem>,
171    /// Source code span where this trait appears
172    #[serde(with = "oak_core::serde_range")]
173    pub span: Range<usize>,
174}
175
176/// Represents items within a trait definition.
177#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
178pub enum TraitItem {
179    /// A method signature
180    Method(Function),
181    /// An associated type
182    Type(TypeAlias),
183}
184
185/// Represents an impl block in Rust source code.
186#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
187pub struct Impl {
188    /// The type being implemented for
189    pub ty: Type,
190    /// Optional trait being implemented
191    pub trait_: Option<Type>,
192    /// List of implementation items
193    pub items: Vec<ImplItem>,
194    /// Source code span where this impl block appears
195    #[serde(with = "oak_core::serde_range")]
196    pub span: Range<usize>,
197}
198
199/// Represents items within an impl block.
200#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
201pub enum ImplItem {
202    /// A method implementation
203    Method(Function),
204    /// An associated type implementation
205    Type(TypeAlias),
206    /// An associated constant
207    Const(Const),
208}
209
210/// Represents a type alias in Rust source code.
211#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
212pub struct TypeAlias {
213    /// The alias name identifier
214    pub name: Identifier,
215    /// The target type
216    pub ty: Type,
217    /// Source code span where this type alias appears
218    #[serde(with = "oak_core::serde_range")]
219    pub span: Range<usize>,
220}
221
222/// Represents a constant definition in Rust source code.
223#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
224pub struct Const {
225    /// The constant name identifier
226    pub name: Identifier,
227    /// The constant type
228    pub ty: Type,
229    /// The constant value expression
230    pub expr: Expr,
231    /// Source code span where this constant appears
232    #[serde(with = "oak_core::serde_range")]
233    pub span: Range<usize>,
234}
235
236/// Represents a static definition in Rust source code.
237#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
238pub struct Static {
239    /// The static name identifier
240    pub name: Identifier,
241    /// The static type
242    pub ty: Type,
243    /// The static value expression
244    pub expr: Expr,
245    /// Whether the static is mutable
246    pub mutable: bool,
247    /// Source code span where this static appears
248    #[serde(with = "oak_core::serde_range")]
249    pub span: Range<usize>,
250}
251
252/// Represents a type in Rust source code.
253#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
254pub enum Type {
255    /// A path type (e.g., std::vec::Vec)
256    Path(String),
257    /// A reference type (e.g., &str, &mut T)
258    Reference {
259        /// Whether the reference is mutable
260        mutable: bool,
261        /// The referenced type
262        ty: Box<Type>,
263    },
264    /// A tuple type (e.g., (i32, String))
265    Tuple(Vec<Type>),
266    /// An array type (e.g., [i32; 10])
267    Array {
268        /// The element type
269        ty: Box<Type>,
270        /// The array size expression
271        size: Expr,
272    },
273    /// A slice type (e.g., [i32])
274    Slice(Box<Type>),
275    /// A function pointer type (e.g., fn(i32) -> String)
276    Fn {
277        /// Parameter types
278        params: Vec<Type>,
279        /// Return type
280        ret: Option<Box<Type>>,
281    },
282    /// An inferred type (_)
283    Infer,
284}
285
286/// Represents a function parameter with its type annotation.
287///
288/// Parameters define the inputs that a function can accept, with their respective types.
289#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
290pub struct Param {
291    /// The parameter name identifier
292    pub name: Identifier,
293    /// The parameter type
294    pub ty: Type,
295    /// Whether the parameter is mutable
296    pub is_mut: bool,
297    /// Source code span where this parameter appears
298    #[serde(with = "oak_core::serde_range")]
299    pub span: Range<usize>,
300}
301
302/// Represents a block of statements enclosed in braces.
303///
304/// Blocks are used to group statements together and define scope boundaries.
305#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
306pub struct Block {
307    /// List of statements within the block
308    pub statements: Vec<Statement>,
309    /// Block start position
310    pub block_start: usize,
311    /// Block end position
312    pub block_end: usize,
313    /// Nested block level
314    pub nested: usize,
315    /// Source code span where this block appears
316    #[serde(with = "oak_core::serde_range")]
317    pub span: Range<usize>,
318}
319
320/// Represents different types of statements in Rust source code.
321///
322/// Statements are executable units that perform actions or declare bindings.
323#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
324pub enum Statement {
325    /// A let binding statement
326    Let {
327        /// The variable name being bound
328        name: Identifier,
329        /// Optional type annotation
330        ty: Option<Type>,
331        /// The expression being assigned to the variable
332        expr: Option<Expr>,
333        /// Whether the binding is mutable
334        mutable: bool,
335        /// Source code span where this let statement appears
336        #[serde(with = "oak_core::serde_range")]
337        span: Range<usize>,
338    },
339    /// An expression statement (expression followed by optional semicolon)
340    ExprStmt {
341        /// The expression being evaluated
342        expr: Expr,
343        /// Whether the statement ends with a semicolon
344        semi: bool,
345        /// Source code span where this expression statement appears
346        #[serde(with = "oak_core::serde_range")]
347        span: Range<usize>,
348    },
349    /// A return statement
350    Return {
351        /// Optional return expression
352        expr: Option<Expr>,
353        /// Source code span where this return statement appears
354        #[serde(with = "oak_core::serde_range")]
355        span: Range<usize>,
356    },
357    /// A break statement
358    Break {
359        /// Optional break expression
360        expr: Option<Expr>,
361        /// Source code span where this break statement appears
362        #[serde(with = "oak_core::serde_range")]
363        span: Range<usize>,
364    },
365    /// A continue statement
366    Continue {
367        /// Source code span where this continue statement appears
368        #[serde(with = "oak_core::serde_range")]
369        span: Range<usize>,
370    },
371    /// An item statement (item declaration within a block)
372    Item(Item),
373}
374
375/// Represents different types of expressions in Rust source code.
376///
377/// Expressions are constructs that evaluate to values and can be used in various contexts.
378#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
379pub enum Expr {
380    /// An identifier expression
381    Ident(Identifier),
382    /// A literal expression
383    Literal {
384        /// The literal value as a string
385        value: String,
386        /// Source code span where this literal appears
387        #[serde(with = "oak_core::serde_range")]
388        span: Range<usize>,
389    },
390    /// A boolean literal expression
391    Bool {
392        /// The boolean value
393        value: bool,
394        /// Source code span where this boolean literal appears
395        #[serde(with = "oak_core::serde_range")]
396        span: Range<usize>,
397    },
398    /// A unary expression (e.g., !x, -x, *x, &x)
399    Unary {
400        /// The unary operator
401        op: RustTokenType,
402        /// The operand expression
403        expr: Box<Expr>,
404        /// Source code span where this unary expression appears
405        #[serde(with = "oak_core::serde_range")]
406        span: Range<usize>,
407    },
408    /// A binary expression (e.g., x + y, x == y)
409    Binary {
410        /// The left operand
411        left: Box<Expr>,
412        /// The binary operator
413        op: RustTokenType,
414        /// The right operand
415        right: Box<Expr>,
416        /// Source code span where this binary expression appears
417        #[serde(with = "oak_core::serde_range")]
418        span: Range<usize>,
419    },
420    /// A function call expression
421    Call {
422        /// The function being called
423        callee: Box<Expr>,
424        /// The arguments passed to the function
425        args: Vec<Expr>,
426        /// Source code span where this call expression appears
427        #[serde(with = "oak_core::serde_range")]
428        span: Range<usize>,
429    },
430    /// A field access expression (e.g., obj.field)
431    Field {
432        /// The object being accessed
433        receiver: Box<Expr>,
434        /// The field being accessed
435        field: Identifier,
436        /// Source code span where this field expression appears
437        #[serde(with = "oak_core::serde_range")]
438        span: Range<usize>,
439    },
440    /// An index expression (e.g., arr[0])
441    Index {
442        /// The object being indexed
443        receiver: Box<Expr>,
444        /// The index expression
445        index: Box<Expr>,
446        /// Source code span where this index expression appears
447        #[serde(with = "oak_core::serde_range")]
448        span: Range<usize>,
449    },
450    /// A parenthesized expression
451    Paren {
452        /// The inner expression
453        expr: Box<Expr>,
454        /// Source code span where this parenthesized expression appears
455        #[serde(with = "oak_core::serde_range")]
456        span: Range<usize>,
457    },
458    /// A block expression
459    Block(Block),
460    /// An if expression
461    If {
462        /// The condition expression
463        condition: Box<Expr>,
464        /// The then block
465        then_block: Block,
466        /// Optional else block
467        else_block: Option<Block>,
468        /// Source code span where this if expression appears
469        #[serde(with = "oak_core::serde_range")]
470        span: Range<usize>,
471    },
472    /// A while loop expression
473    While {
474        /// The condition expression
475        condition: Box<Expr>,
476        /// The loop body
477        body: Block,
478        /// Source code span where this while expression appears
479        #[serde(with = "oak_core::serde_range")]
480        span: Range<usize>,
481    },
482    /// A for loop expression
483    For {
484        /// The loop variable
485        var: Identifier,
486        /// The iterable expression
487        iter: Box<Expr>,
488        /// The loop body
489        body: Block,
490        /// Source code span where this for expression appears
491        #[serde(with = "oak_core::serde_range")]
492        span: Range<usize>,
493    },
494    /// A loop expression
495    Loop {
496        /// The loop body
497        body: Block,
498        /// Source code span where this loop expression appears
499        #[serde(with = "oak_core::serde_range")]
500        span: Range<usize>,
501    },
502    /// A match expression
503    Match {
504        /// The expression being matched
505        expr: Box<Expr>,
506        /// The match arms
507        arms: Vec<MatchArm>,
508        /// Source code span where this match expression appears
509        #[serde(with = "oak_core::serde_range")]
510        span: Range<usize>,
511    },
512    /// A tuple expression
513    Tuple {
514        /// The tuple elements
515        elements: Vec<Expr>,
516        /// Source code span where this tuple expression appears
517        #[serde(with = "oak_core::serde_range")]
518        span: Range<usize>,
519    },
520    /// An array expression
521    Array {
522        /// The array elements
523        elements: Vec<Expr>,
524        /// Source code span where this array expression appears
525        #[serde(with = "oak_core::serde_range")]
526        span: Range<usize>,
527    },
528    /// A struct expression
529    Struct {
530        /// The struct path
531        path: String,
532        /// The struct fields
533        fields: Vec<FieldInit>,
534        /// Source code span where this struct expression appears
535        #[serde(with = "oak_core::serde_range")]
536        span: Range<usize>,
537    },
538}
539
540/// Represents a match arm in a match expression.
541#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
542pub struct MatchArm {
543    /// The pattern to match
544    pub pattern: Pattern,
545    /// Optional guard expression
546    pub guard: Option<Expr>,
547    /// The arm body expression
548    pub body: Expr,
549    /// Source code span where this match arm appears
550    #[serde(with = "oak_core::serde_range")]
551    pub span: Range<usize>,
552}
553
554/// Represents a pattern in pattern matching.
555#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
556pub enum Pattern {
557    /// A wildcard pattern (_)
558    Wildcard,
559    /// An identifier pattern
560    Ident(Identifier),
561    /// A literal pattern
562    Literal(String),
563    /// A tuple pattern
564    Tuple(Vec<Pattern>),
565    /// A struct pattern
566    Struct {
567        /// The struct path
568        path: String,
569        /// The field patterns
570        fields: Vec<FieldPattern>,
571    },
572    /// A wild pattern (alias for Wildcard)
573    Wild,
574}
575
576/// Represents a field pattern in a struct pattern.
577#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
578pub struct FieldPattern {
579    /// The field name
580    pub name: Identifier,
581    /// The field pattern
582    pub pattern: Pattern,
583    /// Source code span where this field pattern appears
584    #[serde(with = "oak_core::serde_range")]
585    pub span: Range<usize>,
586}
587
588/// Represents a field initialization in a struct expression.
589#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
590pub struct FieldInit {
591    /// The field name
592    pub name: Identifier,
593    /// The field value expression
594    pub expr: Expr,
595    /// Source code span where this field initialization appears
596    #[serde(with = "oak_core::serde_range")]
597    pub span: Range<usize>,
598}
599
600/// Represents an extern block in Rust source code.
601#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
602pub struct ExternBlock {
603    /// The ABI string (e.g., "C", "system")
604    pub abi: Option<String>,
605    /// List of items within the extern block
606    pub items: Vec<Item>,
607    /// Source code span where this extern block appears
608    #[serde(with = "oak_core::serde_range")]
609    pub span: Range<usize>,
610}