Skip to main content

limbo_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, miette::Diagnostic)]
11#[diagnostic()]
12pub enum Error {
13    /// I/O Error
14    Io(io::Error),
15    /// Lexer error
16    UnrecognizedToken(
17        Option<(u64, usize)>,
18        #[label("here")] Option<miette::SourceSpan>,
19    ),
20    /// Missing quote or double-quote or backtick
21    UnterminatedLiteral(
22        Option<(u64, usize)>,
23        #[label("here")] Option<miette::SourceSpan>,
24    ),
25    /// Missing `]`
26    UnterminatedBracket(
27        Option<(u64, usize)>,
28        #[label("here")] Option<miette::SourceSpan>,
29    ),
30    /// Missing `*/`
31    UnterminatedBlockComment(
32        Option<(u64, usize)>,
33        #[label("here")] Option<miette::SourceSpan>,
34    ),
35    /// Invalid parameter name
36    BadVariableName(
37        Option<(u64, usize)>,
38        #[label("here")] Option<miette::SourceSpan>,
39    ),
40    /// Invalid number format
41    #[diagnostic(help("Invalid digit in `{3}`"))]
42    BadNumber(
43        Option<(u64, usize)>,
44        #[label("here")] Option<miette::SourceSpan>,
45        Option<usize>,
46        String, // Holds the offending number as a string
47    ),
48    /// Invalid or missing sign after `!`
49    ExpectedEqualsSign(
50        Option<(u64, usize)>,
51        #[label("here")] Option<miette::SourceSpan>,
52    ),
53    /// BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character.
54    MalformedBlobLiteral(
55        Option<(u64, usize)>,
56        #[label("here")] Option<miette::SourceSpan>,
57    ),
58    /// Hexadecimal integer literals follow the C-language notation of "0x" or "0X" followed by hexadecimal digits.
59    MalformedHexInteger(
60        Option<(u64, usize)>,
61        #[label("here")] Option<miette::SourceSpan>,
62        Option<usize>,
63        #[help] Option<&'static str>,
64    ),
65    /// Grammar error
66    ParserError(
67        ParserError,
68        Option<(u64, usize)>,
69        #[label("syntax error")] Option<miette::SourceSpan>,
70    ),
71}
72
73impl fmt::Display for Error {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match *self {
76            Self::Io(ref err) => err.fmt(f),
77            Self::UnrecognizedToken(pos, _) => {
78                if let Some(p) = pos {
79                    write!(f, "unrecognized token at {:?}", p)
80                } else {
81                    write!(f, "unrecognized token at unknown position")
82                }
83            }
84            Self::UnterminatedLiteral(pos, _) => {
85                if let Some(p) = pos {
86                    write!(f, "non-terminated literal at {:?}", p)
87                } else {
88                    write!(f, "non-terminated literal at unknown position")
89                }
90            }
91            Self::UnterminatedBracket(pos, _) => {
92                if let Some(p) = pos {
93                    write!(f, "non-terminated bracket at {:?}", p)
94                } else {
95                    write!(f, "non-terminated bracket at unknown position")
96                }
97            }
98            Self::UnterminatedBlockComment(pos, _) => {
99                if let Some(p) = pos {
100                    write!(f, "non-terminated block comment at {:?}", p)
101                } else {
102                    write!(f, "non-terminated block comment at unknown position")
103                }
104            }
105            Self::BadVariableName(pos, _) => {
106                if let Some(p) = pos {
107                    write!(f, "bad variable name at {:?}", p)
108                } else {
109                    write!(f, "bad variable name at unknown position")
110                }
111            }
112            Self::BadNumber(pos, _, _, _) => {
113                if let Some(p) = pos {
114                    write!(f, "bad number at {:?}", p)
115                } else {
116                    write!(f, "bad number at unknown position")
117                }
118            }
119            Self::ExpectedEqualsSign(pos, _) => {
120                if let Some(p) = pos {
121                    write!(f, "expected = sign at {:?}", p)
122                } else {
123                    write!(f, "expected = sign at unknown position")
124                }
125            }
126            Self::MalformedBlobLiteral(pos, _) => {
127                if let Some(p) = pos {
128                    write!(f, "malformed blob literal at {:?}", p)
129                } else {
130                    write!(f, "malformed blob literal at unknown position")
131                }
132            }
133            Self::MalformedHexInteger(pos, _, _, _) => {
134                if let Some(p) = pos {
135                    write!(f, "malformed hex integer at {:?}", p)
136                } else {
137                    write!(f, "malformed hex integer at unknown position")
138                }
139            }
140            Self::ParserError(ref msg, Some(pos), _) => write!(f, "{msg} at {pos:?}"),
141            Self::ParserError(ref msg, _, _) => write!(f, "{msg}"),
142        }
143    }
144}
145
146impl error::Error for Error {}
147
148impl From<io::Error> for Error {
149    fn from(err: io::Error) -> Self {
150        Self::Io(err)
151    }
152}
153
154impl From<ParserError> for Error {
155    fn from(err: ParserError) -> Self {
156        Self::ParserError(err, None, None)
157    }
158}
159
160impl ScanError for Error {
161    fn position(&mut self, line: u64, column: usize, offset: usize) {
162        match *self {
163            Self::Io(_) => {}
164            Self::UnrecognizedToken(ref mut pos, ref mut src) => {
165                *pos = Some((line, column));
166                *src = Some((offset).into());
167            }
168            Self::UnterminatedLiteral(ref mut pos, ref mut src) => {
169                *pos = Some((line, column));
170                *src = Some((offset).into());
171            }
172            Self::UnterminatedBracket(ref mut pos, ref mut src) => {
173                *pos = Some((line, column));
174                *src = Some((offset).into());
175            }
176            Self::UnterminatedBlockComment(ref mut pos, ref mut src) => {
177                *pos = Some((line, column));
178                *src = Some((offset).into());
179            }
180            Self::BadVariableName(ref mut pos, ref mut src) => {
181                *pos = Some((line, column));
182                *src = Some((offset).into());
183            }
184            Self::ExpectedEqualsSign(ref mut pos, ref mut src) => {
185                *pos = Some((line, column));
186                *src = Some((offset).into());
187            }
188            Self::MalformedBlobLiteral(ref mut pos, ref mut src) => {
189                *pos = Some((line, column));
190                *src = Some((offset).into());
191            }
192            // Exact same handling here
193            Self::MalformedHexInteger(ref mut pos, ref mut src, len, _)
194            | Self::BadNumber(ref mut pos, ref mut src, len, _) => {
195                *pos = Some((line, column));
196                *src = Some((offset, len.unwrap_or(0)).into());
197            }
198            Self::ParserError(_, ref mut pos, _) => *pos = Some((line, column)),
199        }
200    }
201}