aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow/syntax_error.rs
blob: e8c818dc69a6c6ee1c8bb665ac63c7b9b02c3c29 (plain)
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
use std::fmt;

use crate::TextRange;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SyntaxError {
    pub kind: SyntaxErrorKind,
    pub range: TextRange,
}

impl SyntaxError {
    pub fn new(kind: SyntaxErrorKind, range: TextRange) -> SyntaxError {
        SyntaxError { kind, range }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SyntaxErrorKind {
    ParseError(ParseError),
    EmptyChar,
    UnclosedChar,
    LongChar,
    EmptyAsciiEscape,
    InvalidAsciiEscape,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParseError(pub String);

impl fmt::Display for SyntaxErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::SyntaxErrorKind::*;
        match self {
            EmptyAsciiEscape => write!(f, "Empty escape sequence"),
            InvalidAsciiEscape => write!(f, "Invalid escape sequence"),
            EmptyChar => write!(f, "Empty char literal"),
            UnclosedChar => write!(f, "Unclosed char literal"),
            LongChar => write!(f, "Char literal should be one character long"),
            ParseError(msg) => write!(f, "{}", msg.0),
        }
    }
}