sqlite3_parser/lexer/sql/
error.rs1use std::error;
2use std::fmt;
3use std::io;
4
5use crate::lexer::scan::{Pos, ScanError};
6use crate::parser::ParserError;
7
8#[non_exhaustive]
10#[derive(Debug)]
11pub enum Error {
12    Io(io::Error),
14    UnrecognizedToken(Option<Pos>),
16    UnterminatedLiteral(Option<Pos>),
18    UnterminatedBracket(Option<Pos>),
20    UnterminatedBlockComment(Option<Pos>),
22    BadVariableName(Option<Pos>),
24    BadNumber(Option<Pos>),
26    ExpectedEqualsSign(Option<Pos>),
28    MalformedBlobLiteral(Option<Pos>),
30    MalformedHexInteger(Option<Pos>),
32    ParserError(ParserError, Option<Pos>),
34}
35
36impl fmt::Display for Error {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::Io(ref err) => err.fmt(f),
40            Self::UnrecognizedToken(pos) => write!(f, "unrecognized token at {pos:?}"),
41            Self::UnterminatedLiteral(pos) => {
42                write!(f, "non-terminated literal at {pos:?}")
43            }
44            Self::UnterminatedBracket(pos) => {
45                write!(f, "non-terminated bracket at {pos:?}")
46            }
47            Self::UnterminatedBlockComment(pos) => {
48                write!(f, "non-terminated block comment at {pos:?}")
49            }
50            Self::BadVariableName(pos) => write!(f, "bad variable name at {pos:?}"),
51            Self::BadNumber(pos) => write!(f, "bad number at {pos:?}"),
52            Self::ExpectedEqualsSign(pos) => write!(f, "expected = sign at {pos:?}"),
53            Self::MalformedBlobLiteral(pos) => {
54                write!(f, "malformed blob literal at {pos:?}")
55            }
56            Self::MalformedHexInteger(pos) => {
57                write!(f, "malformed hex integer at {pos:?}")
58            }
59            Self::ParserError(ref msg, Some(pos)) => write!(f, "{msg} at {pos}"),
60            Self::ParserError(ref msg, _) => write!(f, "{msg}"),
61        }
62    }
63}
64
65impl error::Error for Error {}
66
67impl From<io::Error> for Error {
68    fn from(err: io::Error) -> Self {
69        Self::Io(err)
70    }
71}
72
73impl From<ParserError> for Error {
74    fn from(err: ParserError) -> Self {
75        Self::ParserError(err, None)
76    }
77}
78
79impl ScanError for Error {
80    fn position(&mut self, p: Pos) {
81        match *self {
82            Self::Io(_) => {}
83            Self::UnrecognizedToken(ref mut pos) => *pos = Some(p),
84            Self::UnterminatedLiteral(ref mut pos) => *pos = Some(p),
85            Self::UnterminatedBracket(ref mut pos) => *pos = Some(p),
86            Self::UnterminatedBlockComment(ref mut pos) => *pos = Some(p),
87            Self::BadVariableName(ref mut pos) => *pos = Some(p),
88            Self::BadNumber(ref mut pos) => *pos = Some(p),
89            Self::ExpectedEqualsSign(ref mut pos) => *pos = Some(p),
90            Self::MalformedBlobLiteral(ref mut pos) => *pos = Some(p),
91            Self::MalformedHexInteger(ref mut pos) => *pos = Some(p),
92            Self::ParserError(_, ref mut pos) => *pos = Some(p),
93        }
94    }
95}