saltwater 0.10.0

A C compiler written in Rust, with a focus on good error messages.
Documentation
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
use std::collections::VecDeque;
use thiserror::Error;

use super::hir::Expr;
use super::*;

use super::Radix;

/// RecoverableResult is a type that represents a Result that can be recovered from.
///
/// See the [`Recover`] trait for more information.
///
/// [`Recover`]: trait.Recover.html
pub type RecoverableResult<T, E = CompileError> = Result<T, (E, T)>;
pub type CompileResult<T> = Result<T, CompileError>;
pub type CompileError = Locatable<Error>;
pub type CompileWarning = Locatable<Warning>;

/// ErrorHandler is a struct that hold errors generated by the compiler
///
/// An error handler is used because multiple errors may be generated by each
/// part of the compiler, this cannot be represented well with Rust's normal
/// `Result`.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ErrorHandler<T = Error> {
    errors: VecDeque<Locatable<T>>,
    pub(crate) warnings: VecDeque<CompileWarning>,
}

// Can't be derived because the derive mistakenly puts a bound of T: Default
impl<T> Default for ErrorHandler<T> {
    fn default() -> Self {
        Self {
            errors: Default::default(),
            warnings: Default::default(),
        }
    }
}

impl<T> ErrorHandler<T> {
    /// Construct a new error handler.
    pub(crate) fn new() -> ErrorHandler<T> {
        Default::default()
    }

    /// Whether any errors have been seen and not handled
    pub(crate) fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }

    /// Add an error to the error handler.
    pub(crate) fn push_back<E: Into<Locatable<T>>>(&mut self, error: E) {
        self.errors.push_back(error.into());
    }

    /// Remove the first error from the queue
    pub(crate) fn pop_front(&mut self) -> Option<Locatable<T>> {
        self.errors.pop_front()
    }

    /// Shortcut for adding a warning
    pub(crate) fn warn<W: Into<Warning>>(&mut self, warning: W, location: Location) {
        self.warnings.push_back(location.with(warning.into()));
    }

    /// Shortcut for adding an error
    pub(crate) fn error<E: Into<T>>(&mut self, error: E, location: Location) {
        self.errors.push_back(location.with(error.into()));
    }

    /// Add an iterator of errors to the error queue
    pub(crate) fn extend<E: Into<Locatable<T>>>(&mut self, iter: impl Iterator<Item = E>) {
        self.errors.extend(iter.map(Into::into));
    }

    /// Move another `ErrorHandler`'s errors and warnings into this one.
    pub(crate) fn append<S>(&mut self, other: &mut ErrorHandler<S>)
    where
        T: From<S>,
    {
        self.errors
            .extend(&mut other.errors.drain(..).map(|loc| loc.map(Into::into)));
        self.warnings.append(&mut other.warnings);
    }
}

impl Iterator for ErrorHandler {
    type Item = CompileError;

    fn next(&mut self) -> Option<CompileError> {
        self.pop_front()
    }
}

