@@ -4150,6 +4150,44 @@ impl<'a> Parser<'a> {
4150
4150
)
4151
4151
}
4152
4152
4153
+ /// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4154
+ pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4155
+ let mut look_back_count = 2;
4156
+ loop {
4157
+ let prev_index = self.index.saturating_sub(look_back_count);
4158
+ if prev_index == 0 {
4159
+ break;
4160
+ }
4161
+ let prev_token = self.token_at(prev_index);
4162
+ match prev_token.token {
4163
+ Token::Whitespace(ref w) => match w {
4164
+ Whitespace::Newline => break,
4165
+ // special consideration required for single line comments since that string includes the newline
4166
+ Whitespace::SingleLineComment { comment, prefix: _ } => {
4167
+ if comment.ends_with('\n') {
4168
+ break;
4169
+ }
4170
+ look_back_count += 1;
4171
+ }
4172
+ _ => look_back_count += 1,
4173
+ },
4174
+ _ => {
4175
+ let current_token = self.get_current_token();
4176
+ if prev_token == current_token {
4177
+ // if we are at the start of the statement, we can skip this check
4178
+ break;
4179
+ }
4180
+
4181
+ self.expected(
4182
+ &format!("newline before current token ({})", current_token),
4183
+ prev_token.clone(),
4184
+ )?
4185
+ }
4186
+ };
4187
+ }
4188
+ Ok(())
4189
+ }
4190
+
4153
4191
/// If the current token is the `expected` keyword, consume it and returns
4154
4192
/// true. Otherwise, no tokens are consumed and returns false.
4155
4193
#[must_use]
@@ -16395,36 +16433,7 @@ impl<'a> Parser<'a> {
16395
16433
16396
16434
/// Parse [Statement::Go]
16397
16435
fn parse_go(&mut self) -> Result<Statement, ParserError> {
16398
- // previous token should be a newline (skipping non-newline whitespace)
16399
- // see also, `previous_token`
16400
- let mut look_back_count = 2;
16401
- loop {
16402
- let prev_index = self.index.saturating_sub(look_back_count);
16403
- if prev_index == 0 {
16404
- break;
16405
- }
16406
- let prev_token = self.token_at(prev_index);
16407
- match prev_token.token {
16408
- Token::Whitespace(ref w) => match w {
16409
- Whitespace::Newline => break,
16410
- Whitespace::SingleLineComment { comment, prefix: _ } => {
16411
- if comment.ends_with('\n') {
16412
- break;
16413
- }
16414
- look_back_count += 1;
16415
- }
16416
- _ => look_back_count += 1,
16417
- },
16418
- _ => {
16419
- if prev_token == self.get_current_token() {
16420
- // if we are at the start of the statement, we can skip this check
16421
- break;
16422
- }
16423
-
16424
- self.expected("newline before GO", prev_token.clone())?
16425
- }
16426
- };
16427
- }
16436
+ self.expect_previously_only_whitespace_until_newline()?;
16428
16437
16429
16438
let count = loop {
16430
16439
// using this peek function because we want to halt this statement parsing upon newline
0 commit comments