Skip to main content

mist_parser/ast/
mod.rs

1use serde::Serialize;
2
3pub mod expr;
4pub mod statement;
5pub mod top_level;
6
7pub use expr::*;
8pub use statement::*;
9pub use top_level::*;
10
11#[derive(Debug, Clone, Serialize)]
12pub struct Path(pub Vec<Identifier>);
13
14#[derive(Debug, Clone, Serialize)]
15pub struct Identifier(pub String);
16
17#[derive(Debug, Clone, Serialize, Default)]
18pub struct ParamList(pub Vec<VarDecl>);
19
20#[derive(Debug, Clone, Serialize)]
21pub struct TypeExpr(pub TypeExprKind, pub Vec<TypePostfix>);
22
23#[derive(Debug, Clone, Serialize)]
24pub enum TypePostfix {
25    Ref,
26    RefMut,
27    RefLifetime(Identifier),
28    RefMutLifetime(Identifier),
29    Dyn,
30}
31
32#[derive(Debug, Clone, Serialize)]
33pub enum TypeExprKind {
34    Path(Path),
35    PathParams(Path, Vec<TypeExpr>),
36    Tuple(Vec<TypeExpr>),
37    Lifetime(Identifier),
38}
39
40#[derive(Debug, Clone, Serialize)]
41pub struct Spanned<T> {
42    pub line: usize,
43    pub column: usize,
44    pub item: T,
45}
46
47impl TypeExpr {
48    pub fn no_px(kind: TypeExprKind) -> Self {
49        Self(kind, Vec::new())
50    }
51}
52
53impl From<GenericDecl> for Generic {
54    fn from(value: GenericDecl) -> Self {
55        match value {
56            GenericDecl::Lifetime(life) => Generic::Lifetime(life),
57            GenericDecl::Type(ty, _) => {
58                Generic::Type(TypeExpr(TypeExprKind::Path(Path(vec![ty])), Vec::new()))
59            }
60        }
61    }
62}
63
64impl<T> Spanned<T> {
65    pub fn get_comment(&self) -> String {
66        format!("/* {}:{} */", self.line, self.column)
67    }
68}