Skip to content

Commit 9bc1eb9

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 8647932 commit 9bc1eb9

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
@@ -4162,35 +4162,26 @@ impl<'a> Parser<'a> {
41624162
}
41634163

41644164
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline or beginning of string
4165-
pub(crate) fn expect_previously_only_whitespace_until_newline(
4166-
&mut self,
4167-
) -> Result<(), ParserError> {
4165+
pub(crate) fn prev_only_whitespace_until_newline(&mut self) -> bool {
41684166
let mut look_back_count = 1;
41694167
loop {
41704168
let prev_token = self.peek_prev_nth_token_no_skip_ref(look_back_count);
41714169
match prev_token.token {
4172-
Token::EOF => break,
4170+
Token::EOF => break true,
41734171
Token::Whitespace(ref w) => match w {
4174-
Whitespace::Newline => break,
4172+
Whitespace::Newline => break true,
41754173
// special consideration required for single line comments since that string includes the newline
41764174
Whitespace::SingleLineComment { comment, prefix: _ } => {
41774175
if comment.ends_with('\n') {
4178-
break;
4176+
break true;
41794177
}
41804178
look_back_count += 1;
41814179
}
41824180
_ => look_back_count += 1,
41834181
},
4184-
_ => self.expected(
4185-
&format!(
4186-
"newline before current token ({})",
4187-
self.get_current_token()
4188-
),
4189-
prev_token.clone(),
4190-
)?,
4182+
_ => break false,
41914183
};
41924184
}
4193-
Ok(())
41944185
}
41954186

41964187
/// If the current token is the `expected` keyword, consume it and returns
@@ -16448,7 +16439,12 @@ impl<'a> Parser<'a> {
1644816439
// select 1
1644916440
// go
1645016441
// ```
16451-
self.expect_previously_only_whitespace_until_newline()?;
16442+
if !self.prev_only_whitespace_until_newline() {
16443+
parser_err!(
16444+
"GO may only be preceded by whitespace on a line",
16445+
self.peek_token().span.start
16446+
)?;
16447+
}
1645216448

1645316449
let count = loop {
1645416450
// 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)