Skip to main content

mist_parser/
ast.rs

1use serde::Serialize;
2
3#[derive(Debug, Clone, Serialize)]
4pub struct Identifier(pub String);
5
6#[derive(Debug, Clone, Serialize, Default)]
7pub struct ParamList(pub Vec<VarDecl>);
8
9#[derive(Debug, Clone, Serialize, Default)]
10pub struct Block(pub Vec<Statement>);
11
12#[derive(Debug, Clone, Serialize)]
13pub enum TypePostfix {
14    Ref,
15    RefMut,
16    RefLifetime(Identifier),
17    RefMutLifetime(Identifier),
18}
19
20#[derive(Debug, Clone, Serialize)]
21pub enum Visibility {
22    Public,
23    Private,
24}
25
26#[derive(Debug, Clone, Serialize)]
27pub enum Attribute {
28    /// #[test]
29    Path(Path),
30
31    /// #[name = "value"]
32    NameValue { path: Path, value: Literal },
33
34    /// #[derive(Clone, Copy)]
35    List { path: Path, items: Vec<Attribute> },
36}
37
38#[derive(Debug, Clone, Serialize)]
39pub enum TypeExprKind {
40    Path(Path),
41    PathParams(Path, Vec<TypeExpr>),
42    Tuple(Vec<TypeExpr>),
43    Lifetime(Identifier),
44}
45
46#[derive(Debug, Clone, Serialize)]
47pub struct TypeExpr(pub TypeExprKind, pub Vec<TypePostfix>);
48
49#[derive(Debug, Clone, Serialize)]
50pub struct Path(pub Vec<Identifier>);
51
52#[derive(Debug, Clone, Serialize)]
53pub enum BinaryOp {
54    Plus,
55    Minus,
56    Multiply,
57    Divide,
58    Modulo,
59    Equal,
60    NotEqual,
61    LessThan,
62    GreaterThan,
63    LessThanOrEqual,
64    GreaterThanOrEqual,
65    And,
66    Or,
67}
68
69#[derive(Debug, Clone, Serialize)]
70pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
71
72#[derive(Debug, Clone, Serialize)]
73pub enum TopLevelKind {
74    ModAttribute,
75    Import(Path),
76    Mod(Identifier),
77    ImplDecl(ImplDecl),
78    TraitDecl {
79        visibility: Visibility,
80        name: Identifier,
81        generics: Generics,
82        requirements: Vec<TypeExpr>,
83        items: Vec<FunctionDecl>,
84    },
85    EnumDecl {
86        visibility: Visibility,
87        name: Identifier,
88        generics: Generics,
89        fields: Vec<EnumItem>,
90    },
91    StructDecl {
92        visibility: Visibility,
93        name: Identifier,
94        generics: Generics,
95        fields: Vec<FieldDecl>,
96    },
97    FunctionDecl(FunctionDecl),
98    ClassDecl {
99        visibility: Visibility,
100        name: Identifier,
101        generics: Generics,
102        fields: Vec<FieldDeclStmt>,
103        constructor: ClassConstructor,
104        items: Vec<ClassItem>,
105    },
106}
107
108#[derive(Debug, Clone, Serialize)]
109pub enum ClassItem {
110    Method(FunctionDecl),
111    ImplDecl(ImplDecl),
112}
113
114#[derive(Debug, Clone, Serialize, Default)]
115pub struct Generics(pub Vec<Generic>);
116
117#[derive(Debug, Clone, Serialize)]
118pub enum Generic {
119    Lifetime(Identifier),
120    Type(Identifier, Vec<TypeExpr>),
121}
122
123#[derive(Debug, Clone, Serialize)]
124pub enum Pattern {
125    NamedTuple(Path, Vec<Identifier>),
126    Struct(Path, Vec<Identifier>),
127    Tuple(Vec<Identifier>),
128    Literal(Literal),
129    Path(Path),
130    Id(Identifier),
131}
132
133#[derive(Debug, Clone, Serialize)]
134pub enum EnumItem {
135    Named(Identifier),
136    Tuple(Identifier, Vec<TypeExpr>),
137    Struct(Identifier, Vec<FieldDecl>),
138}
139
140#[derive(Debug, Clone, Serialize)]
141pub struct ClassConstructor {
142    pub visibility: Visibility,
143    pub generics: Generics,
144    pub params: ParamList,
145    pub body: Block,
146}
147
148#[derive(Debug, Clone, Serialize)]
149pub struct FunctionDecl {
150    pub visibility: Visibility,
151    pub name: Identifier,
152    pub generics: Generics,
153    pub params: ParamList,
154    pub return_type: TypeExpr,
155    pub body: Option<Block>,
156}
157
158#[derive(Debug, Clone, Serialize)]
159pub enum Postfix {
160    FieldAccess(Identifier),
161    Call(Vec<Expression>),
162    MacroCall(String),
163    StructCall(Vec<(Identifier, Expression)>),
164    Index(Expression),
165    Binary(BinaryOp, Expression),
166}
167
168#[derive(Debug, Clone, Serialize)]
169pub enum Prefix {
170    Ref,
171    RefMut,
172    Deref,
173    New,
174    Not,
175}
176
177#[derive(Debug, Clone, Serialize)]
178pub enum Statement {
179    Expression(Expression),
180    Block(Block),
181
182    VarDecl(VarDeclStmt),
183    VarAssign(VarAssignStmt),
184    If {
185        initial: StatementBranch,
186        else_if: Vec<StatementBranch>,
187        else_branch: Option<Box<Statement>>,
188    },
189    While(StatementBranch),
190    CStyleFor {
191        init: Box<Statement>,
192        condition: Expression,
193        update: Box<Statement>,
194        body: Box<Statement>,
195    },
196    For {
197        mutable: bool,
198        pattern: Pattern,
199        iterator: Expression,
200        body: Box<Statement>,
201    },
202    Match(Expression, Vec<(Pattern, Block)>),
203
204    Return(Option<Expression>),
205    Break,
206    Continue,
207}
208
209#[derive(Debug, Clone, Serialize)]
210pub struct ImplDecl {
211    pub generics: Generics,
212    pub target: TypeExpr,
213    pub trait_: Option<TypeExpr>,
214    pub methods: Vec<FunctionDecl>,
215}
216
217#[derive(Debug, Clone, Serialize)]
218pub struct VarDecl {
219    pub mutable: bool,
220    pub name: Pattern,
221    pub type_: Option<TypeExpr>,
222}
223
224#[derive(Debug, Clone, Serialize)]
225pub struct VarDeclStmt {
226    pub decl: VarDecl,
227    pub init: Option<Expression>,
228}
229
230#[derive(Debug, Clone, Serialize)]
231pub struct FieldDecl {
232    pub visibility: Visibility,
233    pub type_: TypeExpr,
234    pub name: Identifier,
235}
236
237#[derive(Debug, Clone, Serialize)]
238pub struct FieldDeclStmt {
239    pub decl: FieldDecl,
240    pub init: Option<Expression>,
241}
242
243#[derive(Debug, Clone, Serialize)]
244pub struct VarAssignStmt {
245    pub target: Expression,
246    pub value: Expression,
247}
248
249#[derive(Debug, Clone, Serialize)]
250pub struct StatementBranch {
251    pub condition: Expression,
252    pub body: Box<Statement>,
253}
254
255#[derive(Debug, Clone, Serialize)]
256pub enum Expression {
257    Literal(Literal),
258    Path(Path),
259    Fix {
260        initial: Box<Expression>,
261        prefixes: Vec<Prefix>,
262        postfixes: Vec<Postfix>,
263    },
264}
265
266#[derive(Debug, Clone, Serialize)]
267pub enum Literal {
268    String(String),
269    Int(i64),
270    Float(f64),
271    Bool(bool),
272    Tuple(Vec<Expression>),
273}