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
//! Reporter for spanned diagnostics.
use flavored::RpName;
use std::fmt;
use std::slice;
use {Source, Span};

#[derive(Debug, Clone, Copy, Serialize)]
pub enum SymbolKind {
    #[serde(rename = "type")]
    Type,
    #[serde(rename = "interface")]
    Interface,
    #[serde(rename = "tuple")]
    Tuple,
    #[serde(rename = "enum")]
    Enum,
    #[serde(rename = "service")]
    Service,
}

/// A single diagnostic emitted by the compiler.
#[derive(Debug, Clone)]
pub enum Diagnostic {
    /// A positional error.
    Error(Span, String),
    /// A positional information string.
    Info(Span, String),
    /// A symbol that was encountered, and its location.
    Symbol {
        kind: SymbolKind,
        span: Span,
        name: RpName,
    },
}

/// A collection of diagnostics emitted by the compiler.
#[derive(Debug, Clone)]
pub struct Diagnostics {
    pub source: Source,
    pub items: Vec<Diagnostic>,
}

impl Diagnostics {
    /// Create a new diagnostics collection.
    pub fn new(source: Source) -> Self {
        Self {
            source,
            items: Vec::new(),
        }
    }

    /// Check if reporter is empty.
    pub fn has_errors(&self) -> bool {
        self.items.iter().any(|item| match *item {
            Diagnostic::Error(_, _) => true,
            _ => false,
        })
    }

    /// Report an error.
    pub fn err<S: Into<Span>, E: fmt::Display>(&mut self, span: S, error: E) {
        self.items
            .push(Diagnostic::Error(span.into(), error.to_string()));
    }

    /// Report information.
    pub fn info<S: Into<Span>, I: fmt::Display>(&mut self, span: S, info: I) {
        self.items
            .push(Diagnostic::Info(span.into(), info.to_string()));
    }

    /// Register a symbol.
    pub fn symbol<P: Into<Span>>(&mut self, kind: SymbolKind, span: P, name: &RpName) {
        self.items.push(Diagnostic::Symbol {
            kind,
            span: span.into(),
            name: name.clone(),
        });
    }

    /// Iterate over all reporter items.
    pub fn items(&self) -> Items {
        Items {
            iter: self.items.iter(),
        }
    }

    /// Clear all existing diagnostics.
    pub fn clear(&mut self) {
        self.items.clear();
    }
}

/// Iterator over items.
///
/// Created using `Diagnostics::items`.
pub struct Items<'a> {
    iter: slice::Iter<'a, Diagnostic>,
}

impl<'a> Iterator for Items<'a> {
    type Item = <slice::Iter<'a, Diagnostic> as Iterator>::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}