hawdb_sql_syntax/
error.rs1use std::fmt;
16
17use crate::Span;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub enum SyntaxErrorCode {
21 InputTooLarge,
22 ParserLimitExceeded,
23 TokenLimitExceeded,
24 CommentNestingLimitExceeded,
25 ExpressionNestingLimitExceeded,
26 GraphPatternNestingLimitExceeded,
27 UnterminatedBlockComment,
28 UnterminatedQuotedIdentifier,
29 UnterminatedString,
30 UnterminatedDollarQuotedString,
31 InvalidParameter,
32 InvalidCharacter,
33 UnexpectedToken,
34 UnexpectedEnd,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct SyntaxError {
39 pub code: SyntaxErrorCode,
40 pub span: Span,
41 pub expected: Option<&'static str>,
42 pub found: Option<String>,
43}
44
45impl SyntaxError {
46 pub fn new(code: SyntaxErrorCode, span: Span) -> Self {
47 Self {
48 code,
49 span,
50 expected: None,
51 found: None,
52 }
53 }
54
55 pub fn expected(mut self, expected: &'static str) -> Self {
56 self.expected = Some(expected);
57 self
58 }
59
60 pub fn found(mut self, found: impl Into<String>) -> Self {
61 self.found = Some(found.into());
62 self
63 }
64}
65
66impl fmt::Display for SyntaxError {
67 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
68 write!(
69 formatter,
70 "SQL syntax error {:?} at bytes {}..{}",
71 self.code, self.span.start, self.span.end
72 )?;
73 if let Some(expected) = self.expected {
74 write!(formatter, "; expected {expected}")?;
75 }
76 if let Some(found) = &self.found {
77 write!(formatter, "; found {found}")?;
78 }
79 Ok(())
80 }
81}
82
83impl std::error::Error for SyntaxError {}