squawk_syntax/syntax_error.rs
1// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/syntax_error.rs
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27use std::fmt;
28
29use rowan::{TextRange, TextSize};
30
31/// Represents the result of unsuccessful tokenization, parsing
32/// or tree validation.
33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct SyntaxError(String, TextRange);
35
36// FIXME: there was an unused SyntaxErrorKind previously (before this enum was removed)
37// It was introduced in this PR: https://github.com/rust-lang/rust-analyzer/pull/846/files#diff-827da9b03b8f9faa1bade5cdd44d5dafR95
38// but it was not removed by a mistake.
39//
40// So, we need to find a place where to stick validation for attributes in match clauses.
41// Code before refactor:
42// InvalidMatchInnerAttr => {
43// write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
44// }
45
46impl SyntaxError {
47 pub fn new(message: impl Into<String>, range: TextRange) -> Self {
48 Self(message.into(), range)
49 }
50 pub fn new_at_offset(message: impl Into<String>, offset: TextSize) -> Self {
51 Self(message.into(), TextRange::empty(offset))
52 }
53
54 pub fn range(&self) -> TextRange {
55 self.1
56 }
57
58 pub fn message(&self) -> &str {
59 &self.0
60 }
61 pub fn with_range(mut self, range: TextRange) -> Self {
62 self.1 = range;
63 self
64 }
65}
66
67impl fmt::Display for SyntaxError {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 self.0.fmt(f)
70 }
71}