oak_python/ast/
mod.rs

1use crate::kind::PythonSyntaxKind;
2
3/// Python 源文件的根节点
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct SourceFile {
6    pub statements: Vec<Element>,
7}
8
9/// Python 语法元素
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum Element {
12    /// 函数定义
13    FunctionDef { name: String, args: Vec<String>, body: Vec<Element> },
14    /// 类定义
15    ClassDef { name: String, bases: Vec<String>, body: Vec<Element> },
16    /// 赋值语句
17    Assign { target: String, value: String },
18    /// 表达式语句
19    Expr { value: String },
20    /// 导入语句
21    Import { module: String, alias: Option<String> },
22    /// 从模块导入
23    ImportFrom { module: String, names: Vec<String> },
24    /// 返回语句
25    Return { value: Option<String> },
26    /// 条件语句
27    If { test: String, body: Vec<Element>, orelse: Vec<Element> },
28    /// 循环语句
29    For { target: String, iter: String, body: Vec<Element> },
30    /// While 循环
31    While { test: String, body: Vec<Element> },
32    /// 注释
33    Comment { text: String },
34    /// 其他元素
35    Other { kind: PythonSyntaxKind, text: String },
36}