Skip to content

Commit

Permalink
feat(parser): report nearby tokens when parse failed (risingwavelabs#…
Browse files Browse the repository at this point in the history
…8465)

Signed-off-by: TennyZhuang <zty0826@gmail.com>
  • Loading branch information
TennyZhuang authored Mar 11, 2023
1 parent 00ea62a commit 4b49428
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 208 deletions.
19 changes: 18 additions & 1 deletion src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,24 @@ impl Parser {

/// Report unexpected token
pub fn expected<T>(&self, expected: &str, found: Token) -> Result<T, ParserError> {
parser_err!(format!("Expected {}, found: {}", expected, found))
let start_off = self.index.saturating_sub(10);
let end_off = self.index.min(self.tokens.len());
let near_tokens = &self.tokens[start_off..end_off];
struct TokensDisplay<'a>(&'a [Token]);
impl<'a> fmt::Display for TokensDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for token in self.0 {
write!(f, "{}", token)?;
}
Ok(())
}
}
parser_err!(format!(
"Expected {}, found: {}\nNear \"{}\"",
expected,
found,
TokensDisplay(near_tokens),
))
}

/// Look for an expected keyword and consume it if it exists
Expand Down
Loading

0 comments on commit 4b49428

Please sign in to comment.