Skip to content

Commit 7863bef

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 b46fefc commit 7863bef

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/dialect/mssql.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ impl Dialect for MsSqlDialect {
121121
fn is_column_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
122122
// if we find maybe whitespace then a newline looking backward, then `GO` ISN'T a column alias
123123
// if we can't find a newline then we assume that `GO` IS a column alias
124-
if kw == &Keyword::GO
125-
&& parser
126-
.expect_previously_only_whitespace_until_newline()
127-
.is_ok()
128-
{
124+
if kw == &Keyword::GO && parser.prev_only_whitespace_until_newline() {
129125
return false;
130126
}
131127

src/parser/mod.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -4077,35 +4077,26 @@ impl<'a> Parser<'a> {
40774077
}
40784078

40794079
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline or beginning of string
4080-
pub(crate) fn expect_previously_only_whitespace_until_newline(
4081-
&mut self,
4082-
) -> Result<(), ParserError> {
4080+
pub(crate) fn prev_only_whitespace_until_newline(&mut self) -> bool {
40834081
let mut look_back_count = 1;
40844082
loop {
40854083
let prev_token = self.peek_prev_nth_token_no_skip_ref(look_back_count);
40864084
match prev_token.token {
4087-
Token::EOF => break,
4085+
Token::EOF => break true,
40884086
Token::Whitespace(ref w) => match w {
4089-
Whitespace::Newline => break,
4087+
Whitespace::Newline => break true,
40904088
// special consideration required for single line comments since that string includes the newline
40914089
Whitespace::SingleLineComment { comment, prefix: _ } => {
40924090
if comment.ends_with('\n') {
4093-
break;
4091+
break true;
40944092
}
40954093
look_back_count += 1;
40964094
}
40974095
_ => look_back_count += 1,
40984096
},
4099-
_ => self.expected(
4100-
&format!(
4101-
"newline before current token ({})",
4102-
self.get_current_token()
4103-
),
4104-
prev_token.clone(),
4105-
)?,
4097+
_ => break false,
41064098
};
41074099
}
4108-
Ok(())
41094100
}
41104101

41114102
/// If the current token is the `expected` keyword, consume it and returns
@@ -15295,7 +15286,12 @@ impl<'a> Parser<'a> {
1529515286
// select 1
1529615287
// go
1529715288
// ```
15298-
self.expect_previously_only_whitespace_until_newline()?;
15289+
if !self.prev_only_whitespace_until_newline() {
15290+
parser_err!(
15291+
"GO may only be preceded by whitespace on a line",
15292+
self.peek_token().span.start
15293+
)?;
15294+
}
1529915295

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

tests/sqlparser_mssql.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ fn parse_mssql_go_keyword() {
22472247
let err = ms().parse_sql_statements(invalid_go_position);
22482248
assert_eq!(
22492249
err.unwrap_err().to_string(),
2250-
"sql parser error: Expected: newline before current token (GO), found: ;"
2250+
"sql parser error: GO may only be preceded by whitespace on a line"
22512251
);
22522252

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

0 commit comments

Comments
 (0)