1use rowan::{TextRange, TextSize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub enum Severity {
11 Error,
13 Warning,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub enum Code {
20 UnexpectedCharacter,
22 BadString,
24 UnterminatedComment,
26 NoModule,
28 MissingModuleEnd,
30 UnexpectedToken,
32 MissingExpression,
34 PrecedenceConflict,
36 MissingOperator,
38 MisplacedOperator,
40 JunctionIndentation,
42 StepLevel,
44 StepNumberMisuse,
46 UnexpectedProof,
48 ProofTooDeep,
50 BadLabel,
52 LabelChangesParse,
54 TimesAsArgument,
56 SetMapWithIn,
58 AtInExcept,
60 BadFairness,
62 NewWithArguments,
64 BadStructuralSelector,
66 NestingTooDeep,
68 InputTooLarge,
70 ModuleNameMismatch,
72 InvalidUtf8,
75}
76
77impl Code {
78 #[must_use]
80 pub fn as_str(self) -> &'static str {
81 match self {
82 Self::UnexpectedCharacter => "DIVE-S0001",
83 Self::BadString => "DIVE-S0002",
84 Self::UnterminatedComment => "DIVE-S0003",
85 Self::NoModule => "DIVE-S0004",
86 Self::MissingModuleEnd => "DIVE-S0005",
87 Self::UnexpectedToken => "DIVE-S0010",
88 Self::MissingExpression => "DIVE-S0011",
89 Self::PrecedenceConflict => "DIVE-S0012",
90 Self::MissingOperator => "DIVE-S0013",
91 Self::MisplacedOperator => "DIVE-S0014",
92 Self::JunctionIndentation => "DIVE-S0015",
93 Self::StepLevel => "DIVE-S0020",
94 Self::StepNumberMisuse => "DIVE-S0021",
95 Self::UnexpectedProof => "DIVE-S0022",
96 Self::ProofTooDeep => "DIVE-S0023",
97 Self::BadLabel => "DIVE-S0030",
98 Self::LabelChangesParse => "DIVE-S0031",
99 Self::TimesAsArgument => "DIVE-S0032",
100 Self::SetMapWithIn => "DIVE-S0033",
101 Self::AtInExcept => "DIVE-S0034",
102 Self::BadFairness => "DIVE-S0035",
103 Self::NewWithArguments => "DIVE-S0036",
104 Self::BadStructuralSelector => "DIVE-S0037",
105 Self::NestingTooDeep => "DIVE-S0040",
106 Self::InputTooLarge => "DIVE-S0041",
107 Self::ModuleNameMismatch => "DIVE-S0006",
108 Self::InvalidUtf8 => "DIVE-S0007",
109 }
110 }
111}
112
113impl std::fmt::Display for Code {
114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115 f.write_str(self.as_str())
116 }
117}
118
119#[derive(Debug, Clone, PartialEq, Eq)]
121pub struct Diagnostic {
122 pub code: Code,
124 pub severity: Severity,
126 pub message: String,
128 pub range: TextRange,
130 pub help: Option<String>,
132}
133
134impl Diagnostic {
135 pub(crate) fn error(code: Code, range: TextRange, message: impl Into<String>) -> Self {
136 Self {
137 code,
138 severity: Severity::Error,
139 message: message.into(),
140 range,
141 help: None,
142 }
143 }
144
145 pub(crate) fn with_help(mut self, help: impl Into<String>) -> Self {
146 self.help = Some(help.into());
147 self
148 }
149
150 #[must_use]
152 pub fn is_error(&self) -> bool {
153 self.severity == Severity::Error
154 }
155}
156
157#[must_use]
159pub fn line_col(src: &str, offset: TextSize) -> (u32, u32) {
160 let offset = usize::from(offset).min(src.len());
161 let mut line = 1u32;
162 let mut col = 1u32;
163 for (i, c) in src.char_indices() {
164 if i >= offset {
165 break;
166 }
167 if c == '\n' {
168 line += 1;
169 col = 1;
170 } else {
171 col += 1;
172 }
173 }
174 (line, col)
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180
181 #[test]
182 fn codes_are_unique() {
183 let all = [
184 Code::UnexpectedCharacter,
185 Code::BadString,
186 Code::UnterminatedComment,
187 Code::NoModule,
188 Code::MissingModuleEnd,
189 Code::UnexpectedToken,
190 Code::MissingExpression,
191 Code::PrecedenceConflict,
192 Code::MissingOperator,
193 Code::MisplacedOperator,
194 Code::JunctionIndentation,
195 Code::StepLevel,
196 Code::StepNumberMisuse,
197 Code::UnexpectedProof,
198 Code::ProofTooDeep,
199 Code::BadLabel,
200 Code::LabelChangesParse,
201 Code::TimesAsArgument,
202 Code::SetMapWithIn,
203 Code::AtInExcept,
204 Code::BadFairness,
205 Code::NewWithArguments,
206 Code::BadStructuralSelector,
207 Code::NestingTooDeep,
208 Code::InputTooLarge,
209 Code::ModuleNameMismatch,
210 Code::InvalidUtf8,
211 ];
212 let mut seen = std::collections::BTreeSet::new();
213 for c in all {
214 assert!(seen.insert(c.as_str()), "{c}");
215 assert!(c.as_str().starts_with("DIVE-S"));
216 }
217 }
218
219 #[test]
220 fn line_col_counts_chars() {
221 assert_eq!(line_col("ab\ncd", TextSize::from(4)), (2, 2));
222 assert_eq!(line_col("∧x", TextSize::from(3)), (1, 2));
223 }
224}