Skip to content

Commit 4e35df3

Browse files
committed
Extract common logic to look back for a newline to a helper
1 parent 4aa0294 commit 4e35df3

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
@@ -4175,6 +4175,44 @@ impl<'a> Parser<'a> {
41754175
)
41764176
}
41774177

4178+
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4179+
pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4180+
let mut look_back_count = 2;
4181+
loop {
4182+
let prev_index = self.index.saturating_sub(look_back_count);
4183+
if prev_index == 0 {
4184+
break;
4185+
}
4186+
let prev_token = self.token_at(prev_index);
4187+
match prev_token.token {
4188+
Token::Whitespace(ref w) => match w {
4189+
Whitespace::Newline => break,
4190+
// special consideration required for single line comments since that string includes the newline
4191+
Whitespace::SingleLineComment { comment, prefix: _ } => {
4192+
if comment.ends_with('\n') {
4193+
break;
4194+
}
4195+
look_back_count += 1;
4196+
}
4197+
_ => look_back_count += 1,
4198+
},
4199+
_ => {
4200+
let current_token = self.get_current_token();
4201+
if prev_token == current_token {
4202+
// if we are at the start of the statement, we can skip this check
4203+
break;
4204+
}
4205+
4206+
self.expected(
4207+
&format!("newline before current token ({})", current_token),
4208+
prev_token.clone(),
4209+
)?
4210+
}
4211+
};
4212+
}
4213+
Ok(())
4214+
}
4215+
41784216
/// If the current token is the `expected` keyword, consume it and returns
41794217
/// true. Otherwise, no tokens are consumed and returns false.
41804218
#[must_use]
@@ -16489,36 +16527,7 @@ impl<'a> Parser<'a> {
1648916527

1649016528
/// Parse [Statement::Go]
1649116529
fn parse_go(&mut self) -> Result<Statement, ParserError> {
16492-
// previous token should be a newline (skipping non-newline whitespace)
16493-
// see also, `previous_token`
16494-
let mut look_back_count = 2;
16495-
loop {
16496-
let prev_index = self.index.saturating_sub(look_back_count);
16497-
if prev_index == 0 {
16498-
break;
16499-
}
16500-
let prev_token = self.token_at(prev_index);
16501-
match prev_token.token {
16502-
Token::Whitespace(ref w) => match w {
16503-
Whitespace::Newline => break,
16504-
Whitespace::SingleLineComment { comment, prefix: _ } => {
16505-
if comment.ends_with('\n') {
16506-
break;
16507-
}
16508-
look_back_count += 1;
16509-
}
16510-
_ => look_back_count += 1,
16511-
},
16512-
_ => {
16513-
if prev_token == self.get_current_token() {
16514-
// if we are at the start of the statement, we can skip this check
16515-
break;
16516-
}
16517-
16518-
self.expected("newline before GO", prev_token.clone())?
16519-
}
16520-
};
16521-
}
16530+
self.expect_previously_only_whitespace_until_newline()?;
1652216531

1652316532
let count = loop {
1652416533
// 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)