oak_rust/ast/
mod.rs

1use core::range::Range;
2
3use crate::RustSyntaxKind;
4
5#[derive(Debug, PartialEq, Eq, Clone)]
6pub struct Identifier {
7    pub name: String,
8    pub span: Range<usize>,
9}
10
11/// 强类型 AST 根
12#[derive(Debug, PartialEq, Clone)]
13pub struct RustRoot {
14    pub items: Vec<Item>,
15}
16
17/// 顶层项:函数或语句
18#[derive(Debug, PartialEq, Clone)]
19pub enum Item {
20    Function(Function),
21    Statement(Statement),
22}
23
24#[derive(Debug, PartialEq, Clone)]
25pub struct Function {
26    pub name: Identifier,
27    pub params: Vec<Param>,
28    pub body: Block,
29    pub span: Range<usize>,
30}
31
32#[derive(Debug, PartialEq, Clone)]
33pub struct Param {
34    pub name: Identifier,
35    pub ty: String,
36    pub span: Range<usize>,
37}
38
39#[derive(Debug, PartialEq, Clone)]
40pub struct Block {
41    pub statements: Vec<Statement>,
42    pub span: Range<usize>,
43}
44
45#[derive(Debug, PartialEq, Clone)]
46pub enum Statement {
47    Let { name: Identifier, expr: Expr, span: Range<usize> },
48    ExprStmt { expr: Expr, semi: bool, span: Range<usize> },
49}
50
51#[derive(Debug, PartialEq, Clone)]
52pub enum Expr {
53    Ident(Identifier),
54    Literal { value: String, span: Range<usize> },
55    Bool { value: bool, span: Range<usize> },
56    Unary { op: RustSyntaxKind, expr: Box<Expr>, span: Range<usize> },
57    Binary { left: Box<Expr>, op: RustSyntaxKind, right: Box<Expr>, span: Range<usize> },
58    Call { callee: Box<Expr>, args: Vec<Expr>, span: Range<usize> },
59    Field { receiver: Box<Expr>, field: Identifier, span: Range<usize> },
60    Index { receiver: Box<Expr>, index: Box<Expr>, span: Range<usize> },
61    Paren { expr: Box<Expr>, span: Range<usize> },
62    Block(Block),
63}