limbo_sqlite3_parser/lexer/sql/
error.rs1use std::error;
2use std::fmt;
3use std::io;
4
5use crate::lexer::scan::ScanError;
6use crate::parser::ParserError;
7
8#[non_exhaustive]
10#[derive(Debug, miette::Diagnostic)]
11#[diagnostic()]
12pub enum Error {
13 Io(io::Error),
15 UnrecognizedToken(
17 Option<(u64, usize)>,
18 #[label("here")] Option<miette::SourceSpan>,
19 ),
20 UnterminatedLiteral(
22 Option<(u64, usize)>,
23 #[label("here")] Option<miette::SourceSpan>,
24 ),
25 UnterminatedBracket(
27 Option<(u64, usize)>,
28 #[label("here")] Option<miette::SourceSpan>,
29 ),
30 UnterminatedBlockComment(
32 Option<(u64, usize)>,
33 #[label("here")] Option<miette::SourceSpan>,
34 ),
35 BadVariableName(
37 Option<(u64, usize)>,
38 #[label("here")] Option<miette::SourceSpan>,
39 ),
40 #[diagnostic(help("Invalid digit in `{3}`"))]
42 BadNumber(
43 Option<(u64, usize)>,
44 #[label("here")] Option<miette::SourceSpan>,
45 Option<usize>,
46 String, ),
48 ExpectedEqualsSign(
50 Option<(u64, usize)>,
51 #[label("here")] Option<miette::SourceSpan>,
52 ),
53 MalformedBlobLiteral(
55 Option<(u64, usize)>,
56 #[label("here")] Option<miette::SourceSpan>,
57 ),
58 MalformedHexInteger(
60 Option<(u64, usize)>,
61 #[label("here")] Option<miette::SourceSpan>,
62 Option<usize>,
63 #[help] Option<&'static str>,
64 ),
65 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 write!(f, "unrecognized token at {:?}", pos.unwrap())
79 }
80 Self::UnterminatedLiteral(pos, _) => {
81 write!(f, "non-terminated literal at {:?}", pos.unwrap())
82 }
83 Self::UnterminatedBracket(pos, _) => {
84 write!(f, "non-terminated bracket at {:?}", pos.unwrap())
85 }
86 Self::UnterminatedBlockComment(pos, _) => {
87 write!(f, "non-terminated block comment at {:?}", pos.unwrap())
88 }
89 Self::BadVariableName(pos, _) => write!(f, "bad variable name at {:?}", pos.unwrap()),
90 Self::BadNumber(pos, _, _, _) => write!(f, "bad number at {:?}", pos.unwrap()),
91 Self::ExpectedEqualsSign(pos, _) => write!(f, "expected = sign at {:?}", pos.unwrap()),
92 Self::MalformedBlobLiteral(pos, _) => {
93 write!(f, "malformed blob literal at {:?}", pos.unwrap())
94 }
95 Self::MalformedHexInteger(pos, _, _, _) => {
96 write!(f, "malformed hex integer at {:?}", pos.unwrap())
97 }
98 Self::ParserError(ref msg, Some(pos), _) => write!(f, "{msg} at {pos:?}"),
99 Self::ParserError(ref msg, _, _) => write!(f, "{msg}"),
100 }
101 }
102}
103
104impl error::Error for Error {}
105
106impl From<io::Error> for Error {
107 fn from(err: io::Error) -> Self {
108 Self::Io(err)
109 }
110}
111
112impl From<ParserError> for Error {
113 fn from(err: ParserError) -> Self {
114 Self::ParserError(err, None, None)
115 }
116}
117
118impl ScanError for Error {
119 fn position(&mut self, line: u64, column: usize, offset: usize) {
120 match *self {
121 Self::Io(_) => {}
122 Self::UnrecognizedToken(ref mut pos, ref mut src) => {
123 *pos = Some((line, column));
124 *src = Some((offset).into());
125 }
126 Self::UnterminatedLiteral(ref mut pos, ref mut src) => {
127 *pos = Some((line, column));
128 *src = Some((offset).into());
129 }
130 Self::UnterminatedBracket(ref mut pos, ref mut src) => {
131 *pos = Some((line, column));
132 *src = Some((offset).into());
133 }
134 Self::UnterminatedBlockComment(ref mut pos, ref mut src) => {
135 *pos = Some((line, column));
136 *src = Some((offset).into());
137 }
138 Self::BadVariableName(ref mut pos, ref mut src) => {
139 *pos = Some((line, column));
140 *src = Some((offset).into());
141 }
142 Self::ExpectedEqualsSign(ref mut pos, ref mut src) => {
143 *pos = Some((line, column));
144 *src = Some((offset).into());
145 }
146 Self::MalformedBlobLiteral(ref mut pos, ref mut src) => {
147 *pos = Some((line, column));
148 *src = Some((offset).into());
149 }
150 Self::MalformedHexInteger(ref mut pos, ref mut src, len, _)
152 | Self::BadNumber(ref mut pos, ref mut src, len, _) => {
153 *pos = Some((line, column));
154 *src = Some((offset, len.unwrap_or(0)).into());
155 }
156 Self::ParserError(_, ref mut pos, _) => *pos = Some((line, column)),
157 }
158 }
159}