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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::lexer::Token;
use std::fmt;
use std::fmt::Formatter;

use crate::syntax::untyped::{SyntaxKind, TextRange};

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParseErrorBuilder {
    pub(super) range: Option<TextRange>,
    pub(super) found: Option<SyntaxKind>,
    pub(super) expected: String,
}

impl ParseErrorBuilder {
    /// expected should only describe what is missing.
    /// It is later passed into "expected <expectedString> but found ..."
    pub fn new<S>(expected: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            range: None,
            found: None,
            expected: expected.into(),
        }
    }

    pub(crate) fn at_token(mut self, token: &Token) -> Self {
        self.range = Some(token.range);
        self.found = Some(token.kind);
        self
    }

    pub(super) fn build(self) -> ParseError {
        ParseError {
            range: self.range.unwrap(),
            found: self.found,
            expected: self.expected,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParseError {
    pub range: TextRange,
    pub found: Option<SyntaxKind>,
    pub expected: String,
}

impl ParseError {
    #[must_use]
    pub fn expected_message(&self) -> String {
        if let Some(found) = self.found {
            format!("expected {} but found {}", self.expected, found)
        } else {
            format!("expected {} but reached end of file", self.expected)
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "error at {}..{}: ",
            u32::from(self.range.start()),
            u32::from(self.range.end()),
        )?;

        write!(f, "{}", self.expected_message())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::T;
    use rowan::TextSize;

    #[test]
    fn parse_error_display() {
        let range = TextRange::new(TextSize::from(3), TextSize::from(5));
        let parse_error = ParseError {
            range,
            found: Some(T!["{%"]),
            expected: "word".to_string(),
        };

        assert_eq!(
            format!("{}", parse_error),
            "error at 3..5: expected word but found {%"
        );
    }
}