mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-12-23 11:12:51 +00:00
Graceful (programming) error handling
This commit is contained in:
parent
b20cdd1c93
commit
5e7d566b22
2 changed files with 19 additions and 14 deletions
|
|
@ -29,14 +29,19 @@ use crate::tokenizer::{Location, Span};
|
|||
pub struct Comments(Vec<CommentWithSpan>);
|
||||
|
||||
impl Comments {
|
||||
pub(crate) fn push(&mut self, comment: CommentWithSpan) {
|
||||
debug_assert!(
|
||||
self.0
|
||||
.last()
|
||||
.map(|last| last.span < comment.span)
|
||||
.unwrap_or(true)
|
||||
);
|
||||
self.0.push(comment);
|
||||
/// Accepts `comment` if its the first or is located strictly after the
|
||||
/// last accepted comment. In other words, this method will skip the
|
||||
/// comment if its comming out of order (as encountered in the parsed
|
||||
/// source code.)
|
||||
pub(crate) fn offer(&mut self, comment: CommentWithSpan) {
|
||||
if self
|
||||
.0
|
||||
.last()
|
||||
.map(|last| last.span < comment.span)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
self.0.push(comment);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds comments starting within the given location range. The order of
|
||||
|
|
@ -221,25 +226,25 @@ mod tests {
|
|||
// */
|
||||
// ```
|
||||
let mut c = Comments(Vec::new());
|
||||
c.push(CommentWithSpan {
|
||||
c.offer(CommentWithSpan {
|
||||
comment: Comment::SingleLine {
|
||||
content: " abc".into(),
|
||||
prefix: "--".into(),
|
||||
},
|
||||
span: Span::new((1, 1).into(), (1, 7).into()),
|
||||
});
|
||||
c.push(CommentWithSpan {
|
||||
c.offer(CommentWithSpan {
|
||||
comment: Comment::MultiLine(" hello ".into()),
|
||||
span: Span::new((2, 3).into(), (2, 14).into()),
|
||||
});
|
||||
c.push(CommentWithSpan {
|
||||
c.offer(CommentWithSpan {
|
||||
comment: Comment::SingleLine {
|
||||
content: ", world".into(),
|
||||
prefix: "--".into(),
|
||||
},
|
||||
span: Span::new((2, 14).into(), (2, 21).into()),
|
||||
});
|
||||
c.push(CommentWithSpan {
|
||||
c.offer(CommentWithSpan {
|
||||
comment: Comment::MultiLine(" def\n ghi\n jkl\n".into()),
|
||||
span: Span::new((3, 3).into(), (7, 1).into()),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -551,7 +551,7 @@ impl<'a> Parser<'a> {
|
|||
for t in self.tokens.into_iter() {
|
||||
match t.token {
|
||||
Token::Whitespace(Whitespace::SingleLineComment { comment, prefix }) => {
|
||||
comments.push(comments::CommentWithSpan {
|
||||
comments.offer(comments::CommentWithSpan {
|
||||
comment: comments::Comment::SingleLine {
|
||||
content: comment,
|
||||
prefix,
|
||||
|
|
@ -560,7 +560,7 @@ impl<'a> Parser<'a> {
|
|||
});
|
||||
}
|
||||
Token::Whitespace(Whitespace::MultiLineComment(comment)) => {
|
||||
comments.push(comments::CommentWithSpan {
|
||||
comments.offer(comments::CommentWithSpan {
|
||||
comment: comments::Comment::MultiLine(comment),
|
||||
span: t.span,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue