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