Skip to main content

mist_parser/ast/
top_level.rs

1use serde::Serialize;
2
3use super::*;
4
5#[derive(Debug, Clone, Serialize)]
6pub enum Visibility {
7    Public,
8    PublicTarget(Path),
9    Private,
10}
11
12#[derive(Debug, Clone, Serialize)]
13pub enum Attribute {
14    /// #[test]
15    Path(Path),
16
17    /// #[name = "value"]
18    NameValue { path: Path, value: Literal },
19
20    /// #[derive(Clone, Copy)]
21    List { path: Path, items: Vec<Attribute> },
22}
23
24#[derive(Debug, Clone, Serialize)]
25pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
26
27#[derive(Debug, Clone, Serialize)]
28pub enum TopLevelKind {
29    ModAttribute,
30    Import(Visibility, Path),
31    Mod(Visibility, Identifier),
32    ImplDecl(ImplDecl),
33    TraitDecl {
34        visibility: Visibility,
35        name: Identifier,
36        generics: GenericsDecl,
37        requirements: Vec<TypeExpr>,
38        items: Vec<Spanned<FunctionDecl>>,
39    },
40    EnumDecl {
41        visibility: Visibility,
42        name: Identifier,
43        generics: GenericsDecl,
44        fields: Vec<Spanned<EnumItem>>,
45    },
46    StructDecl {
47        visibility: Visibility,
48        name: Identifier,
49        generics: GenericsDecl,
50        fields: Vec<Spanned<FieldDecl>>,
51    },
52    FunctionDecl(FunctionDecl),
53    ClassDecl {
54        visibility: Visibility,
55        name: Identifier,
56        generics: GenericsDecl,
57        inherits: Option<TypeExpr>,
58        fields: Vec<Spanned<FieldDeclStmt>>,
59        constructor: Spanned<ClassConstructor>,
60        items: Vec<ClassItem>,
61    },
62}
63
64#[derive(Debug, Clone, Serialize)]
65pub enum ClassItem {
66    Method(Spanned<FunctionDecl>),
67    ImplDecl(Spanned<ImplDecl>),
68}
69
70#[derive(Debug, Clone, Serialize, Default)]
71pub struct GenericsDecl(pub Vec<GenericDecl>);
72
73#[derive(Debug, Clone, Serialize)]
74pub enum GenericDecl {
75    Lifetime(Identifier),
76    Type(Identifier, Vec<TypeExpr>),
77}
78
79#[derive(Debug, Clone, Serialize)]
80pub enum EnumItem {
81    Named(Identifier),
82    Tuple(Identifier, Vec<TypeExpr>),
83    Struct(Identifier, Vec<FieldDecl>),
84}
85
86#[derive(Debug, Clone, Serialize)]
87pub struct ClassConstructor {
88    pub visibility: Visibility,
89    pub generics: GenericsDecl,
90    pub params: ParamList,
91    pub body: Block,
92}
93
94#[derive(Debug, Clone, Serialize)]
95pub struct FunctionDecl {
96    pub visibility: Visibility,
97    pub name: Identifier,
98    pub generics: GenericsDecl,
99    pub params: ParamList,
100    pub return_type: Option<TypeExpr>,
101    pub body: Option<Block>,
102}
103
104#[derive(Debug, Clone, Serialize)]
105pub struct ImplDecl {
106    pub generics: GenericsDecl,
107    pub target: TypeExpr,
108    pub trait_: Option<TypeExpr>,
109    pub methods: Vec<Spanned<FunctionDecl>>,
110}
111
112#[derive(Debug, Clone, Serialize)]
113pub struct FieldDecl {
114    pub visibility: Visibility,
115    pub type_: TypeExpr,
116    pub name: Identifier,
117}
118
119#[derive(Debug, Clone, Serialize)]
120pub struct FieldDeclStmt {
121    pub decl: FieldDecl,
122    pub init: Option<Expression>,
123}