kotlin_parser/
ast.rs

1#[derive(Debug, PartialEq, Clone)]
2pub struct KotlinFile {
3    pub package: Option<Package>,
4    pub imports: Vec<Import>,
5    pub declarations: Vec<Declaration>,
6    pub annotations: Vec<AnnotationSet>,
7}
8
9#[derive(Debug, PartialEq, Clone)]
10pub struct Package {
11    pub path: Path,
12}
13
14#[derive(Debug, PartialEq, Clone)]
15pub struct Import {
16    pub path: Path,
17    pub is_wildcard: bool,
18    pub alias: Option<String>,
19}
20
21#[derive(Debug, PartialEq, Clone)]
22pub enum Statement {
23    Declaration(Declaration),
24    Expression(Expression),
25}
26
27#[derive(Debug, PartialEq, Clone)]
28pub struct Declaration {
29    pub annotations: Vec<AnnotationSet>,
30    pub kind: DeclarationKind,
31}
32
33#[derive(Debug, PartialEq, Clone)]
34pub enum DeclarationKind {
35    Constructor(ConstructorDeclaration),
36    Entity(EntityDeclaration),
37    EnumEntry(EnumEntryDeclaration),
38    Function(FunctionDeclaration),
39    InitBlock(Block),
40    Property(PropertyDeclaration),
41    TypeAlias(TypeAliasDeclaration),
42}
43
44#[derive(Debug, PartialEq, Clone)]
45pub struct EntityDeclaration {
46    pub modifiers: Vec<Modifier>,
47    pub kind: EntityDeclarationKind,
48    pub name: Option<String>,
49    pub type_params: Vec<BoundedTypeParam>,
50    pub primary_constructor: Option<PrimaryConstructorDeclaration>,
51    pub extends: Vec<Type>,
52    pub bounds: Vec<TypeBound>,
53    pub body: Option<Block>,
54}
55
56#[derive(Debug, PartialEq, Clone)]
57pub enum EntityDeclarationKind {
58    Class,
59    Interface,
60    Object,
61    CompanionObject,
62    Enum,
63}
64
65#[derive(Debug, PartialEq, Clone)]
66pub struct PrimaryConstructorDeclaration {
67    pub modifiers: Vec<Modifier>,
68    pub params: Vec<ConstructorParam>,
69}
70
71#[derive(Debug, PartialEq, Clone)]
72pub struct ConstructorDeclaration {
73    pub modifiers: Vec<Modifier>,
74    pub params: Vec<Param>,
75    pub delegate: Option<ConstructorDelegate>,
76    pub body: Option<Block>,
77}
78
79#[derive(Debug, PartialEq, Clone)]
80pub struct ConstructorDelegate {
81    pub kind: ConstructorDelegateKind,
82    pub args: Vec<CallArg>,
83}
84
85#[derive(Debug, PartialEq, Clone)]
86pub enum ConstructorDelegateKind {
87    This,
88    Super,
89}
90
91#[derive(Debug, PartialEq, Clone)]
92pub struct FunctionDeclaration {
93    pub modifiers: Vec<Modifier>,
94    pub type_params: Vec<TypeParam>,
95    pub receiver: Option<Type>,
96    pub name: Option<String>,
97    pub params: Vec<Param>,
98    pub return_ty: Option<Type>,
99    pub bounds: Vec<TypeBound>,
100    pub body: Option<Block>,
101}
102
103#[derive(Debug, PartialEq, Clone)]
104pub struct Block {
105    pub statements: Vec<Statement>,
106}
107
108#[derive(Debug, PartialEq, Clone)]
109pub struct PropertyDeclaration {
110    pub modifiers: Vec<Modifier>,
111    pub is_const: bool,
112    pub is_mutable: bool,
113    pub is_delegated: bool,
114    pub type_params: Vec<TypeParam>,
115    pub vars: Vars,
116    pub receiver: Option<Type>,
117    pub bounds: Vec<TypeBound>,
118    pub init: Option<Expression>,
119    pub accessors: Vec<PropertyAccessor>,
120}
121
122#[derive(Debug, PartialEq, Clone)]
123pub enum PropertyAccessor {
124    Getter {
125        annotations: Vec<AnnotationSet>,
126        modifiers: Vec<Modifier>,
127        return_ty: Option<Type>,
128        body: Option<Block>,
129    },
130    Setter {
131        annotations: Vec<AnnotationSet>,
132        modifiers: Vec<Modifier>,
133        field: Option<PropertySetterField>,
134        return_ty: Option<Type>,
135        body: Option<Block>,
136    },
137}
138
139#[derive(Debug, PartialEq, Clone)]
140pub struct PropertySetterField {
141    pub name: String,
142    pub ty: Option<Type>,
143}
144
145#[derive(Debug, PartialEq, Clone)]
146pub struct TypeAliasDeclaration {
147    pub modifiers: Vec<Modifier>,
148    pub name: String,
149    pub type_params: Vec<TypeParam>,
150    pub ty: Type,
151}
152
153#[derive(Debug, PartialEq, Clone)]
154pub struct EnumEntryDeclaration {
155    pub modifiers: Vec<Modifier>,
156    pub name: String,
157    pub args: Vec<CallArg>,
158    pub body: Vec<Declaration>,
159}
160
161#[derive(Debug, PartialEq, Clone)]
162pub enum Expression {
163    Literal(Literal),
164    Bracket(BracketExpression),
165    BinaryOp(BinaryOperation),
166    Break(BreakExpression),
167    Call(CallExpression),
168    Continue(ContinueExpression),
169    For(ForExpression),
170    If(IfExpression),
171    Lambda(LambdaBlock),
172    Labeled(LabeledExpression),
173    Object(ObjectExpression),
174    Parenthesized(ParenthesizedExpression),
175    MemberReference(MemberReferenceExpression),
176    Reference(Path),
177    Return(ReturnExpression),
178    StringTemplate(StringTemplateExpression),
179    Super(SuperExpression),
180    This(ThisExpression),
181    Throw(ThrowExpression),
182    Try(TryExpression),
183    UnaryOp(UnaryOperation),
184    When(WhenExpression),
185    While(WhileExpression),
186}
187
188#[derive(Debug, PartialEq, Clone)]
189pub enum Literal {
190    UnsignedInteger(u64),
191    Integer(i64),
192    Decimal(f64),
193    String(String),
194    Char(char),
195    Boolean(bool),
196    Null,
197}
198
199#[derive(Debug, PartialEq, Clone)]
200pub struct IfExpression {
201    pub expr: Box<Expression>,
202    pub then: Block,
203    pub otherwise: Option<Box<Expression>>,
204}
205
206#[derive(Debug, PartialEq, Clone)]
207pub struct ForExpression {
208    pub vars: Vars,
209    pub iterable: Box<Expression>,
210    pub body: Block,
211}
212
213#[derive(Debug, PartialEq, Clone)]
214pub struct WhileExpression {
215    pub expr: Box<Expression>,
216    pub body: Block,
217    pub is_do_while: bool,
218}
219
220#[derive(Debug, PartialEq, Clone)]
221pub struct TryExpression {
222    pub body: Block,
223    pub catches: Vec<CatchExpression>,
224    pub finally: Option<Block>,
225}
226
227#[derive(Debug, PartialEq, Clone)]
228pub struct CatchExpression {
229    pub param: Param,
230    pub body: Block,
231}
232
233#[derive(Debug, PartialEq, Clone)]
234pub struct BinaryOperation {
235    pub lhs: Box<Expression>,
236    pub op: BinaryOperator,
237    pub rhs: Box<Expression>,
238}
239
240#[derive(Debug, PartialEq, Clone)]
241pub enum BinaryOperator {
242    Operator(BinaryOp),
243    Infix(String),
244}
245
246#[derive(Debug, PartialEq, Clone)]
247pub enum BinaryOp {
248    Assign,
249    AddAssign,
250    SubtractAssign,
251    MultiplyAssign,
252    DivideAssign,
253    ModuloAssign,
254    Equal,
255    NotEqual,
256    ReferenceEqual,
257    ReferenceNotEqual,
258    LessThan,
259    LessThanOrEqual,
260    GreaterThan,
261    GreaterThanOrEqual,
262    And,
263    Or,
264    In,
265    NotIn,
266    Is,
267    IsNot,
268    RangeTo,
269    RangeUntil,
270    Add,
271    Subtract,
272    Multiply,
273    Divide,
274    Modulo,
275    As,
276    AsNullable,
277    Elvis,
278    Dot,
279    DotSafe,
280}
281
282#[derive(Debug, PartialEq, Clone)]
283pub struct UnaryOperation {
284    pub op: UnaryOperator,
285    pub expr: Box<Expression>,
286    pub is_prefix: bool,
287}
288
289#[derive(Debug, PartialEq, Clone)]
290pub enum UnaryOperator {
291    Plus,
292    Minus,
293    Increment,
294    Decrement,
295    Not,
296    NullDeref,
297}
298
299#[derive(Debug, PartialEq, Clone)]
300pub struct ThisExpression {
301    pub label: Option<String>,
302}
303
304#[derive(Debug, PartialEq, Clone)]
305pub struct SuperExpression {
306    pub label: Option<String>,
307    pub type_arg: Option<Type>,
308}
309
310#[derive(Debug, PartialEq, Clone)]
311pub struct WhenExpression {
312    pub expr: Option<Box<Expression>>,
313    pub entries: Vec<WhenEntry>,
314}
315
316#[derive(Debug, PartialEq, Clone)]
317pub struct WhenEntry {
318    pub exprs: Vec<Expression>,
319    pub body: Block,
320}
321
322#[derive(Debug, PartialEq, Clone)]
323pub struct ObjectExpression {
324    pub annotations: Vec<AnnotationSet>,
325    pub extends: Vec<Type>,
326    pub body: Block,
327}
328
329#[derive(Debug, PartialEq, Clone)]
330pub struct ParenthesizedExpression {
331    pub expr: Box<Expression>,
332}
333
334#[derive(Debug, PartialEq, Clone)]
335pub struct ThrowExpression {
336    pub expr: Box<Expression>,
337}
338
339#[derive(Debug, PartialEq, Clone)]
340pub struct ReturnExpression {
341    pub label: Option<String>,
342    pub expr: Option<Box<Expression>>,
343}
344
345#[derive(Debug, PartialEq, Clone)]
346pub struct ContinueExpression {
347    pub label: Option<String>,
348}
349
350#[derive(Debug, PartialEq, Clone)]
351pub struct BreakExpression {
352    pub label: Option<String>,
353}
354
355#[derive(Debug, PartialEq, Clone)]
356pub struct LabeledExpression {
357    pub label: String,
358    pub expr: Box<Expression>,
359}
360
361#[derive(Debug, PartialEq, Clone)]
362pub struct CallExpression {
363    pub path: Path,
364    pub args: Vec<CallArg>,
365    pub type_args: Vec<Type>,
366    pub lambda: Option<Box<LambdaBlock>>,
367}
368
369#[derive(Debug, PartialEq, Clone)]
370pub struct LambdaBlock {
371    pub label: Option<String>,
372    pub vars: Option<Vars>,
373    pub body: Option<Block>,
374}
375
376#[derive(Debug, PartialEq, Clone)]
377pub struct BracketExpression {
378    pub expr: Box<Expression>,
379}
380
381#[derive(Debug, PartialEq, Clone)]
382pub struct MemberReferenceExpression {
383    pub lhs: Option<Box<Expression>>,
384    pub rhs: Box<Expression>,
385}
386
387#[derive(Debug, PartialEq, Clone)]
388pub enum StringTemplateExpression {
389    Simple(String),
390    Block(Block),
391}
392
393#[derive(Debug, PartialEq, Clone)]
394pub enum Type {
395    Simple(Box<SimpleType>),
396    Function(Box<FunctionType>),
397}
398
399#[derive(Debug, PartialEq, Clone)]
400pub struct SimpleType {
401    pub name: String,
402    pub type_args: Vec<Type>,
403    pub is_nullable: bool,
404}
405
406#[derive(Debug, PartialEq, Clone)]
407pub struct FunctionType {
408    pub receiver: Option<Type>,
409    pub params: Vec<AnonymousParam>,
410    pub return_ty: Type,
411    pub is_nullable: bool,
412}
413
414#[derive(Debug, PartialEq, Clone)]
415pub struct AnonymousParam {
416    pub name: Option<String>,
417    pub ty: Type,
418}
419
420#[derive(Debug, PartialEq, Clone)]
421pub struct Param {
422    pub annotations: Vec<AnnotationSet>,
423    pub name: String,
424    pub ty: Type,
425}
426
427#[derive(Debug, PartialEq, Clone)]
428pub struct ConstructorParam {
429    pub modifiers: Vec<Modifier>,
430    pub property_type: Option<PropertyType>,
431    pub param: Param,
432}
433
434#[derive(Debug, PartialEq, Clone)]
435pub enum PropertyType {
436    Var,
437    Val,
438}
439
440#[derive(Debug, PartialEq, Clone)]
441pub struct TypeParam {
442    pub annotations: Vec<AnnotationSet>,
443    pub name: String,
444    pub ty: Option<Type>,
445}
446
447#[derive(Debug, PartialEq, Clone)]
448pub struct BoundedTypeParam {
449    pub annotations: Vec<AnnotationSet>,
450    pub bounds: Vec<TypeBound>,
451}
452
453#[derive(Debug, PartialEq, Clone)]
454pub struct TypeBound {
455    pub name: String,
456    pub ty: Option<Type>,
457    pub kind: Option<BoundKind>,
458}
459
460#[derive(Debug, PartialEq, Clone)]
461pub enum BoundKind {
462    In,
463    Out,
464}
465
466#[derive(Debug, PartialEq, Clone)]
467pub struct AnnotationSet {
468    pub site: Option<AnnotationSite>,
469    pub annotations: Vec<Annotation>,
470}
471
472#[derive(Debug, PartialEq, Clone)]
473pub struct Annotation {
474    pub path: Path,
475    pub args: Vec<InvocationArg>,
476}
477
478#[derive(Debug, PartialEq, Clone)]
479pub struct CallArg {
480    pub name: Option<String>,
481    pub value: Box<Expression>,
482    pub is_spread: bool,
483}
484
485#[derive(Debug, PartialEq, Clone)]
486pub struct InvocationArg {
487    pub name: Option<String>,
488    pub value: Box<Expression>,
489}
490
491#[derive(Debug, PartialEq, Clone)]
492pub struct Vars {
493    pub is_destructured: bool,
494    pub vars: Vec<Var>,
495}
496
497#[derive(Debug, PartialEq, Clone)]
498pub struct Var {
499    pub name: String,
500    pub ty: Option<Type>,
501}
502
503#[derive(Debug, PartialEq, Clone)]
504pub enum AnnotationSite {
505    Field,
506    Property,
507    Get,
508    Set,
509    Receiver,
510    Param,
511    SetParam,
512    Delegate,
513}
514
515pub type Path = Vec<String>;
516
517#[derive(Debug, PartialEq, Clone)]
518pub enum Modifier {
519    Abstract,
520    Final,
521    Open,
522    Annotation,
523    Sealed,
524    Data,
525    Override,
526    Lateinit,
527    Inner,
528    Private,
529    Protected,
530    Public,
531    Internal,
532    In,
533    Out,
534    NoInline,
535    CrossInline,
536    Vararg,
537    Reified,
538    Tailrec,
539    Operator,
540    Infix,
541    Inline,
542    External,
543    Suspend,
544    Const,
545    Actual,
546    Expect,
547}