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 NativeSemanticError,
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct SyntaxError {
41 pub code: SyntaxErrorCode,
42 pub span: Span,
43 pub expected: Option<&'static str>,
44 pub found: Option<String>,
45}
46
47impl SyntaxError {
48 pub fn new(code: SyntaxErrorCode, span: Span) -> Self {
49 Self {
50 code,
51 span,
52 expected: None,
53 found: None,
54 }
55 }
56
57 pub fn expected(mut self, expected: &'static str) -> Self {
58 self.expected = Some(expected);
59 self
60 }
61
62 pub fn found(mut self, found: impl Into<String>) -> Self {
63 self.found = Some(found.into());
64 self
65 }
66}
67
68impl fmt::Display for SyntaxError {
69 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(
71 formatter,
72 "SQL syntax error {:?} at bytes {}..{}",
73 self.code, self.span.start, self.span.end
74 )?;
75 if let Some(expected) = self.expected {
76 write!(formatter, "; expected {expected}")?;
77 }
78 if let Some(found) = &self.found {
79 write!(formatter, "; found {found}")?;
80 }
81 Ok(())
82 }
83}
84
85impl std::error::Error for SyntaxError {}