Skip to main content

mist_parser/ast/
expr.rs

1use serde::Serialize;
2
3use super::*;
4
5#[derive(Debug, Clone, Serialize)]
6pub enum Expression {
7    Literal(Literal),
8    Path(ExprPath),
9    Statement(Box<Statement>),
10    Fix {
11        initial: Box<Expression>,
12        prefixes: Vec<Prefix>,
13        postfixes: Vec<Postfix>,
14    },
15    Binary {
16        lhs: Box<Expression>,
17        op: String,
18        rhs: Box<Expression>,
19    },
20}
21
22#[derive(Debug, Clone, Serialize)]
23pub enum Literal {
24    String(String),
25    Int(i64),
26    Float(f64),
27    Bool(bool),
28    Tuple(Vec<Expression>),
29    Array(Vec<Expression>),
30    ArrayRepeat(Box<Expression>, Box<Expression>),
31}
32
33#[derive(Debug, Clone, Serialize)]
34pub enum Pattern {
35    NamedTuple(Path, Vec<Box<Pattern>>),
36    Struct(Path, Vec<Option<(Identifier, Option<Box<Pattern>>)>>),
37    Tuple(Vec<Box<Pattern>>),
38    Literal(Literal),
39    Path(bool, Path),
40    Etc,
41}
42
43#[derive(Debug, Clone, Serialize)]
44pub enum MacroDelimiter {
45    Paren,
46    Bracket,
47    Brace,
48}
49
50#[derive(Debug, Clone, Serialize)]
51pub enum Postfix {
52    FieldAccess(Identifier, Option<Generics>),
53    TupleFieldAccess(u8, Option<Generics>),
54    Call(Vec<Expression>),
55    MacroCall {
56        inner: String,
57        delimiter: MacroDelimiter,
58    },
59    StructCall(Vec<(Identifier, Option<Expression>)>),
60    Assign(String, Box<Expression>),
61    Index(Expression),
62    As(TypeExpr),
63    Increment,
64    Decrement,
65    Try,
66}
67
68#[derive(Debug, Clone, Serialize)]
69pub enum Prefix {
70    Ref,
71    RefMut,
72    Deref,
73    Not,
74    Neg,
75    Closure(Option<TypeExpr>, Vec<VarDecl>),
76}
77
78#[derive(Debug, Clone, Serialize)]
79pub struct ExprPathSegment {
80    pub ident: Identifier,
81    pub generics: Option<Generics>,
82}
83
84#[derive(Debug, Clone, Serialize)]
85pub struct ExprPath(pub Vec<ExprPathSegment>);
86
87#[derive(Debug, Clone, Serialize)]
88pub struct Generics(pub Vec<Generic>);
89
90#[derive(Debug, Clone, Serialize)]
91pub enum Generic {
92    Lifetime(Identifier),
93    Type(TypeExpr),
94}
95
96impl Expression {
97    pub fn is_block(&self) -> bool {
98        if let Expression::Statement(stmt) = self {
99            stmt.is_block()
100        } else {
101            false
102        }
103    }
104}
105
106impl From<Path> for ExprPath {
107    fn from(path: Path) -> Self {
108        Self(
109            path.0
110                .into_iter()
111                .map(|v| ExprPathSegment {
112                    ident: v,
113                    generics: None,
114                })
115                .collect(),
116        )
117    }
118}