Skip to content

Commit a76d95c

Browse files
committed
Fix GO failing to find following newline
1 parent 0a16af9 commit a76d95c

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/parser/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16571,11 +16571,27 @@ impl<'a> Parser<'a> {
1657116571
};
1657216572
};
1657316573

16574-
if self.peek_token().token == Token::SemiColon {
16575-
parser_err!(
16576-
"GO may not end with a semicolon",
16577-
self.peek_token().span.start
16578-
)?;
16574+
loop {
16575+
let next_token = self.peek_token_no_skip();
16576+
match next_token.token {
16577+
Token::EOF => break,
16578+
Token::Whitespace(ref w) => match w {
16579+
Whitespace::Newline => break,
16580+
Whitespace::SingleLineComment { comment, prefix: _ } => {
16581+
if comment.ends_with('\n') {
16582+
break;
16583+
}
16584+
_ = self.next_token_no_skip();
16585+
}
16586+
_ => _ = self.next_token_no_skip(),
16587+
},
16588+
_ => {
16589+
parser_err!(
16590+
"GO must be followed by a newline or EOF",
16591+
self.peek_token().span.start
16592+
)?;
16593+
}
16594+
};
1657916595
}
1658016596

1658116597
Ok(Statement::Go(GoStatement { count }))

tests/sqlparser_mssql.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,12 @@ fn parse_mssql_go_keyword() {
25712571
ms().statements_parse_to(multi_line_comment_following, 2, "USE some_database\nGO 42");
25722572
assert_eq!(stmts[1], Statement::Go(GoStatement { count: Some(42) }));
25732573

2574+
let cte_following_go =
2575+
"USE some_database;\nGO\n;WITH cte AS (\nSELECT 1 x\n)\nSELECT * FROM cte;";
2576+
let stmts = ms().parse_sql_statements(cte_following_go).unwrap();
2577+
assert_eq!(stmts.len(), 3);
2578+
assert_eq!(stmts[1], Statement::Go(GoStatement { count: None }));
2579+
25742580
let actually_column_alias = "SELECT NULL GO";
25752581
let stmt = ms().one_statement_parses_to(actually_column_alias, "SELECT NULL AS GO");
25762582
match &stmt {
@@ -2600,6 +2606,13 @@ fn parse_mssql_go_keyword() {
26002606
err.unwrap_err().to_string(),
26012607
"sql parser error: Expected: literal int or newline, found: x"
26022608
);
2609+
2610+
let invalid_go_delimiter = "SELECT 1\nGO;";
2611+
let err = ms().parse_sql_statements(invalid_go_delimiter);
2612+
assert_eq!(
2613+
err.unwrap_err().to_string(),
2614+
"sql parser error: Expected: literal int or newline, found: ;"
2615+
);
26032616
}
26042617

26052618
#[test]

0 commit comments

Comments
 (0)