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/// SQL lexer and parser errors
9#[non_exhaustive]
10#[derive(Debug)]
11pub enum Error {
12    /// I/O Error
13    Io(io::Error),
14    /// Lexer error
15    UnrecognizedToken(Option<(u64, usize)>),
16    /// Missing quote or double-quote or backtick
17    UnterminatedLiteral(Option<(u64, usize)>),
18    /// Missing `]`
19    UnterminatedBracket(Option<(u64, usize)>),
20    /// Missing `*/`
21    UnterminatedBlockComment(Option<(u64, usize)>),
22    /// Invalid parameter name
23    BadVariableName(Option<(u64, usize)>),
24    /// Invalid number format
25    BadNumber(Option<(u64, usize)>),
26    /// Invalid or missing sign after `!`
27    ExpectedEqualsSign(Option<(u64, usize)>),
28    /// BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character.
29    MalformedBlobLiteral(Option<(u64, usize)>),
30    /// Hexadecimal integer literals follow the C-language notation of "0x" or "0X" followed by hexadecimal digits.
31    MalformedHexInteger(Option<(u64, usize)>),
32    /// Grammar error
33    ParserError(ParserError, Option<(u64, usize)>),
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.unwrap()),
41            Self::UnterminatedLiteral(pos) => {
42                write!(f, "non-terminated literal at {:?}", pos.unwrap())
43            }
44            Self::UnterminatedBracket(pos) => {
45                write!(f, "non-terminated bracket at {:?}", pos.unwrap())
46            }
47            Self::UnterminatedBlockComment(pos) => {
48                write!(f, "non-terminated block comment at {:?}", pos.unwrap())
49            }
50            Self::BadVariableName(pos) => write!(f, "bad variable name at {:?}", pos.unwrap()),
51            Self::BadNumber(pos) => write!(f, "bad number at {:?}", pos.unwrap()),
52            Self::ExpectedEqualsSign(pos) => write!(f, "expected = sign at {:?}", pos.unwrap()),
53            Self::MalformedBlobLiteral(pos) => {
54                write!(f, "malformed blob literal at {:?}", pos.unwrap())
55            }
56            Self::MalformedHexInteger(pos) => {
57                write!(f, "malformed hex integer at {:?}", pos.unwrap())
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, line: u64, column: usize) {
81        match *self {
82            Self::Io(_) => {}
83            Self::UnrecognizedToken(ref mut pos) => *pos = Some((line, column)),
84            Self::UnterminatedLiteral(ref mut pos) => *pos = Some((line, column)),
85            Self::UnterminatedBracket(ref mut pos) => *pos = Some((line, column)),
86            Self::UnterminatedBlockComment(ref mut pos) => *pos = Some((line, column)),
87            Self::BadVariableName(ref mut pos) => *pos = Some((line, column)),
88            Self::BadNumber(ref mut pos) => *pos = Some((line, column)),
89            Self::ExpectedEqualsSign(ref mut pos) => *pos = Some((line, column)),
90            Self::MalformedBlobLiteral(ref mut pos) => *pos = Some((line, column)),
91            Self::MalformedHexInteger(ref mut pos) => *pos = Some((line, column)),
92            Self::ParserError(_, ref mut pos) => *pos = Some((line, column)),
93        }
94    }
95}