Skip to content

Commit 1388d29

Browse files
committed
Extract common logic to look back for a newline to a helper
1 parent cd5c7ee commit 1388d29

File tree

3 files changed

+49
-52
lines changed

3 files changed

+49
-52
lines changed

src/dialect/mssql.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::ast::{
2323
use crate::dialect::Dialect;
2424
use crate::keywords::{self, Keyword};
2525
use crate::parser::{Parser, ParserError};
26-
use crate::tokenizer::{Token, Whitespace};
26+
use crate::tokenizer::Token;
2727
#[cfg(not(feature = "std"))]
2828
use alloc::{vec, vec::Vec};
2929

@@ -129,26 +129,14 @@ impl Dialect for MsSqlDialect {
129129
}
130130

131131
fn is_column_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
132-
// if:
133-
// - keyword is `GO`, and
134-
// - looking backwards there's only (any) whitespace preceded by a newline
135-
// then: `GO` iSN'T a column alias
136-
if kw == &Keyword::GO {
137-
let mut look_back_count = 2;
138-
loop {
139-
let prev_index = parser.index().saturating_sub(look_back_count);
140-
if prev_index == 0 {
141-
break;
142-
}
143-
let prev_token = parser.token_at(prev_index);
144-
match prev_token.token {
145-
Token::Whitespace(ref w) => match w {
146-
Whitespace::Newline => return false,
147-
_ => look_back_count += 1,
148-
},
149-
_ => break,
150-
};
151-
}
132+
// if we find maybe whitespace then a newline looking backward, then `GO` ISN'T a column alias
133+
// if we can't find a newline then we assume that `GO` IS a column alias
134+
if kw == &Keyword::GO
135+
&& parser
136+
.expect_previously_only_whitespace_until_newline()
137+
.is_ok()
138+
{
139+
return false;
152140
}
153141

154142
!keywords::RESERVED_FOR_COLUMN_ALIAS.contains(kw) && !RESERVED_FOR_COLUMN_ALIAS.contains(kw)

src/parser/mod.rs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,6 +4150,44 @@ impl<'a> Parser<'a> {
41504150
)
41514151
}
41524152

4153+
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4154+
pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4155+
let mut look_back_count = 2;
4156+
loop {
4157+
let prev_index = self.index.saturating_sub(look_back_count);
4158+
if prev_index == 0 {
4159+
break;
4160+
}
4161+
let prev_token = self.token_at(prev_index);
4162+
match prev_token.token {
4163+
Token::Whitespace(ref w) => match w {
4164+
Whitespace::Newline => break,
4165+
// special consideration required for single line comments since that string includes the newline
4166+
Whitespace::SingleLineComment { comment, prefix: _ } => {
4167+
if comment.ends_with('\n') {
4168+
break;
4169+
}
4170+
look_back_count += 1;
4171+
}
4172+
_ => look_back_count += 1,
4173+
},
4174+
_ => {
4175+
let current_token = self.get_current_token();
4176+
if prev_token == current_token {
4177+
// if we are at the start of the statement, we can skip this check
4178+
break;
4179+
}
4180+
4181+
self.expected(
4182+
&format!("newline before current token ({})", current_token),
4183+
prev_token.clone(),
4184+
)?
4185+
}
4186+
};
4187+
}
4188+
Ok(())
4189+
}
4190+
41534191
/// If the current token is the `expected` keyword, consume it and returns
41544192
/// true. Otherwise, no tokens are consumed and returns false.
41554193
#[must_use]
@@ -16395,36 +16433,7 @@ impl<'a> Parser<'a> {
1639516433

1639616434
/// Parse [Statement::Go]
1639716435
fn parse_go(&mut self) -> Result<Statement, ParserError> {
16398-
// previous token should be a newline (skipping non-newline whitespace)
16399-
// see also, `previous_token`
16400-
let mut look_back_count = 2;
16401-
loop {
16402-
let prev_index = self.index.saturating_sub(look_back_count);
16403-
if prev_index == 0 {
16404-
break;
16405-
}
16406-
let prev_token = self.token_at(prev_index);
16407-
match prev_token.token {
16408-
Token::Whitespace(ref w) => match w {
16409-
Whitespace::Newline => break,
16410-
Whitespace::SingleLineComment { comment, prefix: _ } => {
16411-
if comment.ends_with('\n') {
16412-
break;
16413-
}
16414-
look_back_count += 1;
16415-
}
16416-
_ => look_back_count += 1,
16417-
},
16418-
_ => {
16419-
if prev_token == self.get_current_token() {
16420-
// if we are at the start of the statement, we can skip this check
16421-
break;
16422-
}
16423-
16424-
self.expected("newline before GO", prev_token.clone())?
16425-
}
16426-
};
16427-
}
16436+
self.expect_previously_only_whitespace_until_newline()?;
1642816437

1642916438
let count = loop {
1643016439
// using this peek function because we want to halt this statement parsing upon newline

tests/sqlparser_mssql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ fn parse_mssql_go_keyword() {
26032603
let err = ms().parse_sql_statements(invalid_go_position);
26042604
assert_eq!(
26052605
err.unwrap_err().to_string(),
2606-
"sql parser error: Expected: newline before GO, found: ;"
2606+
"sql parser error: Expected: newline before current token (GO), found: ;"
26072607
);
26082608

26092609
let invalid_go_count = "SELECT 1\nGO x";

0 commit comments

Comments
 (0)