constructo-runtime 0.1.0

The syntax of Constructo data-values: the lexer, parser and printer that generated code depends on
Documentation
//! Source positions and diagnostics.

use std::fmt;

/// A position in a source text. Both fields are 1-based and the column counts characters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Pos {
    /// The line, from 1.
    pub line: u32,
    /// The column, from 1, in characters.
    pub col: u32,
}

impl Pos {
    /// The position of a byte offset in a text, counting characters. An offset past the end is the end.
    pub fn at(text: &str, offset: usize) -> Pos {
        let before = &text[..offset.min(text.len())];
        let line = before.matches('\n').count() as u32 + 1;
        let col = before.rsplit('\n').next().map_or(0, |last| last.chars().count()) as u32 + 1;
        Pos { line, col }
    }
}

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

/// How serious a diagnostic is. An error means that the inputs are wrong, and a warning that they were used but
/// something was left out or may not be what was meant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Severity {
    /// The inputs are wrong, and the run fails.
    #[default]
    Error,
    /// The inputs were used, and the run does not fail.
    Warning,
}

impl Severity {
    /// The word that starts a line of this severity.
    pub fn label(self) -> &'static str {
        match self {
            Severity::Error => "ERROR",
            Severity::Warning => "WARNING",
        }
    }
}

/// The text with every control character written as an escape, so that text from a document that is being read cannot
/// move the cursor, clear the screen or end the line of a diagnostic. Other characters are kept as they are.
pub fn shown(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        match c {
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if c.is_control() => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

/// A problem found in a source, with the name of the file it was found in. It is an error unless it is made as a
/// warning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
    /// Whether it is an error or a warning.
    pub severity: Severity,
    /// The name of the file, as it is shown.
    pub file: String,
    /// Where in the file it was found.
    pub pos: Pos,
    /// What the problem is.
    pub message: String,
    /// Extra information, such as where a duplicated name was first declared.
    pub note: Option<String>,
}

impl Diagnostic {
    /// A diagnostic that is an error, with no note.
    pub fn new(file: &str, pos: Pos, message: impl Into<String>) -> Self {
        Diagnostic {
            severity: Severity::Error,
            file: file.to_string(),
            pos,
            message: message.into(),
            note: None,
        }
    }

    /// A diagnostic that is a warning, which does not make a run fail.
    pub fn warning(file: &str, pos: Pos, message: impl Into<String>) -> Self {
        Diagnostic {
            severity: Severity::Warning,
            ..Diagnostic::new(file, pos, message)
        }
    }

    /// The diagnostic with a note added.
    pub fn with_note(mut self, note: impl Into<String>) -> Self {
        self.note = Some(note.into());
        self
    }
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {} ({}, {})",
            self.severity.label(),
            self.message,
            self.file,
            self.pos
        )?;
        if let Some(note) = &self.note {
            write!(f, "; {}", note)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn shown_escapes_control_characters_only() {
        assert_eq!(shown("a\u{1b}[2J\nb\r\t\u{7}"), "a\\u001b[2J\\nb\\r\\t\\u0007");
        assert_eq!(shown("DEL\u{7f} C1\u{85}"), "DEL\\u007f C1\\u0085");
        assert_eq!(shown("plain \"quoted\" \\ é"), "plain \"quoted\" \\ é");
    }

    use super::*;

    const AT: Pos = Pos { line: 3, col: 9 };

    #[test]
    fn a_diagnostic_is_an_error_unless_it_is_made_as_a_warning() {
        assert_eq!(Diagnostic::new("f", AT, "m").severity, Severity::Error);
        assert_eq!(Diagnostic::warning("f", AT, "m").severity, Severity::Warning);
    }

    #[test]
    fn an_error_starts_with_error() {
        assert_eq!(
            Diagnostic::new("a.values", AT, "Bad value").to_string(),
            "ERROR: Bad value (a.values, line 3, col 9)"
        );
    }

    #[test]
    fn a_warning_has_the_same_form_and_starts_with_warning() {
        assert_eq!(
            Diagnostic::warning("a.values", AT, "More values follow").to_string(),
            "WARNING: More values follow (a.values, line 3, col 9)"
        );
        assert_eq!(
            Diagnostic::warning("a.values", AT, "More values follow")
                .with_note("see --to-array")
                .to_string(),
            "WARNING: More values follow (a.values, line 3, col 9); see --to-array"
        );
    }

    #[test]
    fn the_labels_are_the_words_that_start_a_line() {
        assert_eq!(Severity::Error.label(), "ERROR");
        assert_eq!(Severity::Warning.label(), "WARNING");
        assert_eq!(Severity::default(), Severity::Error);
    }
}