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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! AST nodes for declarations.
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 Dec {
    Val(ValDec),
    Fun(FunDec),
    Type(TypeDec),
    Datatype(DatatypeDec),
    DatatypeRep(DatatypeRepDec),
    Abstype(AbstypeDec),
    Exception(ExceptionDec),
    Local(LocalDec),
    Open(OpenDec),
    Seq(SeqDec),
    Infix(InfixDec),
    Infixr(InfixrDec),
    Nonfix(NonfixDec),
}

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

    fn can_cast(kind: SyntaxKind) -> bool {
        matches!(
            kind,
            VAL_DEC
                | FUN_DEC
                | TY_DEC
                | DATATYPE_DEC
                | DATATYPE_REP
                | ABSTYPE_DEC
                | EXCEPT_DEC
                | LOCAL_DEC
                | OPEN_DEC
                | SEQ_DEC
                | INFIX_DEC
                | INFIXR_DEC
                | NONFIX_DEC
        )
    }

    fn cast(node: SyntaxNode) -> Option<Self>
    where
        Self: Sized,
    {
        let out = match node.kind() {
            VAL_DEC => Self::Val(ValDec::cast(node)?),
            FUN_DEC => Self::Fun(FunDec::cast(node)?),
            TY_DEC => Self::Type(TypeDec::cast(node)?),
            DATATYPE_DEC => Self::Datatype(DatatypeDec::cast(node)?),
            DATATYPE_REP => Self::DatatypeRep(DatatypeRepDec::cast(node)?),
            ABSTYPE_DEC => Self::Abstype(AbstypeDec::cast(node)?),
            EXCEPT_DEC => Self::Exception(ExceptionDec::cast(node)?),
            LOCAL_DEC => Self::Local(LocalDec::cast(node)?),
            OPEN_DEC => Self::Open(OpenDec::cast(node)?),
            SEQ_DEC => Self::Seq(SeqDec::cast(node)?),
            INFIX_DEC => Self::Infix(InfixDec::cast(node)?),
            INFIXR_DEC => Self::Infixr(InfixrDec::cast(node)?),
            NONFIX_DEC => Self::Nonfix(NonfixDec::cast(node)?),
            _ => return None,
        };
        Some(out)
    }

    fn syntax(&self) -> &SyntaxNode {
        match self {
            Self::Val(inner) => inner.syntax(),
            Self::Fun(inner) => inner.syntax(),
            Self::Type(inner) => inner.syntax(),
            Self::Datatype(inner) => inner.syntax(),
            Self::DatatypeRep(inner) => inner.syntax(),
            Self::Abstype(inner) => inner.syntax(),
            Self::Exception(inner) => inner.syntax(),
            Self::Local(inner) => inner.syntax(),
            Self::Open(inner) => inner.syntax(),
            Self::Seq(inner) => inner.syntax(),
            Self::Infix(inner) => inner.syntax(),
            Self::Infixr(inner) => inner.syntax(),
            Self::Nonfix(inner) => inner.syntax(),
        }
    }
}

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

impl_from!(Dec, Val, ValDec);
impl_from!(Dec, Fun, FunDec);
impl_from!(Dec, Type, TypeDec);
impl_from!(Dec, Datatype, DatatypeDec);
impl_from!(Dec, DatatypeRep, DatatypeRepDec);
impl_from!(Dec, Abstype, AbstypeDec);
impl_from!(Dec, Exception, ExceptionDec);
impl_from!(Dec, Local, LocalDec);
impl_from!(Dec, Open, OpenDec);
impl_from!(Dec, Seq, SeqDec);
impl_from!(Dec, Infix, InfixDec);
impl_from!(Dec, Infixr, InfixrDec);
impl_from!(Dec, Nonfix, NonfixDec);

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

impl_ast_node!(ValDec, VAL_DEC);

impl ValDec {
    pub fn tyvarseq(&self) -> impl Iterator<Item = ast::TyVar> {
        support::tokens(self.syntax())
    }

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

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

impl_ast_node!(FunDec, FUN_DEC);

impl FunDec {
    pub fn tyvarseq(&self) -> impl Iterator<Item = ast::TyVar> {
        support::tokens(self.syntax())
    }

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

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

impl_ast_node!(TypeDec, TY_DEC);

impl TypeDec {
    pub fn bindings(&self) -> impl Iterator<Item = ast::TyBind> {
        support::children(self.syntax())
    }
}

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

impl_ast_node!(DatatypeDec, DATATYPE_DEC);

impl DatatypeDec {
    pub fn databinds(&self) -> impl Iterator<Item = ast::DataBind> {
        support::children(self.syntax())
    }

    pub fn withtype(&self) -> bool {
        support::token(self.syntax(), SyntaxKind::WITHTYPE_KW).is_some()
    }

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

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

impl_ast_node!(DatatypeRepDec, DATATYPE_REP);

impl DatatypeRepDec {
    pub fn tycon(&self) -> Option<ast::TyCon> {
        support::tokens(self.syntax()).next()
    }

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

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

impl_ast_node!(AbstypeDec, ABSTYPE_DEC);

impl AbstypeDec {
    pub fn databinds(&self) -> impl Iterator<Item = ast::DataBind> {
        support::children(self.syntax())
    }

    pub fn withtype(&self) -> bool {
        support::token(self.syntax(), SyntaxKind::WITHTYPE_KW).is_some()
    }

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

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

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

impl_ast_node!(ExceptionDec, EXCEPT_DEC);

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

impl_ast_node!(LocalDec, LOCAL_DEC);

impl LocalDec {
    pub fn dec1(&self) -> Option<ast::Dec> {
        support::children(self.syntax()).next()
    }

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

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

impl_ast_node!(OpenDec, OPEN_DEC);

impl OpenDec {
    pub fn longstrids(&self) -> impl Iterator<Item = ast::LongStrId> {
        support::children(self.syntax())
    }
}

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

impl_ast_node!(SeqDec, SEQ_DEC);

impl SeqDec {
    pub fn declarations(&self) -> impl Iterator<Item = ast::Dec> {
        support::children(self.syntax())
    }
}

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

impl_ast_node!(InfixDec, INFIX_DEC);

impl InfixDec {
    pub fn fixity(&self) -> Option<ast::Fixity> {
        support::child(self.syntax())
    }

    pub fn vids(&self) -> impl Iterator<Item = ast::VId> {
        support::tokens(self.syntax())
    }
}

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

impl_ast_node!(InfixrDec, INFIXR_DEC);

impl InfixrDec {
    pub fn fixity(&self) -> Option<ast::Fixity> {
        support::child(self.syntax())
    }

    pub fn vids(&self) -> impl Iterator<Item = ast::VId> {
        support::tokens(self.syntax())
    }
}

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

impl_ast_node!(Fixity, FIXITY);

impl Fixity {
    pub fn value(&self) -> u8 {
        support::token(self.syntax(), SyntaxKind::INT)
            .and_then(|s| s.text().parse().ok())
            .unwrap_or(0)
    }
}

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

impl_ast_node!(NonfixDec, NONFIX_DEC);

impl NonfixDec {
    pub fn vids(&self) -> impl Iterator<Item = ast::VId> {
        support::tokens(self.syntax())
    }
}