#[derive(Clone, Debug, Error, PartialEq)]
pub enum Error {
    #[error("invalid program: {0}")]
    Semantic(#[from] SemanticError),

    #[error("invalid syntax: {0}")]
    Syntax(#[from] SyntaxError),

    #[error("invalid macro: {0}")]
    PreProcessor(#[from] CppError),

    #[error("invalid token: {0}")]
    Lex(#[from] LexError),
}

/// Semantic errors are non-exhaustive and may have new variants added at any time
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum SemanticError {
    #[error("{0}")]
    Generic(String),

    // Declaration specifier errors
    #[error("cannot combine '{new}' specifier with previous '{existing}' type specifier")]
    InvalidSpecifier {
        existing: ast::DeclarationSpecifier,
        new: ast::DeclarationSpecifier,
    },

    #[error("'{0}' is not a qualifier and cannot be used for pointers")]
    NotAQualifier(ast::DeclarationSpecifier),

    #[error("'{}' is too long for {}", vec!["long"; *.0].join(" "), env!("CARGO_PKG_NAME"))]
    TooLong(usize),

    #[error("conflicting storage classes '{0}' and '{1}'")]
    ConflictingStorageClass(StorageClass, StorageClass),

    #[error("conflicting types '{0}' and '{1}'")]
    ConflictingType(Type, Type),

    #[error("'{0}' cannot be signed or unsigned")]
    CannotBeSigned(Type),

    #[error("types cannot be both signed and unsigned")]
    ConflictingSigned,

    #[error("only function-scoped variables can have an `auto` storage class")]
    AutoAtGlobalScope,

    #[error("cannot have empty program")]
    EmptyProgram,

    // Declarator errors
    #[error("expected an integer")]
    NonIntegralLength,

    #[error("arrays must have a positive length")]
    NegativeLength,

    #[error("function parameters always have a storage class of `auto`")]
    ParameterStorageClass(StorageClass),

    #[error("duplicate parameter name '{0}' in function declaration")]
    DuplicateParameter(InternedStr),

    #[error("functions cannot return '{0}'")]
    IllegalReturnType(Type),

    // TODO: print params in the error message
    #[error("arrays cannot contain functions (got '{0}'). help: try storing array of pointer to function: (*{}[])(...)")]
    ArrayStoringFunction(Type),

    #[error("void must be the first and only parameter if specified")]
    InvalidVoidParameter,

    #[error("functions taking `void` must not have variadic arguments")]
    VoidVarargs,

    #[error("functions taking variadic arguments must have at least one parameter first")]
    VarargsWithoutParam,

    #[error("overflow in enumeration constant")]
    EnumOverflow,

    #[error("variable has incomplete type 'void'")]
    VoidType,

    // expression errors
    #[error("use of undeclared identifier '{0}'")]
    UndeclaredVar(InternedStr),

    #[error("expected expression, got typedef")]
    TypedefInExpressionContext,

    #[error("type casts cannot have a storage class")]
    IllegalStorageClass(StorageClass),

    #[error("type casts cannot have a variable name")]
    IdInTypeName(InternedStr),

    #[error("expected integer, got '{0}'")]
    NonIntegralExpr(Type),

    #[error("cannot implicitly convert '{0}' to '{1}'{}",
        if .1.is_pointer() {
            format!(". help: use an explicit cast: ({})", .1)
        } else {
            String::new()
        })
    ]
    InvalidCast(Type, Type),

    // String is the reason it couldn't be assigned
    #[error("cannot assign to {0}")]
    NotAssignable(String),

    #[error("invalid operators for '{0}' (expected either arithmetic types or pointer operation, got '{1} {0} {2}'")]
    InvalidAdd(hir::BinaryOp, Type, Type),

    #[error("cannot perform pointer arithmetic when size of pointed type '{0}' is unknown")]
    PointerAddUnknownSize(Type),

    #[error("called object of type '{0}' is not a function")]
    NotAFunction(Type),

    #[error("too {} arguments to function call: expected {0}, have {1}", if .1 > .0 { "many" } else { "few" })]
    /// (actual, expected)
    WrongArgumentNumber(usize, usize),

    #[error("{0} has not yet been defined")]
    IncompleteDefinitionUsed(Type),

    #[error("no member named '{0}' in '{1}'")]
    NotAMember(InternedStr, Type),

    #[error("expected struct or union, got type '{0}'")]
    NotAStruct(Type),

    #[error("cannot use '->' operator on type that is not a pointer")]
    NotAStructPointer(Type),

    #[error("cannot dereference expression of non-pointer type '{0}'")]
    NotAPointer(Type),

    #[error("cannot take address of {0}")]
    InvalidAddressOf(&'static str),

    #[error("cannot increment or decrement value of type '{0}'")]
    InvalidIncrement(Type),

    #[error("cannot use unary plus on expression of non-arithmetic type '{0}'")]
    NotArithmetic(Type),

    #[error("incompatible types in ternary expression: '{0}' cannot be converted to '{1}'")]
    IncompatibleTypes(Type, Type),

    // const fold errors
    #[error("{} overflow in expresson", if *(.is_positive) { "positive" } else { "negative" })]
    ConstOverflow { is_positive: bool },

    #[error("cannot divide by zero")]
    DivideByZero,

    #[error("cannot shift {} by a negative amount", if *(.is_left) { "left" } else { "right" })]
    NegativeShift { is_left: bool },

    #[error("cannot shift {} by {maximum} or more bits for type '{ctype}' (got {current})",
        if *(.is_left) { "left" } else { "right" })]
    TooManyShiftBits {
        is_left: bool,
        maximum: u64,
        ctype: Type,
        current: u64,
    },

    #[error("not a constant expression: {0}")]
    NotConstant(Expr),

    #[error("cannot dereference NULL pointer")]
    NullPointerDereference,

    #[error("invalid types for '{0}' (expected arithmetic types or compatible pointers, got {1} {0} {2}")]
    InvalidRelationalType(lex::ComparisonToken, Type, Type),

    #[error("cannot cast pointer to float or vice versa")]
    FloatPointerCast(Type),

    // TODO: this shouldn't be an error
    #[error("cannot cast to non-scalar type '{0}'")]
    NonScalarCast(Type),

    #[error("cannot cast void to any type")]
    VoidCast,

    #[error("cannot cast structs to any type")]
    StructCast,

    // Control flow errors
    #[error("unreachable statement")]
    UnreachableStatement,

    // TODO: this error should happen way before codegen
    #[cfg(feature = "codegen")]
    #[error("redeclaration of label {0}")]
    LabelRedeclaration(cranelift::prelude::Block),

    #[error("use of undeclared label {0}")]
    UndeclaredLabel(InternedStr),

    #[error("{}case outside of switch statement", if *(.is_default) { "default " } else { "" })]
    CaseOutsideSwitch { is_default: bool },

    #[error("cannot have multiple {}cases in a switch statement",
            if *(.is_default) { "default " } else { "" } )]
    DuplicateCase { is_default: bool },

    // Initializer errors
    #[error("initializers cannot be empty")]
    EmptyInitializer,

    #[error("scalar initializers for '{0}' may only have one element (initialized with {1})")]
    AggregateInitializingScalar(Type, usize),

    #[error("too many initializers (declared with {0} elements, found {1})")]
    TooManyMembers(usize, usize),

    // Function definition errors
    #[error("illegal storage class {0} for function (only `static` and `extern` are allowed)")]
    InvalidFuncStorageClass(StorageClass),

    #[error("missing parameter name in function definition (parameter {0} of type '{1}')")]
    MissingParamName(usize, Type),

    #[error("forward declaration of {0} is never completed (used in {1})")]
    ForwardDeclarationIncomplete(InternedStr, InternedStr),

    #[error("illegal signature for main function (expected 'int main(void)' or 'int main(int, char **)'")]
    IllegalMainSignature,

    // declaration errors
    #[error("redefinition of '{0}'")]
    Redefinition(InternedStr),

    #[error("redeclaration of '{0}' with different type or qualifiers (originally {}, now {})", .1.get(), .2.get())]
    IncompatibleRedeclaration(InternedStr, hir::Symbol, hir::Symbol),

    #[error("'{0}' can only appear on functions")]
    FuncQualifiersNotAllowed(hir::FunctionQualifiers),

    // stmt errors
    // new with the new parser
    #[error("switch expressions must have an integer type (got {0})")]
    NonIntegralSwitch(Type),

    #[error("function '{0}' does not return a value")]
    MissingReturnValue(InternedStr),

    #[error("void function '{0}' should not return a value")]
    ReturnFromVoid(InternedStr),
}

