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<ExprPath>,
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, PartialEq, Eq, Hash)]
95pub struct Override(pub Option<ExprPath>);
96
97#[derive(Debug, Clone, Serialize)]
98pub struct FunctionDecl {
99    pub visibility: Visibility,
100    pub is_override: Option<Override>,
101    pub name: Identifier,
102    pub generics: GenericsDecl,
103    pub params: ParamList,
104    pub return_type: Option<TypeExpr>,
105    pub body: Option<Block>,
106}
107
108#[derive(Debug, Clone, Serialize)]
109pub struct ImplDecl {
110    pub generics: GenericsDecl,
111    pub target: TypeExpr,
112    pub trait_: Option<TypeExpr>,
113    pub methods: Vec<Spanned<FunctionDecl>>,
114}
115
116#[derive(Debug, Clone, Serialize)]
117pub struct FieldDecl {
118    pub visibility: Visibility,
119    pub type_: TypeExpr,
120    pub name: Identifier,
121}
122
123#[derive(Debug, Clone, Serialize)]
124pub struct FieldDeclStmt {
125    pub decl: FieldDecl,
126    pub init: Option<Expression>,
127}