Skip to content

Commit 357ae5c

Browse files
committed
Fix GO failing to find following newline
1 parent b8b8c7a commit 357ae5c

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
@@ -17436,11 +17436,27 @@ impl<'a> Parser<'a> {
1743617436
};
1743717437
};
1743817438

17439-
if self.peek_token().token == Token::SemiColon {
17440-
parser_err!(
17441-
"GO may not end with a semicolon",
17442-
self.peek_token().span.start
17443-
)?;
17439+
loop {
17440+
let next_token = self.peek_token_no_skip();
17441+
match next_token.token {
17442+
Token::EOF => break,
17443+
Token::Whitespace(ref w) => match w {
17444+
Whitespace::Newline => break,
17445+
Whitespace::SingleLineComment { comment, prefix: _ } => {
17446+
if comment.ends_with('\n') {
17447+
break;
17448+
}
17449+
_ = self.next_token_no_skip();
17450+
}
17451+
_ => _ = self.next_token_no_skip(),
17452+
},
17453+
_ => {
17454+
parser_err!(
17455+
"GO must be followed by a newline or EOF",
17456+
self.peek_token().span.start
17457+
)?;
17458+
}
17459+
};
1744417460
}
1744517461

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

tests/sqlparser_mssql.rs

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

2590+
let cte_following_go =
2591+
"USE some_database;\nGO\n;WITH cte AS (\nSELECT 1 x\n)\nSELECT * FROM cte;";
2592+
let stmts = ms().parse_sql_statements(cte_following_go).unwrap();
2593+
assert_eq!(stmts.len(), 3);
2594+
assert_eq!(stmts[1], Statement::Go(GoStatement { count: None }));
2595+
25902596
let actually_column_alias = "SELECT NULL GO";
25912597
let stmt = ms().one_statement_parses_to(actually_column_alias, "SELECT NULL AS GO");
25922598
match &stmt {
@@ -2616,6 +2622,13 @@ fn parse_mssql_go_keyword() {
26162622
err.unwrap_err().to_string(),
26172623
"sql parser error: Expected: literal int or newline, found: x"
26182624
);
2625+
2626+
let invalid_go_delimiter = "SELECT 1\nGO;";
2627+
let err = ms().parse_sql_statements(invalid_go_delimiter);
2628+
assert_eq!(
2629+
err.unwrap_err().to_string(),
2630+
"sql parser error: Expected: literal int or newline, found: ;"
2631+
);
26192632
}
26202633

26212634
#[test]

0 commit comments

Comments
 (0)