2020-02-17 20:50:58 +00:00
|
|
|
//! See docs for `SyntaxError`.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2018-11-04 15:45:22 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-04-24 21:40:41 +00:00
|
|
|
use crate::{TextRange, TextSize};
|
2018-11-04 15:45:22 +00:00
|
|
|
|
2020-02-06 00:33:18 +00:00
|
|
|
/// Represents the result of unsuccessful tokenization, parsing
|
2020-02-06 11:00:39 +00:00
|
|
|
/// or tree validation.
|
2018-11-04 15:45:22 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-02-06 00:33:18 +00:00
|
|
|
pub struct SyntaxError(String, TextRange);
|
|
|
|
|
|
|
|
// FIXME: there was an unused SyntaxErrorKind previously (before this enum was removed)
|
2022-07-08 13:44:49 +00:00
|
|
|
// It was introduced in this PR: https://github.com/rust-lang/rust-analyzer/pull/846/files#diff-827da9b03b8f9faa1bade5cdd44d5dafR95
|
2020-02-06 00:33:18 +00:00
|
|
|
// but it was not removed by a mistake.
|
|
|
|
//
|
|
|
|
// So, we need to find a place where to stick validation for attributes in match clauses.
|
|
|
|
// Code before refactor:
|
|
|
|
// InvalidMatchInnerAttr => {
|
|
|
|
// write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
|
|
|
|
// }
|
2019-05-29 07:12:08 +00:00
|
|
|
|
2018-11-04 15:45:22 +00:00
|
|
|
impl SyntaxError {
|
2020-02-06 00:33:18 +00:00
|
|
|
pub fn new(message: impl Into<String>, range: TextRange) -> Self {
|
|
|
|
Self(message.into(), range)
|
2018-11-05 17:38:34 +00:00
|
|
|
}
|
2020-04-24 21:40:41 +00:00
|
|
|
pub fn new_at_offset(message: impl Into<String>, offset: TextSize) -> Self {
|
|
|
|
Self(message.into(), TextRange::empty(offset))
|
2018-11-07 10:35:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 00:08:49 +00:00
|
|
|
pub fn range(&self) -> TextRange {
|
|
|
|
self.1
|
2018-11-05 17:38:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 00:33:18 +00:00
|
|
|
pub fn with_range(mut self, range: TextRange) -> Self {
|
|
|
|
self.1 = range;
|
2018-11-05 17:38:34 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SyntaxError {
|
2022-07-20 13:02:08 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-06 00:33:18 +00:00
|
|
|
self.0.fmt(f)
|
2019-05-07 16:38:26 +00:00
|
|
|
}
|
|
|
|
}
|