1use thiserror::Error;
16
17use crate::token::Token;
18
19#[derive(Error, Debug)]
20pub enum ParseError {
21 #[error("{0}: expected `{1}` got `{2}`")]
22 Unexpected(ErrorContext, Token, Token),
23}
24
25#[derive(Error, Debug)]
26pub enum ScanError {
27 #[error("{0}: incomplete UTF8")]
28 IncompleteUtf8(ErrorContext),
29
30 #[error("{0}: unexpected character `{1}`")]
31 Unexpected(ErrorContext, char),
32}
33
34pub struct ErrorContext {
36 pub path: String,
37 pub line: usize,
38 pub col: usize,
39 pub start_of_line: usize,
40}
41
42impl std::fmt::Display for ErrorContext {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "{}:{}:{}", self.path, self.line, self.col)
45 }
46}
47
48impl std::fmt::Debug for ErrorContext {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{}:{}:{}", self.path, self.line, self.col)
51 }
52}