Skip to content

Commit 2c77569

Browse files
committed
Refactor expect_previously_only_whitespace_until_newline to return a bool
- in `parse_go` we don't have a direct way to find what that previous non-whitespace token was, so the error message is adjusted accordingly
1 parent b977abc commit 2c77569

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/dialect/mssql.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ impl Dialect for MsSqlDialect {
131131
fn is_column_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
132132
// if we find maybe whitespace then a newline looking backward, then `GO` ISN'T a column alias
133133
// 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-
{
134+
if kw == &Keyword::GO && parser.prev_only_whitespace_until_newline() {
139135
return false;
140136
}
141137

src/parser/mod.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,35 +4250,26 @@ impl<'a> Parser<'a> {
42504250
}
42514251

42524252
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline or beginning of string
4253-
pub(crate) fn expect_previously_only_whitespace_until_newline(
4254-
&mut self,
4255-
) -> Result<(), ParserError> {
4253+
pub(crate) fn prev_only_whitespace_until_newline(&mut self) -> bool {
42564254
let mut look_back_count = 1;
42574255
loop {
42584256
let prev_token = self.peek_prev_nth_token_no_skip_ref(look_back_count);
42594257
match prev_token.token {
4260-
Token::EOF => break,
4258+
Token::EOF => break true,
42614259
Token::Whitespace(ref w) => match w {
4262-
Whitespace::Newline => break,
4260+
Whitespace::Newline => break true,
42634261
// special consideration required for single line comments since that string includes the newline
42644262
Whitespace::SingleLineComment { comment, prefix: _ } => {
42654263
if comment.ends_with('\n') {
4266-
break;
4264+
break true;
42674265
}
42684266
look_back_count += 1;
42694267
}
42704268
_ => look_back_count += 1,
42714269
},
4272-
_ => self.expected(
4273-
&format!(
4274-
"newline before current token ({})",
4275-
self.get_current_token()
4276-
),
4277-
prev_token.clone(),
4278-
)?,
4270+
_ => break false,
42794271
};
42804272
}
4281-
Ok(())
42824273
}
42834274

42844275
/// If the current token is the `expected` keyword, consume it and returns
@@ -17407,7 +17398,12 @@ impl<'a> Parser<'a> {
1740717398
// select 1
1740817399
// go
1740917400
// ```
17410-
self.expect_previously_only_whitespace_until_newline()?;
17401+
if !self.prev_only_whitespace_until_newline() {
17402+
parser_err!(
17403+
"GO may only be preceded by whitespace on a line",
17404+
self.peek_token().span.start
17405+
)?;
17406+
}
1741117407

1741217408
let count = loop {
1741317409
// 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
@@ -2613,7 +2613,7 @@ fn parse_mssql_go_keyword() {
26132613
let err = ms().parse_sql_statements(invalid_go_position);
26142614
assert_eq!(
26152615
err.unwrap_err().to_string(),
2616-
"sql parser error: Expected: newline before current token (GO), found: ;"
2616+
"sql parser error: GO may only be preceded by whitespace on a line"
26172617
);
26182618

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

0 commit comments

Comments
 (0)