/// Syntax errors are non-exhaustive and may have new variants added at any time
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum SyntaxError {
    #[error("{0}")]
    Generic(String),

    #[error("expected {0}, got <end-of-file>")]
    EndOfFile(&'static str),

    #[error("expected statement, got {0}")]
    NotAStatement(super::Keyword),

    // expected a primary expression, but got EOF or an invalid token
    #[error("expected variable, literal, or '('")]
    MissingPrimary,

    #[error("expected identifier, got '{}'",
        .0.as_ref().map_or("<end-of-file>".into(),
                           |t| std::borrow::Cow::Owned(t.to_string())))]
    ExpectedId(Option<Token>),

    #[error("expected declaration specifier, got keyword '{0}'")]
    ExpectedDeclSpecifier(Keyword),

    #[error("expected declarator in declaration")]
    ExpectedDeclarator,

    #[error("empty type name")]
    ExpectedType,

    #[error("expected '(', '*', or variable, got '{0}'")]
    ExpectedDeclaratorStart(Token),

    #[error("only functions can have a function body (got {0})")]
    NotAFunction(ast::InitDeclarator),

    #[error("functions cannot be initialized (got {0})")]
    FunctionInitializer(ast::Initializer),

    #[error("function not allowed in this context (got {})", .0.as_type())]
    FunctionNotAllowed(ast::FunctionDefinition),

    #[error("function definitions must have a name")]
    MissingFunctionName,

    #[error("`static` for array sizes is only allowed in function declarations")]
    StaticInConcreteArray,
}

/// Preprocessing errors are non-exhaustive and may have new variants added at any time
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum CppError {
    /// A user-defined error (`#error`) was present.
    /// The `Vec<Token>` contains the tokens which followed the error.

    // TODO: this allocates a string for each token,
    // might be worth separating out into a function at some point
    #[error("#error {}", (.0).iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" "))]
    User(Vec<Token>),

    /// An invalid directive was present, such as `#invalid`
    #[error("invalid preprocessing directive")]
    InvalidDirective,

    /// A valid token was present in an invalid position, such as `#if *`
    ///
    /// The `&str` describes the expected token;
    /// the `Token` is the actual token found.
    #[error("expected {0}, got {1}")]
    UnexpectedToken(&'static str, Token),

    /// The file ended unexpectedly.
    ///
    /// This error is separate from an unterminated `#if`:
    /// it occurs if the file ends in the middle of a directive,
    /// such as `#define`.
    ///
    /// The `&str` describes what token was expected.
    #[error("expected {0}, got <end-of-file>")]
    EndOfFile(&'static str),

    #[error("file '{0}' not found")]
    FileNotFound(String),

    #[error("wrong number of arguments: expected {0}, got {1}")]
    TooFewArguments(usize, usize),

    #[error("IO error: {0}")]
    // TODO: find a way to put io::Error in here (doesn't derive Clone or PartialEq)
    IO(String),

    /// The file ended before an `#if`, `#ifdef`, or `#ifndef` was closed.
    #[error("#if is never terminated")]
    UnterminatedIf,

    /// An `#if` occurred without an expression following.
    #[error("expected expression for #if")]
    EmptyExpression,

    #[error("macro name missing")]
    ExpectedMacroId,

    #[error("missing {0} in {1}")]
    Expected(&'static str, &'static str),

    /// A `#define` occured without an identifier following.
    #[error("macro name missing")]
    EmptyDefine,

    /// An `#include<>` or `#include""` was present.
    #[error("empty filename")]
    EmptyInclude,

    /// A `#endif` was present, but no `#if` was currently open
    #[error("#endif without #if")]
    UnexpectedEndIf,

    /// An `#else` was present, but either
    /// a) no `#if` was currently open, or
    /// b) an `#else` has already been seen.
    #[error("#else after #else or #else without #if")]
    UnexpectedElse,

    /// An `#elif` was present, but either
    /// a) no `#if` was currently open, or
    /// b) an `#else` has already been seen.
    #[error("{}", if *early { "#elif without #if" } else { "#elif after #else " })]
    UnexpectedElif { early: bool },

    /// After parsing an `#if` expression, there were tokens left over.
    #[error("trailing tokens in `#if` expression")]
    TooManyTokens,

    /// If a macro is redefined, the new definition must be identical to the
    /// original.
    #[error("redefinition of '{0}' does not match original definition")]
    IncompatibleRedefinition(InternedStr),
}

/// Lex errors are non-exhaustive and may have new variants added at any time
#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum LexError {
    #[error("unterminated /* comment")]
    UnterminatedComment,

    #[error("no newline at end of file")]
    NoNewlineAtEOF,

    #[error("unknown token: '{0}'")]
    UnknownToken(char),

    #[error("missing terminating {} character in {} literal",
        if *(.string) { "\"" } else { "'" },
        if *(.string) { "string" } else { "character" })]
    MissingEndQuote { string: bool },

    #[error("illegal newline while parsing string literal")]
    NewlineInString,

    #[error("{0} character escape out of range")]
    CharEscapeOutOfRange(Radix),

    #[error("overflow while parsing {}integer literal",
        if let Some(signed) = .is_signed {
            if *signed { "signed "} else { "unsigned "}
        } else { "" })]
    IntegerOverflow { is_signed: Option<bool> },

    #[error("exponent for floating literal has no digits")]
    ExponentMissingDigits,

    #[error("missing digits to {0} integer constant")]
    MissingDigits(Radix),

    #[error("{0}")]
    ParseFloat(#[from] std::num::ParseFloatError),

    #[error("invalid digit {digit} in {radix} constant")]
    InvalidDigit { digit: u32, radix: Radix },

    #[error("multi-character character literal")]
    MultiCharCharLiteral,

    #[error("illegal newline while parsing char literal")]
    NewlineInChar,

    #[error("empty character constant")]
    EmptyChar,

    #[error("underflow parsing floating literal")]
    FloatUnderflow,

    #[error("{0}")]
    InvalidHexFloat(#[from] hexponent::ParseError),
}

#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
/// errors are non-exhaustive and may have new variants added at any time
pub enum Warning {
    // for compatibility
    #[error("{0}")]
    Generic(String),

    /// A #warning directive was present, followed by the tokens in this variant.
    // TODO: this allocates a string for each token,
    // might be worth separating out into a function at some point
    #[error("#warning {}", (.0).iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" "))]
    User(Vec<Token>),

    #[error("extraneous semicolon in {0}")]
    ExtraneousSemicolon(&'static str),

    #[error("'{0}' qualifier on return type has no effect")]
    FunctionQualifiersIgnored(hir::Qualifiers),

    #[error("duplicate '{0}' declaration specifier{}",
            if *.1 > 1 { format!(" occurs {} times", .1) } else { String::new() })]
    DuplicateSpecifier(ast::UnitSpecifier, usize),

    #[error("qualifiers in type casts are ignored")]
    IgnoredQualifier(hir::Qualifiers),

    #[error("declaration does not declare anything")]
    EmptyDeclaration,

    #[error("{} does not support #pragma", env!("CARGO_PKG_NAME"))]
    IgnoredPragma,

    #[error("variadic macros are not yet supported")]
    IgnoredVariadic,

    #[error("implicit int is deprecated and may be removed in a future release")]
    ImplicitInt,

    #[error("this is a definition, not a declaration, the 'extern' keyword has no effect")]
    ExtraneousExtern,
}

