1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! AST nodes for types and type expressions.
use crate::{
    ast, ast::support, impl_ast_node, impl_from, language::SyntaxNode, AstNode, SyntaxKind,
};
use SyntaxKind::*;

use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Ty {
    Fun(FunTy),
    Tuple(TupleTy),
    Cons(ConsTy),
    Record(RecordTy),
    TyVar(TyVarTy),
}

impl AstNode for Ty {
    type Language = crate::language::SML;

    fn can_cast(kind: SyntaxKind) -> bool {
        matches!(kind, FUN_TY | TUPLE_TY_EXP | CON_TY | RECORD_TY | TYVAR_TY)
    }

    fn cast(node: SyntaxNode) -> Option<Self>
    where
        Self: Sized,
    {
        let out = match node.kind() {
            FUN_TY => Self::Fun(FunTy { syntax: node }),
            TUPLE_TY_EXP => Self::Tuple(TupleTy { syntax: node }),
            CON_TY => Self::Cons(ConsTy { syntax: node }),
            RECORD_TY => Self::Record(RecordTy { syntax: node }),
            TYVAR_TY => Self::TyVar(TyVarTy { syntax: node }),
            _ => return None,
        };
        Some(out)
    }

    fn syntax(&self) -> &SyntaxNode {
        match self {
            Self::Fun(inner) => inner.syntax(),
            Self::Tuple(inner) => inner.syntax(),
            Self::Cons(inner) => inner.syntax(),
            Self::Record(inner) => inner.syntax(),
            Self::TyVar(inner) => inner.syntax(),
        }
    }
}

impl fmt::Display for Ty {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.syntax())
    }
}

impl_from!(Ty, Fun, FunTy);
impl_from!(Ty, Tuple, TupleTy);
impl_from!(Ty, Cons, ConsTy);
impl_from!(Ty, Record, RecordTy);
impl_from!(Ty, TyVar, TyVarTy);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FunTy {
    syntax: SyntaxNode,
}

impl_ast_node!(FunTy, FUN_TY);

impl FunTy {
    pub fn ty_1(&self) -> Option<ast::Ty> {
        support::child(self.syntax())
    }

    pub fn ty_2(&self) -> Option<ast::Ty> {
        support::children(self.syntax()).nth(1)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TupleTy {
    syntax: SyntaxNode,
}

impl_ast_node!(TupleTy, TUPLE_TY_EXP);

impl TupleTy {
    pub fn tys(&self) -> impl Iterator<Item = ast::Ty> {
        support::children(self.syntax())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConsTy {
    syntax: SyntaxNode,
}

impl_ast_node!(ConsTy, CON_TY);

impl ConsTy {
    pub fn tys(&self) -> impl Iterator<Item = ast::Ty> {
        support::children(self.syntax())
    }

    pub fn longtycon(&self) -> Option<ast::LongTyCon> {
        support::child(self.syntax())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordTy {
    syntax: SyntaxNode,
}

impl_ast_node!(RecordTy, RECORD_TY);

impl RecordTy {
    pub fn tyrows(&self) -> impl Iterator<Item = ast::TyRow> {
        support::children(self.syntax())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TyRow {
    syntax: SyntaxNode,
}

impl_ast_node!(TyRow, TY_ROW);

impl TyRow {
    pub fn label(&self) -> Option<ast::Label> {
        support::tokens(self.syntax()).next()
    }

    pub fn ty(&self) -> Option<ast::Ty> {
        support::child(self.syntax())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TyVarTy {
    syntax: SyntaxNode,
}

impl_ast_node!(TyVarTy, TYVAR_TY);

impl TyVarTy {
    pub fn tyvar(&self) -> Option<ast::TyVar> {
        support::tokens(self.syntax()).next()
    }
}