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
use super::stringable::Stringable;
use codespan::Span;
use codespan_reporting::diagnostic::{Diagnostic as CSDiagnostic, Label};
use pest;
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct Diagnostic {
location: Option<Location>,
message: Stringable,
notes: Vec<String>,
}
impl Diagnostic {
pub fn print_error(&self) -> CSDiagnostic<usize> {
match self.location {
Some(Location { file_id, span }) => {
let diagnostic = CSDiagnostic::error()
.with_message(format!("{}", self.message))
.with_labels(vec![Label::primary(file_id, span).with_message("HERE")])
.with_notes(self.notes.clone());
diagnostic
}
None => {
let diagnostic: CSDiagnostic<usize> =
CSDiagnostic::error().with_message(format!("{}", self.message));
diagnostic
}
}
}
pub fn print_warning(&self) -> CSDiagnostic<usize> {
match self.location {
Some(Location { file_id, span }) => {
let diagnostic = CSDiagnostic::warning()
.with_message(format!("{}", self.message))
.with_labels(vec![Label::primary(file_id, span).with_message("HERE")]);
diagnostic
}
None => {
let diagnostic = CSDiagnostic::warning().with_message(format!("{}", self.message));
diagnostic
}
}
}
}
impl<T> From<T> for Diagnostic
where
T: Into<Stringable>,
{
fn from(input: T) -> Self {
Diagnostic {
location: None,
message: input.into(),
notes: vec![],
}
}
}
impl<T> From<(Location, T)> for Diagnostic
where
T: Into<Stringable>,
{
fn from(input: (Location, T)) -> Self {
Diagnostic {
location: Some(input.0),
message: input.1.into(),
notes: vec![],
}
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub struct Location {
pub file_id: usize,
pub span: Span,
}
impl Location {
pub fn from_pest(file_id: usize, pest_span: pest::Span) -> Location {
Location {
file_id,
span: codespan::Span::new(pest_span.start() as u32, pest_span.end() as u32),
}
}
pub fn from_raw(file_id: usize, start: u32, end: u32) -> Location {
Location {
file_id,
span: codespan::Span::new(start as u32, end as u32),
}
}
}