Skip to content

Commit 816817b

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 3ebfd0a commit 816817b

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
@@ -4187,35 +4187,26 @@ impl<'a> Parser<'a> {
41874187
}
41884188

41894189
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline or beginning of string
4190-
pub(crate) fn expect_previously_only_whitespace_until_newline(
4191-
&mut self,
4192-
) -> Result<(), ParserError> {
4190+
pub(crate) fn prev_only_whitespace_until_newline(&mut self) -> bool {
41934191
let mut look_back_count = 1;
41944192
loop {
41954193
let prev_token = self.peek_prev_nth_token_no_skip_ref(look_back_count);
41964194
match prev_token.token {
4197-
Token::EOF => break,
4195+
Token::EOF => break true,
41984196
Token::Whitespace(ref w) => match w {
4199-
Whitespace::Newline => break,
4197+
Whitespace::Newline => break true,
42004198
// special consideration required for single line comments since that string includes the newline
42014199
Whitespace::SingleLineComment { comment, prefix: _ } => {
42024200
if comment.ends_with('\n') {
4203-
break;
4201+
break true;
42044202
}
42054203
look_back_count += 1;
42064204
}
42074205
_ => look_back_count += 1,
42084206
},
4209-
_ => self.expected(
4210-
&format!(
4211-
"newline before current token ({})",
4212-
self.get_current_token()
4213-
),
4214-
prev_token.clone(),
4215-
)?,
4207+
_ => break false,
42164208
};
42174209
}
4218-
Ok(())
42194210
}
42204211

42214212
/// If the current token is the `expected` keyword, consume it and returns
@@ -16542,7 +16533,12 @@ impl<'a> Parser<'a> {
1654216533
// select 1
1654316534
// go
1654416535
// ```
16545-
self.expect_previously_only_whitespace_until_newline()?;
16536+
if !self.prev_only_whitespace_until_newline() {
16537+
parser_err!(
16538+
"GO may only be preceded by whitespace on a line",
16539+
self.peek_token().span.start
16540+
)?;
16541+
}
1654616542

1654716543
let count = loop {
1654816544
// 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
@@ -2597,7 +2597,7 @@ fn parse_mssql_go_keyword() {
25972597
let err = ms().parse_sql_statements(invalid_go_position);
25982598
assert_eq!(
25992599
err.unwrap_err().to_string(),
2600-
"sql parser error: Expected: newline before current token (GO), found: ;"
2600+
"sql parser error: GO may only be preceded by whitespace on a line"
26012601
);
26022602

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

0 commit comments

Comments
 (0)