@@ -4065,6 +4065,44 @@ impl<'a> Parser<'a> {
4065
4065
)
4066
4066
}
4067
4067
4068
+ /// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4069
+ pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4070
+ let mut look_back_count = 2;
4071
+ loop {
4072
+ let prev_index = self.index.saturating_sub(look_back_count);
4073
+ if prev_index == 0 {
4074
+ break;
4075
+ }
4076
+ let prev_token = self.token_at(prev_index);
4077
+ match prev_token.token {
4078
+ Token::Whitespace(ref w) => match w {
4079
+ Whitespace::Newline => break,
4080
+ // special consideration required for single line comments since that string includes the newline
4081
+ Whitespace::SingleLineComment { comment, prefix: _ } => {
4082
+ if comment.ends_with('\n') {
4083
+ break;
4084
+ }
4085
+ look_back_count += 1;
4086
+ }
4087
+ _ => look_back_count += 1,
4088
+ },
4089
+ _ => {
4090
+ let current_token = self.get_current_token();
4091
+ if prev_token == current_token {
4092
+ // if we are at the start of the statement, we can skip this check
4093
+ break;
4094
+ }
4095
+
4096
+ self.expected(
4097
+ &format!("newline before current token ({})", current_token),
4098
+ prev_token.clone(),
4099
+ )?
4100
+ }
4101
+ };
4102
+ }
4103
+ Ok(())
4104
+ }
4105
+
4068
4106
/// If the current token is the `expected` keyword, consume it and returns
4069
4107
/// true. Otherwise, no tokens are consumed and returns false.
4070
4108
#[must_use]
@@ -15242,36 +15280,7 @@ impl<'a> Parser<'a> {
15242
15280
15243
15281
/// Parse [Statement::Go]
15244
15282
fn parse_go(&mut self) -> Result<Statement, ParserError> {
15245
- // previous token should be a newline (skipping non-newline whitespace)
15246
- // see also, `previous_token`
15247
- let mut look_back_count = 2;
15248
- loop {
15249
- let prev_index = self.index.saturating_sub(look_back_count);
15250
- if prev_index == 0 {
15251
- break;
15252
- }
15253
- let prev_token = self.token_at(prev_index);
15254
- match prev_token.token {
15255
- Token::Whitespace(ref w) => match w {
15256
- Whitespace::Newline => break,
15257
- Whitespace::SingleLineComment { comment, prefix: _ } => {
15258
- if comment.ends_with('\n') {
15259
- break;
15260
- }
15261
- look_back_count += 1;
15262
- }
15263
- _ => look_back_count += 1,
15264
- },
15265
- _ => {
15266
- if prev_token == self.get_current_token() {
15267
- // if we are at the start of the statement, we can skip this check
15268
- break;
15269
- }
15270
-
15271
- self.expected("newline before GO", prev_token.clone())?
15272
- }
15273
- };
15274
- }
15283
+ self.expect_previously_only_whitespace_until_newline()?;
15275
15284
15276
15285
let count = loop {
15277
15286
// using this peek function because we want to halt this statement parsing upon newline
0 commit comments