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
12impl SyntaxError {
13    pub fn new(message: impl Into<String>, range: TextRange) -> Self {
14        Self(message.into(), range)
15    }
16    pub fn new_at_offset(message: impl Into<String>, offset: TextSize) -> Self {
17        Self(message.into(), TextRange::empty(offset))
18    }
19
20    pub fn range(&self) -> TextRange {
21        self.1
22    }
23
24    pub fn with_range(mut self, range: TextRange) -> Self {
25        self.1 = range;
26        self
27    }
28}
29
30impl fmt::Display for SyntaxError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        self.0.fmt(f)
33    }
34}
35
36impl std::error::Error for SyntaxError {}