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, PartialEq, Eq, Hash)]
12pub struct Path(pub Vec<Identifier>);
13
14#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
15pub struct Identifier(pub String);
16
17#[derive(Debug, Clone, Serialize, Default)]
18pub struct ParamList(pub Vec<VarDecl>);
19
20#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
21pub enum TypeExpr {
22    Ref {
23        lifetime: Option<Identifier>,
24        mutable: bool,
25        ty: Box<TypeExpr>,
26    },
27    UnsafePtr {
28        mutable: bool,
29        ty: Box<TypeExpr>,
30    },
31    Dyn(Box<TypeExpr>),
32    Path(Path, Option<Generics>),
33    StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>),
34    Tuple(Vec<TypeExpr>),
35    Lifetime(Identifier),
36}
37
38#[derive(Debug, Clone, Serialize)]
39pub struct Spanned<T> {
40    pub line: usize,
41    pub column: usize,
42    pub item: T,
43}
44
45impl From<GenericDecl> for Generic {
46    fn from(value: GenericDecl) -> Self {
47        match value {
48            GenericDecl::Lifetime(life) => Generic::Lifetime(life),
49            GenericDecl::Type(ty, _) => Generic::Type(TypeExpr::Path(Path(vec![ty]), None)),
50        }
51    }
52}
53
54impl Into<Option<Generics>> for GenericsDecl {
55    fn into(self) -> Option<Generics> {
56        if self.0.len() == 0 {
57            None
58        } else {
59            Some(self.into())
60        }
61    }
62}
63
64impl From<GenericsDecl> for Generics {
65    fn from(value: GenericsDecl) -> Self {
66        Self(value.0.into_iter().map(Generic::from).collect())
67    }
68}
69
70impl<T> Spanned<T> {
71    pub fn get_comment(&self) -> String {
72        format!("/* {}:{} */", self.line, self.column)
73    }
74}