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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use proc_macro2::Span;
use std::fmt::Display;

pub type Error = Vec<(String, Option<Span>)>;
pub trait IntoError {
    fn into_err(self) -> Error;
}
pub trait TrySpan {
    fn try_span(&self) -> Option<Span> {
        None
    }
}

impl TrySpan for Span {
    fn try_span(&self) -> Option<Span> {
        Some(*self)
    }
}
impl<T: TrySpan> TrySpan for &T {
    fn try_span(&self) -> Option<Span> {
        (*self).try_span()
    }
}
impl<T: TrySpan> TrySpan for Option<T> {
    fn try_span(&self) -> Option<Span> {
        self.as_ref().and_then(|t| t.try_span())
    }
}

pub struct Basic {
    pub msg: String,
    pub span: Span,
}

impl IntoError for Basic {
    fn into_err(self) -> Error {
        vec![(self.msg, Some(self.span))]
    }
}

pub struct TypeMismatch<Source, Left, Right, Msg> {
    pub source: Source,
    pub left: Left,
    pub right: Right,
    pub msg: Msg,
}

impl<Source, Left, Right, Msg> IntoError for TypeMismatch<Source, Left, Right, Msg>
where
    Source: TrySpan,
    Msg: Display,
    Left: Display + TrySpan,
    Right: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Type mismatch between the left and right sides: {}",
                    self.msg
                ),
                self.source.try_span(),
            ),
            (
                format!("This element has type {}", self.left),
                self.left.try_span(),
            ),
            (
                format!("While this element has type {}", self.right),
                self.right.try_span(),
            ),
        ]
    }
}

pub struct VarNotFound<Var, Suggest1, Suggest2> {
    pub var: Var,
    pub suggest1: Suggest1,
    pub suggest2: Suggest2,
}

impl<Var, Suggest1, S1, Suggest2, S2> IntoError for VarNotFound<Var, Suggest1, Suggest2>
where
    Var: Display + TrySpan,
    Suggest1: IntoIterator<Item = S1>,
    Suggest2: IntoIterator<Item = S2>,
    S1: Display,
    S2: Display,
{
    fn into_err(self) -> Vec<(String, Option<Span>)> {
        let mut suggest1 = self
            .suggest1
            .into_iter()
            .map(|v| format!("{v}"))
            .collect::<Vec<_>>();
        let mut suggest2 = self
            .suggest2
            .into_iter()
            .map(|v| format!("{v}"))
            .collect::<Vec<_>>();
        suggest1.sort();
        suggest2.sort();
        let suggest1 = if suggest1.is_empty() {
            String::from("(none declared)")
        } else {
            suggest1.join(", ")
        };
        let suggest2 = if suggest2.is_empty() {
            String::from("(none declared)")
        } else {
            suggest2.join(", ")
        };

        vec![
            (
                format!("Variable {} not found in the context.", self.var),
                self.var.try_span(),
            ),
            (
                format!("Perhaps you meant one of the local variables: {}", suggest1),
                None,
            ),
            (
                format!("or one of the global variables: {}", suggest2),
                None,
            ),
        ]
    }
}

pub struct NotConst<Item, Site> {
    pub what: Item,
    pub site: Site,
}

impl<Item, Site> IntoError for NotConst<Item, Site>
where
    Item: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} not valid in const contexts", self.what),
                self.site.try_span(),
            ),
            (
                String::from("You must put this definition inside a node"),
                None,
            ),
        ]
    }
}

pub struct BinopMismatch<Oper, Site, Expect, Left, Right> {
    pub oper: Oper,
    pub site: Site,
    pub expect: Expect,
    pub left: Left,
    pub right: Right,
}

impl<Oper, Site, Expect, Left, Right> IntoError for BinopMismatch<Oper, Site, Expect, Left, Right>
where
    Oper: Display,
    Site: TrySpan,
    Expect: Display,
    Left: Display + TrySpan,
    Right: Display + TrySpan,
{
    fn into_err(self) -> Vec<(String, Option<Span>)> {
        vec![
            (
                format!(
                    "Binary operator `{}` expects arguments of {}",
                    self.oper, self.expect
                ),
                self.site.try_span(),
            ),
            (
                format!("The left-hand-side is found to be of type {}", self.left),
                self.left.try_span(),
            ),
            (
                format!("The right-hand-side is found to be of type {}", self.right),
                self.right.try_span(),
            ),
        ]
    }
}

pub struct UnopMismatch<Oper, Expect, Site, Inner> {
    pub oper: Oper,
    pub expect: Expect,
    pub site: Site,
    pub inner: Inner,
}

