use crate::{
parse::{FileId, SourceList},
ParseTree,
};
use std::{convert::TryInto, ops::Range, sync::Arc};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
start: u32,
end: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Level {
Error,
Warning,
Info,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Message {
pub text: String,
pub file: FileId,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
pub message: Message,
pub level: Level,
}
#[derive(Clone)]
pub struct DiagnosticSet {
pub(crate) messages: Vec<Diagnostic>,
pub(crate) sources: Arc<SourceList>,
pub(crate) max_to_print: usize,
}
impl Span {
pub fn range(&self) -> Range<usize> {
self.start as usize..self.end as usize
}
}
impl Diagnostic {
pub fn new(
level: Level,
file: FileId,
range: Range<usize>,
message: impl Into<String>,
) -> Self {
Diagnostic {
message: Message {
text: message.into(),
span: Span {
start: range.start.try_into().unwrap(),
end: range.end.try_into().unwrap(),
},
file,
},
level,
}
}
pub fn error(file: FileId, span: Range<usize>, message: impl Into<String>) -> Self {
Diagnostic::new(Level::Error, file, span, message)
}
pub fn warning(file: FileId, span: Range<usize>, message: impl Into<String>) -> Self {
Diagnostic::new(Level::Warning, file, span, message)
}
pub fn text(&self) -> &str {
&self.message.text
}
pub fn span(&self) -> Range<usize> {
self.message.span.range()
}
pub fn is_error(&self) -> bool {
matches!(self.level, Level::Error)
}
}
struct DiagnosticDisplayer<'a>(&'a DiagnosticSet);
impl DiagnosticSet {
pub fn new(messages: Vec<Diagnostic>, tree: &ParseTree, max_to_print: usize) -> Self {
Self {
messages,
sources: tree.sources.clone(),
max_to_print,
}
}
pub fn len(&self) -> usize {
self.messages.len()
}
pub fn is_empty(&self) -> bool {
self.messages.is_empty()
}
pub fn has_errors(&self) -> bool {
self.messages.iter().any(|msg| msg.is_error())
}
pub fn set_max_to_print(&mut self, max_to_print: usize) {
self.max_to_print = max_to_print;
}
pub fn discard_warnings(&mut self) {
self.messages.retain(|x| x.is_error());
}
pub fn split_off_warnings(&mut self) -> Option<DiagnosticSet> {
self.messages.sort_unstable_by_key(|d| d.level);
let split_at = self.messages.iter().position(|x| !x.is_error())?;
let warnings = self.messages.split_off(split_at);
Some(Self {
messages: warnings,
sources: self.sources.clone(),
max_to_print: self.max_to_print,
})
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.messages
}
pub fn display(&self) -> impl std::fmt::Display + '_ {
DiagnosticDisplayer(self)
}
pub(crate) fn write(&self, f: &mut impl std::fmt::Write, colorize: bool) -> std::fmt::Result {
let mut first = true;
for err in self.messages.iter().take(self.max_to_print) {
if !first {
writeln!(f)?;
}
write!(f, "{}", self.sources.format_diagnostic(err, colorize))?;
first = false;
}
if let Some(overflow) = self.messages.len().checked_sub(self.max_to_print) {
writeln!(f, "... and {overflow} more errors")?;
}
Ok(())
}
#[cfg(any(test, feature = "test"))]
pub(crate) fn to_string(&self, colorize: bool) -> String {
let mut out = String::new();
self.write(&mut out, colorize).unwrap();
out
}
}
impl std::fmt::Display for DiagnosticDisplayer<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use std::io::IsTerminal;
let colorize = std::io::stderr().is_terminal();
self.0.write(f, colorize)
}
}
impl std::fmt::Debug for DiagnosticSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DiagnosticSet")
.field("messages", &self.messages)
.field("tree", &"ParseTree")
.finish()
}
}