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, Option<Generics>),
35    Tuple(Vec<TypeExpr>),
36    Lifetime(Identifier),
37}
38
39#[derive(Debug, Clone, Serialize)]
40pub struct Spanned<T> {
41    pub line: usize,
42    pub column: usize,
43    pub item: T,
44}
45
46impl TypeExpr {
47    pub fn no_px(kind: TypeExprKind) -> Self {
48        Self(kind, Vec::new())
49    }
50}
51
52impl From<GenericDecl> for Generic {
53    fn from(value: GenericDecl) -> Self {
54        match value {
55            GenericDecl::Lifetime(life) => Generic::Lifetime(life),
56            GenericDecl::Type(ty, _) => Generic::Type(TypeExpr(
57                TypeExprKind::Path(Path(vec![ty]), None),
58                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}