impl<T: Into<String>> From<T> for Warning {
    fn from(msg: T) -> Warning {
        Warning::Generic(msg.into())
    }
}

impl CompileError {
    pub fn location(&self) -> Location {
        self.location
    }
    pub fn is_lex_err(&self) -> bool {
        self.data.is_lex_err()
    }
    pub fn is_syntax_err(&self) -> bool {
        self.data.is_syntax_err()
    }
    pub fn is_semantic_err(&self) -> bool {
        self.data.is_semantic_err()
    }
}

impl Error {
    pub fn is_lex_err(&self) -> bool {
        if let Error::Lex(_) = self {
            true
        } else {
            false
        }
    }
    pub fn is_syntax_err(&self) -> bool {
        if let Error::Syntax(_) = self {
            true
        } else {
            false
        }
    }
    pub fn is_semantic_err(&self) -> bool {
        if let Error::Semantic(_) = self {
            true
        } else {
            false
        }
    }
}

impl From<Locatable<String>> for CompileError {
    fn from(err: Locatable<String>) -> Self {
        err.map(|s| SemanticError::Generic(s).into())
    }
}

impl From<Locatable<SemanticError>> for CompileError {
    fn from(err: Locatable<SemanticError>) -> Self {
        err.map(Error::Semantic)
    }
}

impl From<Locatable<SyntaxError>> for CompileError {
    fn from(err: Locatable<SyntaxError>) -> Self {
        err.map(Error::Syntax)
    }
}

impl From<Locatable<CppError>> for CompileError {
    fn from(err: Locatable<CppError>) -> Self {
        err.map(Error::PreProcessor)
    }
}

impl From<Locatable<LexError>> for CompileError {
    fn from(err: Locatable<LexError>) -> Self {
        err.map(Error::Lex)
    }
}

