Fix panic and infinite loop in parser (#4149)
This commit is contained in:
parent
7253c01190
commit
2201b6d26d
6 changed files with 37 additions and 4 deletions
|
@ -88,6 +88,23 @@ impl Location {
|
|||
}
|
||||
|
||||
pub fn range_of_span(source: &str, span: Span) -> Range<Self> {
|
||||
if source.len() == span.offset as usize {
|
||||
// EOF span
|
||||
let (line_idx, column) = LineIterator::new(source)
|
||||
.map(|(l, _)| l.len())
|
||||
.enumerate()
|
||||
.last()
|
||||
.unwrap_or((0, 0));
|
||||
|
||||
return Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
}..Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 2,
|
||||
};
|
||||
}
|
||||
|
||||
// Bytes of input before substr.
|
||||
let offset = span.offset as usize;
|
||||
let end = offset + span.len as usize;
|
||||
|
|
|
@ -213,8 +213,8 @@ impl<'a> Lexer<'a> {
|
|||
Token {
|
||||
kind: TokenKind::Eof,
|
||||
span: Span {
|
||||
offset: self.last_offset.saturating_sub(1),
|
||||
len: 1,
|
||||
offset: self.last_offset,
|
||||
len: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -344,7 +344,6 @@ impl Parser<'_> {
|
|||
match peek.kind {
|
||||
t!("(") => {
|
||||
self.pop_peek();
|
||||
dbg!("called");
|
||||
self.parse_inner_subquery(ctx, Some(peek.span)).await
|
||||
}
|
||||
t!("IF") => {
|
||||
|
|
|
@ -42,7 +42,10 @@ impl Parser<'_> {
|
|||
loop {
|
||||
match self.peek_kind() {
|
||||
// consume any possible empty statements.
|
||||
t!(";") => continue,
|
||||
t!(";") => {
|
||||
self.pop_peek();
|
||||
continue;
|
||||
}
|
||||
t!("eof") => break,
|
||||
_ => {
|
||||
let stmt = ctx.run(|ctx| self.parse_stmt(ctx)).await?;
|
||||
|
|
|
@ -1,4 +1,13 @@
|
|||
use crate::{sql, syn::parser::mac::test_parse};
|
||||
|
||||
mod limit;
|
||||
mod stmt;
|
||||
mod streaming;
|
||||
mod value;
|
||||
|
||||
#[test]
|
||||
fn multiple_semicolons() {
|
||||
let res = test_parse!(parse_query, r#";;"#).unwrap();
|
||||
let expected = sql::Query(sql::Statements(vec![]));
|
||||
assert_eq!(res, expected);
|
||||
}
|
||||
|
|
|
@ -167,3 +167,8 @@ fn scientific_number() {
|
|||
assert!(matches!(res, Value::Number(Number::Float(_))));
|
||||
assert_eq!(res.to_string(), "0.000097f")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_string() {
|
||||
test_parse!(parse_value, "").unwrap_err();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue