syntax/
syntax_error.rs

1//! See docs for `SyntaxError`.
2
3use std::fmt;
4
5use crate::{TextRange, TextSize};
6
7/// Represents the result of unsuccessful tokenization, parsing
8/// or tree validation.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct SyntaxError(String, TextRange);
11
12// FIXME: there was an unused SyntaxErrorKind previously (before this enum was removed)
13// It was introduced in this PR: https://github.com/rust-lang/rust-analyzer/pull/846/files#diff-827da9b03b8f9faa1bade5cdd44d5dafR95
14// but it was not removed by a mistake.
15//
16// So, we need to find a place where to stick validation for attributes in match clauses.
17// Code before refactor:
18// InvalidMatchInnerAttr => {
19//    write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
20// }
21
22impl SyntaxError {
23    pub fn new(message: impl Into<String>, range: TextRange) -> Self {
24        Self(message.into(), range)
25    }
26    pub fn new_at_offset(message: impl Into<String>, offset: TextSize) -> Self {
27        Self(message.into(), TextRange::empty(offset))
28    }
29
30    pub fn range(&self) -> TextRange {
31        self.1
32    }
33
34    pub fn with_range(mut self, range: TextRange) -> Self {
35        self.1 = range;
36        self
37    }
38}
39
40impl fmt::Display for SyntaxError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        self.0.fmt(f)
43    }
44}
45
46impl std::error::Error for SyntaxError {}