Skip to content

Commit 04a94d9

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 6e68019 commit 04a94d9

3 files changed

Lines changed: 13 additions & 21 deletions

File tree

src/dialect/mssql.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ impl Dialect for MsSqlDialect {
130130
fn is_select_item_alias(&self, explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
131131
// if we find maybe whitespace then a newline looking backward, then `GO` ISN'T a column alias
132132
// if we can't find a newline then we assume that `GO` IS a column alias
133-
if kw == &Keyword::GO
134-
&& parser
135-
.expect_previously_only_whitespace_until_newline()
136-
.is_ok()
137-
{
133+
if kw == &Keyword::GO && parser.prev_only_whitespace_until_newline() {
138134
return false;
139135
}
140136

src/parser/mod.rs

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

45934593
/// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline or beginning of string
4594-
pub(crate) fn expect_previously_only_whitespace_until_newline(
4595-
&mut self,
4596-
) -> Result<(), ParserError> {
4594+
pub(crate) fn prev_only_whitespace_until_newline(&mut self) -> bool {
45974595
let mut look_back_count = 1;
45984596
loop {
45994597
let prev_token = self.peek_prev_nth_token_no_skip_ref(look_back_count);
46004598
match prev_token.token {
4601-
Token::EOF => break,
4599+
Token::EOF => break true,
46024600
Token::Whitespace(ref w) => match w {
4603-
Whitespace::Newline => break,
4601+
Whitespace::Newline => break true,
46044602
// special consideration required for single line comments since that string includes the newline
46054603
Whitespace::SingleLineComment { comment, prefix: _ } => {
46064604
if comment.ends_with('\n') {
4607-
break;
4605+
break true;
46084606
}
46094607
look_back_count += 1;
46104608
}
46114609
_ => look_back_count += 1,
46124610
},
4613-
_ => self.expected(
4614-
&format!(
4615-
"newline before current token ({})",
4616-
self.get_current_token()
4617-
),
4618-
prev_token.clone(),
4619-
)?,
4611+
_ => break false,
46204612
};
46214613
}
4622-
Ok(())
46234614
}
46244615

46254616
/// If the current token is the `expected` keyword, consume it and returns
@@ -19707,7 +19698,12 @@ impl<'a> Parser<'a> {
1970719698
// select 1
1970819699
// go
1970919700
// ```
19710-
self.expect_previously_only_whitespace_until_newline()?;
19701+
if !self.prev_only_whitespace_until_newline() {
19702+
parser_err!(
19703+
"GO may only be preceded by whitespace on a line",
19704+
self.peek_token().span.start
19705+
)?;
19706+
}
1971119707

1971219708
let count = loop {
1971319709
// 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
@@ -2958,7 +2958,7 @@ fn parse_mssql_go_keyword() {
29582958
let err = ms().parse_sql_statements(invalid_go_position);
29592959
assert_eq!(
29602960
err.unwrap_err().to_string(),
2961-
"sql parser error: Expected: newline before current token (GO), found: ;"
2961+
"sql parser error: GO may only be preceded by whitespace on a line"
29622962
);
29632963

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

0 commit comments

Comments
 (0)