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