libsql_sqlite3_parser/lexer/sql/
error.rs

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