impl<Oper, Expect, Site, Inner> IntoError for UnopMismatch<Oper, Expect, Site, Inner>
where
    Oper: Display,
    Expect: Display,
    Site: TrySpan,
    Inner: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Unary operator `{}` expects an argument of {}",
                    self.oper, self.expect
                ),
                self.site.try_span(),
            ),
            (
                format!("The inner value is found to be of type {}", self.inner),
                self.inner.try_span(),
            ),
        ]
    }
}

pub struct BoolRequired<Type, Site, Inner> {
    pub actual: Type,
    pub site: Site,
    pub inner: Inner,
}

impl<Type, Site, Inner> IntoError for BoolRequired<Type, Site, Inner>
where
    Type: Display,
    Site: TrySpan,
    Inner: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} should be of type bool", self.actual),
                self.site.try_span(),
            ),
            (
                format!("The argument is found to be of type {}", self.inner),
                self.inner.try_span(),
            ),
        ]
    }
}

pub struct Cycle<Items> {
    pub items: Items,
}

impl<Items, Item> IntoError for Cycle<Items>
where
    Items: IntoIterator<Item = (Item, Option<Span>)>,
    Item: Display,
{
    fn into_err(self) -> Error {
        let mut v = vec![];
        for (i, (it, sp)) in self.items.into_iter().enumerate() {
            v.push((
                if i == 0 {
                    format!("{it} was found to be part of a dependency cycle")
                } else {
                    format!("The cycle also goes through {it}")
                },
                sp,
            ));
        }
        v
    }
}

pub struct GraphUnitDeclTwice<Unit, NewSite, Prior, PriorSite> {
    pub unit: Unit,
    pub new_site: NewSite,
    pub prior: Prior,
    pub prior_site: PriorSite,
}

impl<Unit, NewSite, Prior, PriorSite> IntoError
    for GraphUnitDeclTwice<Unit, NewSite, Prior, PriorSite>
where
    Unit: Display,
    Prior: Display,
    NewSite: TrySpan,
    PriorSite: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Attempt to redefine {}, when {} already defines it",
                    self.unit, self.prior
                ),
                self.new_site.try_span(),
            ),
            (
                String::from("Already defined here"),
                self.prior_site.try_span(),
            ),
        ]
    }
}

pub struct GraphUnitUndeclared<Unit> {
    pub unit: Unit,
}

impl<Unit> IntoError for GraphUnitUndeclared<Unit>
where
    Unit: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![(
            format!("No definition provided for {} which is required", self.unit),
            self.unit.try_span(),
        )]
    }
}

pub struct GraphUnitDependsOnItself<Unit, Site1, Site2> {
    pub unit: Unit,
    pub def_site: Site1,
    pub usage: Site2,
}

impl<Unit, Site1, Site2> IntoError for GraphUnitDependsOnItself<Unit, Site1, Site2>
where
    Unit: Display,
    Site1: TrySpan,
    Site2: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} depends on itself", self.unit),
                self.def_site.try_span(),
            ),
            (
                String::from("used here within its own definition"),
                self.usage.try_span(),
            ),
        ]
    }
}

pub struct NotPositive<Var, Site> {
    pub var: Var,
    pub site: Site,
    pub available_depth: usize,
    pub attempted_depth: usize,
}
impl<Var, Site> IntoError for NotPositive<Var, Site>
where
    Var: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("Variable {} is not positive at this depth", self.var),
                self.site.try_span(),
            ),
            (
                format!(
                    "tried to reach {} steps into the past, with only {} available",
                    self.attempted_depth, self.available_depth
                ),
                None,
            ),
            (
                String::from("Maybe add a `->` in front of the expression to increase the depth ?"),
                None,
            ),
        ]
    }
}

pub struct UnhandledLitType<Site> {
    pub site: Site,
}
impl<Site> IntoError for UnhandledLitType<Site>
where
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![(
            String::from("Lustre only accepts literals of type int, float, or bool"),
            self.site.try_span(),
        )]
    }
}

pub struct CmpNotAssociative<First, Oper1, Second, Oper2, Third, Site> {
    pub oper1: Oper1,
    pub first: First,
    pub site: Site,
    pub second: Second,
    pub third: Third,
    pub oper2: Oper2,
}
impl<First, Oper1, Second, Oper2, Third, Site> IntoError
    for CmpNotAssociative<First, Oper1, Second, Oper2, Third, Site>
where
    Oper1: Display,
    Oper2: Display,
    First: Display,
    Second: Display,
    Third: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        let Self {
            first,
            oper1,
            second,
            oper2,
            third,
            site,
        } = &self;
        vec![(
            format!("Comparison operator {oper1} is not associative"),
            site.try_span(),
        ),(
            format!("Maybe replace `{first} {oper1} {second} {oper2} {third}` with `{first} {oper1} {second} and {second} {oper2} {third}` ?"),
            None,
        )]
    }
}