@@ -4070,6 +4070,44 @@ impl<'a> Parser<'a> {
4070
4070
)
4071
4071
}
4072
4072
4073
+ /// Look backwards in the token stream and expect that there was only whitespace tokens until the previous newline
4074
+ pub fn expect_previously_only_whitespace_until_newline(&mut self) -> Result<(), ParserError> {
4075
+ let mut look_back_count = 2;
4076
+ loop {
4077
+ let prev_index = self.index.saturating_sub(look_back_count);
4078
+ if prev_index == 0 {
4079
+ break;
4080
+ }
4081
+ let prev_token = self.token_at(prev_index);
4082
+ match prev_token.token {
4083
+ Token::Whitespace(ref w) => match w {
4084
+ Whitespace::Newline => break,
4085
+ // special consideration required for single line comments since that string includes the newline
4086
+ Whitespace::SingleLineComment { comment, prefix: _ } => {
4087
+ if comment.ends_with('\n') {
4088
+ break;
4089
+ }
4090
+ look_back_count += 1;
4091
+ }
4092
+ _ => look_back_count += 1,
4093
+ },
4094
+ _ => {
4095
+ let current_token = self.get_current_token();
4096
+ if prev_token == current_token {
4097
+ // if we are at the start of the statement, we can skip this check
4098
+ break;
4099
+ }
4100
+
4101
+ self.expected(
4102
+ &format!("newline before current token ({})", current_token),
4103
+ prev_token.clone(),
4104
+ )?
4105
+ }
4106
+ };
4107
+ }
4108
+ Ok(())
4109
+ }
4110
+
4073
4111
/// If the current token is the `expected` keyword, consume it and returns
4074
4112
/// true. Otherwise, no tokens are consumed and returns false.
4075
4113
#[must_use]
@@ -15081,36 +15119,7 @@ impl<'a> Parser<'a> {
15081
15119
15082
15120
/// Parse [Statement::Go]
15083
15121
fn parse_go(&mut self) -> Result<Statement, ParserError> {
15084
- // previous token should be a newline (skipping non-newline whitespace)
15085
- // see also, `previous_token`
15086
- let mut look_back_count = 2;
15087
- loop {
15088
- let prev_index = self.index.saturating_sub(look_back_count);
15089
- if prev_index == 0 {
15090
- break;
15091
- }
15092
- let prev_token = self.token_at(prev_index);
15093
- match prev_token.token {
15094
- Token::Whitespace(ref w) => match w {
15095
- Whitespace::Newline => break,
15096
- Whitespace::SingleLineComment { comment, prefix: _ } => {
15097
- if comment.ends_with('\n') {
15098
- break;
15099
- }
15100
- look_back_count += 1;
15101
- }
15102
- _ => look_back_count += 1,
15103
- },
15104
- _ => {
15105
- if prev_token == self.get_current_token() {
15106
- // if we are at the start of the statement, we can skip this check
15107
- break;
15108
- }
15109
-
15110
- self.expected("newline before GO", prev_token.clone())?
15111
- }
15112
- };
15113
- }
15122
+ self.expect_previously_only_whitespace_until_newline()?;
15114
15123
15115
15124
let count = loop {
15116
15125
// using this peek function because we want to halt this statement parsing upon newline
0 commit comments