impl From<Locatable<String>> for Locatable<SemanticError> {
    fn from(err: Locatable<String>) -> Self {
        err.map(SemanticError::Generic)
    }
}

impl<S: Into<String>> From<S> for SemanticError {
    fn from(err: S) -> Self {
        SemanticError::Generic(err.into())
    }
}

impl<S: Into<String>> From<S> for SyntaxError {
    fn from(err: S) -> Self {
        SyntaxError::Generic(err.into())
    }
}

pub(crate) trait Recover {
    type Ok;
    fn recover(self, error_handler: &mut ErrorHandler) -> Self::Ok;
}

impl<T, E: Into<CompileError>> Recover for RecoverableResult<T, E> {
    type Ok = T;
    fn recover(self, error_handler: &mut ErrorHandler) -> T {
        self.unwrap_or_else(|(e, i)| {
            error_handler.push_back(e);
            i
        })
    }
}

impl<T, E: Into<CompileError>> Recover for RecoverableResult<T, Vec<E>> {
    type Ok = T;
    fn recover(self, error_handler: &mut ErrorHandler) -> T {
        self.unwrap_or_else(|(es, i)| {
            error_handler.extend(es.into_iter());
            i
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dummy_error() -> CompileError {
        Location::default().with(Error::Lex(LexError::UnterminatedComment))
    }

    fn new_error(error: Error) -> CompileError {
        Location::default().with(error)
    }

    #[test]
    fn test_error_handler_into_iterator() {
        let mut error_handler = ErrorHandler::new();
        error_handler.push_back(dummy_error());
        let errors = error_handler.collect::<Vec<_>>();
        assert_eq!(errors.len(), 1);
    }

    #[test]
    fn test_compile_error_is_kind() {
        let e = Error::Lex(LexError::UnterminatedComment);
        assert!(e.is_lex_err());
        assert!(!e.is_semantic_err());
        assert!(!e.is_syntax_err());

        let e = Error::Semantic(SemanticError::Generic("".to_string()));
        assert!(!e.is_lex_err());
        assert!(e.is_semantic_err());
        assert!(!e.is_syntax_err());

        let e = Error::Syntax(SyntaxError::Generic("".to_string()));
        assert!(!e.is_lex_err());
        assert!(!e.is_semantic_err());
        assert!(e.is_syntax_err());
    }

    #[test]
    fn test_compile_error_display() {
        assert_eq!(
            dummy_error().data.to_string(),
            "invalid token: unterminated /* comment"
        );

        assert_eq!(
            Error::Semantic(SemanticError::Generic("bad code".to_string())).to_string(),
            "invalid program: bad code"
        );
    }

    #[test]
    fn test_recover_error() {
        let mut error_handler = ErrorHandler::new();
        let r: RecoverableResult<i32> = Ok(1);
        assert_eq!(r.recover(&mut error_handler), 1);
        assert_eq!(error_handler.pop_front(), None);

        let mut error_handler = ErrorHandler::new();
        let r: RecoverableResult<i32> = Err((dummy_error(), 42));
        assert_eq!(r.recover(&mut error_handler), 42);
        let errors = error_handler.collect::<Vec<_>>();
        assert_eq!(errors, vec![dummy_error()]);
    }

    #[test]
    fn test_recover_multiple_errors() {
        let mut error_handler = ErrorHandler::new();
        let r: RecoverableResult<i32, Vec<CompileError>> = Ok(1);
        assert_eq!(r.recover(&mut error_handler), 1);
        assert_eq!(error_handler.pop_front(), None);

        let mut error_handler = ErrorHandler::new();
        let r: RecoverableResult<i32, Vec<CompileError>> = Err((
            vec![
                dummy_error(),
                new_error(Error::Semantic(SemanticError::Generic("pears".to_string()))),
            ],
            42,
        ));
        assert_eq!(r.recover(&mut error_handler), 42);
        let errors = error_handler.collect::<Vec<_>>();
        assert_eq!(
            errors,
            vec![
                dummy_error(),
                new_error(Error::Semantic(SemanticError::Generic("pears".to_string()))),
            ]
        );
    }
}