-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy patherrors.rs
More file actions
329 lines (301 loc) · 11.7 KB
/
errors.rs
File metadata and controls
329 lines (301 loc) · 11.7 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::lang::{
query, Positioned, Query, QueryPosition, Span, VALID_AGGREGATES, VALID_INLINE, VALID_OPERATORS,
};
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
use failure::Fail;
use nom::types::CompleteStr;
use nom::ErrorKind;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::convert::From;
use strsim::normalized_levenshtein;
/// Container for the query string that can be used to parse and report errors.
pub struct QueryContainer {
query: String,
reporter: Box<dyn ErrorReporter>,
}
/// Common syntax errors.
#[derive(PartialEq, Debug, FromPrimitive, Fail, Clone)]
pub enum SyntaxErrors {
#[fail(display = "")]
StartOfError,
#[fail(display = "unterminated single quoted string")]
UnterminatedSingleQuotedString,
#[fail(display = "unterminated double quoted string")]
UnterminatedDoubleQuotedString,
#[fail(display = "expecting an operand for binary operator")]
MissingOperand,
#[fail(display = "expecting a name for the field")]
MissingName,
#[fail(display = "expecting close parentheses")]
MissingParen,
#[fail(display = "Expected an operator")]
NotAnOperator,
#[fail(display = "Not an aggregate operator")]
NotAnAggregateOperator,
#[fail(display = "Invalid filter")]
InvalidFilter,
#[fail(display = "Invalid boolean expression")]
InvalidBooleanExpression,
}
/// Trait that can be used to report errors by the parser and other layers.
pub trait ErrorBuilder {
/// Create a SnippetBuilder for the given error
fn report_error_for<E: ToString>(&self, error: E) -> SnippetBuilder;
}
impl QueryContainer {
pub fn new(query: String, reporter: Box<dyn ErrorReporter>) -> QueryContainer {
QueryContainer { query, reporter }
}
/// Parse the contained query string.
pub fn parse(&self) -> Result<Query, QueryPosition> {
let parse_result = query(Span::new(CompleteStr(&self.query)));
let errors = match parse_result {
Err(nom::Err::Failure(nom::Context::List(ref list))) => Some(list),
Err(nom::Err::Error(nom::Context::List(ref list))) => Some(list),
_ => None,
};
if let Some(ref list) = errors {
// Check for an error from a delimited!() parser. The error list will contain
// the location of the start as the last item and the location of the end as the
// penultimate item.
let last_chunk = list.rchunks_exact(2).next().map(|p| (&p[0], &p[1]));
match last_chunk {
Some((
(ref end_span, ErrorKind::Custom(ref delim_error)),
(ref start_span, ErrorKind::Custom(SyntaxErrors::StartOfError)),
)) => {
self.report_error_for(delim_error)
.with_code_range((*start_span).into(), (*end_span).into(), "")
.with_resolutions(
delim_error
.to_resolution(&(&self.query)[start_span.offset..end_span.offset]),
)
.send_report();
}
_ => {
list.iter().for_each(|(span, error)| match error {
ErrorKind::Custom(ref custom_error) => self
.report_error_for(custom_error)
.with_code_range(
QueryPosition(span.offset),
QueryPosition(span.offset + span.fragment.len()),
"",
)
.with_resolutions(custom_error.to_resolution(
&(&self.query)[span.offset..span.offset + span.fragment.len()],
))
.send_report(),
_other => (),
});
}
}
};
// Return the parsed value or the last position of valid syntax
parse_result.map(|x| x.1).map_err(|e| match e {
nom::Err::Incomplete(_) => QueryPosition(0),
nom::Err::Error(context) | nom::Err::Failure(context) => match context {
nom::Context::Code(span, _) => span.into(),
nom::Context::List(list) => list.first().unwrap().0.into(),
},
})
}
}
impl ErrorBuilder for QueryContainer {
/// Create a SnippetBuilder for the given error
fn report_error_for<E: ToString>(&self, error: E) -> SnippetBuilder {
SnippetBuilder {
query: self,
data: SnippetData {
error: error.to_string(),
source: self.query.to_string(),
..Default::default()
},
}
}
}
fn did_you_mean(input: &str, choices: &[&str]) -> Option<String> {
let similarities = choices
.iter()
.map(|choice| (choice, normalized_levenshtein(choice, input)));
let mut candidates: Vec<_> = similarities.filter(|(_op, score)| *score > 0.6).collect();
candidates.sort_by_key(|(_op, score)| (score * 100_f64) as u16);
candidates
.iter()
.map(|(choice, _score)| (**choice).to_owned())
.next()
}
impl SyntaxErrors {
pub fn to_resolution(&self, code_fragment: &str) -> Vec<String> {
match self {
SyntaxErrors::InvalidFilter => vec!["Filter was invalid".to_string()],
SyntaxErrors::StartOfError => Vec::new(),
SyntaxErrors::NotAnOperator => {
let mut res = vec![format!("{} is not a valid operator", code_fragment)];
if let Some(choice) = did_you_mean(code_fragment, &VALID_OPERATORS) {
res.push(format!("Did you mean \"{}\"?", choice));
}
res
}
SyntaxErrors::NotAnAggregateOperator => {
let mut res = vec![format!(
"{} is not a valid aggregate operator",
code_fragment
)];
if let Some(choice) = did_you_mean(code_fragment, &VALID_AGGREGATES) {
res.push(format!("Did you mean \"{}\"?", choice));
}
if VALID_INLINE.contains(&code_fragment) {
res.push(format!("{} is an inline operator, but only aggregate operators (count, average, etc.) are valid here", code_fragment))
}
res
}
SyntaxErrors::UnterminatedSingleQuotedString => {
vec!["Insert a single quote (') to terminate this string".to_string()]
}
SyntaxErrors::UnterminatedDoubleQuotedString => {
vec!["Insert a double quote (\") to terminate this string".to_string()]
}
SyntaxErrors::MissingParen => {
vec!["Insert a right parenthesis to terminate this expression".to_string()]
}
SyntaxErrors::MissingOperand => {
vec!["Add the operand or delete the operator".to_string()]
}
SyntaxErrors::MissingName => vec!["Give the value a name".to_string()],
SyntaxErrors::InvalidBooleanExpression => {
let mut base = vec![format!(
"The boolean expression {} is invalid",
code_fragment
)];
if code_fragment.contains("and") || code_fragment.contains("or") {
base.push("AND and OR must be in UPPER CASE".to_string());
}
base
}
}
}
}
/// Converts the ordinal from the nom error object back into a SyntaxError.
impl From<u32> for SyntaxErrors {
fn from(ord: u32) -> Self {
// The FromPrimitive trait derived for this enum allows from_u32() to work.
SyntaxErrors::from_u32(ord).unwrap()
}
}
impl From<SyntaxErrors> for ErrorKind {
fn from(e: SyntaxErrors) -> Self {
ErrorKind::Custom(e as u32)
}
}
/// Callback for handling error Snippets.
pub trait ErrorReporter {
fn handle_error(&self, _snippet: Snippet) {}
}
/// Container for data that will be used to construct a Snippet
#[derive(Default)]
pub struct SnippetData {
error: String,
source: String,
annotations: Vec<((usize, usize), String)>,
resolution: Vec<String>,
}
#[must_use = "the send_report() method must eventually be called for this builder"]
pub struct SnippetBuilder<'a> {
query: &'a QueryContainer,
data: SnippetData,
}
impl<'a> SnippetBuilder<'a> {
/// Adds an annotation to a portion of the query string. The given position will be
/// highlighted with the accompanying label.
pub fn with_code_pointer<T, S: ToString>(mut self, pos: &Positioned<T>, label: S) -> Self {
self.data
.annotations
.push(((pos.start_pos.0, pos.end_pos.0), label.to_string()));
self
}
/// Adds an annotation to a portion of the query string. The given position will be
/// highlighted with the accompanying label.
pub fn with_code_range<S: ToString>(
mut self,
start_pos: QueryPosition,
end_pos: QueryPosition,
label: S,
) -> Self {
self.data
.annotations
.push(((start_pos.0, end_pos.0), label.to_string()));
self
}
/// Add a message to help the user resolve the error.
pub fn with_resolution<T: ToString>(mut self, resolution: T) -> Self {
self.data.resolution.push(resolution.to_string());
self
}
/// Add a message to help the user resolve the error.
pub fn with_resolutions<T: IntoIterator<Item = String>>(mut self, resolutions: T) -> Self {
self.data.resolution.extend(resolutions.into_iter());
self
}
/// Build and send the Snippet to the ErrorReporter in the QueryContainer.
pub fn send_report(mut self) {
self.query.reporter.handle_error(Snippet {
title: Some(Annotation {
label: Some(self.data.error),
id: None,
annotation_type: AnnotationType::Error,
}),
slices: vec![Slice {
source: self.data.source,
line_start: 1,
origin: None,
fold: false,
annotations: self
.data
.annotations
.drain(..)
.map(move |anno| SourceAnnotation {
range: anno.0,
label: anno.1,
annotation_type: AnnotationType::Error,
})
.collect(),
}],
footer: self
.data
.resolution
.iter()
.map(|res| Annotation {
label: Some(res.to_string()),
id: None,
annotation_type: AnnotationType::Help,
})
.collect(),
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn did_you_mean() {
assert_eq!(
SyntaxErrors::NotAnAggregateOperator.to_resolution("cont"),
vec![
"cont is not a valid aggregate operator",
"Did you mean \"count\"?"
]
);
assert_eq!(
SyntaxErrors::NotAnOperator.to_resolution("cont"),
vec!["cont is not a valid operator", "Did you mean \"count\"?"]
);
assert_eq!(
SyntaxErrors::NotAnAggregateOperator.to_resolution("parse"),
vec![
"parse is not a valid aggregate operator",
"parse is an inline operator, but only aggregate operators (count, average, etc.) are valid here"
]
);
}
}