@@ -4175,6 +4175,44 @@ impl<'a> Parser<'a> {
4175
4175
)
4176
4176
}
4177
4177
4178
+ /// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4179
+ pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4180
+ let mut look_back_count = 2;
4181
+ loop {
4182
+ let prev_index = self.index.saturating_sub(look_back_count);
4183
+ if prev_index == 0 {
4184
+ break;
4185
+ }
4186
+ let prev_token = self.token_at(prev_index);
4187
+ match prev_token.token {
4188
+ Token::Whitespace(ref w) => match w {
4189
+ Whitespace::Newline => break,
4190
+ // special consideration required for single line comments since that string includes the newline
4191
+ Whitespace::SingleLineComment { comment, prefix: _ } => {
4192
+ if comment.ends_with('\n') {
4193
+ break;
4194
+ }
4195
+ look_back_count += 1;
4196
+ }
4197
+ _ => look_back_count += 1,
4198
+ },
4199
+ _ => {
4200
+ let current_token = self.get_current_token();
4201
+ if prev_token == current_token {
4202
+ // if we are at the start of the statement, we can skip this check
4203
+ break;
4204
+ }
4205
+
4206
+ self.expected(
4207
+ &format!("newline before current token ({})", current_token),
4208
+ prev_token.clone(),
4209
+ )?
4210
+ }
4211
+ };
4212
+ }
4213
+ Ok(())
4214
+ }
4215
+
4178
4216
/// If the current token is the `expected` keyword, consume it and returns
4179
4217
/// true. Otherwise, no tokens are consumed and returns false.
4180
4218
#[must_use]
@@ -16489,36 +16527,7 @@ impl<'a> Parser<'a> {
16489
16527
16490
16528
/// Parse [Statement::Go]
16491
16529
fn parse_go(&mut self) -> Result<Statement, ParserError> {
16492
- // previous token should be a newline (skipping non-newline whitespace)
16493
- // see also, `previous_token`
16494
- let mut look_back_count = 2;
16495
- loop {
16496
- let prev_index = self.index.saturating_sub(look_back_count);
16497
- if prev_index == 0 {
16498
- break;
16499
- }
16500
- let prev_token = self.token_at(prev_index);
16501
- match prev_token.token {
16502
- Token::Whitespace(ref w) => match w {
16503
- Whitespace::Newline => break,
16504
- Whitespace::SingleLineComment { comment, prefix: _ } => {
16505
- if comment.ends_with('\n') {
16506
- break;
16507
- }
16508
- look_back_count += 1;
16509
- }
16510
- _ => look_back_count += 1,
16511
- },
16512
- _ => {
16513
- if prev_token == self.get_current_token() {
16514
- // if we are at the start of the statement, we can skip this check
16515
- break;
16516
- }
16517
-
16518
- self.expected("newline before GO", prev_token.clone())?
16519
- }
16520
- };
16521
- }
16530
+ self.expect_previously_only_whitespace_until_newline()?;
16522
16531
16523
16532
let count = loop {
16524
16533
// using this peek function because we want to halt this statement parsing upon newline
0 commit comments