oak_elixir/ast/
mod.rs

1use std::{boxed::Box, range::Range, string::String, vec::Vec};
2
3use crate::ElixirSyntaxKind;
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 ElixirRoot {
14    pub items: Vec<Item>,
15}
16
17/// 顶层项:模块、函数或语句
18#[derive(Debug, PartialEq, Clone)]
19pub enum Item {
20    Module(Module),
21    Function(Function),
22    Statement(Statement),
23}
24
25#[derive(Debug, PartialEq, Clone)]
26pub struct Module {
27    pub name: Identifier,
28    pub items: Vec<Item>,
29    pub span: Range<usize>,
30}
31
32#[derive(Debug, PartialEq, Clone)]
33pub struct Function {
34    pub name: Identifier,
35    pub params: Vec<Param>,
36    pub body: Block,
37    pub span: Range<usize>,
38}
39
40#[derive(Debug, PartialEq, Clone)]
41pub struct Param {
42    pub name: Identifier,
43    pub ty: Option<String>,
44    pub span: Range<usize>,
45}
46
47#[derive(Debug, PartialEq, Clone)]
48pub struct Block {
49    pub statements: Vec<Statement>,
50    pub span: Range<usize>,
51}
52
53#[derive(Debug, PartialEq, Clone)]
54pub enum Statement {
55    Let { name: Identifier, expr: Expr, span: Range<usize> },
56    ExprStmt { expr: Expr, span: Range<usize> },
57}
58
59#[derive(Debug, PartialEq, Clone)]
60pub enum Expr {
61    Ident(Identifier),
62    Atom { value: String, span: Range<usize> },
63    Number { value: String, span: Range<usize> },
64    String { value: String, span: Range<usize> },
65    Bool { value: bool, span: Range<usize> },
66    Unary { op: ElixirSyntaxKind, expr: Box<Expr>, span: Range<usize> },
67    Binary { left: Box<Expr>, op: ElixirSyntaxKind, right: Box<Expr>, span: Range<usize> },
68    Call { callee: Box<Expr>, args: Vec<Expr>, span: Range<usize> },
69    Field { receiver: Box<Expr>, field: Identifier, span: Range<usize> },
70    Index { receiver: Box<Expr>, index: Box<Expr>, span: Range<usize> },
71    Paren { expr: Box<Expr>, span: Range<usize> },
72    Block(Block),
73}